Search in sources :

Example 1 with MergeCueContext

use of org.craftercms.core.xml.mergers.impl.cues.MergeCueContext in project core by craftercms.

the class MergeParentAndChildMergeCueTest method setUpTestMergeCue.

private void setUpTestMergeCue() {
    ElementMergeMatcherImpl elementMergeMatcher = new ElementMergeMatcherImpl();
    elementMergeMatcher.setIdAttributeName(new QName(ID_ATTR_NAME));
    MergeCueResolver mergeCueResolver = new MergeCueResolver() {

        @Override
        public MergeCueContext getMergeCue(Element parent, Element child) {
            return new MergeCueContext(mergeCue, parent, child, Collections.<String, String>emptyMap());
        }
    };
    mergeCue = new MergeParentAndChildMergeCue();
    mergeCue.setElementMergeMatcher(elementMergeMatcher);
    mergeCue.setMergeCueResolver(mergeCueResolver);
    mergeCue.setMergeOrderParamName(MERGE_ORDER_PARAM_NAME);
    mergeCue.setDefaultMergeOrder(DEFAULT_MERGE_ORDER);
}
Also used : QName(org.dom4j.QName) MergeCueContext(org.craftercms.core.xml.mergers.impl.cues.MergeCueContext) Element(org.dom4j.Element) MergeCueResolver(org.craftercms.core.xml.mergers.impl.cues.MergeCueResolver)

Example 2 with MergeCueContext

use of org.craftercms.core.xml.mergers.impl.cues.MergeCueContext in project core by craftercms.

the class MergeCueResolverImpl method getMergeCue.

@Override
public MergeCueContext getMergeCue(Element parent, Element child) {
    MergeCue parentMergeCue;
    MergeCue childMergeCue;
    Attribute parentMergeCueAttribute = getMergeCueAttribute(parent, parentMergeCues);
    if (parentMergeCueAttribute != null) {
        parentMergeCue = parentMergeCues.get(parentMergeCueAttribute.getQName());
    } else {
        parentMergeCue = defaultParentMergeCue;
    }
    Attribute childMergeCueAttribute = getMergeCueAttribute(child, childMergeCues);
    if (childMergeCueAttribute != null) {
        childMergeCue = childMergeCues.get(childMergeCueAttribute.getQName());
    } else {
        childMergeCue = defaultChildMergeCue;
    }
    MergeCue chosenMergeCue;
    Map<String, String> mergeCueParams;
    if (parentMergeCue.getPriority() > childMergeCue.getPriority()) {
        chosenMergeCue = parentMergeCue;
        if (parentMergeCueAttribute != null) {
            mergeCueParams = getMergeCueParams(parent, parentMergeCueAttribute);
        } else {
            mergeCueParams = Collections.emptyMap();
        }
    } else {
        chosenMergeCue = childMergeCue;
        if (childMergeCueAttribute != null) {
            mergeCueParams = getMergeCueParams(child, childMergeCueAttribute);
        } else {
            mergeCueParams = Collections.emptyMap();
        }
    }
    return new MergeCueContext(chosenMergeCue, parent, child, mergeCueParams);
}
Also used : Attribute(org.dom4j.Attribute) MergeCueContext(org.craftercms.core.xml.mergers.impl.cues.MergeCueContext) MergeCue(org.craftercms.core.xml.mergers.impl.cues.MergeCue)

Example 3 with MergeCueContext

use of org.craftercms.core.xml.mergers.impl.cues.MergeCueContext in project core by craftercms.

the class MergeParentAndChildMergeCue method merge.

