Example usage for org.w3c.dom Node getParentNode

List of usage examples for org.w3c.dom Node getParentNode

Introduction

In this page you can find the example usage for org.w3c.dom Node getParentNode.

Prototype

public Node getParentNode();

Source Link

Document

The parent of this node.

Usage

From source file:com.bstek.dorado.config.xml.XmlParseException.java

private static String populateErrorMessage(String message, Node node, Resource resource) {
    StringBuffer sb = new StringBuffer();
    if (message != null)
        sb.append(message);//from  w w w.  j  av a2  s  . co  m

    if (resource != null) {
        sb.append(" - ").append(resource);
    }

    if (node != null) {
        Element element;
        if (node instanceof Element) {
            element = (Element) node;
        } else {
            element = (Element) node.getParentNode();
        }

        if (element != null) {
            sb.append(" - ").append("<" + element.getTagName() + " ");
            NamedNodeMap names = element.getAttributes();
            for (int i = 0; i < 3 && i < names.getLength(); i++) {
                sb.append(populateXmlAttribute(element, names.item(i).getNodeName())).append(" ");
            }
            sb.append("... ");
        }
    }
    return sb.toString();
}

From source file:Main.java

public static String getNamespace(String prefix, Node e, Node stopNode) {
    while (e != null && (e.getNodeType() == Node.ELEMENT_NODE)) {
        Attr attr = null;// w ww  .j a v a  2s  .c  o  m
        if (prefix == null) {
            attr = ((Element) e).getAttributeNode("xmlns");
        } else {
            attr = ((Element) e).getAttributeNodeNS(NS_URI_XMLNS, prefix);
        }
        if (attr != null)
            return attr.getValue();
        if (e == stopNode)
            return null;
        e = e.getParentNode();
    }
    return null;
}

From source file:de.codesourcery.spring.contextrewrite.XMLRewrite.java

private static Rule wrap(RemoveRule r) {
    return new Rule(r.xpath(), r.id()) {
        @Override//  www  . j a  va 2s.  c om
        public void apply(Document document, Node matchedNode) throws Exception {
            matchedNode.getParentNode().removeChild(matchedNode);
        }

        @Override
        public String toString() {
            return "REMOVE: " + r.xpath();
        }
    };
}

From source file:Main.java

/**
 * Will create a documentFragment of the replacingDocument, will import the
 * replacingDocument as a node of the replacedDocument, and then will
 * replace the replaceNode with the documentFragment of replacingDocument.
 * /*from   ww  w.  java 2  s  .  c o  m*/
 * @param replacedDocument
 *            The document which will have a node replace
 * @param replacingDocument
 *            The document that will replace a node
 * @param replacedNode
 *            The node in replacedDocument that will be replaced
 * @return The new version of replacedDocument will replacedNode replaced
 */
public static Node replaceNode(Document replacedDocument, Document replacingDocument, Node replacedNode) {

    // Create a documentFragment of the replacingDocument
    DocumentFragment docFrag = replacingDocument.createDocumentFragment();
    Element rootElement = replacingDocument.getDocumentElement();
    docFrag.appendChild(rootElement);

    // Import docFrag under the ownership of replacedDocument
    Node replacingNode = ((replacedDocument).importNode(docFrag, true));

    // In order to replace the node need to retrieve replacedNode's parent
    Node replaceNodeParent = replacedNode.getParentNode();
    replaceNodeParent.replaceChild(replacingNode, replacedNode);
    return replacedDocument;
}

From source file:Main.java

/**
 * Gets the parent element./*from  w w  w  . j av a 2s .co  m*/
 * 
 * @param element
 *            the element
 * @return the parent element
 */
public static Element getParentElement(Element element) {
    if (element == null) {
        return null;
    }
    Element parentElement = null;
    Node parentNode = element.getParentNode();
    while (parentNode != null && parentElement == null) {
        if (parentNode.getNodeType() == Node.ELEMENT_NODE) {
            parentElement = (Element) parentNode;
        }
        if (parentNode.getNodeType() == Node.DOCUMENT_NODE) {
            parentElement = ((Document) parentNode).getDocumentElement();
            if (element.isSameNode(parentElement)) {
                parentElement = null;
            }
        }
        parentNode = parentNode.getParentNode();
    }

    return parentElement;
}

From source file:Main.java

/**
 * Will create a documentFragment of the replacingDocument, will import the
 * replacingDocument as a node of the replacedDocument, and then will
 * replace the replaceNode with the documentFragment of replacingDocument.
 * //from  www.jav a 2 s .  co  m
 * @param replacedDocument
 *            The document which will have a node replace
 * @param replacingDocument
 *            The document that will replace a node
 * @param replacedNode
 *            The node in replacedDocument that will be replaced
 * @return The new version of replacedDocument will replacedNode replaced
 */
public static Node replaceNode(final Document replacedDocument, final Document replacingDocument,
        final Node replacedNode) {

    // Create a documentFragment of the replacingDocument
    DocumentFragment docFrag = replacingDocument.createDocumentFragment();
    Element rootElement = replacingDocument.getDocumentElement();
    docFrag.appendChild(rootElement);

    // Import docFrag under the ownership of replacedDocument
    Node replacingNode = ((replacedDocument).importNode(docFrag, true));

    // In order to replace the node need to retrieve replacedNode's parent
    Node replaceNodeParent = replacedNode.getParentNode();
    replaceNodeParent.replaceChild(replacingNode, replacedNode);
    return replacedDocument;
}

