Code example for Document

Methods: appendChild, getDocumentElement

0
     * @param doc The XML document to sign 
     * @return The signed XML document 
     * @throws Exception to indicate any exceptional condition 
     */ 
    private Document createEnvelopingSignature(PrivateKey privateKey, Document doc) throws Exception {
        Element root = doc.getDocumentElement();
        ObjectContainer obj = new ObjectContainer(doc);
        String objectId = root.getLocalName();
 
        if ("document".equalsIgnoreCase(resource)) {
            // TODO insert line break before first element in object 
            obj.appendChild(root);
            obj.setId(objectId);
 
            // TODO insert line break after XML declaration (if one is there) 
            doc.appendChild(sig.getElement());
        } else if ("selection".equalsIgnoreCase(resource)) {
            Document selectionDoc = Utils.parse(textSelection);
            String expression = Utils.getUniqueXPathToNode(doc, selectionDoc);
            XPath xpath = XPathFactory.newInstance().newXPath();
            NamespaceContext ns = new SignatureNamespaceContext();
            xpath.setNamespaceContext(ns);
            Element selectedElement = (Element) xpath.evaluate(expression, doc, XPathConstants.NODE);
            objectId = selectedElement.getLocalName();
 
            // TODO insert line break before first element in object 
            obj.appendChild(selectedElement);
            obj.setId(objectId);
 
            if (!root.isSameNode(selectedElement)) {
                // TODO insert line break before first element 
                root.appendChild(sig.getElement());
            } else { 
                // TODO insert line break after XML declaration (if one is there) 
                doc.appendChild(sig.getElement());
            } 
        } else if ("xpath".equalsIgnoreCase(resource)) {
            XPath xpath = XPathFactory.newInstance().newXPath();
            NamespaceContext ns = new SignatureNamespaceContext();
            xpath.setNamespaceContext(ns);
            Element selectedElement = (Element) xpath.evaluate(expression, doc, XPathConstants.NODE);
            objectId = selectedElement.getLocalName();
 
            // TODO insert line break before first element in object 
            obj.appendChild(selectedElement);
            obj.setId(objectId);
 
            if (!root.isSameNode(selectedElement)) {
                // TODO insert line break before first element 
                root.appendChild(sig.getElement());
            } else { 
                // TODO insert line break after XML declaration (if one is there) 
                doc.appendChild(sig.getElement());
            } 
        } 
 
        sig.appendObject(obj);
 
        Transforms transforms = null;
        if (transformationAlgorithm != null && !"None".equals(transformationAlgorithm)) {
            transforms = new Transforms(doc);
            transforms.addTransform(transformationAlgorithm);
        } 
 
        sig.addDocument("#" + objectId, transforms, messageDigestAlgorithm);