Example usage for org.jdom2 Content getDocument

List of usage examples for org.jdom2 Content getDocument

Introduction

In this page you can find the example usage for org.jdom2 Content getDocument.

Prototype

public Document getDocument() 

Source Link

Document

Return this child's owning document or null if the branch containing this child is currently not attached to a document.

Usage

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 ww w  . j a v  a2  s.  co  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/*w ww . j  a v  a  2  s .  c o  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++;
    }

}