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.enonic.esl.xml.XMLTool.java

public static Node moveNode(Node node, Node fromParent, Node toParent) {
    node.getParentNode().removeChild(node);
    return toParent.appendChild(node);
}

From source file:com.github.wesjd.overcastmappacker.xml.DocumentHandler.java

public void set(Class<? extends ParentXMLModule> parentClass, Class<? extends XMLModule> moduleClass,
        String moduleValue, ContinuingMap<String, String> attributeMapping) {
    final Map<String, String> rawAttributeMapping = attributeMapping.getRaw();
    final XMLModule module = XMLModule.of(moduleClass);

    ParentXMLModule parentModule = XMLConstants.MAIN_BODY_MODULE;
    if (parentClass != null)
        parentModule = (ParentXMLModule) XMLModule.of(parentClass);
    Validate.isTrue(//www.ja  v a  2 s . c  om
            parentModule.getChildXMLModules() == null
                    || parentModule.getChildXMLModules().contains(module.getClass()),
            "Parent module must accept child.");

    Element moduleElement = (Element) document.getElementsByTagName(module.getTag()).item(0);
    if (moduleElement == null) {
        moduleElement = document.createElement(module.getTag());

        Node parentNode = document.getElementsByTagName(parentModule.getTag()).item(0);
        if (parentNode == null) {
            set(null, parentModule.getClass());
            parentNode = document.getElementsByTagName(parentModule.getTag()).item(0);
        }
        parentNode.appendChild(moduleElement);
    }
    moduleElement.setTextContent(moduleValue);

    if (module.getXMLAttributes() != null) {
        final Element finalModuleElement = moduleElement;
        module.getXMLAttributes().forEach(xmlAttribute -> {
            final String attributeName = xmlAttribute.getName();

            String value = rawAttributeMapping.containsKey(attributeName)
                    ? rawAttributeMapping.get(attributeName)
                    : xmlAttribute.getDefaultValue();
            if ((xmlAttribute.getValidValues() == null || !xmlAttribute.getValidValues().contains(value))
                    && xmlAttribute.isRequired())
                value = xmlAttribute.getDefaultValue();

            finalModuleElement.setAttribute(attributeName, value);
        });
    }
}

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

@Override
public EventTrace generate() {
    if (super.m_clockAsSeed)
        setSeed(System.currentTimeMillis());
    EventTrace trace = new EventTrace();

    RandomPicker<Transition> transition_picker = new RandomPicker<Transition>(super.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++) {
        if (super.m_verboseLevel > 0)
            System.out.println("Generating message " + i);
        // Get enabled transitions
        Vector<Transition> enabled_trans = new Vector<Transition>();

        for (Transition trans : m_transitions) {

            if (trans.isEnabled()) {

                enabled_trans.add(trans);
            }//from w  ww . jav  a 2s . co m

        }
        if (enabled_trans.isEmpty()) {
            // No next state: we are in a dead end
            break;
        }
        // Pick one such transition
        Transition t = transition_picker.pick(enabled_trans);
        // Emit event
        Node n = trace.getNode();
        Node n2 = trace.createElement("name");
        Node n3 = trace.createTextNode(t.m_label);
        n2.appendChild(n3);
        n.appendChild(n2);

        Event e = new Event(n);
        trace.add(e);
        // Fire transition
        t.fire();
    }
    return trace;
}

From source file:org.ambraproject.search.service.IndexingServiceImpl.java

/**
 *  Add the article-strkImg tag as a child  to the article-meta
 *  tag of the article ml./*from w  ww  . j  a v  a 2 s .  com*/
 */
private Document addStrikingImage(Document doc, String strkImagURI) {
    NodeList metaNodeLst = doc.getElementsByTagName("article-meta");
    Node metaNode = metaNodeLst.item(0);
    Element strkImgElem = doc.createElement("article-strkImg");

    strkImgElem.setTextContent(strkImagURI);
    metaNode.appendChild(strkImgElem.cloneNode(true));
    return doc;
}

