List of usage examples for org.dom4j Node clone
Object clone();
clone
will return a deep clone or if this node is read-only then clone will return the same instance.
From source file:edu.scripps.fl.pubchem.xml.PubChemXMLDoc.java
License:Apache License
public void organizeXMLDoc(Document doc) { Element parent = (Element) doc.selectSingleNode(rootString); String[] nodesOrder = { name, description, protocol, comment, xref, results, revision, target, activityOutcome, dr, grantNumber, projectCategory, isPanel, panelInfo, categorizedComment }; for (String nodeString : nodesOrder) { Node node = parent.selectSingleNode(nodeString); if (node != null) { Node clone = (Node) node.clone(); node.detach();//from www .j av a2 s. c o m parent.add(clone); } } parent = (Element) doc.selectSingleNode(dbTracking); String[] secondNodesOrder = { "PC-DBTracking_name", "PC-DBTracking_source-id", "PC-DBTracking_date" }; for (String nodeString : secondNodesOrder) { Node node = parent.selectSingleNode(nodeString); if (node != null) { Node clone = (Node) node.clone(); node.detach(); parent.add(clone); } } }
From source file:nl.tue.gale.ae.processor.xmlmodule.AdaptLinkModule.java
License:Open Source License
@Override @SuppressWarnings("unchecked") public Element traverse(Element element, Resource resource) throws ProcessorException { GaleContext gale = GaleContext.of(resource); CacheSession<EntityValue> session = gale.openUmSession(); try {//from ww w . j av a 2 s . c om // initialization processor.traverseChildren(element, resource); boolean useParent = !(element.getName().equals("a")); Element a = (useParent ? element.getParent() : element); String href = a.attributeValue("href"); URI uri = URIs.of(href); //System.out.println(href); String anchorid = uri.getFragment(); href = URIs.builder().uri(uri).fragment(null).build().toString(); session.setBaseUri(session.resolve(href)); // possibly add content to the link, based on previous processing Node n = resource.getTyped(Node.class, "nl.tue.gale.ae.processor.xmlmodule.AdaptLinkModule.content"); if (n != null) { n = (Node) n.clone(); a.add(n); } // should we adapt the class attribute or hide the link boolean adapt = (!"false".equals(a.attributeValue("adapt"))); a.addAttribute("adapt", null); // should the link be removed or hidden if (adapt) { String removeexpr = (String) gale.cfgm().getObject("gale://gale.tue.nl/config/link#remove", resource); if (removeexpr != null) { boolean remove = false; try { remove = (Boolean) gale.eval(session, removeexpr); } catch (Exception e) { log.debug(e, e); } if (remove) { GaleUtil.replaceNode(a, null); return null; } } String hideexpr = (String) gale.cfgm().getObject("gale://gale.tue.nl/config/link#hide", resource); if (hideexpr != null) { boolean hide = false; try { hide = (Boolean) gale.eval(session, hideexpr); } catch (Exception e) { log.debug(e, e); } if (hide) { GaleUtil.replaceNode(a, DocumentFactory.getInstance().createText(a.getText())); return null; } } } // determine and set css class if (adapt) { EntityValue ev = session.get(session.resolve("#link.class")); String cssclass = (ev != null ? ev.getValue().toString() : null); if (cssclass == null) { String classexpr = (String) gale.cfgm().getObject("gale://gale.tue.nl/config/link#classexpr", resource); try { cssclass = gale.eval(session, classexpr).toString(); } catch (Exception e) { log.debug(e, e); cssclass = "unknown"; } } a.addAttribute("class", cssclass); } // add possible exec code String exec = element.attributeValue("exec"); String query = URIs.of(href).getQuery(); if (exec != null) { String guid = GaleUtil.newGUID(); storeInSession(gale, guid, exec); Map<String, String> params = GaleUtil.getQueryParameters(query); params.put("plugin", "exec"); params.put("guid", guid); query = GaleUtil.getQueryString(params); } // add proper link to concept String conceptName = (href.startsWith("?") ? gale.concept().getUriString() : Concept.getConceptURI(session.getBaseUri()).toString()); uri = URIs.of(gale.gsb().getConceptManager().getConceptLink(URIs.of(conceptName), gale.req(), query)); // Added by Vinicius Ramos to log the currentView: "?view=" + gale.currentView() a.addAttribute("href", uri.toString() + (anchorid != null ? "#" + anchorid : "") + (uri.toString().contains("?") ? "" : "?view=" + gale.currentView())); a.setQName(DocumentFactory.getInstance().createQName(a.getName(), "", GaleUtil.xhtmlns)); if (useParent) a.remove(element); // add possible icons if (adapt) { List<String> iconList = (List<String>) gale.cfgm() .getObject("gale://gale.tue.nl/config/link#iconlist", resource); Element span = null; for (String iconExpr : iconList) { try { String icon = (String) gale.eval(session, iconExpr); if (icon == null || "".equals(icon)) continue; boolean pre = false; if (icon.startsWith("pre:")) { icon = icon.substring(4); pre = true; } URI iconUri = URIs.of(icon); if (iconUri.isAbsolute()) { // absolute uri if (iconUri.getScheme().equals("gale")) { icon = GaleUtil.getContextURL(gale.req()) + "/" + GaleUtil.getServletName(gale.req()) + "/${home}" + iconUri.getPath(); } } else { // relative uri } if (span == null) { span = DocumentFactory.getInstance().createElement("span", GaleUtil.xhtmlns); a.getParent().content().add(a.getParent().content().indexOf(a), span); a.getParent().content().remove(a); span.add(a); } Element img = DocumentFactory.getInstance().createElement("img", GaleUtil.xhtmlns); img.addAttribute("src", icon); img.addAttribute("align", "bottom"); img.addAttribute("alt", ""); if (pre) span.content().add(0, img); else span.add(img); } catch (Exception e) { log.debug(e, e); } } } return null; } catch (Exception e) { log.debug(e, e); return (Element) GaleUtil.replaceNode(element, GaleUtil.createErrorElement("[" + e.getMessage() + "]")); } finally { session.rollback(); } }
From source file:nl.tue.gale.ae.processor.xmlmodule.MCModule.java
License:Open Source License
@SuppressWarnings("unchecked") private Question readQuestion(Element element) { Element q = DocumentFactory.getInstance().createElement("span", xhtmlns); for (Node n : (List<Node>) element.content()) { boolean b = true; if (n instanceof Element) if (((Element) n).getName().equals("answer") || ((Element) n).getName().equals("concept_relation")) b = false;//from www. j av a 2s. c om if (b) q.add((Node) n.clone()); } Question result = (new Question()).setQuestion(q).setCount(new Integer(element.attributeValue("answers"))) .setRight(new Integer(element.attributeValue("right"))); //Check if the question has an ID. if (element.attributeValue("id") != null && !"".equals(element.attributeValue("id"))) result.setId(new Integer(element.attributeValue("id"))); if ("checkbox".equals(element.attributeValue("type"))) result.setType(1); for (Element a : (List<Element>) element.elements("answer")) result.getAnswers().add(readAnswer(a)); /* * Created by: Vinicius Ramos * DATE: Sept 13th 2012 * * READ the concepts in XHTML */ int v = 0; //Read in xml file all the concepts related to the question for (Element c : (List<Element>) element.elements("concept_relation")) { result.getConcepts().add(c.getText()); } return result; }
From source file:nl.tue.gale.ae.processor.xmlmodule.MCModule.java
License:Open Source License
@SuppressWarnings("unchecked") private Answer readAnswer(Element element) { Element a = DocumentFactory.getInstance().createElement("span", xhtmlns); for (Node n : (List<Node>) element.content()) { boolean b = true; if (n instanceof Element) if (((Element) n).getName().equals("explain")) b = false;//from w w w . j a v a 2s.c o m if (b) a.add((Node) n.clone()); } Answer result = (new Answer()).setAnswer(a).setExplain(element.element("explain")) .setCorrect(new Boolean(element.attributeValue("correct"))); //Check if the answer has an ID. if (element.attributeValue("id") != null && !"".equals(element.attributeValue("id"))) result.setId(new Integer(element.attributeValue("id"))); return result; }
From source file:org.collectionspace.chain.util.xtmpl.XTmplDocument.java
License:Educational Community License
@SuppressWarnings("unchecked") public void setTexts(String key, String[] texts) { Node basis = document.selectSingleNode(attach.get(key)); Element parent = basis.getParent(); int pos = parent.indexOf(basis); List<Node> nodes = (List<Node>) parent.content(); List<Node> after = new ArrayList(nodes.subList(pos + 1, nodes.size())); basis.detach();/*from w w w . java2s. co m*/ if (texts.length > 0) { for (Node n : after) n.detach(); for (String text : texts) { Node renewed = (Node) basis.clone(); renewed.setText(text); parent.add(renewed); } for (Node n : after) parent.add(n); } }
From source file:org.danann.cernunnos.runtime.SerializeGrammarTask.java
License:Apache License
private static Node serializeEntry(Entry e, DocumentFactory fac) { // Assertions... if (e == null) { String msg = "Argument 'e [Entry]' cannot be null."; throw new IllegalArgumentException(msg); }// w w w .j a va 2 s . com if (fac == null) { String msg = "Argument 'fac' cannot be null."; throw new IllegalArgumentException(msg); } Element rslt = fac.createElement("entry"); rslt.addAttribute("type", e.getType().name()); // Name. Element name = fac.createElement("name"); name.setText(e.getName()); rslt.add(name); // Deprecation. if (e.isDeprecated()) { Element dep = fac.createElement("deprecation"); dep.addAttribute("version", e.getDeprecation().getVersion()); for (Element n : e.getDeprecation().getDescription()) { dep.add((Element) n.clone()); } rslt.add(dep); } // Description. Element desc = e.getDescription() != null ? (Element) e.getDescription().clone() : fac.createElement("description"); rslt.add(desc); // Formula. rslt.add(serializeFormula(e.getFormula(), e.getMappings(), fac)); // Examples. for (Node x : e.getExamples()) { rslt.add((Node) x.clone()); } return rslt; }
From source file:org.firesoa.common.jxpath.model.dom4j.Dom4JNodePointer.java
License:Open Source License
@Override public void setValue(Object value) { if (value == null) value = "";//null?? if ((node instanceof org.dom4j.CharacterData || node instanceof Attribute || node instanceof DocumentType || node instanceof Entity || node instanceof ProcessingInstruction)) { String string = (String) TypeUtils.convert(value, String.class); if (string != null && !string.equals("")) { ((Node) node).setText(string); } else {/* w w w . j a v a 2 s. c o m*/ ((Node) node).getParent().remove((Node) node); } } else if (node instanceof Document) { Document theOriginalDoc = (Document) node; Element theOrigialRoot = theOriginalDoc.getRootElement(); if (value instanceof Document) {//?document Document valueDoc = (Document) value; Element valueRoot = valueDoc.getRootElement(); if (theOrigialRoot == null || valueRoot == null || theOrigialRoot.getQName().equals(valueRoot.getQName())) { theOriginalDoc.clearContent(); List content = valueDoc.content(); if (content != null) { for (int i = 0; i < content.size(); i++) { Node dom4jNode = (Node) content.get(i); Node newDom4jNode = (Node) dom4jNode.clone(); theOriginalDoc.add(newDom4jNode); } } } else { throw new RuntimeException( "Can NOT assign " + valueRoot.getQName() + " to " + theOrigialRoot.getQName()); } } else if (value instanceof Element) { Element valueElem = (Element) value; if (valueElem.getQName().equals(theOrigialRoot.getQName())) { theOriginalDoc.clearContent(); Element newValueElem = (Element) valueElem.clone(); theOriginalDoc.setRootElement(newValueElem); } else { throw new RuntimeException( "Can NOT assign " + valueElem.getQName() + " to " + theOrigialRoot.getQName()); } } else { throw new RuntimeException("Can NOT assign " + value + " to " + theOrigialRoot.getQName()); } // else if (value instanceof Comment){ // Comment cmmt = (Comment)((Comment)value).clone(); // theOriginalDoc.add(cmmt); // // }else if (value instanceof ProcessingInstruction){ // ProcessingInstruction instru = (ProcessingInstruction)((ProcessingInstruction)value).clone(); // theOriginalDoc.add(instru); // } } else if (node instanceof Element) { Element originalElem = ((Element) node); if (value != null && value instanceof Element) { Element valueElm = (Element) value; if (originalElem.getQName().equals(valueElm.getQName())) { originalElem.clearContent(); List content = valueElm.content(); if (content != null) { for (int i = 0; i < content.size(); i++) { Node dom4jNode = (Node) content.get(i); Node newDom4jNode = (Node) dom4jNode.clone(); originalElem.add(newDom4jNode); } } } else { throw new RuntimeException( "Can NOT assign " + valueElm.getQName() + " to " + originalElem.getQName()); } } else if (value != null && value instanceof Text) { originalElem.clearContent(); Text txt = (Text) ((Text) value).clone(); originalElem.add(txt); } else if (value != null && value instanceof CDATA) { originalElem.clearContent(); CDATA cdata = (CDATA) ((CDATA) value).clone(); originalElem.add(cdata); } else if (value != null && value instanceof java.util.Date) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateStr = format.format((java.util.Date) value); originalElem.clearContent(); originalElem.addText(dateStr); } else { String string = (String) TypeUtils.convert(value, String.class); originalElem.clearContent(); originalElem.addText(string); } } }
From source file:org.gbif.portal.util.mhf.message.impl.xml.XMLMessageFactory.java
License:Open Source License
/** * Creates a new Message based on the node that is passed in * * @param node To copy or detach//from w w w . j ava2s . c o m * @param detach If the node may be detached from the parent document * @return A new message */ public XMLMessage build(Node node, boolean detach) { Document document = DocumentFactory.getInstance().createDocument(); if (detach) { document.add(node.detach()); } else { Node cloned = (Node) node.clone(); document.add(cloned); } return new XMLMessage(document); }
From source file:org.intalio.tempo.workflow.fds.tools.SoapTools.java
License:Open Source License
public static Document unwrapMessage(Document soapEnvelope) throws InvalidInputFormatException { if (_log.isDebugEnabled()) _log.debug(soapEnvelope.asXML()); Document result = DocumentHelper.createDocument(); Node node = null; synchronized (path) { node = path.selectSingleNode(soapEnvelope); }//ww w . ja v a 2s . c o m result.add((Node) node.clone()); return result; }
From source file:org.jin.dic.data.pub.ldoce.v5.Convert2Html.java
License:Open Source License
private static void addChildren(Element s, Element d) { Element span = DocumentHelper.createElement("span"); d.add(span);/*from w ww .ja v a 2 s . c o m*/ if (!s.getName().equalsIgnoreCase("base")) { String c; if (s.getName().equals("span")) { c = s.attributeValue("class"); if (c == null || c.length() == 0) { c = null; } else { c = getClass(s); } } else { c = getClass(s); } if (c != null && c.length() > 0) span.addAttribute("class", c); } Iterator i = s.nodeIterator(); Node node; while (i.hasNext()) { node = (Node) i.next(); if (node instanceof Element) addChildren((Element) node, span); else span.add((Node) node.clone()); } }