Example usage for org.apache.ibatis.parsing XNode getStringAttribute

List of usage examples for org.apache.ibatis.parsing XNode getStringAttribute

Introduction

In this page you can find the example usage for org.apache.ibatis.parsing XNode getStringAttribute.

Prototype

public String getStringAttribute(String name) 

Source Link

Usage

From source file:com.ibatis.sqlmap.engine.builder.XmlSqlMapParser.java

License:Apache License

@NodeEvent("/sqlMap/resultMap")
public void sqlMapresultMap(XNode context) throws Exception {
    String xmlName = context.getStringAttribute("xmlName");
    if (xmlName != null) {
        throw new UnsupportedOperationException("xmlName is not supported by iBATIS 3");
    }//from w  w w. j  a  v  a 2 s  .c  o m

    String id = applyNamespace(context.getStringAttribute("id"));
    String resultClassName = context.getStringAttribute("class");
    String extendedId = applyNamespace(context.getStringAttribute("extends"));

    String groupBy = context.getStringAttribute("groupBy");
    if (groupBy != null) {
        groupByProperties = Arrays.asList(groupBy.split(", "));
    }

    Class resultClass;
    try {
        resultClass = config.getTypeAliasRegistry().resolveAlias(resultClassName);
    } catch (Exception e) {
        throw new RuntimeException("Error configuring Result.  Could not set ResultClass.  Cause: " + e, e);
    }

    resultMappingList = new ArrayList<ResultMapping>();
    resultMapBuilder = new ResultMap.Builder(config, id, resultClass, resultMappingList);

    if (extendedId != null) {
        ResultMap extendedResultMap = config.getResultMap(extendedId);
        for (ResultMapping mapping : extendedResultMap.getResultMappings()) {
            resultMappingList.add(mapping);
        }
        resultMapBuilder.discriminator(extendedResultMap.getDiscriminator());
    }

}

From source file:com.ibatis.sqlmap.engine.builder.XmlSqlMapParser.java

License:Apache License

@NodeEvent("/sqlMap/resultMap/discriminator")
public void sqlMapresultMapdiscriminator(XNode context) throws Exception {
    String nullValue = context.getStringAttribute("nullValue");
    if (nullValue != null) {
        throw new UnsupportedOperationException("Null value subsitution is not supported by iBATIS 3.");
    }//from w  w  w.j  a  v  a 2s . c  om
    String columnIndexProp = context.getStringAttribute("columnIndex");
    if (columnIndexProp != null) {
        throw new UnsupportedOperationException(
                "Numerical column indices are not supported.  Use the column name instead.");
    }

    String jdbcType = context.getStringAttribute("jdbcType");
    String javaType = context.getStringAttribute("javaType");
    String columnName = context.getStringAttribute("column");
    String callback = context.getStringAttribute("typeHandler");

    Class javaClass = null;
    try {
        if (javaType != null && javaType.length() > 0) {
            javaClass = config.getTypeAliasRegistry().resolveAlias(javaType);
        }
    } catch (Exception e) {
        throw new RuntimeException("Error setting java type on result discriminator mapping.  Cause: " + e);
    }

    JdbcType jdbcTypeEnum = null;
    if (jdbcType != null) {
        jdbcTypeEnum = JdbcType.valueOf(jdbcType);
    }

    TypeHandler typeHandler = null;
    if (javaClass != null) {
        typeHandler = config.getTypeHandlerRegistry().getTypeHandler(javaClass, jdbcTypeEnum);
    }
    try {
        if (callback != null && callback.length() > 0) {
            typeHandler = (TypeHandler) config.getTypeAliasRegistry().resolveAlias(callback).newInstance();
        }
    } catch (Exception e) {
        throw new RuntimeException("Error occurred during custom type handler configuration.  Cause: " + e, e);
    }

    ResultMapping.Builder resultMappingBuilder = new ResultMapping.Builder(config, columnName, columnName,
            typeHandler);
    resultMappingBuilder.javaType(javaClass);
    resultMappingBuilder.jdbcType(jdbcTypeEnum);
    ResultMapping resultMapping = resultMappingBuilder.build();

    discriminatorSubMap = new HashMap<String, String>();
    discriminatorBuilder = new Discriminator.Builder(config, resultMapping, discriminatorSubMap);

}

From source file:com.ibatis.sqlmap.engine.builder.XmlSqlMapParser.java

License:Apache License

