Example usage for javax.xml.parsers DocumentBuilder setErrorHandler

List of usage examples for javax.xml.parsers DocumentBuilder setErrorHandler

Introduction

In this page you can find the example usage for javax.xml.parsers DocumentBuilder setErrorHandler.

Prototype


public abstract void setErrorHandler(ErrorHandler eh);

Source Link

Document

Specify the ErrorHandler to be used by the parser.

Usage

From source file:com.ikon.util.FormUtils.java

/**
 * Parse form.xml definitions/*from ww w  . j  ava 2s. c o  m*/
 * 
 * @return A Map with all the forms and its form elements.
 */
public static Map<String, List<FormElement>> parseWorkflowForms(InputStream is) throws ParseException {
    log.debug("parseWorkflowForms({})", is);
    Map<String, List<FormElement>> forms = new HashMap<String, List<FormElement>>();

    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        dbf.setValidating(true);
        ErrorHandler handler = new ErrorHandler();
        // EntityResolver resolver = new LocalResolver(Config.DTD_BASE);
        DocumentBuilder db = dbf.newDocumentBuilder();
        db.setErrorHandler(handler);
        db.setEntityResolver(resolver);

        if (is != null) {
            Document doc = db.parse(is);
            doc.getDocumentElement().normalize();
            NodeList nlForm = doc.getElementsByTagName("workflow-form");

            for (int i = 0; i < nlForm.getLength(); i++) {
                Node nForm = nlForm.item(i);

                if (nForm.getNodeType() == Node.ELEMENT_NODE) {
                    String taskName = nForm.getAttributes().getNamedItem("task").getNodeValue();
                    NodeList nlField = nForm.getChildNodes();
                    List<FormElement> fe = parseField(nlField);
                    forms.put(taskName, fe);
                }
            }
        }
    } catch (ParserConfigurationException e) {
        throw new ParseException(e.getMessage(), e);
    } catch (SAXException e) {
        throw new ParseException(e.getMessage(), e);
    } catch (IOException e) {
        throw new ParseException(e.getMessage(), e);
    }

    log.debug("parseWorkflowForms: {}", forms);
    return forms;
}

From source file:com.ikon.util.FormUtils.java

/**
 * Parse PropertyGroups.xml definitions/*from  w ww. java 2  s .c  om*/
 * 
 * @return A Map with all the forms and its form elements.
 */
public static synchronized Map<PropertyGroup, List<FormElement>> parsePropertyGroupsForms(String pgForm)
        throws IOException, ParseException {
    log.debug("parsePropertyGroupsForms({})", pgForm);

    if (pGroups == null) {
        pGroups = new HashMap<PropertyGroup, List<FormElement>>();
        FileInputStream fis = null;

        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setNamespaceAware(true);
            dbf.setValidating(true);
            ErrorHandler handler = new ErrorHandler();
            // EntityResolver resolver = new LocalResolver(Config.DTD_BASE);
            DocumentBuilder db = dbf.newDocumentBuilder();
            db.setErrorHandler(handler);
            db.setEntityResolver(resolver);
            fis = new FileInputStream(pgForm);

            if (fis != null) {
                Document doc = db.parse(fis);
                doc.getDocumentElement().normalize();
                NodeList nlForm = doc.getElementsByTagName("property-group");

                for (int i = 0; i < nlForm.getLength(); i++) {
                    Node nForm = nlForm.item(i);

                    if (nForm.getNodeType() == Node.ELEMENT_NODE) {
                        PropertyGroup pg = new PropertyGroup();

                        Node item = nForm.getAttributes().getNamedItem("label");
                        if (item != null)
                            pg.setLabel(item.getNodeValue());
                        item = nForm.getAttributes().getNamedItem("name");
                        if (item != null)
                            pg.setName(item.getNodeValue());
                        item = nForm.getAttributes().getNamedItem("visible");
                        if (item != null)
                            pg.setVisible(Boolean.valueOf(item.getNodeValue()));
                        item = nForm.getAttributes().getNamedItem("readonly");
                        if (item != null)
                            pg.setReadonly(Boolean.valueOf(item.getNodeValue()));

                        NodeList nlField = nForm.getChildNodes();
                        List<FormElement> fe = parseField(nlField);
                        pGroups.put(pg, fe);
                    }
                }
            }
        } catch (ParserConfigurationException e) {
            throw new ParseException(e.getMessage());
        } catch (SAXException e) {
            throw new ParseException(e.getMessage());
        } catch (IOException e) {
            throw e;
        } finally {
            IOUtils.closeQuietly(fis);
        }
    }

    log.debug("parsePropertyGroupsForms: {}", pGroups);
    return clonedPropertyGroups();
}

