Example usage for org.w3c.dom Node setNodeValue

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

Introduction

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

Prototype

public void setNodeValue(String nodeValue) throws DOMException;

Source Link

Document

The value of this node, depending on its type; see the table above.

Usage

From source file:com.microsoftopentechnologies.intellij.helpers.azure.AzureRestAPIManager.java

public void setSelectedSubscriptions(List<UUID> selectedList) throws AzureCmdException {
    try {/*w  w w  . j  a  va2  s.  c  om*/
        AzureAuthenticationMode mode = getAuthenticationMode();

        if (mode == AzureAuthenticationMode.SubscriptionSettings) {
            String subscriptionFile = PropertiesComponent.getInstance()
                    .getValue(MSOpenTechToolsApplication.AppSettingsNames.SUBSCRIPTION_FILE, "");

            NodeList subscriptionList = (NodeList) XmlHelper.getXMLValue(subscriptionFile, "//Subscription",
                    XPathConstants.NODESET);
            for (int i = 0; i < subscriptionList.getLength(); i++) {
                UUID id = UUID.fromString(XmlHelper.getAttributeValue(subscriptionList.item(i), "Id"));
                Node node = subscriptionList.item(i).getAttributes().getNamedItem("Selected");

                if (node == null) {
                    node = subscriptionList.item(i).getOwnerDocument().createAttribute("Selected");
                }

                node.setNodeValue(selectedList.contains(id) ? "true" : "false");
                subscriptionList.item(i).getAttributes().setNamedItem(node);
            }

            if (subscriptionList.getLength() > 0) {
                String savedXml = XmlHelper.saveXmlToStreamWriter(subscriptionList.item(0).getOwnerDocument());
                PropertiesComponent.getInstance()
                        .setValue(MSOpenTechToolsApplication.AppSettingsNames.SUBSCRIPTION_FILE, savedXml);
            }
        } else if (mode == AzureAuthenticationMode.ActiveDirectory) {
            for (Subscription subscription : subscriptions) {
                subscription.setSelected(selectedList.contains(subscription.getId()));
            }

            ArrayList<String> selectedIds = new ArrayList<String>();
            for (UUID uuid : selectedList) {
                selectedIds.add(uuid.toString());
            }

            PropertiesComponent.getInstance().setValue(
                    MSOpenTechToolsApplication.AppSettingsNames.SELECTED_SUBSCRIPTIONS,
                    StringUtils.join(selectedIds, ","));
        }
    } catch (Exception e) {
        throw new AzureCmdException("Error getting subscription list", e);
    }
}

From source file:com.netspective.sparx.util.xml.XmlSource.java

public void replaceNodeMacros(Node inNode, Set nodeNames, Map variables) {
    if (!variables.containsKey("this"))
        variables.put("this", inNode);

    NamedNodeMap attrs = inNode.getAttributes();
    if (attrs != null && attrs.getLength() > 0) {
        for (int i = 0; i < attrs.getLength(); i++) {
            Node attr = attrs.item(i);
            if (nodeNames.contains(attr.getNodeName())) {
                String nodeValue = attr.getNodeValue();
                String replaced = replaceExpressions(nodeValue, variables);
                if (nodeValue != replaced)
                    attr.setNodeValue(replaced);
            }/*from  ww w . j  a  v a2  s  .c om*/
        }
    }

    NodeList children = inNode.getChildNodes();
    for (int c = 0; c < children.getLength(); c++) {
        Node node = children.item(c);
        if (node.getNodeType() == Node.ELEMENT_NODE && nodeNames.contains(node.getNodeName())) {
            Text textNode = (Text) node.getFirstChild();
            String nodeValue = textNode.getNodeValue();
            String replaced = replaceExpressions(nodeValue, variables);
            if (nodeValue != replaced)
                textNode.setNodeValue(replaced);
        }
    }
}

From source file:com.alfaariss.oa.util.configuration.ConfigurationManager.java

/**
 * Saves the configuration./* w w w  .j  a v  a2  s .  c om*/
 * @see IConfigurationManager#saveConfiguration()
 */
