Search in sources :

Example 1 with Configuration

use of org.mybatis.generator.config.Configuration in project generator by mybatis.

the class IbatorConfigurationParser method parseIbatorConfiguration.

public Configuration parseIbatorConfiguration(Element rootNode) throws XMLParserException {
    Configuration configuration = new Configuration();
    NodeList nodeList = rootNode.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node childNode = nodeList.item(i);
        if (childNode.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }
        if ("properties".equals(childNode.getNodeName())) {
            //$NON-NLS-1$
            parseProperties(configuration, childNode);
        } else if ("classPathEntry".equals(childNode.getNodeName())) {
            //$NON-NLS-1$
            parseClassPathEntry(configuration, childNode);
        } else if ("ibatorContext".equals(childNode.getNodeName())) {
            //$NON-NLS-1$
            parseIbatorContext(configuration, childNode);
        }
    }
    return configuration;
}
Also used : Configuration(org.mybatis.generator.config.Configuration) JavaClientGeneratorConfiguration(org.mybatis.generator.config.JavaClientGeneratorConfiguration) PluginConfiguration(org.mybatis.generator.config.PluginConfiguration) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node)

Example 2 with Configuration

use of org.mybatis.generator.config.Configuration in project generator by mybatis.

the class MyBatisGeneratorTest method testGenerateInvalidConfigWithTwoConnectionSources.

@Test(expected = InvalidConfigurationException.class)
public void testGenerateInvalidConfigWithTwoConnectionSources() throws Exception {
    List<String> warnings = new ArrayList<String>();
    Configuration config = new Configuration();
    Context context = new Context(ModelType.HIERARCHICAL);
    context.setId("MyContext");
    context.setConnectionFactoryConfiguration(new ConnectionFactoryConfiguration());
    context.setJdbcConnectionConfiguration(new JDBCConnectionConfiguration());
    config.addContext(context);
    DefaultShellCallback shellCallback = new DefaultShellCallback(true);
    try {
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, shellCallback, warnings);
        myBatisGenerator.generate(null, null, null, false);
    } catch (InvalidConfigurationException e) {
        assertEquals(3, e.getErrors().size());
        throw e;
    }
}
Also used : Context(org.mybatis.generator.config.Context) Configuration(org.mybatis.generator.config.Configuration) ConnectionFactoryConfiguration(org.mybatis.generator.config.ConnectionFactoryConfiguration) JDBCConnectionConfiguration(org.mybatis.generator.config.JDBCConnectionConfiguration) ArrayList(java.util.ArrayList) DefaultShellCallback(org.mybatis.generator.internal.DefaultShellCallback) JDBCConnectionConfiguration(org.mybatis.generator.config.JDBCConnectionConfiguration) ConnectionFactoryConfiguration(org.mybatis.generator.config.ConnectionFactoryConfiguration) MyBatisGenerator(org.mybatis.generator.api.MyBatisGenerator) InvalidConfigurationException(org.mybatis.generator.exception.InvalidConfigurationException) Test(org.junit.Test)

Example 3 with Configuration

use of org.mybatis.generator.config.Configuration in project generator by mybatis.

the class MyBatisGeneratorTest method testGenerateIbatis2WithInvalidConfig.

@Test(expected = InvalidConfigurationException.class)
public void testGenerateIbatis2WithInvalidConfig() throws Exception {
    List<String> warnings = new ArrayList<String>();
    ConfigurationParser cp = new ConfigurationParser(warnings);
    Configuration config = cp.parseConfiguration(this.getClass().getClassLoader().getResourceAsStream("generatorConfigIbatis2_badConfig.xml"));
    DefaultShellCallback shellCallback = new DefaultShellCallback(true);
    try {
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, shellCallback, warnings);
        myBatisGenerator.generate(null, null, null, false);
    } catch (InvalidConfigurationException e) {
        assertEquals(1, e.getErrors().size());
        throw e;
    }
}
Also used : Configuration(org.mybatis.generator.config.Configuration) ConnectionFactoryConfiguration(org.mybatis.generator.config.ConnectionFactoryConfiguration) JDBCConnectionConfiguration(org.mybatis.generator.config.JDBCConnectionConfiguration) ArrayList(java.util.ArrayList) ConfigurationParser(org.mybatis.generator.config.xml.ConfigurationParser) DefaultShellCallback(org.mybatis.generator.internal.DefaultShellCallback) MyBatisGenerator(org.mybatis.generator.api.MyBatisGenerator) InvalidConfigurationException(org.mybatis.generator.exception.InvalidConfigurationException) Test(org.junit.Test)

Example 4 with Configuration

use of org.mybatis.generator.config.Configuration in project generator by mybatis.

the class XmlCodeGenerationTest method generateXmlFiles.

private static List<GeneratedXmlFile> generateXmlFiles(String configFile) throws Exception {
    List<String> warnings = new ArrayList<String>();
    ConfigurationParser cp = new ConfigurationParser(warnings);
    Configuration config = cp.parseConfiguration(JavaCodeGenerationTest.class.getResourceAsStream(configFile));
    DefaultShellCallback shellCallback = new DefaultShellCallback(true);
    MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, shellCallback, warnings);
    myBatisGenerator.generate(null, null, null, false);
    return myBatisGenerator.getGeneratedXmlFiles();
}
Also used : Configuration(org.mybatis.generator.config.Configuration) ArrayList(java.util.ArrayList) ConfigurationParser(org.mybatis.generator.config.xml.ConfigurationParser) DefaultShellCallback(org.mybatis.generator.internal.DefaultShellCallback) MyBatisGenerator(org.mybatis.generator.api.MyBatisGenerator)

