Example usage for javax.xml.parsers DocumentBuilder newDocument

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

Introduction

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

Prototype


public abstract Document newDocument();

Source Link

Document

Obtain a new instance of a DOM Document object to build a DOM tree with.

Usage

From source file:TestSign.java

/**
 * Method main//from w  ww. jav a 2s  .co  m
 *
 * @param unused
 * @throws Exception
 */
public static void main(String unused[]) throws Exception {
    //J-
    String keystoreType = "JKS";
    String keystoreFile = "data/org/apache/xml/security/samples/input/keystore.jks";
    String keystorePass = "xmlsecurity";
    String privateKeyAlias = "test";
    String privateKeyPass = "xmlsecurity";
    String certificateAlias = "test";
    File signatureFile = new File("signature.xml");
    //J+
    KeyStore ks = KeyStore.getInstance(keystoreType);
    FileInputStream fis = new FileInputStream(keystoreFile);

    ks.load(fis, keystorePass.toCharArray());

    PrivateKey privateKey = (PrivateKey) ks.getKey(privateKeyAlias, privateKeyPass.toCharArray());
    javax.xml.parsers.DocumentBuilderFactory dbf = javax.xml.parsers.DocumentBuilderFactory.newInstance();

    dbf.setNamespaceAware(true);

    javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();
    org.w3c.dom.Document doc = db.newDocument();
    String BaseURI = signatureFile.toURL().toString();
    XMLSignature sig = new XMLSignature(doc, BaseURI, XMLSignature.ALGO_ID_SIGNATURE_DSA);

    doc.appendChild(sig.getElement());

    {
        ObjectContainer obj = new ObjectContainer(doc);
        Element anElement = doc.createElementNS(null, "InsideObject");

        anElement.appendChild(doc.createTextNode("A text in a box"));
        obj.appendChild(anElement);

        String Id = "TheFirstObject";

        obj.setId(Id);
        sig.appendObject(obj);

        Transforms transforms = new Transforms(doc);

        transforms.addTransform(Transforms.TRANSFORM_C14N_WITH_COMMENTS);
        sig.addDocument("#" + Id, transforms, Constants.ALGO_ID_DIGEST_SHA1);
    }

    {
        X509Certificate cert = (X509Certificate) ks.getCertificate(certificateAlias);

        sig.addKeyInfo(cert);
        sig.addKeyInfo(cert.getPublicKey());
        System.out.println("Start signing");
        sig.sign(privateKey);
        System.out.println("Finished signing");
    }

    FileOutputStream f = new FileOutputStream(signatureFile);

    XMLUtils.outputDOMc14nWithComments(doc, f);
    f.close();
    System.out.println("Wrote signature to " + BaseURI);

    for (int i = 0; i < sig.getSignedInfo().getSignedContentLength(); i++) {
        System.out.println("--- Signed Content follows ---");
        System.out.println(new String(sig.getSignedInfo().getSignedContentItem(i)));
    }
}

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

