List of usage examples for org.dom4j Node getParent
Element getParent();
getParent
returns the parent Element
if this node supports the parent relationship or null if it is the root element or does not support the parent relationship.
From source file:com.cladonia.xngreditor.actions.ToolsSortNodeAction.java
License:Open Source License
private void removeOccurences(String xpath, ExchangerDocument document) throws Exception { //select all these nodes by xpath and remove them //List nodeList = document.getDocument().selectNodes(xpath); List nodeList = document.search(xpath); //System.out.println("Removing: "+xpath +": "+nodeList.size()); int size = nodeList.size(); if (size > 0) { int cnt = 0; Node oldNode = (Node) nodeList.get(cnt); while (!nodeList.isEmpty()) { oldNode = (Node) nodeList.get(cnt); oldNode.getParent().remove(oldNode); nodeList.remove(cnt);/*from w w w . j av a 2 s . c o m*/ //cnt++; } } else { //MessageHandler.showError(parent, "No nodes found", //"Tools Sort Node Error"); throw new NullPointerException("no nodes found"); } }
From source file:com.cladonia.xngreditor.actions.ToolsUppercaseAction.java
License:Open Source License
/** * Convert the document to uppercase with the various flags and an xpath filter * @param document the document to be converted * @param xpath the xpath filter to be applied * @param uppercaseElements uppercase elements flag * @param uppercaseAttributes uppercase attributes flag * @param uppercaseElementsAndAttributes uppercase elements and attributes flag * @param traverseChildren process sub elements flag * @return the converted document as a string *//* ww w . j a va 2 s . c o m*/ private String uppercase(ExchangerDocument document, String xpath, boolean uppercaseElements, boolean uppercaseAttributes, boolean uppercaseElementsAndAttributes, boolean traverseChildren) { //used for xpath expressions try { //XElement[] root = document.getElements(xpath); Vector nodes = document.search(xpath); if (nodes.size() < 1) { MessageHandler.showError(parent, "Could Not Resolve XPath:\n" + xpath, "Tools Uppercase"); return (null); } for (int cnt = 0; cnt < nodes.size(); ++cnt) { //for each element //uppercase the attributes Node node = (Node) nodes.get(cnt); if (node instanceof Element) { XElement root = (XElement) nodes.get(cnt); if ((uppercaseAttributes) || (uppercaseElementsAndAttributes)) { if (root.attributeCount() > 0) { root.setAttributes(this.uppercaseAttributes(root)); } } if ((uppercaseElements) || (uppercaseElementsAndAttributes)) { //uppercase the element if (root.getName() != null) { String name = root.getName(); name = uppercaseString(name); Namespace ns = root.getNamespace(); root.setQName(new QName(name, ns)); } } if (traverseChildren) { //then uppercase its children iterateTree(root, uppercaseElements, uppercaseAttributes, uppercaseElementsAndAttributes); } } else if (((uppercaseAttributes) || (uppercaseElementsAndAttributes)) && (node instanceof Attribute)) { Attribute att = (Attribute) node; node.getParent().setAttributes(uppercaseAttributes((XElement) (node.getParent()), att)); } } document.update(); } catch (NullPointerException e) { MessageHandler.showError(parent, "Error - Cannot Uppercase,\nElements or Attributes not found", "Tools Uppercase Error"); return (null); } catch (Exception e) { MessageHandler.showError(parent, "Error - Cannot convert to uppercase", "Tools Uppercase Error"); return (null); } return document.getText(); }
From source file:com.cladonia.xngreditor.ExchangerView.java
License:Open Source License
public void setSelectedNode(Node node, boolean endTag, Vector namespaces, int y) { while (node != null) { Vector results = document.search(node.getUniquePath(), namespaces); if (results.size() > 0) { Node n = (Node) results.elementAt(0); if (n != null) { if (current instanceof Editor) { if (n instanceof XElement) { ((Editor) current).selectElement((XElement) n, endTag, y); } else if (n instanceof XAttribute) { ((Editor) current).selectAttribute((XAttribute) n, y); }/*from w w w . j ava 2 s .c o m*/ } else if (current instanceof Viewer) { if (n instanceof XElement) { ((Viewer) current).setSelectedElement((XElement) n, endTag, y); } else if (n instanceof XAttribute) { ((Viewer) current).setSelectedElement((XElement) ((XAttribute) n).getParent(), false, y); } } else if (current instanceof Designer) { if (n instanceof XElement) { ((Designer) current).setSelectedNode((XElement) n, y); } else if (n instanceof XAttribute) { ((Designer) current).setSelectedNode((XAttribute) n, y); } /*} else if ( current instanceof Grid) { if ( n instanceof XElement) { ((Grid)current).setSelectedElement( (XElement)n, null); } else if ( n instanceof XAttribute) { ((Grid)current).setSelectedElement( (XAttribute)n, null); }*/ } else if (current instanceof PluginViewPanel) { if (n instanceof XElement) { ((PluginViewPanel) current).setSelectedElement((XElement) n); } else if (n instanceof XAttribute) { ((PluginViewPanel) current).setSelectedElement((XAttribute) n); } } } return; } endTag = false; node = (Node) node.getParent(); } }
From source file:com.devoteam.srit.xmlloader.genscript.Param.java
License:Open Source License
public String applySubstitution(String text, Msg msg) throws Exception { String msgAvecParametres = ""; if (getRegle() != null) { String[] regexTab = getRegle().split("#"); // Si la regle est sous forme d'expression rgulire if (regexTab[0].toUpperCase().contains("REGEX")) { GlobalLogger.instance().getApplicationLogger().debug(TextEvent.Topic.CORE, "Replace parameter " + getRegle() + " => " + name); String[] regexRule = Arrays.copyOfRange(regexTab, 1, regexTab.length); msgAvecParametres = regexRemplace(regexRule, 0, text); }/*from www . j a v a2 s. c om*/ // Si la regle est sous forme de xpath else if (regexTab[0].toUpperCase().contains("XPATH")) { GlobalLogger.instance().getApplicationLogger().debug(TextEvent.Topic.CORE, "Replace parameter " + getRegle() + " => " + name); // Rcupration des paramtres String xpathRule = regexTab[1]; String attribut = regexTab[2]; attribut = attribut.replace("@", ""); // Cration de l'arbre DOM correspondant au message SAXReader reader = new SAXReader(); try { Document document = reader.read(new ByteArrayInputStream(text.getBytes("UTF-8"))); // Cration de l'objet XPATH ) selectionner XPath xpath = new DefaultXPath(xpathRule); // Rcupration des noeuds correspondants List<Node> nodes = xpath.selectNodes(document.getRootElement(), xpath); // Pour chaque noeuds modifier Element aRemplacer = null; for (Node n : nodes) { setRemplacedValue(n.getText()); if (n instanceof Element) { aRemplacer = (Element) n; } else { aRemplacer = n.getParent(); } String newValue = getName(); String oldValue = aRemplacer.attribute(attribut).getValue(); // On regarde si on est dans le cas de paramtres mixtes if (regexTab.length > 3) { if (regexTab[3].equals("REGEX")) { setRemplacedValue(null); String[] regexRule = Arrays.copyOfRange(regexTab, 4, regexTab.length); newValue = regexRemplace(regexRule, 0, oldValue); } } aRemplacer.setAttributeValue(attribut, newValue); } // Convertion en chane de caractre de l'arbre DOM du message msgAvecParametres = document.getRootElement().asXML(); } catch (Exception e) { } } // si la rgle est sous forme de pathkey else if (regexTab[0].toUpperCase().contains("PATHKEY")) { String valeurARemplacer = null; // On rcupre la valeur remplacer String pathKeyWord = regexTab[1]; if (msg != null) { Parameter valeurParamARemplacer = msg.getParameter(pathKeyWord); if (valeurParamARemplacer.length() > 0) { valeurARemplacer = valeurParamARemplacer.get(0).toString(); } // On remplace dans tout le message par le parametre if (valeurARemplacer != null) { msgAvecParametres = text.replace(valeurARemplacer, getName()); if (!msgAvecParametres.equals(text)) { GlobalLogger.instance().getApplicationLogger().debug(TextEvent.Topic.CORE, "Replace parameter " + valeurARemplacer + " => " + name); setRemplacedValue(valeurARemplacer); } } } } } // Si le message n'a pas subit de modification, on retourne null if (!isUsed()) { return null; } // Sinon on retourne le message modifi avec les paramtres return msgAvecParametres; }
From source file:com.dotmarketing.viewtools.XmlTool.java
License:Apache License
/** * Returns a new {@link XmlTool} instance that wraps the parent {@link Element}s of each of the {@link Node}s * being wrapped by this instance. This does not return all ancestors, just the immediate parents. *//*ww w . jav a2s . c o m*/ public XmlTool parents() { if (isEmpty()) { return null; } if (size() == 1) { return getParent(); } List<Node> parents = new ArrayList<Node>(size()); for (Node n : nodes) { Element parent = n.getParent(); if (parent != null && !parents.contains(parent)) { parents.add(parent); } } if (parents.isEmpty()) { return null; } return new XmlTool(parents); }
From source file:com.flaptor.hounder.indexer.HtmlParser.java
License:Apache License
/** * Parses a tag to produce a field./* w w w.j a v a2s .c o m*/ * @param doc the doc to modify * @throw exception on error, signaling the main method to return no document. */ private void processTag(Document doc, final String tagName, final String fieldName) throws Exception { Node bodyElement = doc.selectSingleNode("/*/" + tagName); if (null == bodyElement) { logger.warn("Content element missing from document. I was expecting a '" + tagName + "'. Will not add '" + fieldName + "' field."); return; } Node destElement = doc.selectSingleNode("//field[@name='" + fieldName + "']"); if (null != destElement) { logger.warn("Parsed element '" + fieldName + "' already present in document. Will not overwrite."); return; } ParseOutput out = parser.parse("", bodyElement.getText().getBytes("UTF-8"), "UTF-8"); for (String field : extraFields) { String content = out.getField(field); if (null == content) { logger.debug("had document without " + field + " field. Continuing with other fields."); continue; } Element docField = DocumentHelper.createElement("field"); docField.addText(content); docField.addAttribute("name", field); docField.addAttribute("indexed", Boolean.toString(INDEXED)); docField.addAttribute("stored", Boolean.toString(STORED)); docField.addAttribute("tokenized", "true"); bodyElement.getParent().add(docField); } String text = out.getText(); Element field = DocumentHelper.createElement("field"); field.addText(text); field.addAttribute("name", fieldName); field.addAttribute("indexed", Boolean.toString(INDEXED)); field.addAttribute("stored", Boolean.toString(STORED)); field.addAttribute("tokenized", "true"); bodyElement.getParent().add(field); }
From source file:com.globalsight.cxe.adapter.msoffice.WordRepairer.java
License:Apache License
@SuppressWarnings("unchecked") private static void forHyperlinkInWr(Element element) { List<Node> ts = element.selectNodes("//w:r/w:hyperlink/w:r"); for (Node t : ts) { Element hyperlink = t.getParent(); if (hyperlink == null) continue; Element wr = hyperlink.getParent(); if (wr == null) continue; Element wrParent = wr.getParent(); if (wrParent == null) continue; boolean beforeWt = false; List<?> els = wr.content(); for (Object el : els) { if (el instanceof Element) { Element elm = (Element) el; if ("t".equals(elm.getName())) { beforeWt = false;//from w ww . j av a 2s .com break; } else if (hyperlink.equals(elm)) { beforeWt = true; break; } } } wr.remove(hyperlink); @SuppressWarnings("rawtypes") List es = wrParent.elements(); int index = es.indexOf(wr); index = beforeWt ? index : index + 1; hyperlink.setParent(wrParent); es.add(index, hyperlink); } }
From source file:com.globalsight.cxe.adapter.msoffice.WordRepairer.java
License:Apache License
/** * For GBS-3085.//from ww w. j ava 2 s . com */ @SuppressWarnings("unchecked") private static void forHyperlinkInWt(Element element) { List<Node> ts = element.selectNodes("//w:r/w:t/w:hyperlink/w:r"); boolean find = false; for (Node t : ts) { find = true; Element hyperlink = t.getParent(); if (hyperlink == null) continue; Element wt = hyperlink.getParent(); if (wt == null) continue; Element wr = wt.getParent(); if (wr == null) continue; Element wrParent = wr.getParent(); if (wrParent == null) continue; List wtNodes = wt.content(); List removedNodes = new ArrayList(); int index = wtNodes.indexOf(hyperlink); for (int i = wtNodes.size() - 1; i >= index; i--) { removedNodes.add(0, wtNodes.remove(i)); } Element cloneWr = (Element) wr.clone(); List es = wrParent.content(); index = es.indexOf(wr); wrParent.remove(cloneWr); es.add(index, cloneWr); es.add(index + 1, hyperlink); wt.clearContent(); List wts = wt.content(); for (int i = 1; i < removedNodes.size(); i++) { wts.add(removedNodes.get(i)); } } if (find) { forHyperlinkInWt(element); } }
From source file:com.globalsight.cxe.adapter.msoffice.WordRepairer.java
License:Apache License
private static void forWrInWr(Element element) { @SuppressWarnings("unchecked") List<Node> ts = element.selectNodes("//w:r/w:r"); for (Node t : ts) { Element wr = t.getParent(); if (wr == null) continue; List<?> els = wr.content(); StringBuffer sb = new StringBuffer(); Element wt = null;//from w w w . j ava 2 s .c om List<Element> wrs = new ArrayList<Element>(); for (Object el : els) { if (el instanceof Element) { Element elm = (Element) el; if ("t".equals(elm.getName())) { wt = elm; sb.append(elm.getStringValue()); } else if ("r".equals(elm.getName())) { sb.append(elm.getStringValue()); wrs.add(elm); } } } if (wt == null) { wt = wr.addElement("w:t"); wt.addAttribute("xml:space", "preserve"); } wt.setText(sb.toString()); for (Element w : wrs) { wr.remove(w); } } }
From source file:com.globalsight.cxe.adapter.msoffice.WordRepairer.java
License:Apache License
private static void forTextInWr(Element element) { @SuppressWarnings("unchecked") List<Node> ts = element.selectNodes("//w:r/text()"); for (Node t : ts) { if (t.getText().matches("[\n\r]*")) { continue; }//from w w w . ja va 2s. c o m Element wr = t.getParent(); if (wr == null) { continue; } List<?> els = wr.content(); StringBuffer sb = new StringBuffer(); Element wt = null; List<DefaultText> texts = new ArrayList<DefaultText>(); for (Object el : els) { if (el instanceof DefaultText) { DefaultText text = (DefaultText) el; texts.add(text); sb.append(text.getStringValue()); } else if (el instanceof Element) { Element elm = (Element) el; if ("t".equals(elm.getName())) { wt = elm; sb.append(elm.getStringValue()); } } } if (wt == null) { wt = wr.addElement("w:t"); wt.addAttribute("xml:space", "preserve"); } wt.setText(sb.toString()); for (DefaultText text : texts) { wr.remove(text); } } }