Search in sources :

Example 26 with ErrorHandler

use of org.xml.sax.ErrorHandler in project cobar by alibaba.

the class ConfigUtil method getDocument.

public static Document getDocument(final InputStream dtd, InputStream xml) throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);
    factory.setNamespaceAware(false);
    DocumentBuilder builder = factory.newDocumentBuilder();
    builder.setEntityResolver(new EntityResolver() {

        @Override
        public InputSource resolveEntity(String publicId, String systemId) {
            return new InputSource(dtd);
        }
    });
    builder.setErrorHandler(new ErrorHandler() {

        @Override
        public void warning(SAXParseException e) {
        }

        @Override
        public void error(SAXParseException e) throws SAXException {
            throw e;
        }

        @Override
        public void fatalError(SAXParseException e) throws SAXException {
            throw e;
        }
    });
    return builder.parse(xml);
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXParseException(org.xml.sax.SAXParseException) EntityResolver(org.xml.sax.EntityResolver) SAXException(org.xml.sax.SAXException)

Example 27 with ErrorHandler

use of org.xml.sax.ErrorHandler 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)

Example 28 with ErrorHandler

use of org.xml.sax.ErrorHandler in project mybatis-3 by mybatis.

the class XPathParser method createDocument.

private Document createDocument(InputSource inputSource) {
    // important: this must only be called AFTER common constructor
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(validation);
        factory.setNamespaceAware(false);
        factory.setIgnoringComments(true);
        factory.setIgnoringElementContentWhitespace(false);
        factory.setCoalescing(false);
        factory.setExpandEntityReferences(true);
        DocumentBuilder builder = factory.newDocumentBuilder();
        builder.setEntityResolver(entityResolver);
        builder.setErrorHandler(new ErrorHandler() {

            @Override
            public void error(SAXParseException exception) throws SAXException {
                throw exception;
            }

            @Override
            public void fatalError(SAXParseException exception) throws SAXException {
                throw exception;
            }

            @Override
            public void warning(SAXParseException exception) throws SAXException {
            }
        });
        return builder.parse(inputSource);
    } catch (Exception e) {
        throw new BuilderException("Error creating document instance.  Cause: " + e, e);
    }
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) BuilderException(org.apache.ibatis.builder.BuilderException) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXParseException(org.xml.sax.SAXParseException) BuilderException(org.apache.ibatis.builder.BuilderException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException)

Example 29 with ErrorHandler

use of org.xml.sax.ErrorHandler in project nokogiri by sparklemotion.

the class XmlSchema method validate_document_or_file.

IRubyObject validate_document_or_file(ThreadContext context, XmlDocument xmlDocument) {
    RubyArray errors = (RubyArray) this.getInstanceVariable("@errors");
    ErrorHandler errorHandler = new SchemaErrorHandler(context.runtime, errors);
    setErrorHandler(errorHandler);
    try {
        validate(xmlDocument.getDocument());
    } catch (SAXException ex) {
        XmlSyntaxError xmlSyntaxError = XmlSyntaxError.createXMLSyntaxError(context.runtime);
        xmlSyntaxError.setException(ex);
        errors.append(xmlSyntaxError);
    } catch (IOException ex) {
        throw context.runtime.newIOError(ex.getMessage());
    }
    return errors;
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) SchemaErrorHandler(nokogiri.internals.SchemaErrorHandler) IgnoreSchemaErrorsErrorHandler(nokogiri.internals.IgnoreSchemaErrorsErrorHandler) RubyArray(org.jruby.RubyArray) SchemaErrorHandler(nokogiri.internals.SchemaErrorHandler) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Example 30 with ErrorHandler

use of org.xml.sax.ErrorHandler in project spring-framework by spring-projects.

the class PersistenceUnitReader method readPersistenceUnitInfos.

/**
	 * Parse and build all persistence unit infos defined in the given XML files.
	 * @param persistenceXmlLocations the resource locations (can be patterns)
	 * @return the resulting PersistenceUnitInfo instances
	 */
public SpringPersistenceUnitInfo[] readPersistenceUnitInfos(String[] persistenceXmlLocations) {
    ErrorHandler handler = new SimpleSaxErrorHandler(logger);
    List<SpringPersistenceUnitInfo> infos = new LinkedList<>();
    String resourceLocation = null;
    try {
        for (String location : persistenceXmlLocations) {
            Resource[] resources = this.resourcePatternResolver.getResources(location);
            for (Resource resource : resources) {
                resourceLocation = resource.toString();
                InputStream stream = resource.getInputStream();
                try {
                    Document document = buildDocument(handler, stream);
                    parseDocument(resource, document, infos);
                } finally {
                    stream.close();
                }
            }
        }
    } catch (IOException ex) {
        throw new IllegalArgumentException("Cannot parse persistence unit from " + resourceLocation, ex);
    } catch (SAXException ex) {
        throw new IllegalArgumentException("Invalid XML in persistence unit from " + resourceLocation, ex);
    } catch (ParserConfigurationException ex) {
        throw new IllegalArgumentException("Internal error parsing persistence unit from " + resourceLocation);
    }
    return infos.toArray(new SpringPersistenceUnitInfo[infos.size()]);
}
Also used : SimpleSaxErrorHandler(org.springframework.util.xml.SimpleSaxErrorHandler) ErrorHandler(org.xml.sax.ErrorHandler) InputStream(java.io.InputStream) Resource(org.springframework.core.io.Resource) IOException(java.io.IOException) Document(org.w3c.dom.Document) LinkedList(java.util.LinkedList) SAXException(org.xml.sax.SAXException) SimpleSaxErrorHandler(org.springframework.util.xml.SimpleSaxErrorHandler) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Aggregations

ErrorHandler (org.xml.sax.ErrorHandler)38 SAXException (org.xml.sax.SAXException)36 SAXParseException (org.xml.sax.SAXParseException)33 IOException (java.io.IOException)16 DocumentBuilder (javax.xml.parsers.DocumentBuilder)14 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)14 Document (org.w3c.dom.Document)13 InputSource (org.xml.sax.InputSource)9 InputStream (java.io.InputStream)7 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)7 Element (org.w3c.dom.Element)7 FileInputStream (java.io.FileInputStream)6 EntityResolver (org.xml.sax.EntityResolver)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 File (java.io.File)4 StringReader (java.io.StringReader)4 OutputFormat (com.sun.org.apache.xml.internal.serialize.OutputFormat)3 XMLSerializer (com.sun.org.apache.xml.internal.serialize.XMLSerializer)3 Attr (org.w3c.dom.Attr)3 NodeList (org.w3c.dom.NodeList)3