From source file:Main.java

public static String lookupNamespaceURI(Node root, String specifiedPrefix) {
    if (root == null) {
        return null;
    }// ww  w  .  j a va  2 s. c o m
    if (root.hasAttributes()) {
        NamedNodeMap nnm = root.getAttributes();
        for (int i = 0; i < nnm.getLength(); i++) {
            Node n = nnm.item(i);
            if (("xmlns".equals(n.getPrefix()) && specifiedPrefix.equals(n.getNodeName()))
                    || ("xmlns:" + specifiedPrefix).equals(n.getNodeName())) {
                return n.getNodeValue();
            }
        }
    }
    return lookupNamespaceURI(root.getParentNode(), specifiedPrefix);
}

From source file:com.ibm.rpe.web.service.docgen.impl.GenerateBaseTemplate.java

private static void createTemplateXmls(String inputFile, BaseTemplateInput data)
        throws ParserConfigurationException, SAXException, IOException {
    String directory = (new File(inputFile)).getParent() + File.separator;
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = null;

    builder = factory.newDocumentBuilder();

    Document document = builder.parse(new File(inputFile));

    NodeList nlist = document.getElementsByTagName("element");
    if (nlist != null && nlist.getLength() > 0) {
        for (int i = 0; i < nlist.getLength(); i++) {
            Node node = nlist.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                Element ele = (Element) node;
                String attr = ele.getAttribute("id");

                if (attr.contentEquals(data.getElementId())) {
                    Node newNode = document.createTextNode(data.getReplacement());
                    node.getParentNode().insertBefore(newNode, node);
                    try {
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }// ww  w  .  j ava2  s . c o  m
                    Node removedNode = node.getParentNode().removeChild(node);
                    // System.out.println(nodeToString(removedNode));
                    String removed = nodeToString(removedNode);
                    removed = replace(removed, data.getReplaceMap());
                    if (data.isReplaceElementId()) {
                        removed = replaceElementId(removed);
                    }
                    document.normalize();
                    FileUtils.writeFile(directory + data.getOutputFile(), removed);
                    System.out.println("Removed : " + ((Element) removedNode).getAttribute("id"));

                    try {
                        // prettyPrint(document);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                }
            }
        }
        FileUtils.writeFile(inputFile, transformToString(document));
    }
}

From source file:com.centeractive.ws.legacy.SchemaUtils.java

private static void selectDefinitionParts(String wsdlUrl, Map<String, XmlObject> existing, SchemaLoader loader,
        XmlObject xmlObject, String path) throws Exception {
    XmlObject[] wsdlImports = xmlObject.selectPath(path);
    for (int i = 0; i < wsdlImports.length; i++) {
        String location = ((SimpleValue) wsdlImports[i]).getStringValue();
        if (location != null) {
            if (StringUtils.isNotBlank(location)) {
                if (!location.startsWith("file:") && location.indexOf("://") == -1) {
                    location = joinRelativeUrl(wsdlUrl, location);
                }/*w w w. j  av a 2s.  c o  m*/

                getDefinitionParts(location, existing, loader);
            } else {
                Node domNode = ((Attr) wsdlImports[i].getDomNode()).getOwnerElement();
                domNode.getParentNode().removeChild(domNode);
            }
        }
    }
}

From source file:cz.muni.fi.mir.mathmlunificator.MathMLUnificator.java

/**
 * Replace the given node with unification element containing unification
 * representing symbol {@code &#x25CD;} (for Presentation MathML, see
 * {@link Constants#PMATHML_UNIFICATOR}) or {@code &#x25D0;} (for Content
 * MathML, see {@link Constants#CMATHML_UNIFICATOR}).
 *
 * @param oldNode The node to be replaced with the unification representing
 * element./*from  w  w  w .  ja  v a  2s .co  m*/
 * @throws IllegalArgumentException If the given node does not have parent.
 */
public static void replaceNodeWithUnificator(Node oldNode) {

    Node parentNode = oldNode.getParentNode();

    if (parentNode == null) {
        throw new IllegalArgumentException("Cannot replace node [" + oldNode + "] that has no parent.");
    } else {
        if (!Constants.CMATHML_ANNOTATIONS.contains(oldNode.getNodeName())) { // Do not modify annotation elements!
            String unificator = PMATHML_UNIFICATOR;
            String unificatorElementType = oldNode.getNodeName().equals(PMATHML_OPERATOR) ? PMATHML_OPERATOR
                    : PMATHML_IDENTIFIER;
            if (MathMLTools.isContentMathMLNode(oldNode)) {
                unificator = CMATHML_UNIFICATOR;
                unificatorElementType = Constants.CMATHML_IDENTIFIER_OR_NUMBER.contains(oldNode.getNodeName())
                        ? CMATHML_IDENTIFIER
                        : CMATHML_SYMBOL;
            }
            Node newNode = oldNode.getOwnerDocument().createElementNS(oldNode.getNamespaceURI(),
                    unificatorElementType);
            newNode.setTextContent(unificator);
            parentNode.replaceChild(newNode, oldNode);
        }
    }

}