Example usage for org.w3c.dom Node setTextContent

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

Introduction

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

Prototype

public void setTextContent(String textContent) throws DOMException;

Source Link

Document

This attribute returns the text content of this node and its descendants.

Usage

From source file:Main.java

public static void updateUserMgtXML(File userMgtXML, String jndiConfigNameUserDB) throws Exception {
    Document doc = initializeXML(userMgtXML);
    NodeList configNodeList = doc.getElementsByTagName("Configuration");
    Node configNode = configNodeList.item(0); //get the 0 index, since only one available

    NodeList nodeList = configNode.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            if (node.getNodeName().equalsIgnoreCase("Property")) {
                if (node.hasAttributes()) {
                    NamedNodeMap namedNodeMap = node.getAttributes();
                    Node attr = namedNodeMap.getNamedItem("name");
                    if (attr != null && attr.getNodeValue().equalsIgnoreCase("dataSource")) {
                        node.setTextContent(jndiConfigNameUserDB);
                    }//from  w ww . ja va  2s  .c  o m
                }
            }
        }
    }
    finalizeXML(userMgtXML, doc, 4);
}

From source file:Main.java

private static Node replace2TextExceptTags(Document document, Node node, String... tags) {

    NodeList nodeList = node.getChildNodes();

    for (int i = 0; i < nodeList.getLength(); i++) {
        Node subNode = nodeList.item(i);
        if (subNode.getNodeType() == Node.TEXT_NODE) {
            String text = ((Text) subNode).getTextContent();
            text = text.replaceAll("[\t\n]+", "").replaceAll(" +", " ");
            subNode.setTextContent(text);
            continue;
        }//from  ww  w  .  ja v a  2s .  com
        if (subNode.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }

        String nodeName = subNode.getNodeName();
        boolean excepted = false;
        for (String tagName : tags) {
            if (tagName.equals(nodeName)) {
                excepted = true;
                replace2TextExceptTags(document, subNode, tags);
                break;
            }
        }

        if (excepted) {
            continue;
        }

        subNode = replace2TextExceptTags(document, subNode, tags);
        NodeList childList = subNode.getChildNodes();
        List<Node> tempList = new ArrayList<Node>();
        for (int j = 0; j < childList.getLength(); j++) {
            tempList.add(childList.item(j));
        }
        for (Node child : tempList) {
            node.insertBefore(child, subNode);
        }
        node.removeChild(subNode);
    }

    return node;
}

From source file:org.apache.lens.regression.util.Util.java

public static void changeConfig(HashMap<String, String> map, String remotePath) throws Exception {

    Path p = Paths.get(remotePath);
    String fileName = p.getFileName().toString();
    backupFile = localFilePath + "backup-" + fileName;
    localFile = localFilePath + fileName;
    log.info("Copying " + remotePath + " to " + localFile);
    remoteFile("get", remotePath, localFile);
    Files.copy(new File(localFile).toPath(), new File(backupFile).toPath(), REPLACE_EXISTING);

    DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = docBuilder.parse(new FileInputStream(localFile));
    doc.normalize();/*from   ww  w.j  a  va 2s . c o  m*/

    NodeList rootNodes = doc.getElementsByTagName("configuration");
    Node root = rootNodes.item(0);
    Element rootElement = (Element) root;
    NodeList property = rootElement.getElementsByTagName("property");

    for (int i = 0; i < property.getLength(); i++) { //Deleting redundant properties from the document
        Node prop = property.item(i);
        Element propElement = (Element) prop;
        Node propChild = propElement.getElementsByTagName("name").item(0);

        Element nameElement = (Element) propChild;
        if (map.containsKey(nameElement.getTextContent())) {
            rootElement.removeChild(prop);
            i--;
        }
    }

    Iterator<Entry<String, String>> ab = map.entrySet().iterator();
    while (ab.hasNext()) {
        Entry<String, String> entry = ab.next();
        String propertyName = entry.getKey();
        String propertyValue = entry.getValue();
        System.out.println(propertyName + " " + propertyValue + "\n");
        Node newNode = doc.createElement("property");
        rootElement.appendChild(newNode);
        Node newName = doc.createElement("name");
        Element newNodeElement = (Element) newNode;

        newName.setTextContent(propertyName);
        newNodeElement.appendChild(newName);

        Node newValue = doc.createElement("value");
        newValue.setTextContent(propertyValue);
        newNodeElement.appendChild(newValue);
    }
    prettyPrint(doc);
    remoteFile("put", remotePath, localFile);
}

From source file:Main.java

public static void replaceChild(Document doc, Node parentNode, String childName, String childContents) {
    Node node = getChildNode(parentNode, childName);

    // if some one passes in null
    if (childContents == null) {
        // remove existing node
        if (node != null)
            parentNode.removeChild(node);

        return;//from w ww.j av  a2 s.  c o  m
    }

    // this means there's at least contents pass in,
    if (node == null) {
        // no existing node, so we just add one
        appendChild(doc, parentNode, childName, childContents);
    } else {
        // existing node so we update it instead.
        node.setTextContent(childContents);
    }
}

From source file:com.gargoylesoftware.htmlunit.html.impl.SimpleRange.java

private static void setText(final Node node, final String text) {
    if (node instanceof SelectableTextInput) {
        ((SelectableTextInput) node).setText(text);
    } else {//from   ww  w .j av a 2  s  .  c o  m
        node.setTextContent(text);
    }
}

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  ww w  .  java2s .c  o 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);
        }
    }

}

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

