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:org.geosamples.credentials.CredentialsValidator.java

/**
 * Validates user credentials for use with GeoPass from SESAR.
 *
 * @see/*ww  w.ja  v a 2 s .c om*/
 * <a href="http://www.iedadata.org/services/sesar_api#2.sesarusercredential">SESAR
 * credential POST API</a>
 * @param userName
 * @param password
 * @param credentialsService
 * @return ArrayList of user codes for this user
 * @throws IOException
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws NoSuchAlgorithmException
 * @throws KeyStoreException
 * @throws KeyManagementException
 */
private static ArrayList<String> validateUserCredentials(String userName, String password,
        String credentialsService) throws IOException, ParserConfigurationException, SAXException,
        NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
    ArrayList<String> userCodes = new ArrayList<>();

    Map<String, String> dataToPost = new HashMap<>();
    dataToPost.put("username", userName);
    dataToPost.put("password", password);

    org.apache.http.client.methods.HttpPost httpPost = new HttpPost(credentialsService);
    List<NameValuePair> nameValuePairs = new ArrayList<>();
    nameValuePairs.add(new BasicNameValuePair("username", userName));
    nameValuePairs.add(new BasicNameValuePair("password", password));

    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    CloseableHttpClient httpClient = org.geosamples.utilities.HTTPClient.clientWithNoSecurityValidation();

    Document doc;
    try (CloseableHttpResponse httpResponse = httpClient.execute(httpPost)) {
        HttpEntity myEntity = httpResponse.getEntity();
        InputStream response = myEntity.getContent();
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(false);
        doc = factory.newDocumentBuilder().parse(response);
        EntityUtils.consume(myEntity);
    }

    if (doc != null) {
        if (doc.getElementsByTagName("valid").getLength() > 0) {
            if (doc.getElementsByTagName("valid").item(0).getTextContent().trim().equalsIgnoreCase("yes")) {
                NodeList userCodesList = doc.getElementsByTagName("user_code");
                for (int i = 0; i < userCodesList.getLength(); i++) {
                    userCodes.add(userCodesList.item(i).getTextContent().trim());
                }
            }
        }
    }

    return userCodes;
}

From source file:Main.java

private static DocumentBuilder newDocumentBuilder(Schema schema, boolean isNamespaceAware,
        boolean isXIncludeAware) throws SAXException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    factory.setNamespaceAware(isNamespaceAware);
    factory.setXIncludeAware(isXIncludeAware);
    if (schema != null) {
        factory.setSchema(schema);/*from w  w w .  j  a  v  a  2  s . c  om*/
        factory.setValidating(false);
    }
    DocumentBuilder builder = null;
    try {
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException ex) {
        // there is something seriously wrong
        throw new RuntimeException(ex);
    }
    return builder;
}

From source file:Main.java

public static synchronized DocumentBuilder getJaxpDocBuilderNNS() throws ParserConfigurationException {
    try {/*from   w w w . j  av a2 s  . c o  m*/
        System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
                "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
        System.setProperty("javax.xml.parsers.SaxParserFactory", "org.apache.xerces.jaxp.SAXParserFactoryImpl");
        DocumentBuilderFactory docBuildFactory = DocumentBuilderFactory.newInstance();
        docBuildFactory.setAttribute("http://apache.org/xml/features/dom/defer-node-expansion",
                new Boolean(false));
        docBuildFactory.setNamespaceAware(false);
        docBuildFactory.setValidating(false);
        return docBuildFactory.newDocumentBuilder();
    } catch (ParserConfigurationException pce) {
        throw new RuntimeException(pce.getMessage());
    }
}

From source file:DomUtil.java

/**
 * Read XML as DOM./*from  ww w.j ava  2  s . c o m*/
 */
public static Document readXml(InputStream is) throws SAXException, IOException, ParserConfigurationException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    dbf.setValidating(false);
    dbf.setIgnoringComments(false);
    dbf.setIgnoringElementContentWhitespace(true);
    // dbf.setCoalescing(true);
    // dbf.setExpandEntityReferences(true);

    DocumentBuilder db = null;
    db = dbf.newDocumentBuilder();
    db.setEntityResolver(new NullResolver());

    // db.setErrorHandler( new MyErrorHandler());

    Document doc = db.parse(is);
    return doc;
}

From source file:Main.java

/**
 * Returns a default DocumentBuilder instance or throws an
 * ExceptionInInitializerError if it can't be created.
 *
 * @return a default DocumentBuilder instance.
 *//*from  www . ja  va2 s .  c o m*/
