Example usage for org.w3c.dom Document createComment

List of usage examples for org.w3c.dom Document createComment

Introduction

In this page you can find the example usage for org.w3c.dom Document createComment.

Prototype

public Comment createComment(String data);

Source Link

Document

Creates a Comment node given the specified string.

Usage

From source file:test.unit.be.fedict.eid.applet.service.signer.AbstractXmlSignatureServiceTest.java

@Test
public void testCheckDigestedNode2() throws Exception {
    // setup// w ww .j ava 2s  . c om
    Init.init();
    KeyPair keyPair = PkiTestUtils.generateKeyPair();

    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(true);
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    Document document = documentBuilder.newDocument();
    Element rootElement = document.createElementNS("urn:test", "tns:root");
    rootElement.setAttributeNS(Constants.NamespaceSpecNS, "xmlns:tns", "urn:test");
    document.appendChild(rootElement);
    Element dataElement = document.createElementNS("urn:test", "tns:data");
    dataElement.setAttributeNS(null, "Id", "id-1234");
    dataElement.setIdAttributeNS(null, "Id", true);
    dataElement.setTextContent("data to be signed");
    rootElement.appendChild(dataElement);

    Element data2Element = document.createElementNS("urn:test", "tns:data2");
    rootElement.appendChild(data2Element);
    data2Element.setTextContent("hello world");
    data2Element.setAttributeNS(null, "name", "value");
    Element data3Element = document.createElementNS("urn:test", "tns:data3");
    data2Element.appendChild(data3Element);
    data3Element.setTextContent("data 3");
    data3Element.appendChild(document.createComment("some comments"));

    Element emptyElement = document.createElementNS("urn:test", "tns:empty");
    rootElement.appendChild(emptyElement);

    org.apache.xml.security.signature.XMLSignature xmlSignature = new org.apache.xml.security.signature.XMLSignature(
            document, "", org.apache.xml.security.signature.XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA1);
    rootElement.appendChild(xmlSignature.getElement());

    Transforms transforms = new Transforms(document);
    transforms.addTransform(Transforms.TRANSFORM_C14N_WITH_COMMENTS);
    xmlSignature.addDocument("#id-1234", transforms, Constants.ALGO_ID_DIGEST_SHA1);

    xmlSignature.addKeyInfo(keyPair.getPublic());
    xmlSignature.sign(keyPair.getPrivate());

    NodeList signatureNodeList = document.getDocumentElement()
            .getElementsByTagNameNS("http://www.w3.org/2000/09/xmldsig#", "Signature");
    Element signatureElement = (Element) signatureNodeList.item(0);

    // operate & verify
    assertTrue(isDigested(dataElement, signatureElement));
    assertFalse(isDigested(data2Element, signatureElement));
}

From source file:wssec.TestWSSecurityWSS234.java

/**
 * Test that signs and verifies a WS-Security envelope
 * <p/>//w w w  . j  a  v a  2 s.c om
 * 
 * @throws java.lang.Exception Thrown when there is any problem in signing or verification
 */
public void testSignature() throws Exception {
    WSSecSignature builder = new WSSecSignature();
    builder.setUserInfo("16c73ab6-b892-458f-abf5-2f875f74882e", "security");
    LOG.info("Before Signing....");
    Document doc = unsignedEnvelope.getAsDocument();
    WSSecHeader secHeader = new WSSecHeader();
    secHeader.insertSecurityHeader(doc);
    Document signedDoc = builder.build(doc, crypto, secHeader);

    // Add a comment node as the first node element
    org.w3c.dom.Node firstChild = signedDoc.getFirstChild();
    org.w3c.dom.Node newNode = signedDoc.removeChild(firstChild);
    org.w3c.dom.Node commentNode = signedDoc.createComment("This is a comment");
    signedDoc.appendChild(commentNode);
    signedDoc.appendChild(newNode);

    if (LOG.isDebugEnabled()) {
        LOG.debug("After Signing....");
        String outputString = org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(signedDoc);
        LOG.debug(outputString);
    }

    verify(signedDoc);
}