Search in sources :

Example 1 with XMLParserException

use of org.mybatis.generator.exception.XMLParserException in project generator by mybatis.

the class MyBatisGeneratorConfigurationParser method parseProperties.

protected void parseProperties(Configuration configuration, Node node) throws XMLParserException {
    Properties attributes = parseAttributes(node);
    //$NON-NLS-1$
    String resource = attributes.getProperty("resource");
    //$NON-NLS-1$
    String url = attributes.getProperty("url");
    if (!stringHasValue(resource) && !stringHasValue(url)) {
        //$NON-NLS-1$
        throw new XMLParserException(getString("RuntimeError.14"));
    }
    if (stringHasValue(resource) && stringHasValue(url)) {
        //$NON-NLS-1$
        throw new XMLParserException(getString("RuntimeError.14"));
    }
    URL resourceUrl;
    try {
        if (stringHasValue(resource)) {
            resourceUrl = ObjectFactory.getResource(resource);
            if (resourceUrl == null) {
                throw new XMLParserException(getString("RuntimeError.15", //$NON-NLS-1$
                resource));
            }
        } else {
            resourceUrl = new URL(url);
        }
        InputStream inputStream = resourceUrl.openConnection().getInputStream();
        configurationProperties.load(inputStream);
        inputStream.close();
    } catch (IOException e) {
        if (stringHasValue(resource)) {
            throw new XMLParserException(getString("RuntimeError.16", //$NON-NLS-1$
            resource));
        } else {
            throw new XMLParserException(getString("RuntimeError.17", //$NON-NLS-1$
            url));
        }
    }
}
Also used : XMLParserException(org.mybatis.generator.exception.XMLParserException) InputStream(java.io.InputStream) Messages.getString(org.mybatis.generator.internal.util.messages.Messages.getString) IOException(java.io.IOException) Properties(java.util.Properties) URL(java.net.URL)

Example 2 with XMLParserException

use of org.mybatis.generator.exception.XMLParserException in project jeesuite-libs by vakinge.

the class MyBatisGeneratorTool method main.