@NodeEvent("/sqlMap/resultMap/discriminator/subMap")
public void sqlMapresultMapdiscriminatorsubMap(XNode context) throws Exception {
    String value = context.getStringAttribute("value");
    String resultMap = context.getStringAttribute("resultMap");
    resultMap = applyNamespace(resultMap);
    discriminatorSubMap.put(value, resultMap);
}

From source file:com.ibatis.sqlmap.engine.builder.XmlSqlMapParser.java

License:Apache License

@NodeEvent("/sqlMap/resultMap/result")
public void sqlMapresultMapresult(XNode context) throws Exception {
    String nullValue = context.getStringAttribute("nullValue");
    if (nullValue != null) {
        throw new UnsupportedOperationException("Null value subsitution is not supported by iBATIS 3.");
    }//  www .ja v  a 2  s  . co  m
    String columnIndexProp = context.getStringAttribute("columnIndex");
    if (columnIndexProp != null) {
        throw new UnsupportedOperationException(
                "Numerical column indices are not supported.  Use the column name instead.");
    }

    String propertyName = context.getStringAttribute("property");
    String jdbcType = context.getStringAttribute("jdbcType");
    String javaType = context.getStringAttribute("javaType");
    String columnName = context.getStringAttribute("column");

    String statementName = context.getStringAttribute("select");
    String resultMapName = context.getStringAttribute("resultMap");
    String callback = context.getStringAttribute("typeHandler");

    Class javaClass = null;
    try {
        if (javaType != null && javaType.length() > 0) {
            javaClass = config.getTypeAliasRegistry().resolveAlias(javaType);
        }
    } catch (Exception e) {
        throw new RuntimeException("Error setting java type on result discriminator mapping.  Cause: " + e);
    }
    if (javaClass == null && !Map.class.isAssignableFrom(resultMapBuilder.type())
            && !config.getTypeHandlerRegistry().hasTypeHandler(resultMapBuilder.type())) {
        javaClass = MetaClass.forClass(resultMapBuilder.type()).getSetterType(propertyName);
    }
    if (javaClass == null && statementName != null) {
        javaClass = List.class;
    }

    JdbcType jdbcTypeEnum = null;
    if (jdbcType != null) {
        jdbcTypeEnum = JdbcType.valueOf(jdbcType);
    }

    TypeHandler typeHandler = null;
    if (javaClass != null) {
        typeHandler = config.getTypeHandlerRegistry().getTypeHandler(javaClass, jdbcTypeEnum);
    }
    try {
        if (callback != null && callback.length() > 0) {
            Object o = config.getTypeAliasRegistry().resolveAlias(callback).newInstance();
            if (o instanceof TypeHandlerCallback) {
                typeHandler = new TypeHandlerCallbackAdapter((TypeHandlerCallback) o);
            }
        }
    } catch (Exception e) {
        throw new RuntimeException("Error occurred during custom type handler configuration.  Cause: " + e, e);
    }
    if (typeHandler == null && config.getTypeHandlerRegistry().hasTypeHandler(resultMapBuilder.type())) {
        typeHandler = config.getTypeHandlerRegistry().getTypeHandler(resultMapBuilder.type());
    }

    if (typeHandler == null) {
        Class resultClass = resultMapBuilder.type();
        if (resultClass != null && !Map.class.isAssignableFrom(resultClass)) {
            MetaClass metaResultClass = MetaClass.forClass(resultClass);
            Class resultType = null;
            if (metaResultClass.hasGetter(propertyName)) {
                resultType = metaResultClass.getGetterType(propertyName);
            } else if (metaResultClass.hasSetter(propertyName)) {
                resultType = metaResultClass.getSetterType(propertyName);
            }
            if (resultType != null) {
                typeHandler = config.getTypeHandlerRegistry().getTypeHandler(resultType);
            }
        } else {
            typeHandler = config.getTypeHandlerRegistry().getUnknownTypeHandler();
        }
    }

    List<ResultMapping> composites = parseCompositeColumnName(columnName);
    if (composites.size() > 0) {
        ResultMapping first = composites.get(0);
        columnName = first.getColumn();
    }

    ResultMapping.Builder resultMappingBuilder = new ResultMapping.Builder(config, propertyName, columnName,
            typeHandler);
    resultMappingBuilder.javaType(javaClass);
    resultMappingBuilder.nestedQueryId(statementName);
    resultMappingBuilder.nestedResultMapId(resultMapName);
    resultMappingBuilder.jdbcType(jdbcTypeEnum);
    resultMappingBuilder.composites(composites);
    if (groupByProperties != null && groupByProperties.contains(propertyName)) {
        List<ResultFlag> flags = new ArrayList<ResultFlag>();
        resultMappingBuilder.flags(flags);
    }
    resultMappingList.add(resultMappingBuilder.build());
}

