Search in sources :

Example 1 with FilePattern

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;
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) XMLStreamReader(javax.xml.stream.XMLStreamReader) Reader(java.io.Reader) StringReader(java.io.StringReader) UnsupportedEncodingException(java.io.UnsupportedEncodingException) InformationSchema(org.jooq.util.xml.jaxb.InformationSchema) IOException(org.jooq.exception.IOException) FileInputStream(java.io.FileInputStream) IOException(org.jooq.exception.IOException) XMLStreamException(javax.xml.stream.XMLStreamException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) TransformerException(javax.xml.transform.TransformerException) SQLException(java.sql.SQLException) XMLStreamException(javax.xml.stream.XMLStreamException) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) FilePattern(org.jooq.FilePattern) File(java.io.File) TransformerException(javax.xml.transform.TransformerException)

Example 2 with FilePattern

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;
    }
}
Also used : ParserException(org.jooq.impl.ParserException) VisitContext(org.jooq.VisitContext) DSLContext(org.jooq.DSLContext) FilePattern(org.jooq.FilePattern) DefaultVisitListener(org.jooq.impl.DefaultVisitListener) File(java.io.File) Settings(org.jooq.conf.Settings) Name(org.jooq.Name)

Aggregations

File (java.io.File)2 FilePattern (org.jooq.FilePattern)2 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 SQLException (java.sql.SQLException)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1 Transformer (javax.xml.transform.Transformer)1 TransformerException (javax.xml.transform.TransformerException)1 TransformerFactory (javax.xml.transform.TransformerFactory)1 StreamResult (javax.xml.transform.stream.StreamResult)1 StreamSource (javax.xml.transform.stream.StreamSource)1 DSLContext (org.jooq.DSLContext)1 Name (org.jooq.Name)1 VisitContext (org.jooq.VisitContext)1 Settings (org.jooq.conf.Settings)1