From source file:com.openkm.util.FormUtils.java

/**
 * Parse params.xml definitions//from w  w  w .jav  a  2  s .  c  om
 * 
 * @return A List parameter elements.
 */
public static List<FormElement> parseReportParameters(InputStream is) throws ParseException {
    log.debug("parseReportParameters({})", is);
    long begin = System.currentTimeMillis();
    List<FormElement> params = new ArrayList<FormElement>();

    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        dbf.setValidating(true);
        ErrorHandler handler = new ErrorHandler();
        // EntityResolver resolver = new LocalResolver(Config.DTD_BASE);
        DocumentBuilder db = dbf.newDocumentBuilder();
        db.setErrorHandler(handler);
        db.setEntityResolver(resolver);

        if (is != null) {
            Document doc = db.parse(is);
            doc.getDocumentElement().normalize();
            NodeList nlForm = doc.getElementsByTagName("report-parameters");

            for (int i = 0; i < nlForm.getLength(); i++) {
                Node nForm = nlForm.item(i);

                if (nForm.getNodeType() == Node.ELEMENT_NODE) {
                    NodeList nlField = nForm.getChildNodes();
                    params = parseField(nlField);
                }
            }
        }
    } catch (ParserConfigurationException e) {
        throw new ParseException(e.getMessage(), e);
    } catch (SAXException e) {
        throw new ParseException(e.getMessage(), e);
    } catch (IOException e) {
        throw new ParseException(e.getMessage(), e);
    }

    log.trace("parseReportParameters.Time: {}", System.currentTimeMillis() - begin);
    log.debug("parseReportParameters: {}", params);
    return params;
}

From source file:com.openkm.util.FormUtils.java

/**
 * Parse form.xml definitions// ww w  .  jav  a  2s. c  om
 * 
 * @return A Map with all the forms and its form elements.
 */
public static Map<String, List<FormElement>> parseWorkflowForms(InputStream is) throws ParseException {
    log.debug("parseWorkflowForms({})", is);
    long begin = System.currentTimeMillis();
    Map<String, List<FormElement>> forms = new HashMap<String, List<FormElement>>();

    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        dbf.setValidating(true);
        ErrorHandler handler = new ErrorHandler();
        // EntityResolver resolver = new LocalResolver(Config.DTD_BASE);
        DocumentBuilder db = dbf.newDocumentBuilder();
        db.setErrorHandler(handler);
        db.setEntityResolver(resolver);

        if (is != null) {
            Document doc = db.parse(is);
            doc.getDocumentElement().normalize();
            NodeList nlForm = doc.getElementsByTagName("workflow-form");

            for (int i = 0; i < nlForm.getLength(); i++) {
                Node nForm = nlForm.item(i);

                if (nForm.getNodeType() == Node.ELEMENT_NODE) {
                    String taskName = nForm.getAttributes().getNamedItem("task").getNodeValue();
                    NodeList nlField = nForm.getChildNodes();
                    List<FormElement> fe = parseField(nlField);
                    forms.put(taskName, fe);
                }
            }
        }
    } catch (ParserConfigurationException e) {
        throw new ParseException(e.getMessage(), e);
    } catch (SAXException e) {
        throw new ParseException(e.getMessage(), e);
    } catch (IOException e) {
        throw new ParseException(e.getMessage(), e);
    }

    log.trace("parseWorkflowForms.Time: {}", System.currentTimeMillis() - begin);
    log.debug("parseWorkflowForms: {}", forms);
    return forms;
}