public static void main(String[] args) {
    List<String> warnings = new ArrayList<String>();
    String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
    boolean overwrite = true;
    File configFile = new File(path + "/../../src/test/resources/generator/generatorConfig.xml");
    ConfigurationParser cp = new ConfigurationParser(warnings);
    Configuration config = null;
    try {
        config = cp.parseConfiguration(configFile);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (XMLParserException e) {
        e.printStackTrace();
    }
    DefaultShellCallback callback = new DefaultShellCallback(overwrite);
    MyBatisGenerator myBatisGenerator = null;
    try {
        myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
    } catch (InvalidConfigurationException e) {
        e.printStackTrace();
    }
    try {
        myBatisGenerator.generate(null);
    } catch (SQLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
Also used : Configuration(org.mybatis.generator.config.Configuration) XMLParserException(org.mybatis.generator.exception.XMLParserException) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) DefaultShellCallback(org.mybatis.generator.internal.DefaultShellCallback) InvalidConfigurationException(org.mybatis.generator.exception.InvalidConfigurationException) ConfigurationParser(org.mybatis.generator.config.xml.ConfigurationParser) File(java.io.File) MyBatisGenerator(org.mybatis.generator.api.MyBatisGenerator)

Example 3 with XMLParserException

use of org.mybatis.generator.exception.XMLParserException in project generator by mybatis.

the class MyBatisGeneratorConfigurationParser method parseProperties.

protected void parseProperties(Node node) throws XMLParserException {
    Properties attributes = parseAttributes(node);
    // $NON-NLS-1$
    String resource = attributes.getProperty("resource");
    // $NON-NLS-1$
    String url = attributes.getProperty("url");
    if (!stringHasValue(resource) && !stringHasValue(url)) {
        // $NON-NLS-1$
        throw new XMLParserException(getString("RuntimeError.14"));
    }
    if (stringHasValue(resource) && stringHasValue(url)) {
        // $NON-NLS-1$
        throw new XMLParserException(getString("RuntimeError.14"));
    }
    URL resourceUrl;
    try {
        if (stringHasValue(resource)) {
            resourceUrl = ObjectFactory.getResource(resource);
            if (resourceUrl == null) {
                throw new XMLParserException(getString("RuntimeError.15", // $NON-NLS-1$
                resource));
            }
        } else {
            resourceUrl = new URL(url);
        }
        InputStream inputStream = resourceUrl.openConnection().getInputStream();
        configurationProperties.load(inputStream);
        inputStream.close();
    } catch (IOException e) {
        if (stringHasValue(resource)) {
            throw new XMLParserException(getString("RuntimeError.16", // $NON-NLS-1$
            resource));
        } else {
            throw new XMLParserException(getString("RuntimeError.17", // $NON-NLS-1$
            url));
        }
    }
}
Also used : XMLParserException(org.mybatis.generator.exception.XMLParserException) InputStream(java.io.InputStream) Messages.getString(org.mybatis.generator.internal.util.messages.Messages.getString) IOException(java.io.IOException) Properties(java.util.Properties) URL(java.net.URL)

Example 4 with XMLParserException

use of org.mybatis.generator.exception.XMLParserException 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 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<>();
    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<>();
    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 | IOException e) {
        e.printStackTrace(System.out);
        return;
    } catch (InvalidConfigurationException e) {
        // $NON-NLS-1$
        writeLine(getString("Progress.16"));
        for (String error : e.getErrors()) {
            writeLine(error);
        }
        return;
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
    for (String warning : warnings) {
        writeLine(warning);
    }
    if (warnings.isEmpty()) {
        // $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)

Example 5 with XMLParserException

use of org.mybatis.generator.exception.XMLParserException in project generator by mybatis.

the class RunGeneratorThread method run.

/*
     * (non-Javadoc)
     * 
     * @see org.eclipse.core.resources.IWorkspaceRunnable#run(org.eclipse.core.runtime.IProgressMonitor)
     */
public void run(IProgressMonitor monitor) throws CoreException {
    SubMonitor subMonitor = SubMonitor.convert(monitor, 1000);
    subMonitor.beginTask("Generating MyBatis/iBATIS Artifacts:", 1000);
    setClassLoader();
    try {
        subMonitor.subTask("Parsing Configuration");
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(inputFile.getLocation().toFile());
        subMonitor.worked(50);
        MyBatisGenerator mybatisGenerator = new MyBatisGenerator(config, new EclipseShellCallback(), warnings);
        monitor.subTask("Generating Files from Database Tables");
        SubMonitor spm = subMonitor.newChild(950);
        mybatisGenerator.generate(new EclipseProgressCallback(spm));
    } catch (InterruptedException e) {
        throw new OperationCanceledException();
    } catch (SQLException e) {
        Status status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR, e.getMessage(), e);
        Activator.getDefault().getLog().log(status);
        throw new CoreException(status);
    } catch (IOException e) {
        Status status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR, e.getMessage(), e);
        Activator.getDefault().getLog().log(status);
        throw new CoreException(status);
    } catch (XMLParserException e) {
        List<String> errors = e.getErrors();
        MultiStatus multiStatus = new MultiStatus(Activator.PLUGIN_ID, IStatus.ERROR, "XML Parser Errors\n  See Details for more Information", null);
        Iterator<String> iter = errors.iterator();
        while (iter.hasNext()) {
            Status message = new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR, iter.next(), null);
            multiStatus.add(message);
        }
        throw new CoreException(multiStatus);
    } catch (InvalidConfigurationException e) {
        List<String> errors = e.getErrors();
        MultiStatus multiStatus = new MultiStatus(Activator.PLUGIN_ID, IStatus.ERROR, "Invalid Configuration\n  See Details for more Information", null);
        Iterator<String> iter = errors.iterator();
        while (iter.hasNext()) {
            Status message = new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR, iter.next(), null);
            multiStatus.add(message);
        }
        throw new CoreException(multiStatus);
    } finally {
        monitor.done();
        restoreClassLoader();
    }
}
Also used : EclipseShellCallback(org.mybatis.generator.eclipse.core.callback.EclipseShellCallback) MultiStatus(org.eclipse.core.runtime.MultiStatus) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) Configuration(org.mybatis.generator.config.Configuration) SQLException(java.sql.SQLException) XMLParserException(org.mybatis.generator.exception.XMLParserException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) SubMonitor(org.eclipse.core.runtime.SubMonitor) MultiStatus(org.eclipse.core.runtime.MultiStatus) IOException(java.io.IOException) InvalidConfigurationException(org.mybatis.generator.exception.InvalidConfigurationException) EclipseProgressCallback(org.mybatis.generator.eclipse.core.callback.EclipseProgressCallback) CoreException(org.eclipse.core.runtime.CoreException) ConfigurationParser(org.mybatis.generator.config.xml.ConfigurationParser) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) MyBatisGenerator(org.mybatis.generator.api.MyBatisGenerator)

Aggregations

XMLParserException (org.mybatis.generator.exception.XMLParserException)10 IOException (java.io.IOException)9 Configuration (org.mybatis.generator.config.Configuration)8 SQLException (java.sql.SQLException)7 ArrayList (java.util.ArrayList)7 ConfigurationParser (org.mybatis.generator.config.xml.ConfigurationParser)7 InvalidConfigurationException (org.mybatis.generator.exception.InvalidConfigurationException)7 MyBatisGenerator (org.mybatis.generator.api.MyBatisGenerator)6 File (java.io.File)5 Properties (java.util.Properties)4 DefaultShellCallback (org.mybatis.generator.internal.DefaultShellCallback)4 Messages.getString (org.mybatis.generator.internal.util.messages.Messages.getString)4 HashSet (java.util.HashSet)3 StringTokenizer (java.util.StringTokenizer)3 InputStream (java.io.InputStream)2 URL (java.net.URL)2 BuildException (org.apache.tools.ant.BuildException)2 SubMonitor (org.eclipse.core.runtime.SubMonitor)2 EclipseProgressCallback (org.mybatis.generator.eclipse.core.callback.EclipseProgressCallback)2 EclipseShellCallback (org.mybatis.generator.eclipse.core.callback.EclipseShellCallback)2