Example usage for javax.xml.parsers DocumentBuilderFactory setValidating

List of usage examples for javax.xml.parsers DocumentBuilderFactory setValidating

Introduction

In this page you can find the example usage for javax.xml.parsers DocumentBuilderFactory setValidating.

Prototype


public void setValidating(boolean validating) 

Source Link

Document

Specifies that the parser produced by this code will validate documents as they are parsed.

Usage

From source file:com.qualinsight.plugins.sonarqube.badges.internal.SVGImageFontReplacer.java

/**
 * IoC constructor//from www .ja  va  2s .co m
 *
 * @throws SVGImageFontReplacementException if a problem occurs during initialization
 */
public SVGImageFontReplacer() throws SVGImageFontReplacementException {
    try {
        InputStream xslInputStream = null;
        try {
            xslInputStream = getClass().getClassLoader()
                    .getResourceAsStream("com/qualinsight/plugins/sonarqube/badges/internal/svg.xsl");
            final TransformerFactory transformerFactory = TransformerFactory.newInstance();
            this.transformer = transformerFactory.newTransformer(new StreamSource(xslInputStream));
        } finally {
            if (null != xslInputStream) {
                xslInputStream.close();
            }
        }
        final DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        builderFactory.setValidating(false);
        builderFactory.setNamespaceAware(true);
        builderFactory.setFeature("http://xml.org/sax/features/namespaces", false);
        builderFactory.setFeature("http://xml.org/sax/features/validation", false);
        builderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
        builderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        this.builder = builderFactory.newDocumentBuilder();
    } catch (final IOException | TransformerConfigurationException | ParserConfigurationException e) {
        throw new SVGImageFontReplacementException(e);
    }
}

From source file:com.qualinsight.plugins.sonarqube.badges.ws.SVGImageMinimizer.java

/**
 * IoC constructor/*  ww  w  .  ja  va2 s.com*/
 *
 * @throws SVGImageMinimizerException if a problem occurs during initialization
 */
public SVGImageMinimizer() throws SVGImageMinimizerException {
    try {
        InputStream xslInputStream = null;
        try {
            xslInputStream = getClass().getClassLoader()
                    .getResourceAsStream("com/qualinsight/plugins/sonarqube/badges/ws/svg-minimizer.xsl");
            final TransformerFactory transformerFactory = TransformerFactory.newInstance();
            this.transformer = transformerFactory.newTransformer(new StreamSource(xslInputStream));
        } finally {
            if (null != xslInputStream) {
                xslInputStream.close();
            }
        }
        final DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        builderFactory.setValidating(false);
        builderFactory.setNamespaceAware(true);
        builderFactory.setFeature("http://xml.org/sax/features/namespaces", false);
        builderFactory.setFeature("http://xml.org/sax/features/validation", false);
        builderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
        builderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        this.builder = builderFactory.newDocumentBuilder();
    } catch (final IOException | TransformerConfigurationException | ParserConfigurationException e) {
        throw new SVGImageMinimizerException(e);
    }
}

From source file:com.francetelecom.clara.cloud.db.liquibase.CompareChangeLogWithHibernateAutoCreateIT.java

private boolean searchForDifferenceInXml(File xmlFile) {
    boolean differenceFound = false;
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(false);
    factory.setIgnoringElementContentWhitespace(true);
    try {// w w w .j a va 2  s.  c  om
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(xmlFile);
        Element databaseChangeLogRoot = document.getDocumentElement();
        differenceFound = databaseChangeLogRoot.hasChildNodes();
        // Do something with the document here.
    } catch (ParserConfigurationException | IOException | SAXException e) {
        LOGGER.info("Failed to parse xml file: {}", xmlFile, e);
    }
    return differenceFound;
}

From source file:br.com.fatecpg.repositories.sharepoint.support.SpContextImpl.java

private Element generateXmlNode(String sXML) {

    Document documentOptions = null;
    DocumentBuilder builder;/* w w w.  j a v a2  s .  c  o m*/

    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(false);
        builder = factory.newDocumentBuilder();
        documentOptions = builder.parse(new InputSource(new StringReader(sXML)));
    } catch (ParserConfigurationException | IOException | SAXException ex) {
        throw new RuntimeException(String.format("failed to create xml element node with: %s", sXML));
    }

    Element elementOptions = documentOptions.getDocumentElement();
    return elementOptions;
}

From source file:cz.cas.lib.proarc.common.export.mets.MetsUtils.java

/**
 *
 * Validates given XML file against an XSD schema
 *
 * @param file/* w w  w. ja v a 2 s  .c  o m*/
 * @param xsd
 * @return
 */
public static List<String> validateAgainstXSD(File file, InputStream xsd) throws Exception {
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    factory.setResourceResolver(MetsLSResolver.getInstance());
    Schema schema = factory.newSchema(new StreamSource(xsd));
    DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
    dbfactory.setValidating(false);
    dbfactory.setNamespaceAware(true);
    dbfactory.setSchema(schema);
    DocumentBuilder documentBuilder = dbfactory.newDocumentBuilder();
    ValidationErrorHandler errorHandler = new ValidationErrorHandler();
    documentBuilder.setErrorHandler(errorHandler);
    documentBuilder.parse(file);
    return errorHandler.getValidationErrors();
}

From source file:com.l2jfree.gameserver.datatables.SummonItemsData.java