From source file:com.enonic.esl.xml.XMLTool.java

private static Node moveNode(Node node, Node toParent) {
    Node fromParent = node.getParentNode();
    fromParent.removeChild(node);/* w w  w .  j av  a 2 s.c o  m*/
    return toParent.appendChild(node);
}

From source file:net.ion.radon.impl.let.webdav.FCKConnectorRestlet.java

private void getElements(final String protocol, final VFSPath dir, final Node root, final Document doc) {
    final Element files = doc.createElement(protocol + "s");
    root.appendChild(files);
    VFSPathToken[] fileList;//from   www. j av a  2  s.  com
    try {
        fileList = list(dir);
        ArrayList<String> sortedFileList = new ArrayList<String>();
        for (int i = 0; i < fileList.length; i++)
            sortedFileList.add(fileList[i].toString());

        Collections.sort(sortedFileList, new AlphanumericComparator());

        for (final String element : sortedFileList) {
            final VFSPath current = dir.child(new VFSPathToken(element));
            if ("Folder".equals(protocol) == vfs.isCollection(current)) {
                final Element myEl = doc.createElement(protocol);
                myEl.setAttribute("name", element);
                myEl.setAttribute("url", current.toString());

                if (protocol.equals("File"))
                    myEl.setAttribute("size", "" + vfs.getLength(current));

                files.appendChild(myEl);
            }
        }
    } catch (final Exception e) {
        TrivialExceptionHandler.ignore(this, e);
    }
}

From source file:DomUtils.java

/**
 * Insert the supplied node before the supplied reference node (refNode).
 * @param newNode Node to be inserted.// w  ww  .j a va  2s  .  c  o m
 * @param refNode Reference node before which the supplied nodes should
 * be inserted.
 */
public static void insertBefore(Node newNode, Node refNode) {

    Node parentNode = refNode.getParentNode();

    if (parentNode == null) {
        System.out.println(
                "Cannot insert [" + newNode + "] before [" + refNode + "]. [" + refNode + "] has no parent.");
        return;
    }

    if (parentNode instanceof Document && newNode.getNodeType() == Node.ELEMENT_NODE) {
        System.out.println(
                "Request to insert an element before the Document root node.  This is not allowed.  Replacing the Document root with the new Node.");
        parentNode.removeChild(refNode);
        parentNode.appendChild(newNode);
    } else {
        parentNode.insertBefore(newNode, refNode);
    }
}

From source file:com.fiorano.openesb.application.common.Param.java

/**
 * This method converts the specified <code>Document</code> object to
 * the specified <code>Node</code> object.
 *
 * @param document the Document object//from   w  ww. ja v  a2s  . c  om
 * @return the element node with the corresponding text node value.
 * @since Tifosi2.0
 */
public Node toJXMLString(Document document) {
    Node root0 = document.createElement("Param");

    ((Element) root0).setAttribute("name", m_paramName);

    Node pcData = document.createTextNode(m_paramValue);

    root0.appendChild(pcData);
    return root0;
}

From source file:com.sonyericsson.hudson.plugins.gerrit.trigger.spec.DuplicateGerritListenersPreloadedProjectHudsonTestCase.java

/**
 * Adds a compareType and pattern node as child elements to the provided parent node.
 *
 * @param xml         the document to create xml elements from.
 * @param parent      the parent to add to
 * @param compareType the name of the compare type
 *                    (See {@link com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data.CompareType})
 * @param pattern     the pattern.//from   w  w  w. j  a v  a2  s .com
 */
void setXmlConfig(Document xml, Node parent, String compareType, String pattern) {
    Node compareTypeN = xml.createElement("compareType");
    compareTypeN.setTextContent(compareType);
    parent.appendChild(compareTypeN);
    Node patternN = xml.createElement("pattern");
    patternN.setTextContent(pattern);
    parent.appendChild(patternN);
}