From source file:com.ibatis.sqlmap.engine.builder.XmlSqlMapParser.java

License:Apache License

@NodeEvent("/sqlMap/parameterMap")
public void sqlMapparameterMap(XNode context) throws Exception {
    String id = applyNamespace(context.getStringAttribute("id"));
    String parameterClassName = context.getStringAttribute("class");
    Class parameterClass = config.getTypeAliasRegistry().resolveAlias(parameterClassName);
    parameterMappingList = new ArrayList<ParameterMapping>();
    parameterMapBuilder = new ParameterMap.Builder(config, id, parameterClass, parameterMappingList);
}

From source file:com.ibatis.sqlmap.engine.builder.XmlSqlMapParser.java

License:Apache License

@NodeEvent("/sqlMap/parameterMap/parameter")
public void sqlMapparameterMapparameter(XNode context) throws Exception {
    String nullValue = context.getStringAttribute("nullValue");
    if (nullValue != null) {
        throw new UnsupportedOperationException("Null value subsitution is not supported by iBATIS 3.");
    }/*from  www.  ja  v  a  2 s  .  c  om*/

    String propertyName = context.getStringAttribute("property");
    String jdbcType = context.getStringAttribute("jdbcType");
    String javaType = context.getStringAttribute("javaType");
    String resultMap = context.getStringAttribute("resultMap");
    String mode = context.getStringAttribute("mode");
    String callback = context.getStringAttribute("typeHandler");
    String numericScaleProp = context.getStringAttribute("numericScale");

    Class javaClass = null;
    try {
        if (javaType != null && javaType.length() > 0) {
            javaClass = config.getTypeAliasRegistry().resolveAlias(javaType);
        }
    } catch (Exception e) {
        throw new RuntimeException("Error setting javaType on parameter mapping.  Cause: " + e);
    }

    JdbcType jdbcTypeEnum = null;
    if (jdbcType != null) {
        jdbcTypeEnum = JdbcType.valueOf(jdbcType);
    }

    TypeHandler typeHandler = null;
    if (javaClass != null) {
        typeHandler = config.getTypeHandlerRegistry().getTypeHandler(javaClass, jdbcTypeEnum);
    }
    if (callback != null) {
        Object o = config.getTypeAliasRegistry().resolveAlias(callback).newInstance();
        if (o instanceof TypeHandlerCallback) {
            typeHandler = new TypeHandlerCallbackAdapter((TypeHandlerCallback) o);
        }
    }
    if (typeHandler == null && config.getTypeHandlerRegistry().hasTypeHandler(parameterMapBuilder.type())) {
        typeHandler = config.getTypeHandlerRegistry().getTypeHandler(parameterMapBuilder.type());
    }
    if (typeHandler == null) {
        Class parameterClass = parameterMapBuilder.type();
        if (parameterClass != null && !Map.class.isAssignableFrom(parameterClass)) {
            MetaClass metaParamClass = MetaClass.forClass(parameterClass);
            Class paramType = null;
            if (metaParamClass.hasGetter(propertyName)) {
                paramType = metaParamClass.getGetterType(propertyName);
            } else if (metaParamClass.hasSetter(propertyName)) {
                paramType = metaParamClass.getSetterType(propertyName);
            }
            if (paramType != null) {
                typeHandler = config.getTypeHandlerRegistry().getTypeHandler(paramType);
            }
        } else {
            typeHandler = config.getTypeHandlerRegistry().getUnknownTypeHandler();
        }
    }

    ParameterMode paramModeEnum = ParameterMode.IN;
    if (mode != null) {
        paramModeEnum = ParameterMode.valueOf(mode);
    }

    Integer numericScale = null;
    if (numericScaleProp != null) {
        numericScale = new Integer(numericScaleProp);
    }

    ParameterMapping.Builder parameterMappingBuilder = new ParameterMapping.Builder(config, propertyName,
            typeHandler);
    parameterMappingBuilder.javaType(javaClass);
    parameterMappingBuilder.jdbcType(jdbcTypeEnum);
    parameterMappingBuilder.mode(paramModeEnum);
    parameterMappingBuilder.numericScale(numericScale);
    parameterMappingBuilder.resultMapId(resultMap);

    parameterMappingList.add(parameterMappingBuilder.build());
}

From source file:com.ibatis.sqlmap.engine.builder.XmlSqlMapParser.java

License:Apache License