private static void appendTextContent(Document document, String element, String textContent) throws Exception {
    NodeList nodelist = org.apache.xpath.XPathAPI.selectNodeList(document, element);

    for (int i = 0; i < nodelist.getLength(); i++) {
        Node stringProp = nodelist.item(i);
        stringProp.setTextContent(textContent);
    }/*from   w ww. j  av a 2 s .c o m*/
}

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

public static void adaptDBPerformanceJmx(String jmxFileLocation, List<String> name, String dataSource,
        List<String> queryType, List<String> query, int noOfUsers, int rampUpPeriod, int loopCount,
        String dbUrl, String driver, String userName, String passWord) throws Exception {
    File jmxFile = null;// www. j  a  va2s .  c  o m
    File jmxDir = new File(jmxFileLocation + "/tests");
    if (jmxDir.isDirectory()) {
        FilenameFilter filter = new FileListFilter("", "jmx");
        File[] jmxFiles = jmxDir.listFiles(filter);
        jmxFile = jmxFiles[0];
    }

    Document document = com.photon.phresco.framework.impl.util.FrameworkUtil.getDocument(jmxFile);
    appendThreadProperties(document, noOfUsers, rampUpPeriod, loopCount);
    appendJdbcDataSrc(document, dataSource, dbUrl, driver, userName, passWord);
    NodeList nodelist = org.apache.xpath.XPathAPI.selectNodeList(document,
            "jmeterTestPlan/hashTree/hashTree/hashTree/JDBCSampler");
    if (nodelist != null && nodelist.getLength() > 0) {
        Node hashTree = nodelist.item(0).getParentNode();
        hashTree = removeAllChilds(hashTree);
        hashTree.setTextContent(null);

        for (int j = 0; j < name.size(); j++) {
            Node appendJdbcSampler = appendJdbcSampler(document, hashTree, name.get(j), dataSource,
                    queryType.get(j), query.get(j));
            hashTree.appendChild(appendJdbcSampler);
            hashTree.appendChild(document.createElement("hashTree"));
        }
    }
    saveDocument(jmxFile, document);
}

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

public static void adaptPerformanceJmx(String jmxFileLocation, List<String> name, List<String> context,
        List<String> contextType, List<String> contextPostData, List<String> encodingType, int noOfUsers,
        int rampUpPeriod, int loopCount, Map<String, String> headersMap) throws Exception {
    File jmxFile = null;/*from  w w w  .ja v  a  2  s  . c o m*/
    File jmxDir = new File(jmxFileLocation + "/tests");
    if (jmxDir.isDirectory()) {
        FilenameFilter filter = new FileListFilter("", "jmx");
        File[] jmxFiles = jmxDir.listFiles(filter);
        jmxFile = jmxFiles[0];
    }

    Document document = com.photon.phresco.framework.impl.util.FrameworkUtil.getDocument(jmxFile);
    appendThreadProperties(document, noOfUsers, rampUpPeriod, loopCount);
    NodeList nodelist = org.apache.xpath.XPathAPI.selectNodeList(document,
            "jmeterTestPlan/hashTree/hashTree/hashTree/HTTPSamplerProxy");
    if (nodelist != null && nodelist.getLength() > 0) {
        NodeList headerManagerNodelist = org.apache.xpath.XPathAPI.selectNodeList(document,
                "jmeterTestPlan/hashTree/hashTree/hashTree/HeaderManager");

        Node hashTree = nodelist.item(0).getParentNode();
        hashTree = removeAllChilds(hashTree);
        hashTree.setTextContent(null);

        if (headerManagerNodelist != null && headerManagerNodelist.getLength() > 0) {
            for (int i = 0; i < headerManagerNodelist.getLength(); i++) {
                hashTree.appendChild(headerManagerNodelist.item(i));
            }
            hashTree.appendChild(document.createElement("hashTree"));
        }

        if (MapUtils.isNotEmpty(headersMap)) {
            NodeList headerMngrNodelist = org.apache.xpath.XPathAPI.selectNodeList(document,
                    "jmeterTestPlan/hashTree/hashTree/hashTree/HeaderManager/collectionProp");
            if (headerMngrNodelist != null && headerMngrNodelist.getLength() > 0) {
                createHeaderElementProp(document, headersMap, headerMngrNodelist.item(0));
            } else {
                Node appendHeaderManager = appendHeaderManager(document, headersMap);
                hashTree.appendChild(appendHeaderManager);
                hashTree.appendChild(document.createElement("hashTree"));
            }
        }

        for (int j = 0; j < name.size(); j++) {
            Node appendHttpSamplerProxy = appendHttpSamplerProxy(document, hashTree, name.get(j),
                    "${context}/" + context.get(j), contextType.get(j), contextPostData.get(j),
                    encodingType.get(j));
            hashTree.appendChild(appendHttpSamplerProxy);
            hashTree.appendChild(document.createElement("hashTree"));
        }
    }
    saveDocument(jmxFile, document);
}

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

private static void appendTypeProp(Document document, Node parentProp, String tag, String nameAttr,
        String textContent) {// www  . j ava  2 s . c o  m
    Node typeProp = document.createElement(tag);
    NamedNodeMap attributes = typeProp.getAttributes();
    attributes.setNamedItem(createAttribute(document, "name", nameAttr));
    typeProp.setTextContent(textContent);
    parentProp.appendChild(typeProp);
}