Example usage for org.w3c.dom Document renameNode

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

Introduction

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

Prototype

public Node renameNode(Node n, String namespaceURI, String qualifiedName) throws DOMException;

Source Link

Document

Rename an existing node of type ELEMENT_NODE or ATTRIBUTE_NODE.

Usage

From source file:Main.java

/**
 * Recursively strips the namespace information from a node.
 * @param node the starting node./*from  w  w  w.  j a v a  2  s . c  o  m*/
 * @param document host document
 * @return clean node
 */
public static Node removeNamespaceRecursive(final Node node, final Document document) {
    Node newNode = null;

    if (node.getNodeType() == Node.ELEMENT_NODE) {
        newNode = document.renameNode(node, null, removeNsPrefix(node.getNodeName()));
    }

    final NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); ++i) {
        removeNamespaceRecursive(list.item(i), document);
    }

    return newNode;
}

From source file:Main.java

public static void spreadNamespaces(Node node, String tns, boolean overwrite) {
    Document doc = node instanceof Document ? (Document) node : node.getOwnerDocument();
    boolean isParent = false;
    while (node != null) {
        Node next = null;/*from   www . j ava 2s  .c  om*/
        if (!isParent && node.getNodeType() == Node.ELEMENT_NODE) {
            if (node.getNamespaceURI() == null) {
                node = doc.renameNode(node, tns, node.getNodeName());
            } else {
                if (overwrite) {
                    tns = node.getNamespaceURI();
                }
            }
            NamedNodeMap nodeMap = node.getAttributes();
            int nodeMapLengthl = nodeMap.getLength();
            for (int i = 0; i < nodeMapLengthl; i++) {
                Node attr = nodeMap.item(i);
                if (attr.getNamespaceURI() == null) {
                    doc.renameNode(attr, tns, attr.getNodeName());
                }
            }
        }
        isParent = (isParent || (next = node.getFirstChild()) == null)
                && (next = node.getNextSibling()) == null;
        node = isParent ? node.getParentNode() : next;
        if (isParent && node != null) {
            if (overwrite) {
                tns = node.getNamespaceURI();
            }
        }
    }
}

From source file:com.adobe.acs.commons.components.longformtext.impl.LongFormTextComponentImpl.java

/**
 * Method borrowed from: https://blog.avisi.nl/2013/07/24/java-stripping-namespaces-from-xml-using-dom/
 *
 * Recursively renames the namespace of a node.
 * @param node the starting node./*from  w ww. java 2 s .com*/
 */
private void stripNamespaces(org.w3c.dom.Node node) {
    Document document = node.getOwnerDocument();
    if (node.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
        document.renameNode(node, null, node.getNodeName());
    }
    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); ++i) {
        stripNamespaces(list.item(i));
    }
}

From source file:com.msopentech.odatajclient.engine.data.json.JSONPropertySerializer.java

/**
 * {@inheritDoc }// w w w. j  ava2 s . c  o m
 */
@Override
public void serialize(final JSONProperty property, final JsonGenerator jgen, final SerializerProvider provider)
        throws IOException, JsonProcessingException {

    jgen.writeStartObject();

    if (property.getMetadata() != null) {
        jgen.writeStringField(ODataConstants.JSON_METADATA, property.getMetadata().toASCIIString());
    }

    final Element content = property.getContent();
    if (XMLUtils.hasOnlyTextChildNodes(content)) {
        jgen.writeStringField(ODataConstants.JSON_VALUE, content.getTextContent());
    } else {
        try {
            final DocumentBuilder builder = ODataConstants.DOC_BUILDER_FACTORY.newDocumentBuilder();
            final Document document = builder.newDocument();
            final Element wrapper = document.createElement(ODataConstants.ELEM_PROPERTY);

            if (XMLUtils.hasElementsChildNode(content)) {
                wrapper.appendChild(document.renameNode(document.importNode(content, true), null,
                        ODataConstants.JSON_VALUE));

                DOMTreeUtils.writeSubtree(jgen, wrapper);
            } else if (EdmSimpleType.isGeospatial(content.getAttribute(ODataConstants.ATTR_M_TYPE))) {
                wrapper.appendChild(document.renameNode(document.importNode(content, true), null,
                        ODataConstants.JSON_VALUE));

                DOMTreeUtils.writeSubtree(jgen, wrapper, true);
            } else {
                DOMTreeUtils.writeSubtree(jgen, content);
            }
        } catch (Exception e) {
            throw new JsonParseException("Cannot serialize property", JsonLocation.NA, e);
        }
    }

    jgen.writeEndObject();
}

