Last active
August 29, 2015 14:11
-
-
Save lantoli/6b8e3ec653b2886d176c to your computer and use it in GitHub Desktop.
LoopExtension.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.experimental; | |
import java.util.Stack; | |
import nu.xom.Attribute; | |
import nu.xom.Document; | |
import nu.xom.Element; | |
import nu.xom.Elements; | |
import org.concordion.api.Evaluator; | |
import org.concordion.api.extension.ConcordionExtender; | |
import org.concordion.api.extension.ConcordionExtension; | |
import org.concordion.api.listener.DocumentParsingListener; | |
import org.concordion.internal.SimpleEvaluator; | |
import org.concordion.internal.util.Check; | |
public class LoopExtension implements ConcordionExtension, DocumentParsingListener { | |
public static final String EXTENSION_NAMESPACE = "urn:concordion-extensions:2010"; | |
public static final String COMMAND_NAME = "loop"; | |
private final Evaluator evaluator; | |
@Override | |
public void addTo(final ConcordionExtender concordionExtender) { | |
concordionExtender.withDocumentParsingListener(this); | |
} | |
public LoopExtension(Object fixture) { | |
evaluator = new SimpleEvaluator(fixture); | |
} | |
@Override | |
public void beforeParsing(Document document) { | |
Stack<Element> all = new Stack<Element>(); | |
all.add(document.getRootElement()); | |
while(!all.isEmpty()) { | |
Element parent = all.pop(); | |
Elements children = parent.getChildElements(); | |
for (int i = 0; i < children.size(); i++) { | |
Element child = children.get(i); | |
Attribute attr = child.getAttribute(COMMAND_NAME, EXTENSION_NAMESPACE); | |
if (attr == null) { | |
all.add(child); | |
} else { | |
String expression = attr.getValue(); | |
Object result = evaluator.evaluate(expression); | |
Check.isTrue(result != null && result instanceof Integer, | |
"Loop extension expects an expression evaluated to Integer: " + expression); | |
int times = (Integer) result; | |
child.removeAttribute(attr); | |
int index = parent.indexOf(child); | |
for (int j = 0; j < times; j++) { | |
parent.insertChild(new Element(child), index + 1); | |
} | |
child.detach(); | |
} | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.experimental; | |
import org.concordion.api.AbstractCommand; | |
import org.concordion.api.CommandCall; | |
import org.concordion.api.Evaluator; | |
import org.concordion.api.ResultRecorder; | |
import org.concordion.api.extension.ConcordionExtender; | |
import org.concordion.api.extension.ConcordionExtension; | |
import org.concordion.internal.util.Check; | |
public class AttributeExtension extends AbstractCommand implements ConcordionExtension { | |
public static final String EXTENSION_NAMESPACE = "urn:concordion-extensions:2010"; | |
public static final String COMMAND_NAME = "attribute"; | |
@Override | |
public void addTo(final ConcordionExtender concordionExtender) { | |
concordionExtender.withCommand(EXTENSION_NAMESPACE, COMMAND_NAME, this); | |
} | |
@Override | |
public void verify(CommandCall commandCall, Evaluator evaluator, ResultRecorder resultRecorder) { | |
String expressions = commandCall.getExpression(); | |
for (String expression : expressions.split(",")) { | |
int pos = expression.indexOf("="); | |
Check.isTrue(pos >= 0, "Must have attributeName=attributeValue"); | |
String attributeName = expression.substring(0, pos); | |
String attributeExpression = expression.substring(pos + 1); | |
String result = evaluator.evaluate(attributeExpression).toString(); | |
if (result != null && !result.isEmpty()) { | |
commandCall.getElement().addAttribute(attributeName, result); | |
} | |
} | |
commandCall.getChildren().processSequentially(evaluator, resultRecorder); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment