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);
}
}
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());
}
Aggregations