public synchronized void saveConfiguration() throws ConfigurationException {
    boolean bFound = false;
    Date dNow = null;
    StringBuffer sbComment = null;
    Element elRoot = null;
    Node nCurrent = null;
    Node nComment = null;
    String sValue = null;
    try {
        // add date to configuration
        dNow = new Date(System.currentTimeMillis());

        sbComment = new StringBuffer(" Configuration changes saved on ");
        sbComment.append(DateFormat.getDateInstance().format(dNow));
        sbComment.append(". ");

        elRoot = _oDomDocument.getDocumentElement();
        nCurrent = elRoot.getFirstChild();
        while (!bFound && nCurrent != null) // all elements
        {
            if (nCurrent.getNodeType() == Node.COMMENT_NODE) {
                // check if it's a "save changes" comment
                sValue = nCurrent.getNodeValue();
                if (sValue.trim().startsWith("Configuration changes saved on")) {
                    // overwrite message
                    nCurrent.setNodeValue(sbComment.toString());
                    bFound = true;
                }
            }
            nCurrent = nCurrent.getNextSibling();
        }
        if (!bFound) // no comment found: adding new
        {
            // create new comment node
            nComment = _oDomDocument.createComment(sbComment.toString());
            // insert comment before first node
            elRoot.insertBefore(nComment, elRoot.getFirstChild());
        }
        _oConfigHandler.saveConfiguration(_oDomDocument);
    } catch (ConfigurationException e) {
        throw e;
    } catch (Exception e) {
        _logger.fatal("Internal error", e);
        throw new ConfigurationException(SystemErrors.ERROR_INTERNAL);
    }
}

From source file:com.netspective.sparx.util.xml.XmlSource.java

public void replaceNodeValue(Node node, String findStr, String replStr) {
    String srcStr = node.getNodeValue();
    if (srcStr == null || findStr == null || replStr == null)
        return;/*  w ww .j ava 2  s . c om*/

    int findLoc = srcStr.indexOf(findStr);
    if (findLoc >= 0) {
        StringBuffer sb = new StringBuffer(srcStr);
        sb.replace(findLoc, findLoc + findStr.length(), replStr);
        node.setNodeValue(sb.toString());
    }
}

From source file:com.crawljax.plugins.errorreport.ErrorReport.java

private Document addMarker(String id, Document doc, String xpath) {
    try {/*  w  w  w . j a v a2  s. c  o  m*/

        String prefixMarker = "###BEGINMARKER" + id + "###";
        String suffixMarker = "###ENDMARKER###";

        NodeList nodeList = XPathHelper.evaluateXpathExpression(doc, xpath);

        if (nodeList.getLength() == 0 || nodeList.item(0) == null) {
            return doc;
        }
        Node element = nodeList.item(0);

        if (element.getNodeType() == Node.ELEMENT_NODE) {
            Node beginNode = doc.createTextNode(prefixMarker);
            Node endNode = doc.createTextNode(suffixMarker);

            element.getParentNode().insertBefore(beginNode, element);
            if (element.getNextSibling() == null) {
                element.getParentNode().appendChild(endNode);
            } else {
                element.getParentNode().insertBefore(endNode, element.getNextSibling());
            }
        } else if (element.getNodeType() == Node.TEXT_NODE && element.getTextContent() != null) {
            element.setTextContent(prefixMarker + element.getTextContent() + suffixMarker);
        } else if (element.getNodeType() == Node.ATTRIBUTE_NODE) {
            element.setNodeValue(prefixMarker + element.getTextContent() + suffixMarker);
        }

        return doc;
    } catch (Exception e) {
        return doc;
    }
}

From source file:com.wavemaker.tools.ws.WebServiceToolsManager.java

