Search in sources :

Example 1 with Configuration

use of org.jooq.util.jaxb.Configuration in project jOOQ by jOOQ.

the class GenerationTool method load.

/**
     * Load a jOOQ codegen configuration file from an input stream
     */
public static Configuration load(InputStream in) throws IOException {
    // [#1149] If there is no namespace defined, add the default one
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    copyLarge(in, out);
    String xml = out.toString();
    // TODO [#1201] Add better error handling here
    xml = xml.replaceAll("<(\\w+:)?configuration xmlns(:\\w+)?=\"http://www.jooq.org/xsd/jooq-codegen-\\d+\\.\\d+\\.\\d+.xsd\">", "<$1configuration xmlns$2=\"" + Constants.NS_CODEGEN + "\">");
    xml = xml.replace("<configuration>", "<configuration xmlns=\"" + Constants.NS_CODEGEN + "\">");
    try {
        SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        javax.xml.validation.Schema schema = sf.newSchema(GenerationTool.class.getResource("/xsd/" + Constants.XSD_CODEGEN));
        JAXBContext ctx = JAXBContext.newInstance(Configuration.class);
        Unmarshaller unmarshaller = ctx.createUnmarshaller();
        unmarshaller.setSchema(schema);
        unmarshaller.setEventHandler(new ValidationEventHandler() {

            @Override
            public boolean handleEvent(ValidationEvent event) {
                log.warn("Unmarshal warning", event.getMessage());
                return true;
            }
        });
        return (Configuration) unmarshaller.unmarshal(new StringReader(xml));
    } catch (Exception e) {
        throw new GeneratorException("Error while reading XML configuration", e);
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) ValidationEventHandler(javax.xml.bind.ValidationEventHandler) Configuration(org.jooq.util.jaxb.Configuration) JAXBContext(javax.xml.bind.JAXBContext) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StringUtils.defaultString(org.jooq.tools.StringUtils.defaultString) SQLException(java.sql.SQLException) IOException(java.io.IOException) ValidationEvent(javax.xml.bind.ValidationEvent) StringReader(java.io.StringReader) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 2 with Configuration

use of org.jooq.util.jaxb.Configuration in project jOOQ by jOOQ.

the class Plugin method execute.

@Override
public void execute() throws MojoExecutionException {
    if (skip) {
        getLog().info("Skipping jOOQ code generation");
        return;
    }
    if (configurationFile != null) {
        getLog().info("Reading external configuration");
        File file = new File(configurationFile);
        if (!file.isAbsolute())
            file = new File(project.getBasedir(), configurationFile);
        FileInputStream in = null;
        try {
            in = new FileInputStream(file);
            Configuration configuration = GenerationTool.load(in);
            generator = configuration.getGenerator();
            jdbc = configuration.getJdbc();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException ignore) {
                }
            }
        }
    }
    //         correctly at this point. We'll log them all here.
    if (generator == null) {
        getLog().error("Incorrect configuration of jOOQ code generation tool");
        getLog().error("\n" + "The jOOQ-codegen-maven module's generator configuration is not set up correctly.\n" + "This can have a variety of reasons, among which:\n" + "- Your pom.xml's <configuration> contains invalid XML according to " + XSD_CODEGEN + "\n" + "- There is a version or artifact mismatch between your pom.xml and your commandline");
        throw new MojoExecutionException("Incorrect configuration of jOOQ code generation tool. See error above for details.");
    }
    ClassLoader oldCL = Thread.currentThread().getContextClassLoader();
    try {
        // [#2886] Add the surrounding project's dependencies to the current classloader
        Thread.currentThread().setContextClassLoader(getClassLoader());
        // [#5881] Target is allowed to be null
        if (generator.getTarget() == null)
            generator.setTarget(new Target());
        if (generator.getTarget().getDirectory() == null)
            generator.getTarget().setDirectory(DEFAULT_TARGET_DIRECTORY);
        // [#2887] Patch relative paths to take plugin execution basedir into account
        if (!new File(generator.getTarget().getDirectory()).isAbsolute())
            generator.getTarget().setDirectory(project.getBasedir() + File.separator + generator.getTarget().getDirectory());
        Configuration configuration = new Configuration();
        configuration.setLogging(logging);
        configuration.setJdbc(jdbc);
        configuration.setGenerator(generator);
        StringWriter writer = new StringWriter();
        JAXB.marshal(configuration, writer);
        getLog().debug("Using this configuration:\n" + writer.toString());
        GenerationTool.generate(configuration);
    } catch (Exception ex) {
        throw new MojoExecutionException("Error running jOOQ code generation tool", ex);
    } finally // [#2886] Restore old class loader
    {
        Thread.currentThread().setContextClassLoader(oldCL);
    }
    project.addCompileSourceRoot(generator.getTarget().getDirectory());
}
Also used : Target(org.jooq.util.jaxb.Target) Configuration(org.jooq.util.jaxb.Configuration) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) StringWriter(java.io.StringWriter) URLClassLoader(java.net.URLClassLoader) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException)

Aggregations

IOException (java.io.IOException)2 Configuration (org.jooq.util.jaxb.Configuration)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 URLClassLoader (java.net.URLClassLoader)1 SQLException (java.sql.SQLException)1 JAXBContext (javax.xml.bind.JAXBContext)1 Unmarshaller (javax.xml.bind.Unmarshaller)1 ValidationEvent (javax.xml.bind.ValidationEvent)1 ValidationEventHandler (javax.xml.bind.ValidationEventHandler)1 SchemaFactory (javax.xml.validation.SchemaFactory)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 StringUtils.defaultString (org.jooq.tools.StringUtils.defaultString)1 Target (org.jooq.util.jaxb.Target)1