List of usage examples for org.jdom2 Content clone
@Override
public Content clone()
From source file:com.izforge.izpack.util.xmlmerge.action.FullMergeAction.java
License:Open Source License
/** * Performs the actual merge between two source elements. * * @param parentOut The merged element//from w ww . j a v a 2s.c o m * @param origElement The first source element * @param patchElement The second source element * @throws AbstractXmlMergeException If an error occurred during the merge */ private void doIt(Element parentOut, Element origElement, Element patchElement) throws AbstractXmlMergeException { addAttributes(parentOut, patchElement); List<Content> origContentList = origElement.getContent(); List<Content> patchContentList = patchElement.getContent(); List<Content> unmatchedPatchContentList = new ArrayList<Content>(); List<Content> matchedPatchContentList = new ArrayList<Content>(); for (Content origContent : origContentList) { logger.fine("Checking original content: " + origContent + " for matching patch contents"); if (origContent instanceof Element) { boolean patchMatched = false; for (Content patchContent : patchContentList) { logger.fine("Checking patch content: " + patchContent); if (patchContent instanceof Comment || patchContent instanceof Text) { // skip and leave original comment or text logger.fine("Skipped patch content: " + patchContent); } else if (!(patchContent instanceof Element)) { throw new DocumentException(patchContent.getDocument(), "Contents of type " + patchContent.getClass().getName() + " in patch document not supported"); } else { if (((Matcher) m_matcherFactory.getOperation((Element) patchContent, (Element) origContent)) .matches((Element) patchContent, (Element) origContent)) { logger.fine("Apply matching patch: " + patchContent + " -> " + origContent); applyAction(parentOut, (Element) origContent, (Element) patchContent); patchMatched = true; if (!matchedPatchContentList.contains(patchContent)) { matchedPatchContentList.add(patchContent); } } else { if (!unmatchedPatchContentList.contains(patchContent)) { unmatchedPatchContentList.add(patchContent); } } // Continue searching here for finding multiple matches } } if (!patchMatched) { logger.fine("Apply original: " + origContent); applyAction(parentOut, (Element) origContent, null); } } else if (origContent instanceof Comment || origContent instanceof Text) { // leave original comment or text parentOut.addContent((Content) origContent.clone()); } else { throw new DocumentException(origContent.getDocument(), "Contents of type " + origContent.getClass().getName() + " in original document not supported"); } } for (Content unmatchedPatchContent : unmatchedPatchContentList) { if (!matchedPatchContentList.contains(unmatchedPatchContent)) { logger.fine("Apply unmatching patch: " + unmatchedPatchContent); applyAction(parentOut, null, (Element) unmatchedPatchContent); } } }
From source file:com.izforge.izpack.util.xmlmerge.action.OrderedMergeAction.java
License:Open Source License
/** * Performs the actual merge between two source elements. * * @param parentOut The merged element/*from w w w.j av a 2s . co m*/ * @param parentIn1 The first source element * @param parentIn2 The second source element * @throws AbstractXmlMergeException If an error occurred during the merge */ private void doIt(Element parentOut, Element parentIn1, Element parentIn2) throws AbstractXmlMergeException { addAttributes(parentOut, parentIn2); Content[] list1 = (Content[]) parentIn1.getContent().toArray(new Content[] {}); Content[] list2 = (Content[]) parentIn2.getContent().toArray(new Content[] {}); int offsetTreated1 = 0; int offsetTreated2 = 0; for (Content content1 : list1) { logger.fine("List 1: " + content1); if (content1 instanceof Comment || content1 instanceof Text) { parentOut.addContent((Content) content1.clone()); offsetTreated1++; } else if (!(content1 instanceof Element)) { throw new DocumentException(content1.getDocument(), "Contents of type " + content1.getClass().getName() + " not supported"); } else { Element e1 = (Element) content1; // does e1 exist on list2 and has not yet been treated int posInList2 = -1; for (int j = offsetTreated2; j < list2.length; j++) { logger.fine("List 2: " + list2[j]); if (list2[j] instanceof Element) { if (((Matcher) m_matcherFactory.getOperation(e1, (Element) list2[j])).matches(e1, (Element) list2[j])) { logger.fine("Match found: " + e1 + " and " + list2[j]); posInList2 = j; break; } } else if (list2[j] instanceof Comment || list2[j] instanceof Text) { // skip } else { throw new DocumentException(list2[j].getDocument(), "Contents of type " + list2[j].getClass().getName() + " not supported"); } } // element found in second list, but there is some elements to // be treated before in second list while (posInList2 != -1 && offsetTreated2 < posInList2) { Content contentToAdd; if (list2[offsetTreated2] instanceof Element) { applyAction(parentOut, null, (Element) list2[offsetTreated2]); } else { // FIXME prevent double comments in output by enhancing applyAction() to // Content type instead of Element // Workaround: Add only comments from original document (List1) if (!(list2[offsetTreated2] instanceof Comment)) { contentToAdd = (Content) list2[offsetTreated2].clone(); parentOut.addContent(contentToAdd); } } offsetTreated2++; } // element found in all lists if (posInList2 != -1) { applyAction(parentOut, (Element) list1[offsetTreated1], (Element) list2[offsetTreated2]); offsetTreated1++; offsetTreated2++; } else { // element not found in second list applyAction(parentOut, (Element) list1[offsetTreated1], null); offsetTreated1++; } } } // at the end of list1, are there some elements in list2 which must be still treated? while (offsetTreated2 < list2.length) { Content contentToAdd; if (list2[offsetTreated2] instanceof Element) { applyAction(parentOut, null, (Element) list2[offsetTreated2]); } else { // FIXME prevent double comments in output by enhancing applyAction() to Content // type instead of Element // Workaround: Add only comments from original document (List1) if (!(list2[offsetTreated2] instanceof Comment)) { contentToAdd = (Content) list2[offsetTreated2].clone(); parentOut.addContent(contentToAdd); } } offsetTreated2++; } }
From source file:io.wcm.handler.richtext.util.RichTextUtil.java
License:Apache License
/** * Rewrites all children/sub-tree of the given parent element. * For rewrite operations the given rewrite content handler is called. * @param parent Parent element/*from w w w .j av a 2 s. com*/ * @param rewriteContentHandler Rewrite content handler */ public static void rewriteContent(Element parent, RewriteContentHandler rewriteContentHandler) { // iterate through content list and build new content list List<Content> originalContent = parent.getContent(); List<Content> newContent = new ArrayList<Content>(); for (Content contentElement : originalContent) { // handle element if (contentElement instanceof Element) { Element element = (Element) contentElement; // check if rewrite is needed for element List<Content> rewriteContent = rewriteContentHandler.rewriteElement(element); if (rewriteContent != null) { // element was removed if (rewriteContent.isEmpty()) { // do not add to newContent } // element is the same - rewrite child elements else if (rewriteContent.size() == 1 && rewriteContent.get(0) == element) { //NOPMD rewriteContent(element, rewriteContentHandler); newContent.add(element); } // element was replaced with other content - rewrite and add instead else { for (Content newContentItem : rewriteContent) { if (newContentItem instanceof Element) { Element newElement = (Element) newContentItem; rewriteContent(newElement, rewriteContentHandler); } newContent.add(newContentItem.clone()); } } } // nothing to rewrite - do nothing, but rewrite child element else { rewriteContent(element, rewriteContentHandler); newContent.add(element); } } // handle text node else if (contentElement instanceof Text) { Text text = (Text) contentElement; // check if rewrite is needed for text node List<Content> rewriteContent = rewriteContentHandler.rewriteText(text); if (rewriteContent != null) { // element was removed if (rewriteContent.isEmpty()) { // do not add to newContent } // element is the same - ignore else if (rewriteContent.size() == 1 && rewriteContent.get(0) == text) { //NOPMD // add original element newContent.add(text); } // element was replaced with other content - add instead else { for (Content newContentItem : rewriteContent) { newContent.add(newContentItem.clone()); } } } // nothing to rewrite - do nothing, but add original text element else { newContent.add(text); } } // unknown element - just add to new content else { newContent.add(contentElement); } } // replace original content with new content parent.removeContent(); parent.addContent(newContent); }
From source file:org.kdp.word.transformer.ListParagraphTransformer.java
License:Apache License
private void processListItemBatch(Context context, Element parent, List<Element> listItems) { boolean ordered = false; for (Element el : listItems) { removeNestedSpanElements(el);/*from ww w. jav a 2s .co m*/ normalizeListItemText(el); ordered = processItemMarker(el); el.getAttributes().clear(); } Element firstItem = listItems.get(0); int index = parent.indexOf(firstItem); for (Element el : listItems) { parent.removeContent(el); } JDOMFactory factory = context.getJDOMFactory(); Element ul = factory.element(ordered ? "ol" : "ul"); for (Element el : listItems) { Element li = factory.element("li"); li.setAttribute("class", "MsoListParagraph"); for (Content co : el.getContent()) { li.addContent(co.clone()); } ul.addContent(li); } parent.addContent(index, ul); }
From source file:org.mycore.datamodel.metadata.MCRMetaXML.java
License:Open Source License
private static void cloneListContent(List<Content> dest, List<Content> source) { dest.clear();/*w w w . ja va2s .c om*/ for (Content c : source) { dest.add((Content) c.clone()); } }
From source file:org.xflatdb.xflat.query.XPathUpdate.java
License:Apache License
/** * Applies the update operations to the given DOM Element representing * the data in a selected row./*from w ww .jav a2 s .com*/ * @param rowData The DOM Element representing the data in a selected row. * @return true if any updates were applied. */ public int apply(Element rowData) { int updateCount = 0; for (Update update : this.updates) { //the update's value will be one or the other, don't know which Content asContent = null; String asString = null; if (update.value instanceof String) { asString = (String) update.value; } if (update.value instanceof Content) { asContent = (Content) update.value; } for (Object node : update.path.evaluate(rowData)) { if (node == null) continue; Parent parent; Element parentElement; if (update.getUpdateType() == UpdateType.UNSET) { if (node instanceof Attribute) { parentElement = ((Attribute) node).getParent(); if (parentElement != null) { parentElement.removeAttribute((Attribute) node); updateCount++; } } else if (node instanceof Content) { parent = ((Content) node).getParent(); //remove this node from its parent element if (parent != null) { parent.removeContent((Content) node); updateCount++; } } continue; } //it's a set if (node instanceof Attribute) { //for attributes we set the value to empty string //this way it can still be selected by xpath for future updates if (update.value == null) { ((Attribute) node).setValue(""); updateCount++; } else { if (asString == null) { asString = getStringValue(update.value); } //if we fail conversion then do nothing. if (asString != null) { ((Attribute) node).setValue(asString); updateCount++; } } continue; } else if (!(node instanceof Content)) { //can't do anything continue; } Content contentNode = (Content) node; //need to convert if (update.value != null && asContent == null) { asContent = getContentValue(update.value); if (asContent == null) { //failed conversion, try text asString = getStringValue(update.value); if (asString != null) { //success! asContent = new Text(asString); } } } if (node instanceof Element) { //for elements we also set the value, but the value could be Content if (update.value == null) { ((Element) node).removeContent(); updateCount++; } else if (asContent != null) { if (asContent.getParent() != null) { //we used the content before, need to clone it asContent = asContent.clone(); } ((Element) node).setContent(asContent); updateCount++; } continue; } //at this point the node is Text, CDATA or something else. //The strategy now is to replace the value in its parent. parentElement = contentNode.getParentElement(); if (parentElement == null) { //can't do anything continue; } if (update.value == null || asContent != null) { //replace this content in the parent element int index = parentElement.indexOf(contentNode); parentElement.removeContent(index); if (update.value != null) { //if it was null then act like an unset, otherwise //its a replace if (asContent.getParent() != null) { //we used the content before, need to clone it asContent = asContent.clone(); } parentElement.addContent(index, asContent); } updateCount++; } } } return updateCount; }