private void modifyServiceName(java.io.File wsdlf)
        throws IOException, ParserConfigurationException, SAXException, TransformerException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);//w  w w.  j a va 2  s  .  co  m
    DocumentBuilder docBuilder = dbf.newDocumentBuilder();
    Document doc = docBuilder.parse(wsdlf);
    NodeList nlistOp = doc.getElementsByTagNameNS("*", "operation");
    NodeList nlistSvc = doc.getElementsByTagNameNS("*", "service");

    if (nlistOp == null || nlistOp.getLength() == 0 || nlistSvc == null || nlistSvc.getLength() == 0) {
        return;
    }

    String[] opList = new String[nlistOp.getLength()];
    for (int i = 0; i < nlistOp.getLength(); i++) {
        Node nodeOp = nlistOp.item(i);
        NamedNodeMap nMap = nodeOp.getAttributes();
        for (int j = 0; j < nMap.getLength(); j++) {
            Node attr = nMap.item(j);
            if (attr.getNodeName().equals("name")) {
                opList[i] = attr.getNodeValue();
                break;
            }
        }
    }

    String svcName = null;

    Node nodeSvc = nlistSvc.item(0); // assumes there is only once service
                                     // name defined in a WSDL
    Node svcNameAttr = null;
    NamedNodeMap nMap = nodeSvc.getAttributes();
    nMap.getLength();
    for (int j = 0; j < nMap.getLength(); j++) {
        svcNameAttr = nMap.item(j);
        if (svcNameAttr.getNodeName().equals("name")) {
            svcName = svcNameAttr.getNodeValue();
            break;
        }
    }

    if (opList.length == 0 || svcName == null) {
        return;
    }

    boolean sameName = false;
    for (String opName : opList) {
        if (opName != null && opName.equals(svcName)) {
            sameName = true;
            break;
        }
    }

    if (!sameName) {
        return;
    }

    svcNameAttr.setNodeValue(svcName + "Svc");

    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer tFormer = tFactory.newTransformer();

    Source source = new DOMSource(doc);
    Result dest = new StreamResult(wsdlf);
    tFormer.transform(source, dest);
}

From source file:edu.uams.clara.webapp.xml.processor.impl.DefaultXmlProcessorImpl.java

private void escapeNodeText(final NodeList nodes) {

    int l = nodes.getLength();

    if (nodes != null && l > 0) {
        for (int i = 0; i < l; i++) {
            Node currentNode = nodes.item(i);

            //node value
            if (currentNode.getNodeType() == Node.TEXT_NODE) {
                currentNode.setNodeValue(StringEscapeUtils.escapeXml(currentNode.getNodeValue()));
            }//from   ww  w.  j  av  a2  s.  c  om
            //attributes
            NamedNodeMap attributes = currentNode.getAttributes();

            if (attributes != null) {
                int len = attributes.getLength();

                for (int j = 0; j < len; j++) {
                    Node attribute = attributes.item(j);
                    attribute.setNodeValue(StringEscapeUtils.escapeXml(attribute.getNodeValue()));
                }
            }

            if (currentNode.hasChildNodes()) {
                escapeNodeText(currentNode.getChildNodes());
            }

            //logger.info("current: " + DomUtils.elementToString(currentNode, true, Encoding.ISO8859_1));
        }
    }

}

From source file:de.betterform.xml.xforms.model.submission.Submission.java

private void updateInstanceAndModel(Model referedModel, Document responseInstance) throws XFormsException {
    if (this.targetExpr != null) {
        Node targetNode;
        if (this.instance == null)
            targetNode = XPathUtil.getAsNode(XPathCache.getInstance().evaluate(evalInScopeContext(), 1,
                    this.targetExpr, this.prefixMapping, this.xpathFunctionContext), 1);
        else {//from   ww  w .ja  v a 2s  . c o m
            targetNode = XPathUtil.getAsNode(XPathCache.getInstance().evaluate(
                    referedModel.getInstance(this.instance).getRootContext().getNodeset(), 1, this.targetExpr,
                    this.prefixMapping, this.xpathFunctionContext), 1);
        }
        if (targetNode != null && targetNode.getNodeType() == Node.ELEMENT_NODE) {
            targetNode.getParentNode().replaceChild(
                    targetNode.getOwnerDocument().importNode(responseInstance.getDocumentElement(), true),
                    targetNode);

        } else if (targetNode != null && targetNode.getNodeType() == Node.ATTRIBUTE_NODE) {
            if (LOGGER.isDebugEnabled()) {
                DOMUtil.prettyPrintDOM(responseInstance);
            }
            // targetNode.setContent(responseInstance.getTextContent());
            String attrValue = responseInstance.getDocumentElement().getTextContent();
            targetNode.setNodeValue(attrValue);
        } else {
            throw new XFormsSubmitError("Invalid target", this.getTarget(),
                    XFormsSubmitError.constructInfoObject(this.element, this.container, locationPath,
                            XFormsConstants.TARGET_ERROR, getResourceURI(), 200d, null, "", ""));
        }
    } else if (this.instance != null && referedModel.getInstance(this.instance) == null) {
        this.container.dispatch(referedModel.getId(), XFormsEventNames.BINDING_EXCEPTION);
        // throw new XFormsBindingException("invalid instance id at " + DOMUtil.getCanonicalPath(this.getElement()), this.target, this.instance);
    } else if (this.instance != null) {
        referedModel.getInstance(this.instance).setInstanceDocument(responseInstance);
    } else {
        referedModel.getInstance(getInstanceId()).setInstanceDocument(responseInstance);
    }

    // perform rebuild, recalculate, revalidate, and refresh
    referedModel.rebuild();
    referedModel.recalculate();
    referedModel.revalidate();
}

