Java Code Examples for org.w3c.dom.Document#replaceChild()

The following examples show how to use org.w3c.dom.Document#replaceChild() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
/** {@inheritDoc} */
public Element marshall(XMLObject xmlObject, Document document) throws MarshallingException {
    Element signatureElement = createSignatureElement((SignatureImpl) xmlObject, document);

    Element documentRoot = document.getDocumentElement();
    if (documentRoot != null) {
        document.replaceChild(signatureElement, documentRoot);
    } else {
        document.appendChild(signatureElement);
    }

    return signatureElement;
}
 
Example 2
/**
 * Sets the given element as the Document Element of the given Document. If the document already has a Document
 * Element it is replaced by the given element.
 * 
 * @param document the document
 * @param element the Element that will serve as the Document Element
 */
protected void setDocumentElement(Document document, Element element) {
    Element documentRoot = document.getDocumentElement();
    if (documentRoot != null) {
        document.replaceChild(element, documentRoot);
    } else {
        document.appendChild(element);
    }
}
 
Example 3
Source Project: Java-OCA-OCPP   File: SOAPCommunicator.java    License: MIT License 5 votes vote down vote up
private void setNamespace(Document document, String namespace) {
  Element orgElement = document.getDocumentElement();
  Element newElement = document.createElementNS(namespace, orgElement.getNodeName());

  NodeList childNodes = orgElement.getChildNodes();
  for (int i = 0; i < childNodes.getLength(); i++) {
    appendChildNS(document, newElement, childNodes.item(i), namespace);
  }

  document.replaceChild(newElement, orgElement);
}
 
Example 4
Source Project: jpx   File: XML.java    License: Apache License 2.0 5 votes vote down vote up
static Document removeNS(final Document doc) {
	if (doc == null) return null;

	final Node root = doc.getDocumentElement();
	final Element newRoot = doc.createElement(root.getNodeName());

	final NodeList children = root.getChildNodes();
	for (int i = 0; i < children.getLength(); ++i) {
		newRoot.appendChild(children.item(i).cloneNode(true));
	}

	doc.replaceChild(newRoot, root);
	return doc;
}
 
Example 5
Source Project: oodt   File: ExecServerConfig.java    License: Apache License 2.0 4 votes vote down vote up
/** Yield this exce-server configuration as serialized XML.
 *
 * @return This object as a string serialized XML document.
 * @throws DOMException If we can't create the XML document.
 */
public String toXML() throws DOMException {
	Document doc = Configuration.createDocument("execServer");
	doc.replaceChild(toXML(doc), doc.getDocumentElement());
	return XML.serialize(doc);
}
 
Example 6
Source Project: oodt   File: Configuration.java    License: Apache License 2.0 4 votes vote down vote up
/** Serialize this configuration into a serialized XML document.
 *
 * @return Serialized XML version of this configuration.
 * @throws DOMException If an error occurs constructing the XML structure.
 */
public String toXML() throws DOMException {
	Document doc = createDocument("configuration");
	doc.replaceChild(toXML(doc), doc.getDocumentElement());
	return XML.serialize(doc);
}
 
Example 7
Source Project: keycloak   File: XMLEncryptionUtil.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Encrypt the root document element inside a Document. <b>NOTE:</b> The document root element will be replaced by
 * the
 * wrapping element.
 *
 * @param document Document that contains an element to encrypt
 * @param publicKey The Public Key used to encrypt the secret encryption key
 * @param secretKey The secret encryption key
 * @param keySize Length of key
 * @param wrappingElementQName QName of the element to be used to wrap around the cipher data.
 * @param addEncryptedKeyInKeyInfo Should the encrypted key be inside a KeyInfo or added as a peer of Cipher Data
 *
 * @return An element that has the wrappingElementQName
 *
 * @throws ProcessingException
 * @throws org.keycloak.saml.common.exceptions.ConfigurationException
 */
public static Element encryptElementInDocument(Document document, PublicKey publicKey, SecretKey secretKey, int keySize,
                                               QName wrappingElementQName, boolean addEncryptedKeyInKeyInfo) throws ProcessingException, ConfigurationException {
    String wrappingElementPrefix = wrappingElementQName.getPrefix();
    if (wrappingElementPrefix == null || "".equals(wrappingElementPrefix))
        throw logger.wrongTypeError("Wrapping element prefix invalid");

    XMLCipher cipher = null;
    EncryptedKey encryptedKey = encryptKey(document, secretKey, publicKey, keySize);

    String encryptionAlgorithm = getXMLEncryptionURL(secretKey.getAlgorithm(), keySize);
    // Encrypt the Document
    try {
        cipher = XMLCipher.getInstance(encryptionAlgorithm);
        cipher.init(XMLCipher.ENCRYPT_MODE, secretKey);
    } catch (XMLEncryptionException e1) {
        throw logger.configurationError(e1);
    }

    Document encryptedDoc;
    try {
        encryptedDoc = cipher.doFinal(document, document.getDocumentElement());
    } catch (Exception e) {
        throw logger.processingError(e);
    }

    // The EncryptedKey element is added
    Element encryptedKeyElement = cipher.martial(document, encryptedKey);

    final String wrappingElementName;

    if (StringUtil.isNullOrEmpty(wrappingElementPrefix)) {
        wrappingElementName = wrappingElementQName.getLocalPart();
    } else {
        wrappingElementName = wrappingElementPrefix + ":" + wrappingElementQName.getLocalPart();
    }
    // Create the wrapping element and set its attribute NS
    Element wrappingElement = encryptedDoc.createElementNS(wrappingElementQName.getNamespaceURI(), wrappingElementName);

    if (! StringUtil.isNullOrEmpty(wrappingElementPrefix)) {
        wrappingElement.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns:" + wrappingElementPrefix, wrappingElementQName.getNamespaceURI());
    }

    Element encryptedDocRootElement = encryptedDoc.getDocumentElement();
    // Bring in the encrypted wrapping element to wrap the root node
    encryptedDoc.replaceChild(wrappingElement, encryptedDocRootElement);

    wrappingElement.appendChild(encryptedDocRootElement);

    if (addEncryptedKeyInKeyInfo) {
        // Outer ds:KeyInfo Element to hold the EncryptionKey
        Element sigElement = encryptedDoc.createElementNS(XMLSignature.XMLNS, DS_KEY_INFO);
        sigElement.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns:ds", XMLSignature.XMLNS);
        sigElement.appendChild(encryptedKeyElement);

        // Insert the Encrypted key before the CipherData element
        NodeList nodeList = encryptedDocRootElement.getElementsByTagNameNS(EncryptionConstants.EncryptionSpecNS, EncryptionConstants._TAG_CIPHERDATA);
        if (nodeList == null || nodeList.getLength() == 0)
            throw logger.domMissingElementError("xenc:CipherData");

        Element cipherDataElement = (Element) nodeList.item(0);
        encryptedDocRootElement.insertBefore(sigElement, cipherDataElement);
    } else {
        // Add the encrypted key as a child of the wrapping element
        wrappingElement.appendChild(encryptedKeyElement);
    }

    return encryptedDoc.getDocumentElement();
}