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:de.xplib.xdbm.util.Config.java

/**
 * //from  w ww. j  a  va2 s  .  c om
 */
public void save() {

    Document doc = null;
    try {
        doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();

        Element config = (Element) doc.appendChild(doc.createElementNS(CONFIG_NS, "config"));
        config.setAttribute("xmlns", CONFIG_NS);

        Element ui = (Element) config.appendChild(doc.createElementNS(CONFIG_NS, "ui-settings"));

        Node lang = ui.appendChild(doc.createElementNS(CONFIG_NS, "language"));
        lang.appendChild(doc.createTextNode(this.locale.getLanguage()));

        Element drivers = (Element) config.appendChild(doc.createElementNS(CONFIG_NS, "drivers"));
        Iterator dIt = this.dbDrivers.keySet().iterator();
        while (dIt.hasNext()) {

            String jar = (String) dIt.next();

            Element driver = (Element) drivers.appendChild(doc.createElementNS(CONFIG_NS, "driver"));

            driver.setAttribute("jar", jar);
            driver.setAttribute("class", (String) this.dbDrivers.get(jar));

            if (!this.dbUris.containsKey(jar)) {
                continue;
            }

            String value = "";

            ArrayList uris = (ArrayList) this.dbUris.get(jar);
            for (int i = 0, l = uris.size(); i < l; i++) {
                value += uris.get(i) + "||";
            }
            value = value.substring(0, value.length() - 2);

            driver.setAttribute("uris", value);
        }

        Element conns = (Element) config.appendChild(doc.createElementNS(CONFIG_NS, "connections"));

        for (int i = 0, s = this.connections.size(); i < s; i++) {

            Connection conn = (Connection) this.connections.get(i);

            Element conne = (Element) conns.appendChild(doc.createElementNS(CONFIG_NS, "connection"));
            conne.setAttribute("jar", conn.getJarFile());
            conne.setAttribute("class", conn.getClassName());
            conne.setAttribute("uri", conn.getUri());
        }

        Element panels = (Element) config.appendChild(doc.createElementNS(CONFIG_NS, "plugins"));

        Iterator it = this.plugins.keySet().iterator();
        while (it.hasNext()) {

            PluginFile pluginFile = (PluginFile) this.plugins.get(it.next());

            Element elem = (Element) panels.appendChild(doc.createElementNS(CONFIG_NS, "plugin"));
            elem.setAttribute("jar", pluginFile.getJarFile());
            elem.setAttribute("class", pluginFile.getClassName());
            elem.setAttribute("id", pluginFile.getId());
            elem.setAttribute("position", pluginFile.getPosition());
        }
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }

    if (doc != null) {

        OutputFormat of = new OutputFormat(doc);
        of.setIndenting(true);
        of.setIndent(1);
        of.setStandalone(true);

        StringWriter sw = new StringWriter();
        XMLSerializer xs = new XMLSerializer(sw, of);
        xs.setOutputCharStream(sw);

        try {
            xs.serialize(doc);

            sw.flush();

            System.out.println(sw.toString());

            sw.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        try {
            FileWriter fw = new FileWriter(this.cfgFile);
            fw.write(sw.toString());
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:aurelienribon.gdxsetupui.ProjectUpdate.java

private void writeClasspath(File classpathFile, List<ClasspathEntry> classpath) {
    try {/*w ww .ja  v  a  2  s  .  c om*/
        Document doc = XmlUtils.createParser().parse(classpathFile);
        Node root = (Node) XmlUtils.xpath("classpath", doc, XPathConstants.NODE);
        NodeList libsNodes = (NodeList) XmlUtils.xpath("classpath/classpathentry[@kind='lib' and @path]", doc,
                XPathConstants.NODESET);

        for (int i = 0; i < libsNodes.getLength(); i++) {
            root.removeChild(libsNodes.item(i));
        }

        for (ClasspathEntry entry : classpath) {
            Element elem = doc.createElement("classpathentry");
            root.appendChild(elem);

            elem.setAttribute("kind", "lib");
            if (entry.exported)
                elem.setAttribute("exported", "true");
            elem.setAttribute("path", entry.path);
            if (entry.sourcepath != null)
                elem.setAttribute("sourcepath", entry.sourcepath);
        }

        XmlUtils.clean(doc);
        String str = XmlUtils.transform(doc);
        FileUtils.writeStringToFile(classpathFile, str);

    } catch (SAXException ex) {
        throw new RuntimeException(ex);
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    } catch (TransformerException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:org.alfresco.web.config.WebConfigRuntime.java

/**
 * @param parent// w ww  .j  a  v  a 2s.  c  o m
 * @param child
 */
private void appendChild(Node parent, Node child) {
    parent.appendChild(child);
    Node previousNode = child.getPreviousSibling();
    if (previousNode != null && previousNode instanceof org.w3c.dom.Text) {
        previousNode.getParentNode().removeChild(previousNode);
    }
}

From source file:com.stratelia.webactiv.util.XMLConfigurationStore.java

public void put(String key, String values[]) {
    Node paramNode = getXMLParamNode(key);
    if (paramNode != null) {
        Node paramNodeParent = paramNode.getParentNode();
        if (paramNodeParent != null) {
            paramNodeParent.removeChild(paramNode);
        }//  w  w w.ja v  a2s. c  o m
    }
    Element param = xmlConfigDOMDoc.createElement("param");
    Node name = xmlConfigDOMDoc.createElement("param-name");
    Node description = xmlConfigDOMDoc.createElement("param-description");
    Node nameValue = xmlConfigDOMDoc.createTextNode(key);
    for (String value : values) {
        Node vValue = xmlConfigDOMDoc.createTextNode(value);
        Node v = xmlConfigDOMDoc.createElement("param-value");
        param.appendChild(v);
        v.appendChild(vValue);
    }
    rootNode.appendChild(param);
    param.appendChild(name);
    name.appendChild(nameValue);
    param.appendChild(description);
    xmlConfigDOMDoc.getDocumentElement().normalize();
}

From source file:com.photon.phresco.framework.impl.ConfigurationWriter.java

public void updateTestConfiguration(SettingsInfo settingsInfo, String browser, String resultConfigXml)
        throws PhrescoException {
    try {//from www .j a  v  a  2s . co  m
        Node configNode = getNode("environment");
        Node node = getNode("environment/" + settingsInfo.getType());
        Node browserNode = getNode("environment/Browser");
        if (node != null) {
            configNode.removeChild(node);
        }

        if (browserNode != null) {
            browserNode.setTextContent(browser);
        } else {
            Element browserEle = getDocument().createElement("Browser");
            browserEle.setTextContent(browser);
            configNode.appendChild(browserEle);
        }
        configNode.appendChild(createConfigElement(settingsInfo));
    } catch (Exception e) {
        throw new PhrescoException("Configuration not found to delete");
    }
}

From source file:com.stratelia.webactiv.util.XMLConfigurationStore.java

public void replaceValue(Node n, String key, String value) {
    Node entry = findNode(n, key);
    if (entry != null) {
        n.removeChild(entry);/* ww  w .  j av  a2  s .  c o  m*/
    }
    Node newElement = xmlConfigDOMDoc.createElement(key);
    Node newValue = xmlConfigDOMDoc.createTextNode(value);
    newElement.appendChild(newValue);
    n.appendChild(newElement);
}

From source file:DomUtils.java

/**
 * Insert the supplied nodes before the supplied reference node (refNode).
 * @param newNodes Nodes to be inserted.
 * @param refNode Reference node before which the supplied nodes should
 * be inserted.// www  . jav  a2 s  .  c o  m
 */
public static void insertBefore(NodeList newNodes, Node refNode) {

    Node parentNode = refNode.getParentNode();

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

    int nodeCount = newNodes.getLength();
    List nodeList = DomUtils.copyNodeList(newNodes);

    if (nodeCount == 0) {
        return;
    }

    if (parentNode instanceof Document) {
        List elements = DomUtils.getElements(newNodes, "*", null);

        if (!elements.isEmpty()) {
            System.out.println(
                    "Request to insert a NodeList before the Document root node.  Will replace the root element with the 1st element node from the NodeList.");
            parentNode.removeChild(refNode);
            parentNode.appendChild((Node) elements.get(0));
        } else {
            System.out.println(
                    "Cannot insert beforen the document root element from a NodeList that doesn't contain an element node.");
        }

        for (int i = 0; i < nodeCount; i++) {
            Node node = (Node) nodeList.get(i);
            if (node.getNodeType() != Node.ELEMENT_NODE) {
                System.out.println("****" + node);
                parentNode.insertBefore(node, refNode);
            }
        }
    } else {
        for (int i = 0; i < nodeCount; i++) {
            parentNode.insertBefore((Node) nodeList.get(i), refNode);
        }
    }
}

From source file:org.guanxi.sp.engine.service.shibboleth.AuthConsumerServiceThread.java

/**
 * This prepares the response to the guard regarding the AA process.
 * /*from  www .  ja  va 2 s.c  o m*/
 * @param samlResponse  Used to generate the Guard request, this has been collected before this thread starts
 * @param guardSession  The string indicating the guard session to use
 * @param aaURL         The Attribute Authority URL which was just used to get the attributes
 * @param aaResponse    The response from talking to the Attribute Authority
 * @return              An EnvelopeDocument that must be sent to the Guard
 * @throws XmlException If there is a problem parsing the aaResponse
 */
private EnvelopeDocument prepareGuardRequest(ResponseType samlResponse, String guardSession, String aaURL,
        String aaResponse) throws XmlException {
    EnvelopeDocument soapEnvelopeDoc;
    Envelope soapEnvelope;

    soapEnvelopeDoc = EnvelopeDocument.Factory.parse(aaResponse);

    soapEnvelope = soapEnvelopeDoc.getEnvelope();

    // Before we send the SAML Response from the AA to the Guard, add the Guanxi SOAP header
    Header soapHeader = soapEnvelope.addNewHeader();
    Element gx = soapHeader.getDomNode().getOwnerDocument().createElementNS("urn:guanxi:sp",
            "GuanxiGuardSessionID");
    Node gxNode = soapHeader.getDomNode().appendChild(gx);
    org.w3c.dom.Text gxTextNode = soapHeader.getDomNode().getOwnerDocument().createTextNode(guardSession);
    gxNode.appendChild(gxTextNode);

    // Add the SAML Response from the IdP to the SOAP headers
    Header authHeader = soapEnvelope.addNewHeader();
    Element auth = authHeader.getDomNode().getOwnerDocument().createElementNS("urn:guanxi:sp", "AuthnFromIdP");
    auth.setAttribute("aa", aaURL);
    Node authNode = authHeader.getDomNode().appendChild(auth);
    authNode.appendChild(authNode.getOwnerDocument().importNode(samlResponse.getDomNode(), true));

    return soapEnvelopeDoc;
}

From source file:de.betterform.session.DefaultSerializer.java

/**
 * inlines all instances from the processor into the output document. Eventually existent @src Attributes
 * have already been removed during the 'reset' transformation.
 *
 * @param out the output document for serialization
 *///from w  w w . j ava  2  s  .c  om
public void inlineInstances(Document out) throws XFormsException {
    //imlining instances
    NodeInfo context = getDocumentElementContext(out);
    //iterate all models to get all instances
    List models = this.processor.getContainer().getModels();
    for (int i = 0; i < models.size(); i++) {
        Model model = (Model) models.get(i);

        List instances = model.getInstances();
        for (int j = 0; j < instances.size(); j++) {
            Instance instance = (Instance) instances.get(j);
            String id = instance.getId();

            //get node from out
            String search = "//*[@id='" + id + "']";
            Node outInstance = XPathUtil.getAsNode(
                    XPathCache.getInstance().evaluate(context, search, Collections.EMPTY_MAP, null), 1);
            Node imported = out.adoptNode(instance.getInstanceDocument().getDocumentElement());
            if (imported == null) {
                throw new XFormsException("Root Element for Instance '" + instance.getId() + "' not found");
            }

            Node firstChild = DOMUtil.getFirstChildElement(outInstance);

            if (firstChild != null) {
                outInstance.removeChild(firstChild);
            }

            outInstance.appendChild(imported);
            //                    outInstance.replaceChild(imported,DOMUtil.getFirstChildElement(outInstance));

        }
    }
}

From source file:net.sf.zekr.engine.bookmark.BookmarkSet.java

private void _updateXml(BookmarkItem item, Node node) {
    List<BookmarkItem> list = item.getChildren();
    for (BookmarkItem childItem : list) {
        if (childItem.isFolder()) {
            Element fe = xmlDocument.createElement("folder");
            fe.setAttribute("name", childItem.getName());
            fe.setAttribute("desc", childItem.getDescription());
            _updateXml(childItem, fe);/*w  w  w  . j a  va2s  .  co m*/
            node.appendChild(fe);
        } else {
            Element ie = xmlDocument.createElement("item");
            ie.setAttribute("name", childItem.getName());
            ie.setAttribute("desc", childItem.getDescription());
            ie.setAttribute("data", CollectionUtils.toString(childItem.getLocations(), ","));
            node.appendChild(ie);
        }
    }
}