From source file:DOMImport.java

public void inandout(String infile1, String infile2, String outfile) {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(true);/*from  w w  w.j  av a2s  .  c o  m*/
    dbf.setNamespaceAware(true);
    dbf.setIgnoringElementContentWhitespace(true);

    Document doc1 = null;
    Document doc2 = null;
    try {
        DocumentBuilder builder = dbf.newDocumentBuilder();
        builder.setErrorHandler(new MyErrorHandler());
        InputSource is1 = new InputSource(infile1);
        doc1 = builder.parse(is1);
        InputSource is2 = new InputSource(infile2);
        doc2 = builder.parse(is2);
        importName(doc1, doc2);
        FileOutputStream fos = new FileOutputStream(outfile);
        TreeToXML ttxml = new TreeToXML();
        ttxml.write(fos, doc2);
        fos.close();
    } catch (SAXException e) {
        System.exit(1);
    } catch (ParserConfigurationException e) {
        System.err.println(e);
        System.exit(1);
    } catch (IOException e) {
        System.err.println(e);
        System.exit(1);
    }
}

From source file:com.openkm.util.FormUtils.java

/**
 * Parse PropertyGroups.xml definitions/*from  w ww .  ja  v  a2  s .  c om*/
 * 
 * @param pgDefFile Path to file where is the Property Groups definition.
 * @return A Map with all the forms and its form elements.
 */
public static synchronized Map<PropertyGroup, List<FormElement>> parsePropertyGroupsForms(String pgDefFile)
        throws IOException, ParseException {
    log.debug("parsePropertyGroupsForms({})", pgDefFile);

    if (pGroups == null) {
        long begin = System.currentTimeMillis();
        pGroups = new HashMap<PropertyGroup, List<FormElement>>();
        FileInputStream fis = null;

        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setNamespaceAware(true);
            dbf.setValidating(true);
            ErrorHandler handler = new ErrorHandler();
            // EntityResolver resolver = new LocalResolver(Config.DTD_BASE);
            DocumentBuilder db = dbf.newDocumentBuilder();
            db.setErrorHandler(handler);
            db.setEntityResolver(resolver);
            fis = new FileInputStream(pgDefFile);

            if (fis != null) {
                Document doc = db.parse(fis);
                doc.getDocumentElement().normalize();
                NodeList nlForm = doc.getElementsByTagName("property-group");

                for (int i = 0; i < nlForm.getLength(); i++) {
                    Node nForm = nlForm.item(i);

                    if (nForm.getNodeType() == Node.ELEMENT_NODE) {
                        PropertyGroup pg = new PropertyGroup();

                        Node item = nForm.getAttributes().getNamedItem("label");
                        if (item != null)
                            pg.setLabel(item.getNodeValue());
                        item = nForm.getAttributes().getNamedItem("name");
                        if (item != null)
                            pg.setName(item.getNodeValue());
                        item = nForm.getAttributes().getNamedItem("visible");
                        if (item != null)
                            pg.setVisible(Boolean.valueOf(item.getNodeValue()));
                        item = nForm.getAttributes().getNamedItem("readonly");
                        if (item != null)
                            pg.setReadonly(Boolean.valueOf(item.getNodeValue()));

                        NodeList nlField = nForm.getChildNodes();
                        List<FormElement> fe = parseField(nlField);
                        pGroups.put(pg, fe);
                    }
                }
            }
        } catch (ParserConfigurationException e) {
            throw new ParseException(e.getMessage());
        } catch (SAXException e) {
            throw new ParseException(e.getMessage());
        } catch (IOException e) {
            throw e;
        } finally {
            IOUtils.closeQuietly(fis);
        }

        log.trace("parsePropertyGroupsForms.Time: {}", System.currentTimeMillis() - begin);
    }

    log.debug("parsePropertyGroupsForms: {}", pGroups);
    return clonedPropertyGroups();
}