@Override
@SuppressWarnings("unchecked")
public Element merge(Element parent, Element child, Map<String, String> params) throws XmlMergeException {
    Element merged = DocumentHelper.createElement(child.getQName());
    org.craftercms.core.util.CollectionUtils.move(child.attributes(), merged.attributes());
    if (parent.isTextOnly() && child.isTextOnly()) {
        String parentText = parent.getText();
        String childText = child.getText();
        if (getMergeOrder(params).equalsIgnoreCase("after")) {
            merged.setText(parentText + childText);
        } else {
            merged.setText(childText + parentText);
        }
    } else {
        List<Element> parentElements = parent.elements();
        List<Element> childElements = child.elements();
        List<Element> mergedElements = merged.elements();
        if (CollectionUtils.isNotEmpty(parentElements) && CollectionUtils.isNotEmpty(childElements)) {
            for (Iterator<Element> i = parentElements.iterator(); i.hasNext(); ) {
                Element parentElement = i.next();
                boolean elementsMerged = false;
                for (Iterator<Element> j = childElements.iterator(); !elementsMerged && j.hasNext(); ) {
                    Element childElement = j.next();
                    if (elementMergeMatcher.matchForMerge(parentElement, childElement)) {
                        MergeCueContext context = mergeCueResolver.getMergeCue(parentElement, childElement);
                        if (context != null) {
                            i.remove();
                            j.remove();
                            Element mergedElement = context.doMerge();
                            mergedElements.add(mergedElement);
                            elementsMerged = true;
                        } else {
                            throw new XmlMergeException("No merge cue was resolved for matching elements " + parentElement + " (parent) and " + childElement + " (child)");
                        }
                    }
                }
            }
        }
        if (getMergeOrder(params).equalsIgnoreCase("after")) {
            org.craftercms.core.util.CollectionUtils.move(parentElements, mergedElements);
            org.craftercms.core.util.CollectionUtils.move(childElements, mergedElements);
        } else {
            org.craftercms.core.util.CollectionUtils.move(childElements, mergedElements);
            org.craftercms.core.util.CollectionUtils.move(parentElements, mergedElements);
        }
    }
    return merged;
}
Also used : MergeCueContext(org.craftercms.core.xml.mergers.impl.cues.MergeCueContext) Element(org.dom4j.Element) XmlMergeException(org.craftercms.core.exception.XmlMergeException)

Example 4 with MergeCueContext

use of org.craftercms.core.xml.mergers.impl.cues.MergeCueContext in project core by craftercms.

the class MergeCueResolverImplTest method testResolver.

@Test
public void testResolver() throws Exception {
    Element parentElement1 = (Element) parentDoc.selectSingleNode("/root/element1");
    Element parentElement2 = (Element) parentDoc.selectSingleNode("/root/element2");
    Element parentElement3 = (Element) parentDoc.selectSingleNode("/root/element3");
    Element parentElement4 = (Element) parentDoc.selectSingleNode("/root/element4");
    Element childElement1 = (Element) childDoc.selectSingleNode("/root/element1");
    Element childElement2 = (Element) childDoc.selectSingleNode("/root/element2");
    Element childElement3 = (Element) childDoc.selectSingleNode("/root/element3");
    Element childElement4 = (Element) childDoc.selectSingleNode("/root/element4");
    MergeCueContext context = resolver.getMergeCue(parentElement1, childElement1);
    assertNotNull(context);
    assertSame(parentElement1, context.getParent());
    assertSame(childElement1, context.getChild());
    assertSame(hiPriorityParentMergeCue, context.getMergeCue());
    assertEquals(hiPriorityParentMergeCueParams, context.getMergeCueParams());
    context = resolver.getMergeCue(parentElement2, childElement2);
    assertNotNull(context);
    assertSame(parentElement2, context.getParent());
    assertSame(childElement2, context.getChild());
    assertSame(hiPriorityChildMergeCue, context.getMergeCue());
    assertEquals(hiPriorityChildMergeCueParams, context.getMergeCueParams());
    context = resolver.getMergeCue(parentElement3, childElement3);
    assertNotNull(context);
    assertSame(parentElement3, context.getParent());
    assertSame(childElement3, context.getChild());
    assertSame(hiPriorityParentMergeCue, context.getMergeCue());
    assertEquals(0, context.getMergeCueParams().size());
    context = resolver.getMergeCue(parentElement4, childElement4);
    assertNotNull(context);
    assertSame(parentElement4, context.getParent());
    assertSame(childElement4, context.getChild());
    assertSame(hiPriorityChildMergeCue, context.getMergeCue());
    assertEquals(0, context.getMergeCueParams().size());
}
Also used : MergeCueContext(org.craftercms.core.xml.mergers.impl.cues.MergeCueContext) Element(org.dom4j.Element) Test(org.junit.Test)

Aggregations

MergeCueContext (org.craftercms.core.xml.mergers.impl.cues.MergeCueContext)4 Element (org.dom4j.Element)3 XmlMergeException (org.craftercms.core.exception.XmlMergeException)1 MergeCue (org.craftercms.core.xml.mergers.impl.cues.MergeCue)1 MergeCueResolver (org.craftercms.core.xml.mergers.impl.cues.MergeCueResolver)1 Attribute (org.dom4j.Attribute)1 QName (org.dom4j.QName)1 Test (org.junit.Test)1