Example usage for org.w3c.dom Node appendChild

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

Introduction

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

Prototype

public Node appendChild(Node newChild) throws DOMException;

Source Link

Document

Adds the node newChild to the end of the list of children of this node.

Usage

From source file:com.photon.phresco.framework.commons.QualityUtil.java

private static void appendHeaderManagerCollectionProp(Document document, Node elementProp,
        Map<String, String> headersMap) {
    Node collectionProp = document.createElement("collectionProp");
    NamedNodeMap attributes = collectionProp.getAttributes();
    attributes.setNamedItem(createAttribute(document, "name", "HeaderManager.headers"));
    createHeaderElementProp(document, headersMap, collectionProp);
    elementProp.setTextContent(null);//from   w w  w.j av a 2 s .  com
    elementProp.appendChild(collectionProp);
}

From source file:de.erdesignerng.model.serializer.CommonAbstractXMLSerializer.java

protected Element addElement(Document aDocument, Node aNode, String aElementName) {
    Element theElement = aDocument.createElement(aElementName);
    aNode.appendChild(theElement);
    return theElement;
}

From source file:Main.java

/**
 * This method sets value to given node/* w w w.jav a  2 s .  com*/
 * 
 * @param inputNode
 *            Node to which value needs to be set
 * @param nodeValue
 *            Value to set
 * @throws IllegalArgumentException
 *             if input is invalid
 */
public static void setNodeTextValue(final Node inputNode, final String nodeValue)
        throws IllegalArgumentException {
    // Child list
    NodeList childList = null;

    // Validate input stream
    if (inputNode == null) {
        throw new IllegalArgumentException("Input Node cannot be null in XmlUtils.setNodeValue");
    }

    // Get child list
    childList = inputNode.getChildNodes();

    // If child nodes found
    if ((childList != null) && (childList.getLength() > 0)) {
        // Get child count
        final int childCount = childList.getLength();

        // For each child
        for (int childIndex = 0; childIndex < childCount; childIndex++) {
            final Node childNode = childList.item(childIndex);
            // Check if text node
            if ((childNode != null) && (childNode.getNodeType() == Node.TEXT_NODE)) {
                // Set value to text node
                childNode.setNodeValue(nodeValue);
                break;
            }
        }
    } else {
        // Create text node and set node value
        inputNode.appendChild(inputNode.getOwnerDocument().createTextNode(nodeValue));
    }
}

From source file:ca.uqac.info.trace.generation.FsmGenerator.java

@Override
public EventTrace generate() {
    if (super.m_clockAsSeed)
        setSeed(System.currentTimeMillis());
    EventTrace trace = new EventTrace();
    String current_state = m_initialStateLabel;
    RandomPicker<FsmTriplet> triplet_picker = new RandomPicker<FsmTriplet>(m_random);
    // We choose the number of messages to produce
    int n_messages = super.m_random.nextInt(super.m_maxMessages + 1 - super.m_minMessages)
            + super.m_minMessages;
    for (int i = 0; i < n_messages; i++) {
        // Gather possible next states
        Set<FsmTriplet> triplets = m_triplets.get(current_state);
        if (triplets == null || triplets.size() == 0) {
            // No next state: we are in a dead end
            break;
        }//from  www . j  av  a 2s . c o  m
        // Pick one next state
        FsmTriplet t = triplet_picker.pick(triplets);
        // Emit event
        Node n = trace.getNode();
        n.appendChild(trace.createTextNode(t.m_label));
        Event e = new Event(n);
        trace.add(e);
        // Set new current state
        current_state = t.m_to;
    }
    return trace;
}

From source file:com.photon.phresco.framework.commons.QualityUtil.java