From source file:com.sitewhere.configuration.ConfigurationMigrationSupport.java

/**
 * Moves event processing elements from previous locations into new element.
 * /*from  w ww.j  a v a  2s  . c om*/
 * @param config
 * @param dcomm
 * @param eproc
 * @param document
 * @throws SiteWhereException
 */
protected static void moveEventProcessingElements(Element config, Element dcomm, Element eproc,
        Document document) throws SiteWhereException {
    Element iproc = DomUtils.getChildElementByTagName(dcomm, "inbound-processing-strategy");
    if (iproc != null) {
        dcomm.removeChild(iproc);
        eproc.appendChild(iproc);
    }
    Element ichain = DomUtils.getChildElementByTagName(config, "inbound-processing-chain");
    if (ichain != null) {
        config.removeChild(ichain);
        eproc.appendChild(ichain);
    }
    Element oproc = DomUtils.getChildElementByTagName(dcomm, "outbound-processing-strategy");
    if (oproc != null) {
        dcomm.removeChild(oproc);
        eproc.appendChild(oproc);
    }
    Element ochain = DomUtils.getChildElementByTagName(config, "outbound-processing-chain");
    if (ochain != null) {
        config.removeChild(ochain);
        eproc.appendChild(ochain);
    }
    Element reg = DomUtils.getChildElementByTagName(dcomm, "registration");
    if (reg != null) {
        String qname = (reg.getPrefix() != null) ? (reg.getPrefix() + ":" + "device-services")
                : "device-services";
        document.renameNode(reg, reg.getNamespaceURI(), qname);
    }
}

From source file:com.mirth.connect.model.converters.DICOMSerializer.java

private void renameAttrToTag(Document document, Node node) throws DOMException {
    if (node.getNodeName().equals("attr")) {
        String tag = node.getAttributes().getNamedItem("tag").getNodeValue();

        if (!tag.equals("?")) {
            document.renameNode(node, null, "tag" + tag);
        }/*from www  .java 2  s.c o m*/
    }
}

From source file:com.mirth.connect.model.converters.DICOMSerializer.java

private void renameTagToAttr(Document document, Node node) throws DOMException {
    NamedNodeMap attr = node.getAttributes();

    if (attr != null) {
        Node tagAttr = attr.getNamedItem("tag");

        if (tagAttr != null) {
            String tag = "tag" + tagAttr.getNodeValue();

            if (!tag.equals("?") && tag.equals(node.getNodeName())) {
                document.renameNode(node, null, "attr");
            }/* w  w w  .jav a 2s. c o  m*/
        }
    }
}

From source file:com.whatsthatlight.teamcity.hipchat.HipChatConfigurationController.java

private void upgradeConfigurationFromV0dot1ToV0dot2() throws IOException, SAXException,
        ParserConfigurationException, TransformerFactoryConfigurationError, TransformerException {
    File configFile = new File(this.configFilePath);
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(configFile);
    Element rootElement = document.getDocumentElement();
    NodeList nodes = rootElement.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        if (nodes.item(i).getNodeName().equals(HipChatConfiguration.DEFAULT_ROOM_ID_KEY_V0DOT1)
                && nodes.item(i) instanceof Element) {
            Element roomElement = (Element) nodes.item(i);
            document.renameNode(roomElement, roomElement.getNamespaceURI(),
                    HipChatConfiguration.DEFAULT_ROOM_ID_KEY);
        }//from   w  ww . j  a  v  a 2  s. co  m
    }

    // Save
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    Result output = new StreamResult(configFile);
    Source input = new DOMSource(document);
    transformer.transform(input, output);
}

From source file:nl.b3p.kaartenbalie.service.requesthandler.WMSRequestHandler.java

private static void prefixElements(Document source, Document destination, String spAbbr) {
    Element root_source = source.getDocumentElement();
    NodeList nodelist_source = root_source.getChildNodes();
    int size_source = nodelist_source.getLength();

    for (int i = 0; i < size_source; i++) {
        Node node_source = nodelist_source.item(i);

        if (node_source instanceof Element) {
            Element element_source = (Element) node_source;
            String tagName = element_source.getTagName();

            if (!tagName.equalsIgnoreCase("ServiceException")) {
                Node importedNode = destination.importNode(element_source, true);
                Node newNode = destination.renameNode(importedNode, importedNode.getNamespaceURI(),
                        OGCCommunication.attachSp(spAbbr, tagName));

                Element root_destination = destination.getDocumentElement();
                root_destination.appendChild(newNode);
            }// w ww. j a v  a2s .  co  m
        }
    }
}