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);
}
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);
}
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;
}
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());
}
Aggregations