private SummonItemsData() {
    _summonitems = new FastMap<Integer, L2SummonItem>();
    Document doc = null;/*from  ww  w .  j av  a  2s  .  c  o  m*/
    File file = new File(Config.DATAPACK_ROOT, "data/summon_items.xml");

    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(true);
        factory.setIgnoringComments(true);
        doc = factory.newDocumentBuilder().parse(file);

        int itemID = 0, npcID = 0;
        byte summonType = 0;
        Node a;
        for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling()) {
            if ("list".equalsIgnoreCase(n.getNodeName())) {
                for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling()) {
                    if ("item".equalsIgnoreCase(d.getNodeName())) {
                        a = d.getAttributes().getNamedItem("id");
                        if (a == null)
                            throw new Exception("Error in summon item defenition!");
                        itemID = Integer.parseInt(a.getNodeValue());

                        for (Node e = d.getFirstChild(); e != null; e = e.getNextSibling()) {
                            if ("npcId".equalsIgnoreCase(e.getNodeName())) {
                                a = e.getAttributes().getNamedItem("val");
                                if (a == null)
                                    throw new Exception(
                                            "Not defined npc id for summon item id=" + itemID + "!");
                                npcID = Integer.parseInt(a.getNodeValue());
                            } else if ("summonType".equalsIgnoreCase(e.getNodeName())) {
                                a = e.getAttributes().getNamedItem("val");
                                if (a == null)
                                    throw new Exception(
                                            "Not defined summon type for summon item id=" + itemID + "!");
                                summonType = Byte.parseByte(a.getNodeValue());
                            }
                        }
                        L2SummonItem summonitem = new L2SummonItem(itemID, npcID, summonType);
                        _summonitems.put(itemID, summonitem);
                    }
                }
            }
        }
        _summonItemIds = new int[_summonitems.size()];
        int i = 0;
        for (int itemId : _summonitems.keySet())
            _summonItemIds[i++] = itemId;
    } catch (IOException e) {
        _log.warn("SummonItemsData: Can not find " + file.getAbsolutePath() + " !", e);
    } catch (Exception e) {
        _log.warn("SummonItemsData: Error while parsing " + file.getAbsolutePath() + " !", e);
    }
    _log.info("SummonItemsData: Loaded " + _summonitems.size() + " Summon Items from " + file.getName());
}

From source file:cz.cas.lib.proarc.common.export.mets.MetsUtils.java

/**
 *
 * Generates an XML document from list of elements
 *
 * @param elements//from   w w w  .ja va2s.c  o  m
 * @return
 */
public static Document getDocumentFromList(List<Element> elements) throws MetsExportException {
    Document document = null;
    try {
        DocumentBuilderFactory builder = DocumentBuilderFactory.newInstance();
        builder.setValidating(true);
        builder.setNamespaceAware(true);
        document = builder.newDocumentBuilder().newDocument();
    } catch (ParserConfigurationException e1) {
        throw new MetsExportException("Error while getting document from list", false, e1);
    }

    for (Element element : elements) {
        Node newNode = element.cloneNode(true);
        document.adoptNode(newNode);
        document.appendChild(newNode);
    }
    return document;
}

From source file:cz.cas.lib.proarc.common.export.mets.MetsUtils.java

/**
 *
 * Validates given document agains an XSD schema
 *
 * @param document/*  w ww  .  ja v a2 s  .  co  m*/
 * @param xsd
 * @return
 */
public static List<String> validateAgainstXSD(Document document, InputStream xsd) throws Exception {
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    factory.setResourceResolver(MetsLSResolver.getInstance());
    Schema schema = factory.newSchema(new StreamSource(xsd));
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();
    DOMSource domSource = new DOMSource(document);
    StreamResult sResult = new StreamResult();
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    sResult.setOutputStream(bos);
    transformer.transform(domSource, sResult);
    InputStream is = new ByteArrayInputStream(bos.toByteArray());
    DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
    dbfactory.setValidating(false);
    dbfactory.setNamespaceAware(true);
    dbfactory.setSchema(schema);
    DocumentBuilder documentBuilder = dbfactory.newDocumentBuilder();
    ValidationErrorHandler errorHandler = new ValidationErrorHandler();
    documentBuilder.setErrorHandler(errorHandler);
    documentBuilder.parse(is);
    return errorHandler.getValidationErrors();
}

From source file:de.betterform.session.SessionSerializerTest.java

/**
 * Sets up the test./*w  w w .java  2 s  .  co  m*/
 *
 * @throws Exception in any error occurred during setup.
 */
protected void setUp() throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setValidating(false);
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document form = builder.parse(getClass().getResourceAsStream("session.xhtml"));

    String path = getClass().getResource("session.xhtml").getPath();
    this.baseURI = "file://" + path.substring(0, path.lastIndexOf("session.xhtml"));

    this.processor = new XFormsProcessorImpl();
    this.processor.setBaseURI(baseURI);
    this.processor.setXForms(form);
    this.processor.init();
}

From source file:com.l2jfree.gameserver.document.DocumentBase.java

final void parse() {
    try {/*from  ww w .  j ava2 s  . c om*/
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(true);
        factory.setIgnoringComments(true);
        parseDocument(factory.newDocumentBuilder().parse(_file));
    } catch (Exception e) {
        _log.fatal("Error in file: " + _file, e);
    }
}