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

The following examples show how to use org.w3c.dom.Node#setPrefix() . 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
Source File: NodeUtils.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
/**
 * 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
Source File: NodeUtils.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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 3
Source File: Namespaces.java    From ttt with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private static void normalizeNode(Node node, Map<String,String> normalizedPrefixes, String[] na, int[] ca) {
    String nsUri = node.getNamespaceURI();
    String normalizedPrefix = normalizedPrefixes.get(nsUri);
    if (normalizedPrefix != null) {
        if (normalizedPrefix.length() == 0)
            normalizedPrefix = null;
        node.setPrefix(normalizedPrefix);
    }
    if (nsUri != null) {
        int index = Arrays.binarySearch(na, nsUri, new Comparator<String>() {
            public int compare(String s1, String s2) {
                return s1.compareTo(s2);
            }
        });
        if (index >= 0) {
            ca[index] += 1;
        }
    }
}
 
Example 4
Source File: ManifestMerger.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * 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 5
Source File: ManifestMerger.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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 6
Source File: ChangeNamespacePrefixProcessor.java    From jaxb2-maven-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void process(final Node aNode) {

    if (aNode instanceof Attr) {

        final Attr attribute = (Attr) aNode;
        final Element parentElement = attribute.getOwnerElement();

        if (isNamespaceDefinition(attribute)) {

            // Use the incredibly smooth DOM way to rename an attribute...
            parentElement.setAttributeNS(attribute.getNamespaceURI(), XMLNS + newPrefix, aNode.getNodeValue());
            parentElement.removeAttribute(XMLNS + oldPrefix);

        } else if (isElementReference(attribute)
                || isTypeAttributeWithPrefix(attribute)
                || isExtension(attribute)) {

            // Simply alter the value of the reference
            final String value = attribute.getValue();
            final String elementName = value.substring(value.indexOf(":") + 1);
            attribute.setValue(newPrefix + ":" + elementName);
        }
    }

    if (oldPrefix.equals(aNode.getPrefix())) {
        // Simply change the prefix to the new one.
        aNode.setPrefix(newPrefix);
    }
}
 
Example 7
Source File: TestSOAPHandler.java    From cxf with Apache License 2.0 4 votes vote down vote up
private boolean getReturnValue(boolean outbound, SOAPMessageContext ctx) {
    boolean ret = true;
    try {
        SOAPMessage msg = ctx.getMessage();
        SOAPBody body = msg.getSOAPPart().getEnvelope().getBody();

        if (body.getFirstChild().getFirstChild() == null) {
            return true;
        }

        Node commandNode = body.getFirstChild().getFirstChild().getFirstChild();
        String arg = commandNode.getNodeValue();
        String namespace = body.getFirstChild().getFirstChild().getNamespaceURI();

        StringTokenizer strtok = new StringTokenizer(arg, " ");
        String hid = "";
        String direction = "";
        String command = "";
        if (strtok.countTokens() >= 3) {
            hid = strtok.nextToken();
            direction = strtok.nextToken();
            command = strtok.nextToken();
        }

        if (!getHandlerId().equals(hid)) {
            return true;
        }

        if ("stop".equals(command)) {
            if (!outbound && "inbound".equals(direction)) {
                 // remove the incoming request body.
                Document doc = body.getOwnerDocument();
                // build the SOAP response for this message
                //
                Node wrapper = doc.createElementNS(namespace, "pingResponse");
                wrapper.setPrefix("ns4");
                body.removeChild(body.getFirstChild());
                body.appendChild(wrapper);

                for (String info : getHandlerInfoList(ctx)) {
                    // copy the previously invoked handler list into the response.
                    // Ignore this handler's information as it will be added again later.
                    //
                    if (!info.contains(getHandlerId())) {
                        Node newEl = doc.createElementNS(namespace, "HandlersInfo");
                        newEl.setPrefix("ns4");
                        newEl.appendChild(doc.createTextNode(info));
                        wrapper.appendChild(newEl);
                    }
                }
                ret = false;
            } else if (outbound && "outbound".equals(direction)) {
                ret = false;
            }
        } else if ("throw".equals(command)) {
            String exceptionType = null;
            String exceptionText = "HandleMessage throws exception";
            if (strtok.hasMoreTokens()) {
                exceptionType = strtok.nextToken();
            }
            if (strtok.hasMoreTokens()) {
                exceptionText = strtok.nextToken();
            }
            if (exceptionType != null && !outbound && "inbound".equals(direction)) {
                if ("RuntimeException".equals(exceptionType)) {
                    throw new RuntimeException(exceptionText);
                } else if ("ProtocolException".equals(exceptionType)) {
                    throw new ProtocolException(exceptionText);
                } else if ("SOAPFaultException".equals(exceptionType)) {
                    throw createSOAPFaultException(exceptionText);
                } else if ("SOAPFaultExceptionWDetail".equals(exceptionType)) {
                    throw createSOAPFaultExceptionWithDetail(exceptionText);
                }
            } else if (exceptionType != null && outbound && "outbound".equals(direction)) {
                if ("RuntimeException".equals(exceptionType)) {
                    throw new RuntimeException(exceptionText);
                } else if ("ProtocolException".equals(exceptionType)) {
                    throw new ProtocolException(exceptionText);
                } else if ("SOAPFaultException".equals(exceptionType)) {
                    throw createSOAPFaultException(exceptionText);
                } else if ("SOAPFaultExceptionWDetail".equals(exceptionType)) {
                    throw createSOAPFaultExceptionWithDetail(exceptionText);
                }
            }

        }

    } catch (SOAPException e) {
        e.printStackTrace();
    }

    return ret;
}
 
Example 8
Source File: CustomizationParser.java    From cxf with Apache License 2.0 4 votes vote down vote up
private void copyBindingsToWsdl(Node node, Node bindings, MapNamespaceContext ctx) {
    if (bindings.getNamespaceURI().equals(ToolConstants.JAXWS_BINDINGS.getNamespaceURI())) {
        bindings.setPrefix("jaxws");
    }

    for (Map.Entry<String, String> ent : ctx.getUsedNamespaces().entrySet()) {
        if (node.lookupNamespaceURI(ent.getKey()) == null) {
            node.getOwnerDocument().getDocumentElement().setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
                                                                        "xmlns:" + ent.getKey(),
                                                                        ent.getValue());
        }

    }
    Element element = DOMUtils.getFirstElement(bindings);
    while (element != null) {
        if (element.getNamespaceURI().equals(ToolConstants.JAXWS_BINDINGS.getNamespaceURI())) {
            element.setPrefix("jaxws");
        }
        element = DOMUtils.getNextElement(element);
    }
    Node cloneNode = ProcessorUtil.cloneNode(node.getOwnerDocument(), bindings, true);
    Node firstChild = DOMUtils.getChild(node, "jaxws:bindings");
    if (firstChild == null && cloneNode.getNodeName().indexOf("bindings") == -1) {
        wsdlNode.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
                              "xmlns:jaxws",
                              ToolConstants.JAXWS_BINDINGS.getNamespaceURI());
        Element jaxwsBindingElement = node.getOwnerDocument()
                .createElementNS(ToolConstants.JAXWS_BINDINGS.getNamespaceURI(),
                                 "jaxws:bindings");
        node.appendChild(jaxwsBindingElement);
        firstChild = jaxwsBindingElement;
    }

    if (firstChild == null && cloneNode.getNodeName().indexOf("bindings") > -1) {
        firstChild = node;
        if (wsdlNode.getAttributeNodeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "jaxws") == null) {
            wsdlNode.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
                                    "xmlns:jaxws", ToolConstants.JAXWS_BINDINGS.getNamespaceURI());
        }
    }

    Element cloneEle = (Element)cloneNode;
    cloneEle.removeAttribute("node");

    Element elem = DOMUtils.getFirstElement(cloneNode);
    while (elem != null) {
        Node attrNode = elem.getAttributeNode("node");
        if (attrNode != null) {
            cloneNode.removeChild(elem);
        }
        elem = DOMUtils.getNextElement(elem);
    }

    if (firstChild != null) {
        firstChild.appendChild(cloneNode);
    }
}