| 1 |
package org.kravemir.svg.labels; |
| 2 |
|
| 3 |
import org.kravemir.svg.labels.model.LabelTemplateDescriptor; |
| 4 |
import org.kravemir.svg.labels.utils.ExpressionEvaluator; |
| 5 |
import org.kravemir.svg.labels.utils.RenderingUtils; |
| 6 |
import org.w3c.dom.Node; |
| 7 |
import org.w3c.dom.NodeList; |
| 8 |
import org.w3c.dom.svg.SVGDocument; |
| 9 |
|
| 10 |
import javax.xml.xpath.*; |
| 11 |
import java.util.Map; |
| 12 |
import java.util.stream.IntStream; |
| 13 |
import java.util.stream.Stream; |
| 14 |
|
| 15 |
public class InstanceRendererImpl implements InstanceRenderer { |
| 16 |
|
| 17 |
private final ExpressionEvaluator expressionEvaluator = new ExpressionEvaluator(); |
| 18 |
private final XPath xpath = XPathFactory.newInstance().newXPath(); |
| 19 |
private final XPathExpression tspanXPath; |
| 20 |
|
| 21 |
public InstanceRendererImpl() { |
| 22 |
try { |
| 23 |
tspanXPath = xpath.compile("*[local-name()='tspan']"); |
| 24 |
} catch (XPathException e) { |
| 25 |
throw new RuntimeException("This shouldn't have happened!", e); |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
@Override |
| 30 |
public String render(String svgTemplate, |
| 31 |
LabelTemplateDescriptor templateDescriptor, |
| 32 |
Map<String, String> instanceContent) { |
| 33 |
|
| 34 |
final SVGDocument document = RenderingUtils.parseSVG(svgTemplate); |
| 35 |
|
| 36 |
for (LabelTemplateDescriptor.ContentReplaceRule rule : templateDescriptor.getContentReplaceRules()) { |
| 37 |
XPathExpression elementXPath; |
| 38 |
try { |
| 39 |
elementXPath = xpath.compile(rule.getElementXPath()); |
| 40 |
} catch (XPathExpressionException e) { |
| 41 |
// TODO: clean-up code |
| 42 |
throw new RuntimeException("This should not happen!", e); |
| 43 |
} |
| 44 |
|
| 45 |
String value = expressionEvaluator.evaluateExpression(rule.getValue(), instanceContent); |
| 46 |
String[] valueLines = value.split("\n"); |
| 47 |
|
| 48 |
if (!shouldEvaluate(instanceContent, rule)) |
| 49 |
continue; |
| 50 |
|
| 51 |
getNodeStream(document.getRootElement(), elementXPath).forEach(node -> { |
| 52 |
replaceNodeTextContents(node, valueLines); |
| 53 |
}); |
| 54 |
} |
| 55 |
|
| 56 |
return RenderingUtils.documentToString(document); |
| 57 |
} |
| 58 |
|
| 59 |
private void replaceNodeTextContents(Node node, String[] valueLines) { |
| 60 |
NodeList spanNodes = getNodes(node, tspanXPath); |
| 61 |
int line = 0; |
| 62 |
for(; line < Math.min(spanNodes.getLength(), valueLines.length); line++ ) { |
| 63 |
Node tspanNode = spanNodes.item(line); |
| 64 |
tspanNode.setTextContent(valueLines[line]); |
| 65 |
} |
| 66 |
for(; line < spanNodes.getLength(); line++ ) { |
| 67 |
Node tspanNode = spanNodes.item(line); |
| 68 |
tspanNode.setTextContent(""); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
private boolean shouldEvaluate(Map<String, String> instanceContent, LabelTemplateDescriptor.ContentReplaceRule rule) { |
| 73 |
if(rule.getIf() == null || "".equals(rule.getIf().trim())) |
| 74 |
return true; |
| 75 |
|
| 76 |
return (boolean) expressionEvaluator.evaluateExpressionWithJEXL(rule.getIf(), instanceContent); |
| 77 |
} |
| 78 |
|
| 79 |
private Stream<Node> getNodeStream(Node root, XPathExpression expression){ |
| 80 |
NodeList nodes = getNodes(root, expression); |
| 81 |
return IntStream.range(0, nodes.getLength()).mapToObj(nodes::item); |
| 82 |
} |
| 83 |
|
| 84 |
private NodeList getNodes(Node root, XPathExpression expression){ |
| 85 |
try { |
| 86 |
return (NodeList) expression.evaluate(root, XPathConstants.NODESET); |
| 87 |
} catch (XPathExpressionException e) { |
| 88 |
throw new RuntimeException(e); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
} |