use of org.jooq.FilePattern in project jOOQ by jOOQ.
the class XMLDatabase method info.
private InformationSchema info() {
if (info == null) {
// [#8118] Regardless of failure, prevent NPEs from subsequent calls
info = new InformationSchema();
// [#8115] Support old property name style for backwards compatibility reasons
final String xml = getProperties().getProperty("xmlFiles", getProperties().getProperty("xmlFile", getProperties().getProperty("xml-file")));
final String xsl = getProperties().getProperty("xslFile", getProperties().getProperty("xsl-file"));
final String sort = getProperties().getProperty("sort", "semantic").toLowerCase();
if (xml == null)
throw new RuntimeException("Must provide an xmlFile property");
try {
new FilePattern().basedir(new File(getBasedir())).pattern(xml).sort(Sort.of(sort)).load(source -> {
String content;
Reader reader = null;
try {
if (StringUtils.isBlank(xsl)) {
// [#7414] Default to reading UTF-8
content = source.readString();
// [#7414] Alternatively, read the encoding from the XML file
try {
XMLStreamReader xmlReader = XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(content));
String encoding = xmlReader.getCharacterEncodingScheme();
// See https://stackoverflow.com/a/27147259/521799
if (encoding != null && !"UTF-8".equals(encoding))
content = new String(content.getBytes("UTF-8"), encoding);
} catch (XMLStreamException e1) {
log.warn("Could not open XML Stream: " + e1.getMessage());
} catch (UnsupportedEncodingException e2) {
log.warn("Unsupported encoding: " + e2.getMessage());
}
} else {
InputStream xslIs = null;
try {
log.info("Using XSL file", xsl);
xslIs = XMLDatabase.class.getResourceAsStream(xsl);
if (xslIs == null)
xslIs = new FileInputStream(xsl);
StringWriter writer = new StringWriter();
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(new StreamSource(xslIs));
transformer.transform(new StreamSource(reader), new StreamResult(writer));
content = writer.getBuffer().toString();
} catch (java.io.IOException e3) {
throw new IOException("Error while loading XSL file", e3);
} catch (TransformerException e4) {
throw new RuntimeException("Error while transforming XML file " + xml + " with XSL file " + xsl, e4);
} finally {
JDBCUtils.safeClose(xslIs);
}
}
} finally {
JDBCUtils.safeClose(reader);
}
// TODO [#1201] Add better error handling here
content = content.replaceAll("<(\\w+:)?information_schema xmlns(:\\w+)?=\"http://www.jooq.org/xsd/jooq-meta-\\d+\\.\\d+\\.\\d+.xsd\">", "<$1information_schema xmlns$2=\"" + Constants.NS_META + "\">");
content = content.replace("<information_schema>", "<information_schema xmlns=\"" + Constants.NS_META + "\">");
info = MiniJAXB.append(info, MiniJAXB.unmarshal(content, InformationSchema.class));
});
} catch (Exception e) {
throw new RuntimeException("Error while opening files " + xml + " or " + xsl, e);
}
}
return info;
}
use of org.jooq.FilePattern in project jOOQ by jOOQ.
the class DDLDatabase method export.
@Override
protected void export() throws Exception {
Settings defaultSettings = new Settings();
String scripts = getProperties().getProperty("scripts");
String encoding = getProperties().getProperty("encoding", "UTF-8");
String sort = getProperties().getProperty("sort", "semantic").toLowerCase();
final String defaultNameCase = getProperties().getProperty("defaultNameCase", "as_is").toUpperCase();
boolean parseIgnoreComments = !"false".equalsIgnoreCase(getProperties().getProperty("parseIgnoreComments"));
String parseIgnoreCommentStart = getProperties().getProperty("parseIgnoreCommentStart", defaultSettings.getParseIgnoreCommentStart());
String parseIgnoreCommentStop = getProperties().getProperty("parseIgnoreCommentStop", defaultSettings.getParseIgnoreCommentStop());
logExecutedQueries = !"false".equalsIgnoreCase(getProperties().getProperty("logExecutedQueries"));
logExecutionResults = !"false".equalsIgnoreCase(getProperties().getProperty("logExecutionResults"));
if (isBlank(scripts)) {
scripts = "";
log.warn("No scripts defined", "It is recommended that you provide an explicit script directory to scan");
}
try {
final DSLContext ctx = DSL.using(connection(), new Settings().withParseIgnoreComments(parseIgnoreComments).withParseIgnoreCommentStart(parseIgnoreCommentStart).withParseIgnoreCommentStop(parseIgnoreCommentStop).withParseUnknownFunctions(ParseUnknownFunctions.IGNORE));
// [#7771] [#8011] Ignore all parsed storage clauses when executing the statements
ctx.data("org.jooq.ddl.ignore-storage-clauses", true);
// [#8910] Parse things a bit differently for use with the DDLDatabase
ctx.data("org.jooq.ddl.parse-for-ddldatabase", true);
if (!"AS_IS".equals(defaultNameCase)) {
ctx.configuration().set(new DefaultVisitListener() {
@Override
public void visitStart(VisitContext vc) {
if (vc.queryPart() instanceof Name) {
Name n = (Name) vc.queryPart();
Name[] parts = n.parts();
boolean changed = false;
for (int i = 0; i < parts.length; i++) {
// flag for DSL.systemName() names
if (parts[i].quoted() == Quoted.UNQUOTED) {
parts[i] = DSL.quotedName("UPPER".equals(defaultNameCase) ? parts[i].first().toUpperCase(renderLocale(ctx.settings())) : parts[i].first().toLowerCase(renderLocale(ctx.settings())));
changed = true;
}
}
if (changed)
vc.queryPart(DSL.name(parts));
}
}
});
}
new FilePattern().encoding(encoding).basedir(new File(getBasedir())).pattern(scripts).sort(Sort.of(sort)).load(source -> DDLDatabase.this.load(ctx, source));
} catch (ParserException e) {
log.error("An exception occurred while parsing script source : " + scripts + ". Please report this error to https://github.com/jOOQ/jOOQ/issues/new", e);
throw e;
}
}
Aggregations