[#3830] Add support for XSL transformation to XMLDatabase

This commit is contained in:
lukaseder 2014-12-02 17:19:53 +01:00
parent fef01e9885
commit 020f17f58c
2 changed files with 88 additions and 29 deletions

View File

@ -160,31 +160,6 @@
</dependencies>
</plugin>
<!-- The XSLT plugin transforming XML files prior to code generation -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>xml-maven-plugin</artifactId>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>transform</goal>
</goals>
</execution>
</executions>
<configuration>
<transformationSets>
<transformationSet>
<dir>src/main/resources</dir>
<includes>
<include>vertabelo-export.xml</include>
</includes>
<stylesheet>src/main/resources/vertabelo-2-jooq.xsl</stylesheet>
</transformationSet>
</transformationSets>
</configuration>
</plugin>
<!-- The jOOQ code generator plugin -->
<plugin>
<groupId>org.jooq</groupId>
@ -210,7 +185,11 @@
</property>
<property>
<key>xml-file</key>
<value>target/generated-resources/xml/xslt/vertabelo-export.xml</value>
<value>src/main/resources/vertabelo-export.xml</value>
</property>
<property>
<key>xsl-file</key>
<value>src/main/resources/vertabelo-2-jooq.xsl</value>
</property>
</properties>
<inputSchema>PUBLIC</inputSchema>

View File

@ -46,16 +46,27 @@ import static org.jooq.util.xml.jaxb.TableConstraintType.PRIMARY_KEY;
import static org.jooq.util.xml.jaxb.TableConstraintType.UNIQUE;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.xml.bind.JAXB;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.jooq.DSLContext;
import org.jooq.SQLDialect;
import org.jooq.impl.DSL;
import org.jooq.tools.JooqLogger;
import org.jooq.tools.StringUtils;
import org.jooq.util.AbstractDatabase;
import org.jooq.util.ArrayDefinition;
@ -87,11 +98,80 @@ import org.jooq.util.xml.jaxb.TableConstraintType;
*/
public class XMLDatabase extends AbstractDatabase {
InformationSchema info;
private static final JooqLogger log = JooqLogger.getLogger(XMLDatabase.class);
/**
* The property name for the XML file
*/
public static final String P_XML_FILE = "xml-file";
/**
* The property name for the XSL file that pre-processes the XML file
*/
public static final String P_XSL_FILE = "xsl-file";
/**
* The property name for the dialect name
*/
public static final String P_DIALECT = "dialect";
InformationSchema info;
private InformationSchema info() {
if (info == null) {
info = JAXB.unmarshal(new File(getProperties().getProperty("xml-file")), InformationSchema.class);
String xml = getProperties().getProperty(P_XML_FILE);
String xsl = getProperties().getProperty(P_XSL_FILE);
InputStream xmlIs = null;
InputStream xslIs = null;
log.info("Using XML file", xml);
try {
xmlIs = XMLDatabase.class.getResourceAsStream(xml);
if (xmlIs == null)
xmlIs = new FileInputStream(xml);
if (StringUtils.isBlank(xsl)) {
info = JAXB.unmarshal(new File(xml), InformationSchema.class);
}
else {
log.info("Using XSL file", xsl);
xslIs = XMLDatabase.class.getResourceAsStream(xsl);
if (xslIs == null)
xslIs = new FileInputStream(xsl);
try {
StringWriter writer = new StringWriter();
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(new StreamSource(xslIs));
transformer.transform(new StreamSource(xmlIs), new StreamResult(writer));
info = JAXB.unmarshal(new StringReader(writer.getBuffer().toString()), InformationSchema.class);
}
catch (TransformerException e) {
throw new RuntimeException("Error while transforming XML file " + xml + " with XSL file " + xsl, e);
}
}
}
catch (IOException e) {
throw new RuntimeException("Error while opening files " + xml + " or " + xsl, e);
}
finally {
if (xmlIs != null) {
try {
xmlIs.close();
}
catch (Exception ignore) {}
}
if (xslIs != null) {
try {
xslIs.close();
}
catch (Exception ignore) {}
}
}
}
return info;
@ -104,7 +184,7 @@ public class XMLDatabase extends AbstractDatabase {
SQLDialect dialect = SQLDialect.SQL99;
try {
dialect = SQLDialect.valueOf(getProperties().getProperty("dialect"));
dialect = SQLDialect.valueOf(getProperties().getProperty(P_DIALECT));
}
catch (Exception ignore) {}