public static DocumentBuilder getDocumentBuilder() throws Exception {
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setIgnoringComments(true);
        dbf.setCoalescing(true);
        dbf.setIgnoringElementContentWhitespace(true);
        dbf.setValidating(false);
        return dbf.newDocumentBuilder();
    } catch (Exception exc) {
        throw new Exception(exc.getMessage());
    }
}

From source file:Main.java

public static Document createDocument() {
    // creo la factory
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    // setX (che tipo di document builder ci deve produrre)

    dbf.setNamespaceAware(false);//from  www. j  a  va2 s  .  c o  m
    // se metti a true la valiazione il parser fa la validazione (prende la dtd associata e valida l'xml)
    dbf.setValidating(false);
    try {
        // dalla factory creo il DocumentBuilder
        DocumentBuilder db = dbf.newDocumentBuilder();
        return db.newDocument();
    } catch (ParserConfigurationException ex) {
        System.err.println("Impossibile creare il parser XML!");
    }

    return null;
}

From source file:Main.java

public static DocumentBuilder getJaxpDocBuilder() {
    try {//from   w w  w. j  a v a2 s .c  om
        synchronized (jaxpLock) {
            System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
                    "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
            System.setProperty("javax.xml.parsers.SaxParserFactory",
                    "org.apache.xerces.jaxp.SAXParserFactoryImpl");
            DocumentBuilderFactory docBuildFactory = DocumentBuilderFactory.newInstance();
            docBuildFactory.setAttribute("http://apache.org/xml/features/dom/defer-node-expansion",
                    Boolean.FALSE);
            docBuildFactory.setNamespaceAware(true);
            docBuildFactory.setValidating(false);
            return docBuildFactory.newDocumentBuilder();
        }
    } catch (ParserConfigurationException pce) {
        throw new RuntimeException(pce.getMessage());
    }
}

From source file:com.act.lcms.MzMLParser.java

/**
 * Helper function: builds an XML DocumentBuilderFactory that can be used repeatedly in this class.
 * <p>/*from w  w w. j  a  va  2  s .c  o  m*/
 * TODO: move this to an XML utility class, as I'm sure we'll use it again some day.
 *
 * @return An XML DocumentBuilderFactory.
 * @throws ParserConfigurationException
 */
public static DocumentBuilderFactory mkDocBuilderFactory() throws ParserConfigurationException {
    /* This factory must be configured within the context of a method call for exception handling.
     * TODO: can we work around this w/ dependency injection? */
    // from http://stackoverflow.com/questions/155101/make-documentbuilder-parse-ignore-dtd-references
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    docFactory.setValidating(false);
    docFactory.setNamespaceAware(true);
    docFactory.setFeature("http://xml.org/sax/features/namespaces", false);
    docFactory.setFeature("http://xml.org/sax/features/validation", false);
    docFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
    docFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    return docFactory;
}

From source file:hadoopInstaller.io.XMLDocumentReader.java

public static Document parse(FileObject xmlDocument, FileObject xsdDocument)
        throws InstallerConfigurationParseError {

    try {//from   w w  w  . j  ava  2s  .  c  om
        // Validate against XML Schema
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
                .newSchema(new StreamSource(xsdDocument.getContent().getInputStream()));
        dbf.setValidating(false);
        dbf.setSchema(schema);
        DocumentBuilder db = dbf.newDocumentBuilder();
        db.setErrorHandler(new ParseErrorHandler());
        return db.parse(xmlDocument.getContent().getInputStream());
    } catch (ParserConfigurationException | SAXException | IOException e) {
        throw new InstallerConfigurationParseError(e, "XMLDocumentReader.ParseError", xmlDocument.getName()); //$NON-NLS-1$
    }
}

From source file:Main.java

public static Document createDocumentFromString(String str)
        throws ParserConfigurationException, SAXException, IOException {
    // path to file is global
    //      String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
    //      String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
    // String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";

    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    // validate against XML Schema in dbsql2xml.xsd
    // documentBuilderFactory.setNamespaceAware(true);

    //INFO change validation to true. Someday when xsd file is complete...
    documentBuilderFactory.setValidating(false);
    //      documentBuilderFactory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
    // documentBuilderFactory.setAttribute(JAXP_SCHEMA_SOURCE, new File(pathToXsdFile));
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    Document document = documentBuilder.parse(new InputSource(new StringReader(str)));

    return document;
}