private static void createHeaderElementProp(Document document, Map<String, String> headersMap,
        Node collectionProp) {
    for (Map.Entry<String, String> entry : headersMap.entrySet()) {
        Node subElementProp = document.createElement("elementProp");
        NamedNodeMap subElementAttributes = subElementProp.getAttributes();
        subElementAttributes.setNamedItem(createAttribute(document, "name", ""));
        subElementAttributes.setNamedItem(createAttribute(document, "elementType", "Header"));
        collectionProp.appendChild(subElementProp);
        appendTypeProp(document, subElementProp, "stringProp", "Header.name", entry.getKey());
        appendTypeProp(document, subElementProp, "stringProp", "Header.value", entry.getValue());
    }//from   www. j  av a2  s  . c  o  m
}

From source file:bridge.toolkit.commands.S1000DConverter.java

/**
 * Receive the node, the DOM object and the new name of the node
 * /*  w  w  w  .ja  v  a  2  s . c  o  m*/
 * @param nodo
 * @param doc
 * @param newname
 */
public static Node changeNodename(Node nodo, org.w3c.dom.Document doc, String newname) {
    // Create an element with the new name
    Node element2 = doc.createElement(newname);

    // Copy the attributes to the new element
    NamedNodeMap attrs = nodo.getAttributes();
    for (int i = 0; i < attrs.getLength(); i++) {
        Attr attr2 = (Attr) doc.importNode(attrs.item(i), true);
        element2.getAttributes().setNamedItem(attr2);
    }

    // Move all the children
    while (nodo.hasChildNodes()) {
        element2.appendChild(nodo.getFirstChild());
    }

    // Replace the old node with the new node
    nodo.getParentNode().replaceChild(element2, nodo);
    return element2;

}

From source file:com.photon.phresco.impl.WindowsApplicationProcessor.java

private static void updateContent(Document doc, List<ArtifactGroup> artifactGroups, List<Node> itemGroup,
        String elementName) {/*from  w  ww  .jav  a2  s  . c  o  m*/
    for (Node node : itemGroup) {
        NodeList childNodes = node.getChildNodes();
        for (int j = 0; j < childNodes.getLength(); j++) {
            Node item = childNodes.item(j);
            if (item.getNodeName().equals(elementName)) {
                Node parentNode = item.getParentNode();
                for (ArtifactGroup artifactGroup : artifactGroups) {
                    if (artifactGroup.getType().name().equals(Type.FEATURE.name())) {
                        Element content = doc.createElement(elementName);
                        if (elementName.equalsIgnoreCase(REFERENCE)) {
                            content.setAttribute(INCLUDE, artifactGroup.getName() + DLL);
                            Element hintPath = doc.createElement(HINTPATH);
                            hintPath.setTextContent(
                                    DOUBLE_DOT + COMMON + File.separator + artifactGroup.getName() + DLL);
                            content.appendChild(hintPath);
                        } else {
                            content.setAttribute(INCLUDE, artifactGroup.getName() + DLL);
                        }
                        parentNode.appendChild(content);
                    }
                }
                break;
            }
        }
    }
}

From source file:com.wavemaker.tools.pws.install.PwsInstall.java

public static Document insertImport(Document doc, String resource) {
    List<Node> targetList = new ArrayList<Node>();

    // First, delete old lines if any.

    NodeList list = doc.getElementsByTagName("import");
    Node node = null;//  ww  w. j  a va 2  s  .c  o  m
    for (int i = 0; i < list.getLength(); i++) {
        node = list.item(i);
        NamedNodeMap attributes = node.getAttributes();
        for (int j = 0; j < attributes.getLength(); j++) {
            Node attr = attributes.item(j);
            if (attr.getNodeName().equals("resource") && attr.getNodeValue().equals(resource)) {
                targetList.add(node);
                break;
            }
        }
    }

    NodeList beans_list = doc.getElementsByTagName("beans");
    Node beans_node = beans_list.item(0);

    if (targetList.size() > 0) {
        for (Node target : targetList) {
            beans_node.removeChild(target);
        }
    }

    // Now, add the new line

    NodeList list1 = beans_node.getChildNodes();
    Node bean_node = null;
    for (int i = 0; i < list1.getLength(); i++) {
        Node node1 = list1.item(i);
        if (node1.getNodeName().equals("bean")) {
            bean_node = node1;
            break;
        }
    }

    Element elem = doc.createElement("import");
    elem.setAttribute("resource", resource);

    try {
        if (bean_node != null) {
            beans_node.insertBefore(elem, bean_node);
        } else {
            beans_node.appendChild(elem);
        }
    } catch (DOMException ex) {
        ex.printStackTrace();
    }

    return doc;
}

