List of usage examples for org.dom4j Node detach
Node detach();
Removes this node from its parent if there is one.
From source file:com.cladonia.xngreditor.actions.ToolsRemoveNodeAction.java
License:Open Source License
public String removeByXPath(ExchangerDocument document, String xpathPredicate) throws Exception { //List nodeList = document.getDocument().selectNodes( xpathPredicate ); Vector nodeList = document.search(xpathPredicate); if (nodeList.size() > 0) { try {/*from w w w . j a va2 s .c o m*/ for (int cnt = 0; cnt < nodeList.size(); ++cnt) { Node node = (Node) nodeList.get(cnt); node.detach(); } } catch (NullPointerException e) { MessageHandler.showError(parent, "XPath: " + xpathPredicate + "\nCannot be resolved", "Tools Remove Node Error"); return (null); } catch (Exception e) { MessageHandler.showError(parent, "Error removing nodes from the document", "Tools Remove Node Error"); return (null); } document.update(); } else { MessageHandler.showError(parent, "No nodes could be found for:\n" + xpathPredicate, "Tools Remove Node Error"); return (null); } return document.getText(); }
From source file:com.easyjf.generator.AllGenerator.java
License:Apache License
private void genXml(AllProcessor processor) { for (String templateFile : this.xmlfiles.keySet()) { tg.setProcess(processor);// w ww . j av a 2s .c o m String fileName = (String) xmlfiles.get(templateFile); File targetFile = new File(fileName); tg.setTargetDir(""); tg.setTemplateDir(templateDir); tg.setTemplateName(templateFile); if (targetFile.exists()) { tg.setTargetName(fileName + "_tmp"); tg.generator(false); try { Document doc = this.getDocument(fileName + "_tmp"); Document document = this.getDocument(fileName); String existNode = "/easyjf-web/modules/module[@name='" + this.lowerBeanName + "']"; Node node = "mvc.xml".equals(templateFile) ? document.selectSingleNode(existNode) : findBean(document, this.lowerBeanName + ("dao.xml".equals(templateFile) ? "Dao" : "Service")); if (node == null) { String appendNode = "/easyjf-web/modules", cnode = appendNode + "/module"; if (!"mvc.xml".equals(templateFile)) appendNode = "/beans"; Element moduleE = (Element) document.selectSingleNode(appendNode); Node n = "mvc.xml".equals(templateFile) ? doc.selectSingleNode(cnode) : findBean(doc, this.lowerBeanName + ("dao.xml".equals(templateFile) ? "Dao" : "Service")); if (moduleE != null && n != null) { n.detach(); moduleE.add(n); } OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter output = new XMLWriter(new FileWriter(new File(fileName)), format); output.write(document); output.close(); } new File(fileName + "_tmp").deleteOnExit(); new File(fileName + "_tmp").delete(); } catch (Exception e) { // e.printStackTrace(); } System.out.println( I18n.getLocaleMessage("generator.Successfully.add.to.configuration.information.to.a.file") + fileName); } else { System.out.println( I18n.getLocaleMessage("generator.Successful.configuration.file.generation") + fileName); tg.setTargetName(fileName); new File(fileName + "_tmp").deleteOnExit(); new File(fileName + "_tmp").delete(); tg.generator(false); } } }
From source file:com.flaptor.hounder.util.HtmlParser.java
License:Apache License
@SuppressWarnings("unchecked") private void ignoreXpath(Document htmlDoc) { if (null == xpathIgnore) { return;/* w w w .ja v a 2 s . c om*/ } List<Node> nodes = (List<Node>) htmlDoc.selectNodes(xpathIgnore.toString()); for (Node node : nodes) { try { node.detach(); } catch (Exception e) { logger.warn("Ignoring exception", e); } } }
From source file:com.flaptor.util.parser.HtmlParser.java
License:Apache License
@SuppressWarnings("unchecked") private void ignoreXpath(Document htmlDoc) { if (null == xpathIgnore) { return;// w w w . j a va 2 s . c o m } List<Node> nodes = (List<Node>) htmlDoc.selectNodes(xpathIgnore); for (Node node : nodes) { try { node.detach(); } catch (Exception e) { logger.debug("Ignoring exception", e); } } }
From source file:com.globalsight.everest.edit.offline.page.TmxUtil.java
License:Apache License
public static void removeNodes(Element p_segment, String p_path) { List nodes = p_segment.selectNodes(p_path); for (int i = 0; i < nodes.size(); i++) { Node node = (Node) nodes.get(i); node.detach(); }/* w ww . j a va2s . co m*/ }
From source file:com.globalsight.everest.edit.offline.page.TmxUtil.java
License:Apache License
/** * Removes the given <sub> element from the segment. <sub> is special since * it does not only surround embedded tags but also text, which must be * pulled out of the <sub> and added to the parent tag. *//* ww w .jav a2 s .co m*/ public static void replaceNbsp(Element p_element) { Element parent = p_element.getParent(); int index = parent.indexOf(p_element); // We copy the current content, clear out the parent, and then // re-add the old content, inserting the <sub>'s textual // content instead of the <sub> (this clears any embedded TMX // tags in the subflow). ArrayList newContent = new ArrayList(); List content = parent.content(); for (int i = content.size() - 1; i >= 0; --i) { Node node = (Node) content.get(i); newContent.add(node.detach()); } Collections.reverse(newContent); parent.clearContent(); for (int i = 0, max = newContent.size(); i < max; ++i) { Node node = (Node) newContent.get(i); if (i == index) { parent.addText("\u00A0"); } else { parent.add(node); } } }
From source file:com.globalsight.everest.edit.offline.page.TmxUtil.java
License:Apache License
/** * Removes the given <sub> element from the segment. <sub> is special since * it does not only surround embedded tags but also text, which must be * pulled out of the <sub> and added to the parent tag. *//*from w w w . j av a2 s . co m*/ public static void removeSubElement(Element p_element) { Element parent = p_element.getParent(); int index = parent.indexOf(p_element); // We copy the current content, clear out the parent, and then // re-add the old content, inserting the <sub>'s textual // content instead of the <sub> (this clears any embedded TMX // tags in the subflow). ArrayList newContent = new ArrayList(); List content = parent.content(); for (int i = content.size() - 1; i >= 0; --i) { Node node = (Node) content.get(i); newContent.add(node.detach()); } Collections.reverse(newContent); parent.clearContent(); for (int i = 0, max = newContent.size(); i < max; ++i) { Node node = (Node) newContent.get(i); if (i == index) { parent.addText(p_element.getText()); } else { parent.add(node); } } }
From source file:com.globalsight.everest.edit.offline.page.TmxUtil.java
License:Apache License
/** * Removes the given TMX 1.4 <hi> element from the segment. <hi> is special * since it does not surround embedded tags but text, which must be pulled * out of the <hi> and added to the parent segment. */// w w w .j a v a2 s.c om private static void removeHiElement(Element p_element) { Element parent = p_element.getParent(); int index = parent.indexOf(p_element); // We copy the current content, clear out the parent, and then // re-add the old content, inserting the <hi>'s content // instead of the <hi>. ArrayList newContent = new ArrayList(); List content = parent.content(); for (int i = content.size() - 1; i >= 0; --i) { Node node = (Node) content.get(i); newContent.add(node.detach()); } Collections.reverse(newContent); parent.clearContent(); for (int i = 0, max = newContent.size(); i < max; ++i) { Node node = (Node) newContent.get(i); if (i == index) { parent.appendContent(p_element); } else { parent.add(node); } } }
From source file:com.globalsight.everest.projecthandler.ProjectTmTuvT.java
License:Apache License
/** * Removes the given TMX 1.4 <hi> element from the segment. <hi> is special * since it does not surround embedded tags but text, which must be pulled * out of the <hi> and added to the parent segment. *///from w w w.j a v a 2 s . c o m private void removeHiElement(Element p_element) { Element parent = p_element.getParent(); int index = parent.indexOf(p_element); // We copy the current content, clear out the parent, and then // re-add the old content, inserting the <hi>'s content // instead of the <hi>. ArrayList newContent = new ArrayList(); List content = parent.content(); for (int i = content.size() - 1; i >= 0; --i) { Node node = (Node) content.get(i); newContent.add(node.detach()); } Collections.reverse(newContent); parent.clearContent(); for (int i = 0, max = newContent.size(); i < max; ++i) { Node node = (Node) newContent.get(i); if (i == index) { parent.appendContent(p_element); } else { parent.add(node); } } }
From source file:com.globalsight.everest.tm.exporter.TmxWriter.java
License:Apache License
/** * Removes nodes identified by the XPath p_path from a DOM Element. *//*from w ww. jav a2s .c o m*/ private static void removeNodes(Element p_segment, String p_path) { List nodes = p_segment.selectNodes(p_path); for (int i = 0; i < nodes.size(); i++) { Node node = (Node) nodes.get(i); node.detach(); } }