Search in sources :

Example 56 with ErrorHandler

use of org.xml.sax.ErrorHandler in project Mycat-Server by MyCATApache.

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 57 with ErrorHandler

use of org.xml.sax.ErrorHandler in project JGroups by belaban.

the class WriteVersionTo method parseVersionAndCodenameFromPOM.

public static String[] parseVersionAndCodenameFromPOM(String pom) throws IOException {
    String version, codename;
    try (InputStream in = Util.getResourceAsStream(pom, Version.class)) {
        if (in == null)
            throw new FileNotFoundException(pom);
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        final AtomicReference<SAXParseException> ex = new AtomicReference<>();
        builder.setErrorHandler(new ErrorHandler() {

            public void warning(SAXParseException e) throws SAXException {
                System.err.printf(Util.getMessage("ParseFailure"), e);
            }

            public void fatalError(SAXParseException exception) throws SAXException {
                ex.set(exception);
            }

            public void error(SAXParseException exception) throws SAXException {
                ex.set(exception);
            }
        });
        Document document = builder.parse(in);
        if (ex.get() != null)
            throw ex.get();
        Element root_element = document.getDocumentElement();
        String root_name = root_element.getNodeName().trim().toLowerCase();
        if (!"project".equals(root_name))
            throw new IOException("the POM does not start with a <project> element: " + root_name);
        version = Util.getChild(root_element, "version");
        codename = Util.getChild(root_element, "properties.codename");
        return new String[] { version, codename };
    } catch (Exception x) {
        throw new IOException(String.format(Util.getMessage("ParseError"), x.getLocalizedMessage()));
    }
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Element(org.w3c.dom.Element) AtomicReference(java.util.concurrent.atomic.AtomicReference) Document(org.w3c.dom.Document) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXParseException(org.xml.sax.SAXParseException)

Example 58 with ErrorHandler

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

the class SoapHeaderInterceptor method validateHeader.

private void validateHeader(final SoapMessage message, MessagePartInfo mpi, Schema schema) {
    Header param = findHeader(message, mpi);
    if (param != null && param.getDataBinding() == null) {
        Node source = (Node) param.getObject();
        if (!(source instanceof Element)) {
            return;
        }
        if (schema != null) {
            final Element el = (Element) source;
            DOMSource ds = new DOMSource(el);
            try {
                Validator v = schema.newValidator();
                ErrorHandler errorHandler = new ErrorHandler() {

                    public void warning(SAXParseException exception) throws SAXException {
                    }

                    public void error(SAXParseException exception) throws SAXException {
                        String msg = exception.getMessage();
                        if (msg.contains(el.getLocalName()) && (msg.contains(":" + message.getVersion().getAttrNameRole()) || msg.contains(":" + message.getVersion().getAttrNameMustUnderstand()))) {
                            return;
                        }
                        throw exception;
                    }

                    public void fatalError(SAXParseException exception) throws SAXException {
                        throw exception;
                    }
                };
                v.setErrorHandler(errorHandler);
                v.validate(ds);
            } catch (SAXException | IOException e) {
                throw new Fault("COULD_NOT_VALIDATE_SOAP_HEADER_CAUSED_BY", LOG, e, e.getClass().getCanonicalName(), e.getMessage());
            }
        }
    }
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) DOMSource(javax.xml.transform.dom.DOMSource) Header(org.apache.cxf.headers.Header) SAXParseException(org.xml.sax.SAXParseException) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) Fault(org.apache.cxf.interceptor.Fault) IOException(java.io.IOException) Validator(javax.xml.validation.Validator) SAXException(org.xml.sax.SAXException)

Example 59 with ErrorHandler

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

the class SvgAnalyzer method main.

/**
 * @param args
 * @throws Exception
 * @throws SAXException
 */
public static void main(String[] args) throws SAXException, Exception {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setValidating(false);
    XMLReader parser = factory.newSAXParser().getXMLReader();
    SvgAnalysisHandler analysisHandler = new SvgAnalysisHandler();
    parser.setContentHandler(analysisHandler);
    parser.setDTDHandler(analysisHandler);
    parser.setProperty("http://xml.org/sax/properties/lexical-handler", analysisHandler);
    parser.setProperty("http://xml.org/sax/properties/declaration-handler", analysisHandler);
    parser.setFeature("http://xml.org/sax/features/string-interning", true);
    parser.setEntityResolver(new NullEntityResolver());
    parser.setErrorHandler(new ErrorHandler() {

        public void error(SAXParseException exception) throws SAXException {
        }

        public void fatalError(SAXParseException exception) throws SAXException {
        }

        public void warning(SAXParseException exception) throws SAXException {
        }
    });
    File dir = new File(args[0]);
    File[] list = dir.listFiles();
    int nsError = 0;
    int encodingErrors = 0;
    int otherIllFormed = 0;
    double total = (double) list.length;
    for (int i = 0; i < list.length; i++) {
        File file = list[i];
        try {
            InputSource is = new InputSource(new FileInputStream(file));
            is.setSystemId(file.toURL().toExternalForm());
            parser.parse(is);
        } catch (SAXParseException e) {
            String msg = e.getMessage();
            if (msg.startsWith("The prefix ")) {
                nsError++;
            } else if (msg.contains("Prefixed namespace bindings may not be empty.")) {
                nsError++;
            } else if (msg.startsWith("Invalid byte ")) {
                encodingErrors++;
            } else {
                otherIllFormed++;
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(-1);
        }
    }
    System.out.print("NS errors: ");
    System.out.println(((double) nsError) / total);
    System.out.print("Encoding errors: ");
    System.out.println(((double) encodingErrors) / total);
    System.out.print("Other WF errors: ");
    System.out.println(((double) otherIllFormed) / total);
    analysisHandler.print();
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) NullEntityResolver(nu.validator.xml.NullEntityResolver) InputSource(org.xml.sax.InputSource) FileInputStream(java.io.FileInputStream) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException) SAXParseException(org.xml.sax.SAXParseException) File(java.io.File) XMLReader(org.xml.sax.XMLReader) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 60 with ErrorHandler

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

the class ValidationWorker method setupValidator.

private Validator setupValidator(Set<Schema> schemas) {
    PropertyMapBuilder builder = new PropertyMapBuilder();
    builder.put(ValidateProperty.ERROR_HANDLER, new ErrorHandler() {

        public void error(SAXParseException exception) throws SAXException {
            validationErrors.add(replaceSpecificValues(exception.getMessage()));
        }

        public void fatalError(SAXParseException exception) throws SAXException {
            // should not happen
            validationErrors.add(replaceSpecificValues(exception.getMessage()));
        }

        public void warning(SAXParseException exception) throws SAXException {
        }
    });
    PropertyMap map = builder.toPropertyMap();
    Validator rv = null;
    for (Schema schema : schemas) {
        Validator v = schema.createValidator(map);
        if (rv == null) {
            rv = v;
        } else {
            rv = new CombineValidator(rv, v);
        }
    }
    return rv;
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) PropertyMap(com.thaiopensource.util.PropertyMap) SAXParseException(org.xml.sax.SAXParseException) Schema(com.thaiopensource.validate.Schema) CheckerSchema(org.whattf.checker.jing.CheckerSchema) PropertyMapBuilder(com.thaiopensource.util.PropertyMapBuilder) CombineValidator(com.thaiopensource.relaxng.impl.CombineValidator) Validator(com.thaiopensource.validate.Validator) CombineValidator(com.thaiopensource.relaxng.impl.CombineValidator) SAXException(org.xml.sax.SAXException)

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