Search in sources :

Example 1 with UtfBomStripperInputStream

use of liquibase.resource.UtfBomStripperInputStream in project liquibase by liquibase.

the class XMLChangeLogSAXParser method parseToNode.

@Override
protected ParsedNode parseToNode(String physicalChangeLogLocation, ChangeLogParameters changeLogParameters, ResourceAccessor resourceAccessor) throws ChangeLogParseException {
    InputStream inputStream = null;
    try {
        SAXParser parser = saxParserFactory.newSAXParser();
        try {
            parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");
        } catch (SAXNotRecognizedException e) {
        //ok, parser must not support it
        } catch (SAXNotSupportedException e) {
        //ok, parser must not support it
        }
        XMLReader xmlReader = parser.getXMLReader();
        LiquibaseEntityResolver resolver = new LiquibaseEntityResolver(this);
        resolver.useResoureAccessor(resourceAccessor, FilenameUtils.getFullPath(physicalChangeLogLocation));
        xmlReader.setEntityResolver(resolver);
        xmlReader.setErrorHandler(new ErrorHandler() {

            @Override
            public void warning(SAXParseException exception) throws SAXException {
                LogFactory.getLogger().warning(exception.getMessage());
                throw exception;
            }

            @Override
            public void error(SAXParseException exception) throws SAXException {
                LogFactory.getLogger().severe(exception.getMessage());
                throw exception;
            }

            @Override
            public void fatalError(SAXParseException exception) throws SAXException {
                LogFactory.getLogger().severe(exception.getMessage());
                throw exception;
            }
        });
        inputStream = StreamUtil.singleInputStream(physicalChangeLogLocation, resourceAccessor);
        if (inputStream == null) {
            if (physicalChangeLogLocation.startsWith("WEB-INF/classes/")) {
                physicalChangeLogLocation = physicalChangeLogLocation.replaceFirst("WEB-INF/classes/", "");
                inputStream = StreamUtil.singleInputStream(physicalChangeLogLocation, resourceAccessor);
            }
            if (inputStream == null) {
                throw new ChangeLogParseException(physicalChangeLogLocation + " does not exist");
            }
        }
        XMLChangeLogSAXHandler contentHandler = new XMLChangeLogSAXHandler(physicalChangeLogLocation, resourceAccessor, changeLogParameters);
        xmlReader.setContentHandler(contentHandler);
        xmlReader.parse(new InputSource(new UtfBomStripperInputStream(inputStream)));
        return contentHandler.getDatabaseChangeLogTree();
    } catch (ChangeLogParseException e) {
        throw e;
    } catch (IOException e) {
        throw new ChangeLogParseException("Error Reading Migration File: " + e.getMessage(), e);
    } catch (SAXParseException e) {
        throw new ChangeLogParseException("Error parsing line " + e.getLineNumber() + " column " + e.getColumnNumber() + " of " + physicalChangeLogLocation + ": " + e.getMessage(), e);
    } catch (SAXException e) {
        Throwable parentCause = e.getException();
        while (parentCause != null) {
            if (parentCause instanceof ChangeLogParseException) {
                throw ((ChangeLogParseException) parentCause);
            }
            parentCause = parentCause.getCause();
        }
        String reason = e.getMessage();
        String causeReason = null;
        if (e.getCause() != null) {
            causeReason = e.getCause().getMessage();
        }
        //            }
        if (reason == null) {
            if (causeReason != null) {
                reason = causeReason;
            } else {
                reason = "Unknown Reason";
            }
        }
        throw new ChangeLogParseException("Invalid Migration File: " + reason, e);
    } catch (Exception e) {
        throw new ChangeLogParseException(e);
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
            // probably ok
            }
        }
    }
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) InputSource(org.xml.sax.InputSource) UtfBomStripperInputStream(liquibase.resource.UtfBomStripperInputStream) InputStream(java.io.InputStream) SAXNotRecognizedException(org.xml.sax.SAXNotRecognizedException) UtfBomStripperInputStream(liquibase.resource.UtfBomStripperInputStream) IOException(java.io.IOException) SAXNotSupportedException(org.xml.sax.SAXNotSupportedException) IOException(java.io.IOException) ChangeLogParseException(liquibase.exception.ChangeLogParseException) SAXNotRecognizedException(org.xml.sax.SAXNotRecognizedException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException) SAXNotSupportedException(org.xml.sax.SAXNotSupportedException) SAXParseException(org.xml.sax.SAXParseException) ChangeLogParseException(liquibase.exception.ChangeLogParseException) SAXParser(javax.xml.parsers.SAXParser) XMLReader(org.xml.sax.XMLReader)

Aggregations

IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 SAXParser (javax.xml.parsers.SAXParser)1 ChangeLogParseException (liquibase.exception.ChangeLogParseException)1 UtfBomStripperInputStream (liquibase.resource.UtfBomStripperInputStream)1 ErrorHandler (org.xml.sax.ErrorHandler)1 InputSource (org.xml.sax.InputSource)1 SAXException (org.xml.sax.SAXException)1 SAXNotRecognizedException (org.xml.sax.SAXNotRecognizedException)1 SAXNotSupportedException (org.xml.sax.SAXNotSupportedException)1 SAXParseException (org.xml.sax.SAXParseException)1 XMLReader (org.xml.sax.XMLReader)1