From source file:Sax2Dom.java

public void startElement(String namespace, String localName, String qName, Attributes attrs) {
    final Element tmp = (Element) _document.createElementNS(namespace, qName);

    // Add namespace declarations first
    if (_namespaceDecls != null) {
        final int nDecls = _namespaceDecls.size();
        for (int i = 0; i < nDecls; i++) {
            final String prefix = (String) _namespaceDecls.elementAt(i++);

            if (prefix == null || prefix.equals(EMPTYSTRING)) {
                tmp.setAttributeNS(XMLNS_URI, XMLNS_PREFIX, (String) _namespaceDecls.elementAt(i));
            } else {
                tmp.setAttributeNS(XMLNS_URI, XMLNS_STRING + prefix, (String) _namespaceDecls.elementAt(i));
            }/*from w  w w  .  j  ava  2s  .  co  m*/
        }
        _namespaceDecls.clear();
    }

    // Add attributes to element
    final int nattrs = attrs.getLength();
    for (int i = 0; i < nattrs; i++) {
        if (attrs.getLocalName(i) == null) {
            tmp.setAttribute(attrs.getQName(i), attrs.getValue(i));
        } else {
            tmp.setAttributeNS(attrs.getURI(i), attrs.getQName(i), attrs.getValue(i));
        }
    }

    // Append this new node onto current stack node
    Node last = (Node) _nodeStk.peek();
    last.appendChild(tmp);

    // Push this node onto stack
    _nodeStk.push(tmp);
}

From source file:com.rapidminer.gui.OperatorDocLoader.java

/**
 * /*  www.  j  a v  a  2s.c  om*/
 * @param operatorWikiName
 * @param opDesc
 * @return The parsed <tt>Document</tt> (not finally parsed) of the selected operator.
 * @throws MalformedURLException
 * @throws ParserConfigurationException
 */
