Search in sources :

Example 6 with BuilderException

use of org.apache.ibatis.builder.BuilderException in project mybatis-3 by mybatis.

the class XMLConfigBuilder method settingsAsProperties.

private Properties settingsAsProperties(XNode context) {
    if (context == null) {
        return new Properties();
    }
    Properties props = context.getChildrenAsProperties();
    // Check that all settings are known to the configuration class
    MetaClass metaConfig = MetaClass.forClass(Configuration.class, localReflectorFactory);
    for (Object key : props.keySet()) {
        if (!metaConfig.hasSetter(String.valueOf(key))) {
            throw new BuilderException("The setting " + key + " is not known.  Make sure you spelled it correctly (case sensitive).");
        }
    }
    return props;
}
Also used : BuilderException(org.apache.ibatis.builder.BuilderException) MetaClass(org.apache.ibatis.reflection.MetaClass) Properties(java.util.Properties)

Example 7 with BuilderException

use of org.apache.ibatis.builder.BuilderException in project mybatis-3 by mybatis.

the class XMLConfigBuilder method typeAliasesElement.

private void typeAliasesElement(XNode parent) {
    if (parent != null) {
        for (XNode child : parent.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) {
                        typeAliasRegistry.registerAlias(clazz);
                    } else {
                        typeAliasRegistry.registerAlias(alias, clazz);
                    }
                } catch (ClassNotFoundException e) {
                    throw new BuilderException("Error registering typeAlias for '" + alias + "'. Cause: " + e, e);
                }
            }
        }
    }
}
Also used : BuilderException(org.apache.ibatis.builder.BuilderException) XNode(org.apache.ibatis.parsing.XNode)

Example 8 with BuilderException

use of org.apache.ibatis.builder.BuilderException in project mybatis-3 by mybatis.

the class XMLConfigBuilder method propertiesElement.

private void propertiesElement(XNode context) throws Exception {
    if (context != null) {
        Properties defaults = context.getChildrenAsProperties();
        String resource = context.getStringAttribute("resource");
        String url = context.getStringAttribute("url");
        if (resource != null && url != null) {
            throw new BuilderException("The properties element cannot specify both a URL and a resource based property file reference.  Please specify one or the other.");
        }
        if (resource != null) {
            defaults.putAll(Resources.getResourceAsProperties(resource));
        } else if (url != null) {
            defaults.putAll(Resources.getUrlAsProperties(url));
        }
        Properties vars = configuration.getVariables();
        if (vars != null) {
            defaults.putAll(vars);
        }
        parser.setVariables(defaults);
        configuration.setVariables(defaults);
    }
}
Also used : BuilderException(org.apache.ibatis.builder.BuilderException) Properties(java.util.Properties)

Example 9 with BuilderException

use of org.apache.ibatis.builder.BuilderException in project mybatis-3 by mybatis.

the class XMLConfigBuilder method mapperElement.

private void mapperElement(XNode parent) throws Exception {
    if (parent != null) {
        for (XNode child : parent.getChildren()) {
            if ("package".equals(child.getName())) {
                String mapperPackage = child.getStringAttribute("name");
                configuration.addMappers(mapperPackage);
            } else {
                String resource = child.getStringAttribute("resource");
                String url = child.getStringAttribute("url");
                String mapperClass = child.getStringAttribute("class");
                if (resource != null && url == null && mapperClass == null) {
                    ErrorContext.instance().resource(resource);
                    InputStream inputStream = Resources.getResourceAsStream(resource);
                    XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, resource, configuration.getSqlFragments());
                    mapperParser.parse();
                } else if (resource == null && url != null && mapperClass == null) {
                    ErrorContext.instance().resource(url);
                    InputStream inputStream = Resources.getUrlAsStream(url);
                    XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, url, configuration.getSqlFragments());
                    mapperParser.parse();
                } else if (resource == null && url == null && mapperClass != null) {
                    Class<?> mapperInterface = Resources.classForName(mapperClass);
                    configuration.addMapper(mapperInterface);
                } else {
                    throw new BuilderException("A mapper element may only specify a url, resource or class, but not more than one.");
                }
            }
        }
    }
}
Also used : BuilderException(org.apache.ibatis.builder.BuilderException) InputStream(java.io.InputStream) XNode(org.apache.ibatis.parsing.XNode)

Example 10 with BuilderException

use of org.apache.ibatis.builder.BuilderException in project mybatis-3 by mybatis.

the class XMLConfigBuilder method parseConfiguration.

private void parseConfiguration(XNode root) {
    try {
        //issue #117 read properties first
        propertiesElement(root.evalNode("properties"));
        Properties settings = settingsAsProperties(root.evalNode("settings"));
        loadCustomVfs(settings);
        typeAliasesElement(root.evalNode("typeAliases"));
        pluginElement(root.evalNode("plugins"));
        objectFactoryElement(root.evalNode("objectFactory"));
        objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
        reflectorFactoryElement(root.evalNode("reflectorFactory"));
        settingsElement(settings);
        // read it after objectFactory and objectWrapperFactory issue #631
        environmentsElement(root.evalNode("environments"));
        databaseIdProviderElement(root.evalNode("databaseIdProvider"));
        typeHandlerElement(root.evalNode("typeHandlers"));
        mapperElement(root.evalNode("mappers"));
    } catch (Exception e) {
        throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);
    }
}
Also used : BuilderException(org.apache.ibatis.builder.BuilderException) Properties(java.util.Properties) BuilderException(org.apache.ibatis.builder.BuilderException)

Aggregations

BuilderException (org.apache.ibatis.builder.BuilderException)14 Properties (java.util.Properties)6 XNode (org.apache.ibatis.parsing.XNode)4 IncompleteElementException (org.apache.ibatis.builder.IncompleteElementException)2 NodeList (org.w3c.dom.NodeList)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Annotation (java.lang.annotation.Annotation)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 CacheNamespaceRef (org.apache.ibatis.annotations.CacheNamespaceRef)1 BindingException (org.apache.ibatis.binding.BindingException)1 DataSourceFactory (org.apache.ibatis.datasource.DataSourceFactory)1 MetaClass (org.apache.ibatis.reflection.MetaClass)1 TransactionFactory (org.apache.ibatis.transaction.TransactionFactory)1 Node (org.w3c.dom.Node)1 ErrorHandler (org.xml.sax.ErrorHandler)1