Java Code Examples for org.w3c.dom.Node.setPrefix()

The following are Jave code examples for showing how to use setPrefix() of the org.w3c.dom.Node class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: javaide   File: NodeUtils.java   Source Code and License Vote up 6 votes
/**
 * Update the namespace of a given node to work with a given document.
 *
 * @param node the node to update
 * @param document the new document
 *
 * @return false if the attribute is to be dropped
 */
private static boolean processSingleNodeNamespace(Node node, Document document) {
    if ("xmlns".equals(node.getLocalName())) {
        return false;
    }

    String ns = node.getNamespaceURI();
    if (ns != null) {
        NamedNodeMap docAttributes = document.getAttributes();

        String prefix = getPrefixForNs(docAttributes, ns);
        if (prefix == null) {
            prefix = getUniqueNsAttribute(docAttributes);
            Attr nsAttr = document.createAttribute(prefix);
            nsAttr.setValue(ns);
            document.getChildNodes().item(0).getAttributes().setNamedItem(nsAttr);
        }

        // set the prefix on the node, by removing the xmlns: start
        prefix = prefix.substring(6);
        node.setPrefix(prefix);
    }

    return true;
}
 
Example 2
Project: javaide   File: ManifestMerger.java   Source Code and License Vote up 5 votes
/**
 * Changes the namespace prefix of all nodes, recursively.
 *
 * @param node The node to process, as well as all it's descendants. Can be null.
 * @param srcPrefix The prefix to match.
 * @param destPrefix The new prefix to replace with.
 */
private void changePrefix(Node node, String srcPrefix, String destPrefix) {
    for (; node != null; node = node.getNextSibling()) {
        if (srcPrefix.equals(node.getPrefix())) {
            node.setPrefix(destPrefix);
        }
        Node child = node.getFirstChild();
        if (child != null) {
            changePrefix(child, srcPrefix, destPrefix);
        }
    }
}
 
Example 3
Project: hashsdn-controller   File: DocumentedException.java   Source Code and License Vote up 4 votes
public Document toXMLDocument() {
    Document doc = null;
    try {
        doc = BUILDER_FACTORY.newDocumentBuilder().newDocument();

        Node rpcReply = doc.createElementNS( URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0, RPC_REPLY_KEY);
        doc.appendChild( rpcReply );

        Node rpcError = doc.createElementNS( URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0, RPC_ERROR );
        rpcReply.appendChild( rpcError );

        rpcError.appendChild( createTextNode( doc, ERROR_TYPE, getErrorType().getTypeValue() ) );
        rpcError.appendChild( createTextNode( doc, ERROR_TAG, getErrorTag().getTagValue() ) );
        rpcError.appendChild( createTextNode( doc, ERROR_SEVERITY, getErrorSeverity().getSeverityValue() ) );
        rpcError.appendChild( createTextNode( doc, ERROR_MESSAGE, getLocalizedMessage() ) );

        Map<String, String> errorInfoMap = getErrorInfo();
        if( errorInfoMap != null && !errorInfoMap.isEmpty() ) {
            /*
             * <error-info>
             *   <bad-attribute>message-id</bad-attribute>
             *   <bad-element>rpc</bad-element>
             * </error-info>
             */

            Node errorInfoNode = doc.createElementNS( URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0, ERROR_INFO );
            errorInfoNode.setPrefix( rpcReply.getPrefix() );
            rpcError.appendChild( errorInfoNode );

            for ( Entry<String, String> entry : errorInfoMap.entrySet() ) {
                errorInfoNode.appendChild( createTextNode( doc, entry.getKey(), entry.getValue() ) );
            }
        }
    }
    catch( final ParserConfigurationException e ) {
        // this shouldn't happen
        LOG.error("Error outputting to XML document", e);
    }

    return doc;
}