Example 5 with Configuration

use of org.mybatis.generator.config.Configuration in project generator by mybatis.

the class ShellRunner method main.

public static void main(String[] args) {
    if (args.length == 0) {
        usage();
        System.exit(0);
        // only to satisfy compiler, never returns
        return;
    }
    Map<String, String> arguments = parseCommandLine(args);
    if (arguments.containsKey(HELP_1)) {
        usage();
        System.exit(0);
        // only to satisfy compiler, never returns
        return;
    }
    if (!arguments.containsKey(CONFIG_FILE)) {
        //$NON-NLS-1$
        writeLine(getString("RuntimeError.0"));
        return;
    }
    List<String> warnings = new ArrayList<String>();
    String configfile = arguments.get(CONFIG_FILE);
    File configurationFile = new File(configfile);
    if (!configurationFile.exists()) {
        //$NON-NLS-1$
        writeLine(getString("RuntimeError.1", configfile));
        return;
    }
    Set<String> fullyqualifiedTables = new HashSet<String>();
    if (arguments.containsKey(TABLES)) {
        //$NON-NLS-1$
        StringTokenizer st = new StringTokenizer(arguments.get(TABLES), ",");
        while (st.hasMoreTokens()) {
            String s = st.nextToken().trim();
            if (s.length() > 0) {
                fullyqualifiedTables.add(s);
            }
        }
    }
    Set<String> contexts = new HashSet<String>();
    if (arguments.containsKey(CONTEXT_IDS)) {
        StringTokenizer st = new StringTokenizer(arguments.get(CONTEXT_IDS), //$NON-NLS-1$
        ",");
        while (st.hasMoreTokens()) {
            String s = st.nextToken().trim();
            if (s.length() > 0) {
                contexts.add(s);
            }
        }
    }
    try {
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(configurationFile);
        DefaultShellCallback shellCallback = new DefaultShellCallback(arguments.containsKey(OVERWRITE));
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, shellCallback, warnings);
        ProgressCallback progressCallback = arguments.containsKey(VERBOSE) ? new VerboseProgressCallback() : null;
        myBatisGenerator.generate(progressCallback, contexts, fullyqualifiedTables);
    } catch (XMLParserException e) {
        //$NON-NLS-1$
        writeLine(getString("Progress.3"));
        writeLine();
        for (String error : e.getErrors()) {
            writeLine(error);
        }
        return;
    } catch (SQLException e) {
        e.printStackTrace();
        return;
    } catch (IOException e) {
        e.printStackTrace();
        return;
    } catch (InvalidConfigurationException e) {
        //$NON-NLS-1$
        writeLine(getString("Progress.16"));
        for (String error : e.getErrors()) {
            writeLine(error);
        }
        return;
    } catch (InterruptedException e) {
    // ignore (will never happen with the DefaultShellCallback)
    }
    for (String warning : warnings) {
        writeLine(warning);
    }
    if (warnings.size() == 0) {
        //$NON-NLS-1$
        writeLine(getString("Progress.4"));
    } else {
        writeLine();
        //$NON-NLS-1$
        writeLine(getString("Progress.5"));
    }
}
Also used : Configuration(org.mybatis.generator.config.Configuration) XMLParserException(org.mybatis.generator.exception.XMLParserException) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Messages.getString(org.mybatis.generator.internal.util.messages.Messages.getString) DefaultShellCallback(org.mybatis.generator.internal.DefaultShellCallback) IOException(java.io.IOException) InvalidConfigurationException(org.mybatis.generator.exception.InvalidConfigurationException) StringTokenizer(java.util.StringTokenizer) ConfigurationParser(org.mybatis.generator.config.xml.ConfigurationParser) File(java.io.File) HashSet(java.util.HashSet)

Aggregations

Configuration (org.mybatis.generator.config.Configuration)14 ArrayList (java.util.ArrayList)11 MyBatisGenerator (org.mybatis.generator.api.MyBatisGenerator)10 ConfigurationParser (org.mybatis.generator.config.xml.ConfigurationParser)9 InvalidConfigurationException (org.mybatis.generator.exception.InvalidConfigurationException)9 DefaultShellCallback (org.mybatis.generator.internal.DefaultShellCallback)8 XMLParserException (org.mybatis.generator.exception.XMLParserException)6 IOException (java.io.IOException)5 SQLException (java.sql.SQLException)5 ConnectionFactoryConfiguration (org.mybatis.generator.config.ConnectionFactoryConfiguration)5 JDBCConnectionConfiguration (org.mybatis.generator.config.JDBCConnectionConfiguration)5 HashSet (java.util.HashSet)4 StringTokenizer (java.util.StringTokenizer)4 Test (org.junit.Test)4 File (java.io.File)3 Properties (java.util.Properties)2 BuildException (org.apache.tools.ant.BuildException)2 SubMonitor (org.eclipse.core.runtime.SubMonitor)2 Context (org.mybatis.generator.config.Context)2 JavaClientGeneratorConfiguration (org.mybatis.generator.config.JavaClientGeneratorConfiguration)2