Search in sources :

Example 66 with ErrorHandler

use of org.xml.sax.ErrorHandler in project keycloak by keycloak.

the class JAXBUtil method getSchemaFactory.

private static SchemaFactory getSchemaFactory() {
    SchemaFactory scFact = SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI);
    // Always install the resolver unless the system property is set
    if (SecurityActions.getSystemProperty("org.picketlink.identity.federation.jaxb.ls", null) == null)
        scFact.setResourceResolver(new IDFedLSInputResolver());
    scFact.setErrorHandler(new ErrorHandler() {

        public void error(SAXParseException exception) throws SAXException {
            StringBuilder builder = new StringBuilder();
            builder.append("Line Number=").append(exception.getLineNumber());
            builder.append(" Col Number=").append(exception.getColumnNumber());
            builder.append(" Public ID=").append(exception.getPublicId());
            builder.append(" System ID=").append(exception.getSystemId());
            builder.append(" exc=").append(exception.getLocalizedMessage());
            logger.trace("SAX Error:" + builder.toString());
        }

        public void fatalError(SAXParseException exception) throws SAXException {
            StringBuilder builder = new StringBuilder();
            builder.append("Line Number=").append(exception.getLineNumber());
            builder.append(" Col Number=").append(exception.getColumnNumber());
            builder.append(" Public ID=").append(exception.getPublicId());
            builder.append(" System ID=").append(exception.getSystemId());
            builder.append(" exc=").append(exception.getLocalizedMessage());
            logger.error("SAX Fatal Error:" + builder.toString());
        }

        public void warning(SAXParseException exception) throws SAXException {
            StringBuilder builder = new StringBuilder();
            builder.append("Line Number=").append(exception.getLineNumber());
            builder.append(" Col Number=").append(exception.getColumnNumber());
            builder.append(" Public ID=").append(exception.getPublicId());
            builder.append(" System ID=").append(exception.getSystemId());
            builder.append(" exc=").append(exception.getLocalizedMessage());
            logger.trace("SAX Warn:" + builder.toString());
        }
    });
    return scFact;
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) ErrorHandler(org.xml.sax.ErrorHandler) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException)

Example 67 with ErrorHandler

use of org.xml.sax.ErrorHandler in project jphp by jphp-compiler.

the class WrapXmlProcessor method __construct.

@Signature(@Arg(value = "flags", optional = @Optional("0")))
public Memory __construct(final Environment env, Memory... args) throws ParserConfigurationException, TransformerConfigurationException {
    transformerFactory = TransformerFactory.newInstance();
    transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    builderFactory = DocumentBuilderFactory.newInstance();
    builder = builderFactory.newDocumentBuilder();
    builder.setErrorHandler(new ErrorHandler() {

        @Override
        public void warning(SAXParseException exception) throws SAXException {
            if (onWarning != null) {
                onWarning.callAny(new JavaException(env, exception));
            }
        }

        @Override
        public void error(SAXParseException exception) throws SAXException {
            if (onError != null) {
                onError.callAny(new JavaException(env, exception));
            }
        }

        @Override
        public void fatalError(SAXParseException exception) throws SAXException {
            if (onFatalError != null) {
                onFatalError.callAny(new JavaException(env, exception));
            } else {
                throw exception;
            }
        }
    });
    return Memory.NULL;
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) JavaException(php.runtime.ext.java.JavaException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException)

Example 68 with ErrorHandler

use of org.xml.sax.ErrorHandler in project wso2-synapse by wso2.

the class SyslogMessageBuilderTest method test.