public static void main(String args[]) throws Exception {
    // use this if you want to configure logging, normally would put this in a static block, 
    // but this is just for testing (see jre\lib\logging.properties)
    org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
            .getLog(SignatureGenerator.class.getName());

    //System.out.println("Using the logger: "+log.getClass().getName());  

    //log.debug("Debug is on");
    //log.warn("Warning is on");
    //log.error("Error is on");
    log.info("**** Testing Signature Generator *****");

    //All the parameters for the keystore
    String keystoreType = "JKS";
    String keystoreFile = "data/keystore.jks";
    String keystorePass = "xmlexchanger";
    String privateKeyAlias = "exchanger";
    String privateKeyPass = "xmlexchanger";
    String certificateAlias = "exchanger";

    // set the keystore and private key properties
    KeyBuilder.setParams(keystoreType, keystoreFile, keystorePass, privateKeyAlias, privateKeyPass,
            certificateAlias);/*from   w w w  .j ava 2s.com*/

    // get the private key for signing.
    PrivateKey privateKey = KeyBuilder.getPrivateKey();

    // get the cert
    X509Certificate cert = KeyBuilder.getCertificate();

    // ************* create a sample to be signed ******************
    javax.xml.parsers.DocumentBuilderFactory dbf = javax.xml.parsers.DocumentBuilderFactory.newInstance();

    //XML Signature needs to be namespace aware
    dbf.setNamespaceAware(true);

    javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();
    org.w3c.dom.Document document = db.newDocument();

    //Build a sample document. It will look something like:
    //<!-- Comment before -->
    //<cladonia:Exchanger xmlns:cladonia="http://www.exchangerxml.com">
    //</cladonia:Exchanger>

    document.appendChild(document.createComment(" Comment before "));
    Element root = document.createElementNS("http://www.exchangerxml.com", "cladonia:Exchanger");
    root.setAttributeNS(null, "attr1", "test1");
    root.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:foo", "http://www.exchangerxml.com/#foo");
    root.setAttributeNS("http://example.org/#foo", "foo:attr1", "foo's test");
    root.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:cladonia", "http://www.exchangerxml.com");
    document.appendChild(root);
    Element firstchild = document.createElementNS("http://www.exchangerxml.com", "cladonia:Editor");
    firstchild.appendChild(document.createTextNode("simple text\n"));
    firstchild.setAttributeNS(null, "Id", "CladoniaId");
    root.appendChild(firstchild);
    //******************** End of sample to be signed*************************

    // *************** Signature 1
    // create SignatureGenerator using private key, cert and the dom (i.e an enveloped signature)
    SignatureGenerator gen = new SignatureGenerator(privateKey, cert, document);

    // set the c14n algorithm (Exclusive)
    gen.setC14nAlgorithm(SignatureGenerator.TRANSFORM_C14N_EXCL_WITH_COMMENTS);

    // set the xpath transform
    gen.setXpath("//cladonia:Editor");

    // set the id
    gen.setId("CladoniaId");

    // sign the document
    document = gen.sign(null);

    // output the enveloped signature
    FileOutputStream fos = new FileOutputStream("c:\\temp\\sigout.xml");
    XMLUtils.outputDOMc14nWithComments(document, fos);
    fos.close();

    System.out.println("Created Signature 1 - an enveloped signature");

    // ************** Signature 2
    // now sign the previous output as an example of a detached signature
    SignatureGenerator gen2 = new SignatureGenerator(privateKey, cert, "file:///c:/temp/sigout.xml");

    // set the c14n algorithm
    gen2.setC14nAlgorithm(SignatureGenerator.TRANSFORM_C14N_WITH_COMMENTS);

    // sign the document
    Document document2 = gen2.sign(null);

    // output the detached signature
    FileOutputStream fos2 = new FileOutputStream("c:\\temp\\sigout2.xml");
    XMLUtils.outputDOMc14nWithComments(document2, fos2);
    fos2.close();

    System.out.println("Created Signature 2 - a detached signature");
    System.out.println("");
}

From source file:Main.java

public static Document newDocument() throws ParserConfigurationException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = dbf.newDocumentBuilder();
    return builder.newDocument();
}

From source file:Main.java

public static org.w3c.dom.Document newDocument() throws ParserConfigurationException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    return builder.newDocument();
}

From source file:Main.java

public static Document createDocument() throws ParserConfigurationException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    DocumentBuilder builder = factory.newDocumentBuilder();
    return builder.newDocument();
}

From source file:Main.java

private static Document createDocument() throws ParserConfigurationException {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    return dBuilder.newDocument();
}

From source file:Main.java

public static Document newDocument() throws ParserConfigurationException {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    return docBuilder.newDocument();
}

From source file:Main.java

public static Document getNewDocument() throws ParserConfigurationException {
    DocumentBuilder builder = getDocumentBuilder(false);
    return builder.newDocument();
}

From source file:Main.java

public static Document createNewDocument() throws ParserConfigurationException {
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(false);
    DocumentBuilder builder = domFactory.newDocumentBuilder();
    return builder.newDocument();
}

From source file:Main.java

public static Document newDocument() throws Exception {
    DocumentBuilder parser = dbFactory.newDocumentBuilder();
    return parser.newDocument();
}