From source file:edu.internet2.middleware.shibboleth.common.config.SpringDocumentLoader.java

/** {@inheritDoc} */
public Document loadDocument(InputSource inputSource, EntityResolver entityResolver, ErrorHandler errorHandler,
        int validationMode, boolean namespaceAware) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
            "http://www.w3.org/2001/XMLSchema");
    factory.setCoalescing(true);/*from  w  ww .  j  a  v  a2  s.  com*/
    factory.setIgnoringComments(true);
    factory.setNamespaceAware(true);
    factory.setValidating(true);

    DocumentBuilder builder = factory.newDocumentBuilder();
    builder.setErrorHandler(new LoggingErrorHandler(log));
    builder.setEntityResolver(new ClasspathResolver());
    return builder.parse(inputSource);
}

From source file:com.ebay.jetstream.event.processor.esper.raw.EsperTestConfigurationValidator.java

public boolean validate(String instance, String schema) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    try {/*w ww . j  ava2 s  .com*/
        warnings = 0;
        errors = 0;
        fatalErrors = 0;
        try {
            //Set the validation feature
            factory.setFeature("http://xml.org/sax/features/validation", true);

            //Set the schema validation feature
            factory.setFeature("http://apache.org/xml/features/validation/schema", true);

            //Set schema full grammar checking
            factory.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);

            // If there is an external schema set it
            if (schema != null) {
                SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
                Schema s = sf.newSchema(new StreamSource(schema));
                factory.setSchema(s);
            }
            DocumentBuilder parser = factory.newDocumentBuilder();
            parser.setErrorHandler(this);
            // Parse and validate
            parser.parse(instance);
            // Return true if we made it this far with no errors
            return ((errors == 0) && (fatalErrors == 0));
        } catch (SAXException e) {
            log.error("Could not activate validation features - " + e.getMessage());
        }
    } catch (Exception e) {
        log.error(e.getMessage());
    }
    return false;
}

From source file:com.smartitengineering.cms.spi.impl.type.validator.XMLSchemaBasedTypeValidator.java

private Document getDocumentForSource(InputStream contentTypeDef)
        throws SAXException, ParserConfigurationException, IOException {
    final DocumentBuilderFactory docBuilderFactor = DocumentBuilderFactory.newInstance();
    docBuilderFactor.setNamespaceAware(true);
    DocumentBuilder parser = docBuilderFactor.newDocumentBuilder();
    parser.setErrorHandler(new ErrorHandler() {

        public void warning(SAXParseException exception) throws SAXException {
            throw exception;
        }/*from  ww  w .jav a  2  s  .  c  om*/

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

        public void fatalError(SAXParseException exception) throws SAXException {
            throw exception;
        }
    });
    Document document = parser.parse(contentTypeDef);
    return document;
}

From source file:pydio.sdk.java.http.XMLDocEntity.java

public XMLDocEntity(HttpEntity notConsumedEntity)
        throws ParserConfigurationException, IOException, SAXException {
    ErrorHandler myErrorHandler = new ErrorHandler() {
        public void fatalError(SAXParseException exception) throws SAXException {
            System.err.println("fatalError: " + exception);
        }/*  ww w  .  j a  v a2  s  .  c  o m*/

        public void error(SAXParseException exception) throws SAXException {
            System.err.println("error: " + exception);
        }

        public void warning(SAXParseException exception) throws SAXException {
            System.err.println("warning: " + exception);
        }

    };

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    db.setErrorHandler(myErrorHandler);
    try {
        doc = db.parse(notConsumedEntity.getContent());
    } catch (SAXException e) {
        this.currentE = e;
    } catch (IOException e) {
        e.printStackTrace();
    }
}