List of usage examples for org.dom4j Node setText
void setText(String text);
Sets the text data of this node or this method will throw an UnsupportedOperationException
if it is read-only.
From source file:org.sofun.sensor.arjel.impl.event.XMLTraceImpl.java
License:Open Source License
@Override public void setElementTextValue(String name, String value) { if (name != null && value != null) { final String xpath = "//" + getEventType() + "/" + name; Node node = getDocument().selectSingleNode(xpath); if (node != null) { node.setText(value); } else {//from ww w. j a va 2 s.c o m log.error("Cannot find path=" + xpath); } } else { log.error("Name and value pair should be not null."); } }
From source file:org.withinsea.izayoi.commons.dom.DOMUtils.java
License:Mozilla Public License
@SuppressWarnings("unchecked") public static void mergeTexts(Branch root) { List<Node> nodes = (List<Node>) root.content(); for (int i = nodes.size() - 2; i >= 0; i--) { Node node = nodes.get(i); Node next = nodes.get(i + 1); if (node instanceof Text && next instanceof Text) { node.setText(node.getText() + next.getText()); next.detach();/*from www . j a va 2 s . c o m*/ } } for (Node node : (List<Node>) root.content()) { if (node instanceof Branch) { mergeTexts((Branch) node); } } }
From source file:org.xwiki.tool.xar.FormatMojo.java
License:Open Source License
private void format(String fileName, Document domdoc, String defaultLanguage) throws Exception { Node node = domdoc.selectSingleNode("xwikidoc/author"); if (node != null) { node.setText(AUTHOR); }//from www .j av a 2s . co m node = domdoc.selectSingleNode("xwikidoc/contentAuthor"); if (node != null) { node.setText(AUTHOR); } node = domdoc.selectSingleNode("xwikidoc/creator"); if (node != null) { node.setText(AUTHOR); } node = domdoc.selectSingleNode("xwikidoc/version"); if (node != null) { node.setText(VERSION); } node = domdoc.selectSingleNode("xwikidoc/minorEdit"); if (node != null) { node.setText("false"); } // Also update the attachment authors for (Object attachmentAuthorNode : domdoc.selectNodes("xwikidoc/attachment/author")) { ((Node) attachmentAuthorNode).setText(AUTHOR); } // Set the default language Element element = (Element) domdoc.selectSingleNode("xwikidoc/defaultLanguage"); if (element != null) { if (StringUtils.isEmpty(defaultLanguage)) { removeContent(element); } else { element.setText(defaultLanguage); } } // Remove any content of the <comment> element element = (Element) domdoc.selectSingleNode("xwikidoc/comment"); if (element != null) { removeContent(element); } // If the page is technical, make sure it's hidedn element = (Element) domdoc.selectSingleNode("xwikidoc/hidden"); if (isTechnicalPage(fileName)) { element.setText("true"); } }
From source file:org.xwiki.tool.xar.XARMojo.java
License:Open Source License
private void performTransformations() throws Exception { if (this.transformations == null) { return;//from w w w . jav a 2 s . com } // Copy XML pages from dependent XAR if we modify them. unpackTransformedXARs(); SAXReader reader = new SAXReader(); // For each defined file, perform the transformation asked for (Transformation transformation : this.transformations) { File file = new File(this.project.getBuild().getOutputDirectory(), transformation.getFile()); Document document = reader.read(file); Node node = document.selectSingleNode(transformation.getXpath()); String value = transformation.getValue(); if (!StringUtils.isEmpty(value)) { // Get the current value at node and replace $1 with it (if any) String currentValue = node.getText(); node.setText(value.replace("$1", currentValue)); } // Write the modified file to disk XMLWriter writer = new XMLWriter(new FileOutputStream(file)); writer.write(document); writer.flush(); writer.close(); } }
From source file:poisondog.xml.Dom4jParser.java
License:Apache License
private Node createNode(Element element) { Node node = new Node(element.getQName().getName()); for (int i = 0; i < element.attributeCount(); i++) { Attribute attribute = element.attribute(i); node.setAttribute(attribute.getQName().getName(), attribute.getValue()); }//from w ww .j a va2s .c om for (Iterator i = element.elementIterator(); i.hasNext();) { Element e = (Element) i.next(); node.addChild(createNode(e)); } node.setText(element.getText()); return node; }
From source file:poisondog.xml.XMLUtils.java
License:Apache License
private static Node createNode(Element element) { Node node = new Node(element.getQName().getName()); for (int i = 0; i < element.attributeCount(); i++) { Attribute attribute = element.attribute(i); node.setAttribute(attribute.getQName().getName(), attribute.getValue()); }//from w w w . j a v a 2 s .c o m for (Iterator i = element.elementIterator(); i.hasNext();) { Element e = (Element) i.next(); node.addChild(createNode(e)); } node.setText(element.getText()); return node; }
From source file:pt.webdetails.cpf.PluginSettings.java
License:Open Source License
/** * Writes a setting directly to .xml./*from w ww . j a v a 2 s .c o m*/ * * @param section * @param value * @return whether value was written */ protected boolean writeSetting(String section, String value) { if (settings != null) { Node node = settings.selectSingleNode(getNodePath(section)); if (node != null) { // update value String oldValue = node.getText(); node.setText(value); // save file String saveMsg = "changed '" + section + "' from '" + oldValue + "' to '" + value + "'"; return saveSettingsFile(saveMsg); } else { logger.error("Couldn't find node"); } } else { logger.error("No settings!"); } return false; }
From source file:revaligner.service.FileAligner.java
public void fixTxlfTrgTags(org.dom4j.Document doc) throws Exception { org.dom4j.Element root = doc.getRootElement(); List<org.dom4j.Element> list_transunit = root.selectNodes("//*[name() = 'trans-unit']"); for (org.dom4j.Element e : list_transunit) { org.dom4j.Element source = e.element("source"); org.dom4j.Element target = e.element("target"); if ((target != null) && (!target.getText().equals(""))) { HashMap<String, List<Object>> src_map = new HashMap(); boolean tagnodeappear = false; boolean textnodeappear = false; boolean sourcetagsurroundtext = true; int maxid = 1; for (int i = 0; i < source.content().size(); i++) { org.dom4j.Node node = (org.dom4j.Node) source.content().get(i); if (node.getNodeType() == 1) { org.dom4j.Element tag = (org.dom4j.Element) node; maxid = Math.max(maxid, Integer.parseInt(tag.attributeValue("id"))); }// w ww .j a v a 2 s. c o m } maxid++; for (int i = 0; i < source.content().size(); i++) { org.dom4j.Node node = (org.dom4j.Node) source.content().get(i); if (node.getNodeType() == 1) { if (textnodeappear) { tagnodeappear = true; } org.dom4j.Element tag = (org.dom4j.Element) node; String key = ""; if ((tag.getName().equals("ex")) || (tag.getName().equals("ept"))) { key = tag.getName() + "#" + tag.attributeValue("rid"); } else if (tag.getName().equals("x")) { key = tag.getName() + "#" + tag.attributeValue("ctype"); } else { key = tag.getName(); } if (src_map.containsKey(key)) { ((List) src_map.get(key)).add(node.clone()); } else { List<Object> list = new ArrayList(); list.add(node.clone()); src_map.put(key, list); } } else { if (tagnodeappear) { sourcetagsurroundtext = false; } textnodeappear = true; } } HashMap<String, String> rid_map = new HashMap(); List trg_contents = new ArrayList(); boolean targetnotag = true; for (int i = 0; i < target.content().size(); i++) { org.dom4j.Node node = (org.dom4j.Node) target.content().get(i); if (node.getNodeType() == 1) { org.dom4j.Element tag = (org.dom4j.Element) node; String rid = tag.attributeValue("rid"); String key = ""; if ((tag.getName().equals("ph")) || (tag.getName().equals("bpt")) || (tag.getName().equals("bx"))) { key = tag.getName(); if (src_map.containsKey(key)) { List list = (List) src_map.get(key); trg_contents.add(list.get(0)); targetnotag = false; rid_map.put(rid, ((org.dom4j.Element) list.get(0)).attributeValue("rid")); list.remove(0); if (list.size() == 0) { src_map.remove(key); } } } else if (tag.getName().equals("x")) { key = tag.getName() + "#" + tag.attributeValue("ctype"); if (src_map.containsKey(key)) { List list = (List) src_map.get(key); trg_contents.add(list.get(0)); targetnotag = false; list.remove(0); if (list.size() == 0) { src_map.remove(key); } } else if ((tag.getName().equals("x")) && (tag.attributeValue("ctype").equals("x-tab"))) { org.dom4j.Element tab = DocumentHelper.createElement("x"); tab.addAttribute("ctype", "x-tab"); tab.addAttribute("id", Integer.toString(maxid)); maxid++; tab.addAttribute("equiv-text", " "); trg_contents.add(tab); } else if ((tag.getName().equals("x")) && (tag.attributeValue("ctype").equals("lb"))) { org.dom4j.Element lb = DocumentHelper.createElement("x"); lb.addAttribute("ctype", "lb"); lb.addAttribute("id", Integer.toString(maxid)); maxid++; lb.addAttribute("equiv-text", " "); trg_contents.add(lb); } } else if ((tag.getName().equals("ex")) || (tag.getName().equals("ept"))) { String mapped_rid = (String) rid_map.get(tag.attributeValue("rid")); key = tag.getName() + "#" + mapped_rid; if (src_map.containsKey(key)) { List list = (List) src_map.get(key); trg_contents.add(list.get(0)); rid_map.put(rid, ((org.dom4j.Element) list.get(0)).attributeValue("rid")); list.remove(0); if (list.size() == 0) { src_map.remove(key); } } } } else if (node.getNodeType() == 3) { String text = node.getText(); if (text.contains("<br> ")) { String key = "x#lb"; String[] ss = text.split("<br> "); for (int s = 0; s < ss.length; s++) { trg_contents.add(DocumentHelper.createText(ss[s])); if (s < ss.length - 1) { if (src_map.containsKey(key)) { List list = (List) src_map.get(key); trg_contents.add(list.get(0)); targetnotag = false; list.remove(0); if (list.size() == 0) { src_map.remove(key); } } else { org.dom4j.Element lb = DocumentHelper.createElement("x"); lb.addAttribute("ctype", "lb"); lb.addAttribute("id", Integer.toString(maxid)); maxid++; lb.addAttribute("equiv-text", " "); trg_contents.add(lb); } } } } else if (text.contains("<br>")) { String key = "x#lb"; String[] ss = text.split("<br> "); for (int s = 0; s < ss.length; s++) { trg_contents.add(DocumentHelper.createText(ss[s])); if (s < ss.length - 1) { if (src_map.containsKey(key)) { List list = (List) src_map.get(key); trg_contents.add(list.get(0)); targetnotag = false; list.remove(0); if (list.size() == 0) { src_map.remove(key); } } else { org.dom4j.Element lb = DocumentHelper.createElement("x"); lb.addAttribute("ctype", "lb"); lb.addAttribute("id", Integer.toString(maxid)); maxid++; lb.addAttribute("equiv-text", " "); trg_contents.add(lb); } } } } else { trg_contents.add(node); } } else { trg_contents.add(node); } } while (trg_contents.size() > 0) { org.dom4j.Node nd = (org.dom4j.Node) trg_contents.get(0); if (nd.getNodeType() == 3) { if (nd.getText().trim().equals("")) { trg_contents.remove(0); } else { nd.setText(nd.getText().replaceAll("^(\\s)+", "")); break; } } else if ((nd.getNodeType() == 1) && (nd.getName().equals("x")) && (((org.dom4j.Element) nd).attributeValue("ctype").equals("lb"))) { trg_contents.remove(0); } else { if ((nd.getNodeType() != 1) || (!nd.getName().equals("x")) || (!((org.dom4j.Element) nd).attributeValue("ctype").equals("x-tab"))) { break; } trg_contents.remove(0); } } while (trg_contents.size() > 0) { org.dom4j.Node nd = (org.dom4j.Node) trg_contents.get(trg_contents.size() - 1); if (nd.getNodeType() == 3) { if (nd.getText().trim().equals("")) { trg_contents.remove(trg_contents.size() - 1); } else { nd.setText(nd.getText().replaceAll("(\\s)+$", "")); break; } } else if ((nd.getNodeType() == 1) && (nd.getName().equals("x")) && (((org.dom4j.Element) nd).attributeValue("ctype").equals("lb"))) { trg_contents.remove(trg_contents.size() - 1); } else { if ((nd.getNodeType() != 1) || (!nd.getName().equals("x")) || (!((org.dom4j.Element) nd).attributeValue("ctype").equals("x-tab"))) { break; } trg_contents.remove(trg_contents.size() - 1); } } boolean isnotignoredsegment = true; if (trg_contents.size() == 1) { org.dom4j.Node node = (org.dom4j.Node) trg_contents.get(0); if ((node.getNodeType() == 3) && (node.getText().replaceFirst("", "").equals(""))) { isnotignoredsegment = false; } } if ((targetnotag) && (sourcetagsurroundtext) && (isnotignoredsegment)) { boolean isleadingtag = true; int index = 0; for (int i = 0; i < source.content().size(); i++) { org.dom4j.Node node = (org.dom4j.Node) source.content().get(i); if (node.getNodeType() == 1) { if (isleadingtag) { trg_contents.add(index, node); index++; } else { trg_contents.add(node); } } else { isleadingtag = false; index++; } } } target.setContent(trg_contents); } } }