From source file:lcmc.data.VMSXML.java

/** Modify xml of the domain. */
public Node modifyDomainXML(final String domainName, final Map<String, String> parametersMap) {
    final String configName = namesConfigsMap.get(domainName);
    if (configName == null) {
        return null;
    }/*  w  ww.  ja va2s .c o m*/
    final Node domainNode = getDomainNode(domainName);
    if (domainNode == null) {
        return null;
    }
    final XPath xpath = XPathFactory.newInstance().newXPath();
    final Map<String, String> paths = new HashMap<String, String>();
    paths.put(VM_PARAM_MEMORY, "memory");
    paths.put(VM_PARAM_CURRENTMEMORY, "currentMemory");
    paths.put(VM_PARAM_VCPU, "vcpu");
    paths.put(VM_PARAM_BOOTLOADER, "bootloader");
    paths.put(VM_PARAM_BOOT, "os/boot");
    paths.put(VM_PARAM_BOOT_2, "os/boot");
    paths.put(VM_PARAM_TYPE, "os/type");
    paths.put(VM_PARAM_TYPE_ARCH, "os/type");
    paths.put(VM_PARAM_TYPE_MACHINE, "os/type");
    paths.put(VM_PARAM_INIT, "os/init");
    paths.put(VM_PARAM_LOADER, "os/loader");
    paths.put(VM_PARAM_CPU_MATCH, "cpu");
    paths.put(VM_PARAM_ACPI, "features");
    paths.put(VM_PARAM_APIC, "features");
    paths.put(VM_PARAM_PAE, "features");
    paths.put(VM_PARAM_HAP, "features");
    paths.put(VM_PARAM_ON_POWEROFF, "on_poweroff");
    paths.put(VM_PARAM_ON_REBOOT, "on_reboot");
    paths.put(VM_PARAM_ON_CRASH, "on_crash");
    paths.put(VM_PARAM_EMULATOR, "devices/emulator");
    final Document doc = domainNode.getOwnerDocument();
    try {
        for (final String param : parametersMap.keySet()) {
            final String path = paths.get(param);
            if (path == null) {
                continue;
            }
            final NodeList nodes = (NodeList) xpath.evaluate(path, domainNode, XPathConstants.NODESET);
            Element node = (Element) nodes.item(0);
            if (node == null) {
                continue;
            }
            if (VM_PARAM_BOOT_2.equals(param)) {
                if (nodes.getLength() > 1) {
                    node = (Element) nodes.item(1);
                } else {
                    node = (Element) node.getParentNode().appendChild(doc.createElement(OS_BOOT_NODE));
                }
            }
            String value = parametersMap.get(param);
            if (VM_PARAM_MEMORY.equals(param) || VM_PARAM_CURRENTMEMORY.equals(param)) {
                value = Long.toString(Tools.convertToKilobytes(value));
            }
            if (VM_PARAM_CPU_MATCH.equals(param) || VM_PARAM_ACPI.equals(param) || VM_PARAM_APIC.equals(param)
                    || VM_PARAM_PAE.equals(param) || VM_PARAM_HAP.equals(param)) {
                domainNode.removeChild(node);
            } else if (VM_PARAM_BOOT.equals(param)) {
                node.setAttribute(OS_BOOT_NODE_DEV, value);
            } else if (VM_PARAM_BOOT_2.equals(param)) {
                if (value == null || "".equals(value)) {
                    node.getParentNode().removeChild(node);
                } else {
                    node.setAttribute(OS_BOOT_NODE_DEV, value);
                }
            } else if (VM_PARAM_TYPE_ARCH.equals(param)) {
                node.setAttribute("arch", value);
            } else if (VM_PARAM_TYPE_MACHINE.equals(param)) {
                node.setAttribute("machine", value);
            } else if (VM_PARAM_CPU_MATCH.equals(param)) {
                if ("".equals(value)) {
                    node.getParentNode().removeChild(node);
                } else {
                    node.setAttribute("match", value);
                }
            } else if (VM_PARAM_CPUMATCH_TOPOLOGY_THREADS.equals(param)) {
                node.setAttribute("threads", value);
            } else {
                final Node n = getChildNode(node, "#text");
                if (n == null) {
                    node.appendChild(doc.createTextNode(value));
                } else {
                    n.setNodeValue(value);
                }
            }
        }
        addCPUMatchNode(doc, domainNode, parametersMap);
        addFeatures(doc, domainNode, parametersMap);
    } catch (final javax.xml.xpath.XPathExpressionException e) {
        Tools.appError("could not evaluate: ", e);
        return null;
    }
    return domainNode;
}