private SyslogMessage test(String message) throws Exception {
    MessageContext msgContext = new MessageContext();
    ByteArrayInputStream in = new ByteArrayInputStream(message.getBytes("us-ascii"));
    OMElement element = new SyslogMessageBuilder().processDocument(in, null, msgContext);
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = factory.newSchema(new StreamSource(SyslogMessageBuilderTest.class.getResource("schema.xsd").toExternalForm()));
    Validator validator = schema.newValidator();
    validator.setErrorHandler(new ErrorHandler() {

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

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

        public void warning(SAXParseException exception) throws SAXException {
            throw exception;
        }
    });
    validator.validate(new OMSource(element));
    String pidString = element.getAttributeValue(new QName(SyslogConstants.PID));
    return new SyslogMessage(element.getAttributeValue(new QName(SyslogConstants.FACILITY)), element.getAttributeValue(new QName(SyslogConstants.SEVERITY)), element.getAttributeValue(new QName(SyslogConstants.TAG)), pidString == null ? -1 : Integer.parseInt(pidString), element.getText());
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) ErrorHandler(org.xml.sax.ErrorHandler) QName(javax.xml.namespace.QName) Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) OMSource(org.apache.axiom.om.impl.jaxp.OMSource) OMElement(org.apache.axiom.om.OMElement) SAXException(org.xml.sax.SAXException) ByteArrayInputStream(java.io.ByteArrayInputStream) SAXParseException(org.xml.sax.SAXParseException) MessageContext(org.apache.axis2.context.MessageContext) Validator(javax.xml.validation.Validator)

Example 69 with ErrorHandler

use of org.xml.sax.ErrorHandler in project knox by apache.

the class TopologyValidator method validateTopology.

public boolean validateTopology() {
    errors = new LinkedList<>();
    try {
        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        URL schemaUrl = getClass().getResource("/conf/topology-v1.xsd");
        Schema s = schemaFactory.newSchema(schemaUrl);
        Validator validator = s.newValidator();
        validator.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, "");
        validator.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
        final List<SAXParseException> exceptions = new LinkedList<>();
        validator.setErrorHandler(new ErrorHandler() {

            @Override
            public void warning(SAXParseException exception) {
                exceptions.add(exception);
            }

            @Override
            public void fatalError(SAXParseException exception) {
                exceptions.add(exception);
            }

            @Override
            public void error(SAXParseException exception) {
                exceptions.add(exception);
            }
        });
        File xml = new File(filePath);
        validator.validate(new StreamSource(xml));
        if (!exceptions.isEmpty()) {
            for (SAXParseException e : exceptions) {
                errors.add("Line: " + e.getLineNumber() + " -- " + e.getMessage());
            }
            return false;
        } else {
            return true;
        }
    } catch (IOException e) {
        errors.add("Error reading topology file");
        errors.add(e.getMessage());
        return false;
    } catch (SAXException e) {
        errors.add("There was a fatal error in parsing the xml file.");
        errors.add(e.getMessage());
        return false;
    } catch (NullPointerException n) {
        errors.add("Error retrieving schema from ClassLoader");
        return false;
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) ErrorHandler(org.xml.sax.ErrorHandler) Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) IOException(java.io.IOException) URL(java.net.URL) LinkedList(java.util.LinkedList) SAXException(org.xml.sax.SAXException) SAXParseException(org.xml.sax.SAXParseException) File(java.io.File) Validator(javax.xml.validation.Validator)

Aggregations

ErrorHandler (org.xml.sax.ErrorHandler)69 SAXException (org.xml.sax.SAXException)59 SAXParseException (org.xml.sax.SAXParseException)55 DocumentBuilder (javax.xml.parsers.DocumentBuilder)26 IOException (java.io.IOException)25 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)24 Document (org.w3c.dom.Document)20 InputSource (org.xml.sax.InputSource)17 FileInputStream (java.io.FileInputStream)11 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)11 Element (org.w3c.dom.Element)10 File (java.io.File)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8 InputStream (java.io.InputStream)8 Validator (javax.xml.validation.Validator)8 EntityResolver (org.xml.sax.EntityResolver)8 StreamSource (javax.xml.transform.stream.StreamSource)6 Schema (javax.xml.validation.Schema)6 SchemaFactory (javax.xml.validation.SchemaFactory)6 Node (org.w3c.dom.Node)5