@NodeEvent("/sqlMap/sql")
public void sqlMapsql(XNode context) throws Exception {
    String id = context.getStringAttribute("id");
    if (configParser.isUseStatementNamespaces()) {
        id = applyNamespace(id);/*w w  w  .  j a va2 s  . co m*/
    }
    if (configParser.hasSqlFragment(id)) {
        throw new SqlMapException("Duplicate <sql>-include '" + id + "' found.");
    } else {
        configParser.addSqlFragment(id, context);
    }
}

From source file:com.ibatis.sqlmap.engine.builder.XmlSqlStatementParser.java

License:Apache License

public void parseGeneralStatement(XNode context) {
    // get attributes
    String id = context.getStringAttribute("id");
    String parameterMapName = context.getStringAttribute("parameterMap");
    String parameterClassName = context.getStringAttribute("parameterClass");
    String resultMapName = context.getStringAttribute("resultMap");
    String resultClassName = context.getStringAttribute("resultClass");
    String cacheModelName = context.getStringAttribute("cacheModel");
    String resultSetType = context.getStringAttribute("resultSetType");
    String fetchSize = context.getStringAttribute("fetchSize");
    String timeout = context.getStringAttribute("timeout");
    // 2.x -- String allowRemapping = context.getStringAttribute("remapResults");

    if (context.getStringAttribute("xmlResultName") != null) {
        throw new UnsupportedOperationException("xmlResultName is not supported by iBATIS 3");
    }//from  ww  w .  jav a2s .  co m

    if (mapParser.getConfigParser().isUseStatementNamespaces()) {
        id = mapParser.applyNamespace(id);
    }

    String[] additionalResultMapNames = null;
    if (resultMapName != null) {
        additionalResultMapNames = getAllButFirstToken(resultMapName);
        resultMapName = getFirstToken(resultMapName);
        resultMapName = mapParser.applyNamespace(resultMapName);
        for (int i = 0; i < additionalResultMapNames.length; i++) {
            additionalResultMapNames[i] = mapParser.applyNamespace(additionalResultMapNames[i]);
        }
    }

    String[] additionalResultClassNames = null;
    if (resultClassName != null) {
        additionalResultClassNames = getAllButFirstToken(resultClassName);
        resultClassName = getFirstToken(resultClassName);
    }
    Class[] additionalResultClasses = null;
    if (additionalResultClassNames != null) {
        additionalResultClasses = new Class[additionalResultClassNames.length];
        for (int i = 0; i < additionalResultClassNames.length; i++) {
            additionalResultClasses[i] = resolveClass(additionalResultClassNames[i]);
        }
    }

    Integer timeoutInt = timeout == null ? null : new Integer(timeout);
    Integer fetchSizeInt = fetchSize == null ? null : new Integer(fetchSize);

    // 2.x -- boolean allowRemappingBool = "true".equals(allowRemapping);

    SqlSource sqlSource = new SqlSourceFactory(mapParser).newSqlSourceIntance(mapParser, context);

    String nodeName = context.getNode().getNodeName();
    SqlCommandType sqlCommandType;
    try {
        sqlCommandType = SqlCommandType.valueOf(nodeName.toUpperCase());
    } catch (Exception e) {
        sqlCommandType = SqlCommandType.UNKNOWN;
    }

    MappedStatement.Builder builder = new MappedStatement.Builder(configuration, id, sqlSource, sqlCommandType);

    builder.useCache(true);
    if (!"select".equals(context.getNode().getNodeName())) {
        builder.flushCacheRequired(true);
    }

    if (parameterMapName != null) {
        parameterMapName = mapParser.applyNamespace(parameterMapName);
        builder.parameterMap(configuration.getParameterMap(parameterMapName));
    } else if (parameterClassName != null) {
        Class parameterClass = resolveClass(parameterClassName);
        List<ParameterMapping> parameterMappings = new ArrayList<ParameterMapping>();
        if (sqlSource instanceof SimpleSqlSource) {
            parameterMappings = sqlSource.getBoundSql(null).getParameterMappings();
        }
        ParameterMap.Builder parameterMapBuilder = new ParameterMap.Builder(configuration, id + "-ParameterMap",
                parameterClass, parameterMappings);
        builder.parameterMap(parameterMapBuilder.build());
    }

    List<ResultMap> resultMaps = new ArrayList<ResultMap>();
    if (resultMapName != null) {
        resultMaps.add(configuration.getResultMap(resultMapName));
        if (additionalResultMapNames != null) {
            for (String additionalResultMapName : additionalResultMapNames) {
                resultMaps.add(configuration.getResultMap(additionalResultMapName));
            }
        }
    } else if (resultClassName != null) {
        Class resultClass = resolveClass(resultClassName);
        ResultMap.Builder resultMapBuilder = new ResultMap.Builder(configuration, id + "-ResultMap",
                resultClass, new ArrayList<ResultMapping>());
        resultMaps.add(resultMapBuilder.build());
        if (additionalResultClasses != null) {
            for (Class additionalResultClass : additionalResultClasses) {
                resultMapBuilder = new ResultMap.Builder(configuration, id + "-ResultMap",
                        additionalResultClass, new ArrayList<ResultMapping>());
                resultMaps.add(resultMapBuilder.build());
            }
        }
    }
    builder.resultMaps(resultMaps);

    builder.fetchSize(fetchSizeInt);

    builder.timeout(timeoutInt);

    if (cacheModelName != null) {
        cacheModelName = mapParser.applyNamespace(cacheModelName);
        Cache cache = configuration.getCache(cacheModelName);
        builder.cache(cache);
    }

    if (resultSetType != null) {
        builder.resultSetType(ResultSetType.valueOf(resultSetType));
    }

    // allowRemappingBool -- silently ignored

    findAndParseSelectKey(id, context);

    configuration.addMappedStatement(builder.build());
}