From source file:lcmc.data.VMSXML.java

/** Modify xml of some device element. */
private void modifyXML(final Node domainNode, final String domainName, final Map<String, String> tagMap,
        final Map<String, String> attributeMap, final Map<String, String> parametersMap, final String path,
        final String elementName, final VirtualHardwareComparator vhc) {
    final String configName = namesConfigsMap.get(domainName);
    if (configName == null) {
        return;//from  w w  w . j  ava2 s .c  om
    }
    //final Node domainNode = getDomainNode(domainName);
    if (domainNode == null) {
        return;
    }
    final XPath xpath = XPathFactory.newInstance().newXPath();
    final Node devicesNode = getDevicesNode(xpath, domainNode);
    if (devicesNode == null) {
        return;
    }
    try {
        final NodeList nodes = (NodeList) xpath.evaluate(path, domainNode, XPathConstants.NODESET);
        Element hwNode = vhc.getElement(nodes, parametersMap);
        if (hwNode == null) {
            hwNode = (Element) devicesNode
                    .appendChild(domainNode.getOwnerDocument().createElement(elementName));
        }
        for (final String param : parametersMap.keySet()) {
            final String value = parametersMap.get(param);
            if (!tagMap.containsKey(param) && attributeMap.containsKey(param)) {
                /* attribute */
                final Node attributeNode = hwNode.getAttributes().getNamedItem(attributeMap.get(param));
                if (attributeNode == null) {
                    if (value != null && !"".equals(value)) {
                        hwNode.setAttribute(attributeMap.get(param), value);
                    }
                } else if (value == null || "".equals(value)) {
                    hwNode.removeAttribute(attributeMap.get(param));
                } else {
                    attributeNode.setNodeValue(value);
                }
                continue;
            }
            Element node = (Element) getChildNode(hwNode, tagMap.get(param));
            if ((attributeMap.containsKey(param) || "True".equals(value)) && node == null) {
                node = (Element) hwNode
                        .appendChild(domainNode.getOwnerDocument().createElement(tagMap.get(param)));
            } else if (node != null && !attributeMap.containsKey(param)
                    && (value == null || "".equals(value))) {
                hwNode.removeChild(node);
            }
            if (attributeMap.containsKey(param)) {
                final Node attributeNode = node.getAttributes().getNamedItem(attributeMap.get(param));
                if (attributeNode == null) {
                    if (value != null && !"".equals(value)) {
                        node.setAttribute(attributeMap.get(param), value);
                    }
                } else {
                    if (value == null || "".equals(value)) {
                        node.removeAttribute(attributeMap.get(param));
                    } else {
                        attributeNode.setNodeValue(value);
                    }
                }
            }
        }
        final Element hwAddressNode = (Element) getChildNode(hwNode, HW_ADDRESS);
        if (hwAddressNode != null) {
            hwNode.removeChild(hwAddressNode);
        }
    } catch (final javax.xml.xpath.XPathExpressionException e) {
        Tools.appError("could not evaluate: ", e);
        return;
    }
}