List of usage examples for org.w3c.dom Node setNodeValue
public void setNodeValue(String nodeValue) throws DOMException;
From source file:Main.java
static public void setElementText(Element elm, String text) { Node node = elm.getFirstChild(); if (node == null) { if (text != null) elm.appendChild(elm.getOwnerDocument().createTextNode(text)); } else if (node.getNodeType() == Node.TEXT_NODE) { if (text == null) node.getParentNode().removeChild(node); else/* w ww. j av a2s. c o m*/ node.setNodeValue(text); } else if (text != null) { Text textNode = node.getOwnerDocument().createTextNode(text); elm.insertBefore(textNode, elm.getFirstChild()); } }
From source file:Main.java
public static boolean setNodeValue(Node domNode, String string) { if (domNode == null) return false; short nodeType = domNode.getNodeType(); switch (nodeType) { case Node.ELEMENT_NODE: { setElementText((Element) domNode, string); break;//from w ww . ja v a 2 s.co m } case Node.ATTRIBUTE_NODE: case Node.TEXT_NODE: { domNode.setNodeValue(string); break; } case Node.PROCESSING_INSTRUCTION_NODE: { ((ProcessingInstruction) domNode).setData(string); break; } case Node.CDATA_SECTION_NODE: { ((CDATASection) domNode).setData(string); break; } default: { return false; } } return true; }
From source file:DomUtil.java
/** * Set or replace the text value//w w w . j a v a2 s .com */ public static void setText(Node node, String val) { Node chld = DomUtil.getChild(node, Node.TEXT_NODE); if (chld == null) { Node textN = node.getOwnerDocument().createTextNode(val); node.appendChild(textN); return; } // change the value chld.setNodeValue(val); }
From source file:Main.java
/** * Identify variables in attribute values and CDATA contents and replace * with their values as defined in the variableDefs map. Variables without * definitions are left alone./*from w w w. j a v a2 s .c o m*/ * * @param node the node to do variable replacement in * @param variableDefs map from variable names to values. */ public static void replaceVariables(final Node node, Map<String, String> variableDefs) { switch (node.getNodeType()) { case Node.ELEMENT_NODE: final Element element = (Element) node; final NamedNodeMap atts = element.getAttributes(); for (int i = 0; i < atts.getLength(); i++) { final Attr attr = (Attr) atts.item(i); attr.setNodeValue(replaceVariablesInString(attr.getValue(), variableDefs)); } break; case Node.CDATA_SECTION_NODE: String content = node.getTextContent(); node.setNodeValue(replaceVariablesInString(content, variableDefs)); break; default: break; } // process children final NodeList children = node.getChildNodes(); for (int childIndex = 0; childIndex < children.getLength(); childIndex++) replaceVariables(children.item(childIndex), variableDefs); }
From source file:de.codesourcery.spring.contextrewrite.XMLRewrite.java
private static Rule wrap(ReplaceRule r) { final String newValue; if (!ContextRewritingBootStrapper.NULL_STRING.equals(r.replacement())) { if (r.replacementClassName() != ReplaceRule.NULL_CLASS) { throw new RuntimeException("Either replacement or replacementClassName needs to be set"); }/*from w w w. j av a 2 s . c o m*/ newValue = r.replacement(); } else if (r.replacementClassName() != ReplaceRule.NULL_CLASS) { newValue = r.replacementClassName().getName(); } else { throw new RuntimeException( "You need to provide EITHER 'replacement' OR 'replacementClassName' attributes"); } return new Rule(r.xpath(), r.id()) { public void apply(Document document, Node matchedNode) throws Exception { switch (matchedNode.getNodeType()) { case Node.ATTRIBUTE_NODE: matchedNode.setNodeValue(newValue); break; case Node.ELEMENT_NODE: final Document replDocument = parseXMLFragment(newValue); final Node firstChild = replDocument.getFirstChild(); final Node adoptedNode = document.importNode(firstChild, true); matchedNode.getParentNode().replaceChild(adoptedNode, matchedNode); break; default: throw new RuntimeException(); } } @Override public String toString() { return "REPLACE: " + r.xpath() + " with '" + newValue; } }; }
From source file:Main.java
/** * For compatibility reasons the following is required: * If the value of a text node is to be changed, but a CDATA section with this name * already exists, the CDATA section is removed an a text node is created or changed. * * If the value of a CDATA section is to be changed, but a text node with this name * already exists, the text node is removed an a CDATA section is created or changed. * *//*from ww w . jav a 2 s .com*/ public static void setElementText(Element e, String newValue, boolean cdata) { if (e == null) { return; } Node node = null; NodeList children = e.getChildNodes(); if (children != null) { Node childToRemove = null; boolean changed = false; int listLength = children.getLength(); for (int i = 0; i < listLength; i++) { node = children.item(i); int nodeType = node.getNodeType(); if (nodeType == Node.TEXT_NODE) { if (cdata) { childToRemove = node; } else { node.setNodeValue(newValue); changed = true; } } if (nodeType == Node.CDATA_SECTION_NODE) { if (!cdata) { childToRemove = node; } else { node.setNodeValue(newValue); changed = true; } } } if (childToRemove != null) { // System.out.println("removing child " + childToRemove.getNodeValue()); childToRemove.setNodeValue(""); e.removeChild(childToRemove); } if (changed) { return; } } Document doc = e.getOwnerDocument(); if (cdata) { node = doc.createCDATASection(newValue); } else { node = doc.createTextNode(newValue); } e.appendChild(node); }
From source file:Main.java
/** * The method inserts end-of-line+indentation Text nodes where indentation is necessary. * * @param node - node to be pretty formatted * @param identLevel - initial indentation level of the node * @param ident - additional indentation inside the node *//*w w w. ja va2 s. com*/ private static void prettyFormat(Node node, String identLevel, String ident) { NodeList nodelist = node.getChildNodes(); int iStart = 0; Node item = nodelist.item(0); if (item != null) { short type = item.getNodeType(); if (type == Node.ELEMENT_NODE || type == Node.COMMENT_NODE) { Node newChild = node.getOwnerDocument().createTextNode(EOL_XML + identLevel + ident); node.insertBefore(newChild, item); iStart = 1; } } for (int i = iStart; i < nodelist.getLength(); i++) { item = nodelist.item(i); if (item != null) { short type = item.getNodeType(); if (type == Node.TEXT_NODE && item.getNodeValue().trim().length() == 0) { if (i + 1 < nodelist.getLength()) { item.setNodeValue(EOL_XML + identLevel + ident); } else { item.setNodeValue(EOL_XML + identLevel); } } else if (type == Node.ELEMENT_NODE) { prettyFormat(item, identLevel + ident, ident); if (i + 1 < nodelist.getLength()) { Node nextItem = nodelist.item(i + 1); if (nextItem != null) { short nextType = nextItem.getNodeType(); if (nextType == Node.ELEMENT_NODE || nextType == Node.COMMENT_NODE) { Node newChild = node.getOwnerDocument() .createTextNode(EOL_XML + identLevel + ident); node.insertBefore(newChild, nextItem); i++; continue; } } } else { Node newChild = node.getOwnerDocument().createTextNode(EOL_XML + identLevel); node.appendChild(newChild); i++; continue; } } } } }
From source file:fi.csc.kapaVirtaAS.MessageTransformer.java
private Node setNodeValueToNode(Node n, String value) { n.setNodeValue(value); return n; }
From source file:jp.ikedam.jenkins.plugins.jobcopy_builder.ReplaceOperation.java
/** * Returns modified XML Document of the job configuration. * //from w ww . j a v a2 s . co m * Replace the strings in the job configuration: * only applied to strings in text nodes, so the XML structure is never destroyed. * * @param doc XML Document of the job to be copied (job/NAME/config.xml) * @param env Variables defined in the build. * @param logger The output stream to log. * @return modified XML Document. Return null if an error occurs. * @see jp.ikedam.jenkins.plugins.jobcopy_builder.AbstractXmlJobcopyOperation#perform(org.w3c.dom.Document, hudson.EnvVars, java.io.PrintStream) */ @Override public Document perform(Document doc, EnvVars env, PrintStream logger) { String fromStr = getFromStr(); String toStr = getToStr(); if (StringUtils.isEmpty(fromStr)) { logger.println("From String is empty"); return null; } if (toStr == null) { toStr = ""; } String expandedFromStr = isExpandFromStr() ? env.expand(fromStr) : fromStr; String expandedToStr = isExpandToStr() ? env.expand(toStr) : toStr; if (StringUtils.isEmpty(expandedFromStr)) { logger.println("From String got to be empty"); return null; } if (expandedToStr == null) { expandedToStr = ""; } logger.print("Replacing: " + expandedFromStr + " -> " + expandedToStr); try { // Retrieve all text nodes. NodeList textNodeList = getNodeList(doc, "//text()"); // Perform replacing to all text nodes. // NodeList does not implement Collection, and foreach is not usable. for (int i = 0; i < textNodeList.getLength(); ++i) { Node node = textNodeList.item(i); node.setNodeValue(StringUtils.replace(node.getNodeValue(), expandedFromStr, expandedToStr)); } logger.println(""); return doc; } catch (Exception e) { logger.print("Error occured in XML operation"); e.printStackTrace(logger); return null; } }
From source file:de.betterform.connector.serializer.MultipartRelatedSerializer.java
protected void visitNode(Map cache, Node node, MimeMultipart multipart) throws Exception { ModelItem item = (ModelItem) node.getUserData(""); if (item != null && item.getDeclarationView().getDatatype() != null && item.getDeclarationView().getDatatype().equalsIgnoreCase("anyURI")) { String name = item.getFilename(); if (name == null || item.getValue() == null || item.getValue().equals("")) { return; }/* www . jav a 2s .c om*/ String cid = (String) cache.get(name); if (cid == null) { int count = multipart.getCount(); cid = name + "@part" + (count + 1); MimeBodyPart part = new MimeBodyPart(); part.setContentID("<" + cid + ">"); DataHandler dh = new DataHandler(new ModelItemDataSource(item)); part.setDataHandler(dh); part.addHeader("Content-Type", item.getMediatype()); part.addHeader("Content-Transfer-Encoding", "base64"); part.setDisposition("attachment"); part.setFileName(name); multipart.addBodyPart(part); cache.put(name, cid); } Element element = (Element) node; // remove text node NodeList list = node.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { Node n = list.item(i); if (n.getNodeType() != Node.TEXT_NODE) { continue; } n.setNodeValue("cid:" + cid); break; } } else { NodeList list = node.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { Node n = list.item(i); if (n.getNodeType() == Node.ELEMENT_NODE) { visitNode(cache, n, multipart); } } } }