From source file:com.ibatis.sqlmap.engine.builder.XmlSqlStatementParser.java

License:Apache License

private void buildSelectKeyStatement(String parentId, XNode context, boolean runStatementFirstParam)
        throws ClassNotFoundException {
    final String keyPropName = context.getStringAttribute("keyProperty");
    String resultClassName = context.getStringAttribute("resultClass");
    final SimpleSqlSource source = new SimpleSqlSource(mapParser, context);

    final boolean runStatementFirst = "post"
            .equalsIgnoreCase(context.getStringAttribute("type", runStatementFirstParam ? "post" : "pre"));
    final String keyStatementId = SqlMapSessionImpl.selectKeyIdFor(parentId);
    TypeHandler typeHandler = configuration.getTypeHandlerRegistry().getUnknownTypeHandler();
    if (resultClassName != null) {
        final Class resultClass = configuration.getTypeAliasRegistry().resolveAlias(resultClassName);
        typeHandler = configuration.getTypeHandlerRegistry().getTypeHandler(resultClass);
    }//from   www .j  a va2s  . c  o m

    ResultMapping.Builder mappingBuilder = new ResultMapping.Builder(configuration, keyPropName, keyPropName,
            typeHandler);
    ArrayList<ResultMapping> resultMappingArrayList = new ArrayList<ResultMapping>();
    resultMappingArrayList.add(mappingBuilder.build());

    ResultMap.Builder resultMapBuilder = new ResultMap.Builder(configuration, keyStatementId + "ResultMap",
            HashMap.class, resultMappingArrayList);
    ArrayList<ResultMap> resultMapList = new ArrayList<ResultMap>();
    resultMapList.add(resultMapBuilder.build());

    MappedStatement.Builder builder = new MappedStatement.Builder(configuration, keyStatementId, source,
            SqlCommandType.SELECT);
    builder.resultMaps(resultMapList);

    configuration.setPostSelectKey(keyStatementId, runStatementFirst);
    configuration.addMappedStatement(builder.build());
}

From source file:com.jdy.ddj.common.utils.SqlSessionFactoryBean.java

License:Apache License

/**
 * ??//  www  . j a v a  2  s .c  o  m
 * @param parser
 * @param configuration
 */
private void parseTypeAliasesElement(XPathParser parser, Configuration configuration) {
    XNode typeAliasesElement = parser.evalNode("/configuration/typeAliases");
    if (typeAliasesElement != null) {
        for (XNode child : typeAliasesElement.getChildren()) {
            if ("package".equals(child.getName())) {
                String typeAliasPackage = child.getStringAttribute("name");
                configuration.getTypeAliasRegistry().registerAliases(typeAliasPackage);
            } else {
                String alias = child.getStringAttribute("alias");
                String type = child.getStringAttribute("type");
                try {
                    Class<?> clazz = Resources.classForName(type);
                    if (alias == null) {
                        configuration.getTypeAliasRegistry().registerAlias(clazz);
                    } else {
                        configuration.getTypeAliasRegistry().registerAlias(alias, clazz);
                    }
                } catch (ClassNotFoundException e) {
                    throw new BuilderException("Error registering typeAlias for '" + alias + "'. Cause: " + e,
                            e);
                }
            }
        }
    }
}