private static Document parseDocumentForOperator(String operatorWikiName, OperatorDescription opDesc)
        throws MalformedURLException, ParserConfigurationException {
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    builderFactory.setIgnoringComments(true);
    builderFactory.setIgnoringElementContentWhitespace(true);
    DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder();
    documentBuilder.setEntityResolver(new XHTMLEntityResolver());

    Document document = null;
    URL url = new URL(WIKI_PREFIX_FOR_OPERATORS + operatorWikiName);
    if (url != null) {
        try {
            document = documentBuilder.parse(WebServiceTools.openStreamFromURL(url));
        } catch (IOException e) {
            logger.warning("Could not open " + url.toExternalForm() + ": " + e.getMessage());
        } catch (SAXException e) {
            logger.warning("Could not parse operator documentation: " + e.getMessage());
        }

        int i = 0;

        if (document != null) {
            Element contentElement = document.getElementById("content");

            // removing content element from document
            if (contentElement != null) {
                contentElement.getParentNode().removeChild(contentElement);
            }

            // removing everything from body
            NodeList bodies = document.getElementsByTagName("body");
            for (int k = 0; k < bodies.getLength(); k++) {
                Node body = bodies.item(k);
                while (body.hasChildNodes()) {
                    body.removeChild(body.getFirstChild());
                }

                // read content element to body
                if (contentElement != null && k == 0) {
                    body.appendChild(contentElement);
                }
            }

            // removing everything from head
            NodeList heads = document.getElementsByTagName("head");
            for (int k = 0; k < heads.getLength(); k++) {
                Node head = heads.item(k);
                while (head.hasChildNodes()) {
                    head.removeChild(head.getFirstChild());
                }
            }
            // removing...<head/> from document
            if (heads != null) {
                while (i < heads.getLength()) {
                    Node head = heads.item(i);
                    head.getParentNode().removeChild(head);
                }
            }

            // removing jump-to-nav element from document
            Element jumpToNavElement = document.getElementById("jump-to-nav");
            if (jumpToNavElement != null) {
                jumpToNavElement.getParentNode().removeChild(jumpToNavElement);
            }

            // removing mw-normal-catlinks element from document
            Element mwNormalCatlinksElement = document.getElementById("mw-normal-catlinks");
            if (mwNormalCatlinksElement != null) {
                mwNormalCatlinksElement.getParentNode().removeChild(mwNormalCatlinksElement);
            }

            // removing complete link navigation
            Element tocElement = document.getElementById("toc");
            if (tocElement != null) {
                tocElement.getParentNode().removeChild(tocElement);
            }

            // removing everything from class printfooter
            NodeList nodeListDiv = document.getElementsByTagName("div");
            for (int k = 0; k < nodeListDiv.getLength(); k++) {
                Element div = (Element) nodeListDiv.item(k);
                if (div.getAttribute("class").equals("printfooter")) {
                    div.getParentNode().removeChild(div);
                }
            }

            // removing everything from class editsection
            NodeList spanList = document.getElementsByTagName("span");
            for (int k = 0; k < spanList.getLength(); k++) {
                Element span = (Element) spanList.item(k);
                if (span.getAttribute("class").equals("editsection")) {
                    span.getParentNode().removeChild(span);
                }
            }

            // Synopsis Header
            boolean doIt = true;
            NodeList pList = document.getElementsByTagName("p");
            for (int k = 0; k < pList.getLength(); k++) {

                if (doIt) {
                    Node p = pList.item(k);
                    NodeList pChildList = p.getChildNodes();

                    for (int j = 0; j < pChildList.getLength(); j++) {

                        Node pChild = pChildList.item(j);
                        if (pChild.getNodeType() == Node.TEXT_NODE && pChild.getNodeValue() != null
                                && StringUtils.isNotBlank(pChild.getNodeValue())
                                && StringUtils.isNotEmpty(pChild.getNodeValue())) {

                            String pChildString = pChild.getNodeValue();
                            Element newPWithoutSpaces = document.createElement("p");
                            newPWithoutSpaces.setTextContent(pChildString);

                            Node synopsis = document.createTextNode("Synopsis");

                            Element span = document.createElement("span");
                            span.setAttribute("class", "mw-headline");
                            span.setAttribute("id", "Synopsis");
                            span.appendChild(synopsis);

                            Element h2 = document.createElement("h2");
                            h2.appendChild(span);

                            Element div = document.createElement("div");
                            div.setAttribute("id", "synopsis");
                            div.appendChild(h2);
                            div.appendChild(newPWithoutSpaces);

                            Node pChildParentParent = pChild.getParentNode().getParentNode();
                            Node pChildParent = pChild.getParentNode();

                            pChildParentParent.replaceChild(div, pChildParent);
                            doIt = false;
                            break;
                        }
                    }
                } else {
                    break;
                }
            }

            // removing all <br...>-Tags
            NodeList brList = document.getElementsByTagName("br");

            while (i < brList.getLength()) {
                Node br = brList.item(i);
                Node parentBrNode = br.getParentNode();
                parentBrNode.removeChild(br);
            }

            // removing everything from script
            NodeList scriptList = document.getElementsByTagName("script");
            while (i < scriptList.getLength()) {
                Node scriptNode = scriptList.item(i);
                Node parentNode = scriptNode.getParentNode();
                parentNode.removeChild(scriptNode);
            }

            // removing all empty <p...>-Tags
            NodeList pList2 = document.getElementsByTagName("p");
            int ccc = 0;
            while (ccc < pList2.getLength()) {
                Node p = pList2.item(ccc);
                NodeList pChilds = p.getChildNodes();

                int kk = 0;

                while (kk < pChilds.getLength()) {
                    Node pChild = pChilds.item(kk);
                    if (pChild.getNodeType() == Node.TEXT_NODE) {
                        String pNodeValue = pChild.getNodeValue();
                        if (pNodeValue == null || StringUtils.isBlank(pNodeValue)
                                || StringUtils.isEmpty(pNodeValue)) {
                            kk++;
                        } else {
                            ccc++;
                            break;
                        }
                    } else {
                        ccc++;
                        break;
                    }
                    if (kk == pChilds.getLength()) {
                        Node parentBrNode = p.getParentNode();
                        parentBrNode.removeChild(p);
                    }
                }
            }

            // removing firstHeading element from document
            Element firstHeadingElement = document.getElementById("firstHeading");
            if (firstHeadingElement != null) {
                CURRENT_OPERATOR_NAME_READ_FROM_RAPIDWIKI = firstHeadingElement.getFirstChild().getNodeValue()
                        .replaceFirst(".*:", "");
                firstHeadingElement.getParentNode().removeChild(firstHeadingElement);
            }

            // setting operator plugin name
            if (opDesc != null && opDesc.getProvider() != null) {
                CURRENT_OPERATOR_PLUGIN_NAME = opDesc.getProvider().getName();
            }

            // removing sitesub element from document
            Element siteSubElement = document.getElementById("siteSub");
            if (siteSubElement != null) {
                siteSubElement.getParentNode().removeChild(siteSubElement);
            }

            // removing contentSub element from document
            Element contentSubElement = document.getElementById("contentSub");
            if (contentSubElement != null) {
                contentSubElement.getParentNode().removeChild(contentSubElement);
            }

            // removing catlinks element from document
            Element catlinksElement = document.getElementById("catlinks");
            if (catlinksElement != null) {
                catlinksElement.getParentNode().removeChild(catlinksElement);
            }

            // removing <a...> element from document, if they are empty
            NodeList aList = document.getElementsByTagName("a");
            if (aList != null) {
                int k = 0;
                while (k < aList.getLength()) {
                    Node a = aList.item(k);
                    Element aElement = (Element) a;
                    if (aElement.getAttribute("class").equals("internal")) {
                        a.getParentNode().removeChild(a);
                    } else {
                        Node aChild = a.getFirstChild();
                        if (aChild != null
                                && (aChild.getNodeValue() != null && aChild.getNodeType() == Node.TEXT_NODE
                                        && StringUtils.isNotBlank(aChild.getNodeValue())
                                        && StringUtils.isNotEmpty(aChild.getNodeValue())
                                        || aChild.getNodeName() != null)) {
                            Element aChildElement = null;
                            if (aChild.getNodeName().startsWith("img")) {
                                aChildElement = (Element) aChild;

                                Element imgElement = document.createElement("img");
                                imgElement.setAttribute("alt", aChildElement.getAttribute("alt"));
                                imgElement.setAttribute("class", aChildElement.getAttribute("class"));
                                imgElement.setAttribute("height", aChildElement.getAttribute("height"));
                                imgElement.setAttribute("src",
                                        WIKI_PREFIX_FOR_IMAGES + aChildElement.getAttribute("src"));
                                imgElement.setAttribute("width", aChildElement.getAttribute("width"));
                                imgElement.setAttribute("border", "1");

                                Node aParent = a.getParentNode();
                                aParent.replaceChild(imgElement, a);
                            } else {
                                k++;
                            }
                        } else {
                            a.getParentNode().removeChild(a);
                        }
                    }
                }
            }

        }
    }
    return document;
}