Example usage for javax.xml.parsers DocumentBuilderFactory setAttribute

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

Introduction

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

Prototype

public abstract void setAttribute(String name, Object value) throws IllegalArgumentException;

Source Link

Document

Allows the user to set specific attributes on the underlying implementation.

Usage

From source file:com.cladonia.security.signature.SignatureVerifier.java

public static void main(String[] args) throws Exception {
    org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
            .getLog(SignatureGenerator.class.getName());

    log.info("**** Testing Signature Verification *****");

    javax.xml.parsers.DocumentBuilderFactory dbf = javax.xml.parsers.DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);// w  w w.  j a v a  2  s  . c o m
    dbf.setAttribute("http://xml.org/sax/features/namespaces", Boolean.TRUE);

    File f = new File("c:\\temp\\sigout.xml");
    javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();
    org.w3c.dom.Document doc = db.parse(new java.io.FileInputStream(f));

    SignatureVerifier verifier = new SignatureVerifier(doc, XngrURLUtilities.getURLFromFile(f).toString());
    if (verifier.verify())
        System.out.println("Signature 1 - verification passed");
    else
        System.out.println("Signature 1 - verification failed");

    File f2 = new File("c:\\temp\\sigout2.xml");
    org.w3c.dom.Document doc2 = db.parse(new java.io.FileInputStream(f2));

    // Note the apache api requires that you pass in a base uri, the location where the 
    // signature file is fine, if you do not know this (i.e in the case where the signature
    // file has not been saved), then just pass in "file:", as it works!!
    SignatureVerifier verifier2 = new SignatureVerifier(doc2, "file:");
    if (verifier2.verify())
        System.out.println("Signature 2 - verification passed");
    else
        System.out.println("Signature 2 - verification failed");

    File f3 = new File("c:\\temp\\vordelsig.xml");
    org.w3c.dom.Document doc3 = db.parse(new java.io.FileInputStream(f3));
    SignatureVerifier verifier3 = new SignatureVerifier(doc3, XngrURLUtilities.getURLFromFile(f3).toString());
    if (verifier3.verify())
        System.out.println("Vordel Signature - verification passed");
    else
        System.out.println("Vordel Signature - verification failed");

    File f4 = new File("c:\\temp\\license.xml");
    org.w3c.dom.Document doc4 = db.parse(new java.io.FileInputStream(f4));
    SignatureVerifier verifier4 = new SignatureVerifier(doc4, XngrURLUtilities.getURLFromFile(f4).toString());
    if (verifier4.verify())
        System.out.println("License Signature - verification passed");
    else
        System.out.println("License Signature - verification failed");

}

From source file:Main.java

private static void setAttributeEL(DocumentBuilderFactory factory, String name, Object value) {
    try {//from  w ww.jav  a  2s  . com
        factory.setAttribute(name, value);
    } catch (Throwable t) {
        //SystemOut.printDate("attribute ["+name+"] is not allowed for ["+factory.getClass().getName()+"]");
    }
}

From source file:Main.java

private static void setAttributeEL(DocumentBuilderFactory factory, String name, Object value) {
    try {//  ww  w. java2  s . com
        factory.setAttribute(name, value);
    } catch (Throwable t) {
        if (t instanceof ThreadDeath)
            throw (ThreadDeath) t;
        //SystemOut.printDate("attribute ["+name+"] is not allowed for ["+factory.getClass().getName()+"]");
    }
}

From source file:Main.java

public static synchronized DocumentBuilder getJaxpDocBuilderNNS() throws ParserConfigurationException {
    try {/*from ww w  .java2 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:Main.java

public static DocumentBuilder getJaxpDocBuilder() {
    try {//from  w  ww .  j a  v a  2s  . com
        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:Main.java

/** Creates a schema-aware XML parser */
private static DocumentBuilder createSchemaAwareParser(URL schemaURL) {
    DocumentBuilderFactory dbf = createNamespaceAwareDocumentBuilderFactory();
    dbf.setValidating(true);/*  www . ja va2 s . c o  m*/
    dbf.setAttribute(ATTRIBUTE_SCHEMA_LANGUAGE, SCHEMA_URL);
    dbf.setAttribute(ATTRIBUTE_SCHEMA_SOURCE, schemaURL.toString());

    try {
        return dbf.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        throw new IllegalStateException("No document builder found, probably Java is misconfigured!", e);
    }
}

From source file:Main.java

public static Document getDocumentFromString(String xmlString) throws Exception {
    DocumentBuilderFactory oDocumentBuilderFactory = DocumentBuilderFactory.newInstance();
    try {/*from w w w.  ja va2s.co m*/
        // Do not load the DTD
        oDocumentBuilderFactory.setAttribute("http://apache.org/xml/features/nonvalidating/load-external-dtd",
                Boolean.FALSE);
    } catch (IllegalArgumentException e) {
    }
    DocumentBuilder oDocumentBuilder = oDocumentBuilderFactory.newDocumentBuilder();

    InputSource inputSource = new InputSource(new StringReader(xmlString));
    return oDocumentBuilder.parse(inputSource);
}

From source file:Main.java

public static Document validate(File xml, File xmlSchema)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);//  ww w  .  j  av a  2s.  c o m
    factory.setValidating(true);
    factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
            "http://www.w3.org/2001/XMLSchema");
    factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", xmlSchema);
    DocumentBuilder documentBuilder = factory.newDocumentBuilder();
    documentBuilder.setErrorHandler(new ErrorHandler() {
        public void warning(SAXParseException ex) throws SAXException {
            throw ex;
        }

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

        public void fatalError(SAXParseException ex) throws SAXException {
            throw ex;
        }
    });
    return documentBuilder.parse(xml);
}

From source file:Main.java

/**
 * Valida un documento XML con un esquema XML (XSD).
 * @param xml File to validate/*from   www  . j ava  2 s  .co  m*/
 * @param xmlSchema File with the schema
 * @return Dom document
 * @throws ParserConfigurationException 
 * @throws SAXException 
 * @throws IOException  
 */
public static Document validate(File xml, File xmlSchema)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    factory.setNamespaceAware(true);
    factory.setValidating(true);
    factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
            "http://www.w3.org/2001/XMLSchema");
    factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", xmlSchema);

    // Parsing 
    DocumentBuilder documentBuilder = factory.newDocumentBuilder();
    documentBuilder.setErrorHandler(new ErrorHandler() {
        public void warning(SAXParseException ex) throws SAXException {
            throw ex;
        }

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

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

    return documentBuilder.parse(xml);
}

From source file:Main.java

public static Document CreateDocument(Path xml, Path xsd)
        throws SAXException, IOException, ParserConfigurationException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);/*from  w w w  .ja  v a 2 s. c  om*/
    factory.setNamespaceAware(true);
    factory.setIgnoringComments(true);
    factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
            "http://www.w3.org/2001/XMLSchema");
    if (xsd != null)
        factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", xsd.toString());
    return factory.newDocumentBuilder().parse(xml.toString());
}