Update transformer

This commit is contained in:
Victor Shcherb 2014-09-03 19:04:08 +02:00
parent a6a2324640
commit 6451541daf

View file

@ -2,6 +2,8 @@ package net.osmand.render;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
@ -37,16 +39,76 @@ public class RenderingRulesTransformer {
StreamResult streamResult = new StreamResult(new File(targetFile)); StreamResult streamResult = new StreamResult(new File(targetFile));
transformer.transform(source, streamResult); transformer.transform(source, streamResult);
} }
static Map<String, Element> patterns = new HashMap<String, Element>();
public static void transform(Document document) { public static void transform(Document document) {
collectPatterns(document);
applyPatterns(document);
combineAllApplyTags(document);
replaceTag(document, "ifelse", "filter"); replaceTag(document, "ifelse", "filter");
replaceTag(document, "check", "filter"); replaceTag(document, "check", "filter");
replaceTag(document, "apply", "filter");
replaceTag(document, "check_and_apply", "filter"); replaceTag(document, "check_and_apply", "filter");
} }
private static void combineAllApplyTags(Document document) {
NodeList nl = document.getElementsByTagName("apply");
while(nl.getLength() > 0) {
Element app = (Element) nl.item(0);
Element parent = (Element) app.getParentNode();
NamedNodeMap attrs = app.getAttributes();
for(int i = 0; i < attrs.getLength(); i++) {
Node ns = attrs.item(i);
parent.setAttribute(ns.getNodeName(), ns.getNodeValue());
}
while(app.getChildNodes().getLength() > 0) {
Node ni = app.getChildNodes().item(0);
app.getParentNode().insertBefore(ni, app);
}
app.getParentNode().removeChild(app);
}
}
private static void applyPatterns(Document document) {
NodeList nl = document.getElementsByTagName("apply");
for (int i = 0; i < nl.getLength();) {
Element app = (Element) nl.item(i);
String pt = app.getAttribute("pattern");
if (!pt.equals("")) {
if (!patterns.containsKey(pt)) {
throw new IllegalStateException("Pattern '" + pt + "' is not defined");
}
Element patt = patterns.get(pt);
final NodeList pattChildren = patt.getChildNodes();
for(int ki = 0; ki < pattChildren.getLength(); ki++) {
Node ni = patt.getChildNodes().item(ki);
app.getParentNode().insertBefore(ni.cloneNode(true), app);
}
app.getParentNode().removeChild(app);
} else {
i++;
}
}
}
private static void collectPatterns(Document document) {
NodeList nl = document.getElementsByTagName("pattern");
while(nl.getLength() > 0) {
Element pt = (Element) nl.item(0);
String id = pt.getAttribute("id");
patterns.put(id, pt);
pt.getParentNode().removeChild(pt);
}
}
protected static void replaceTag(Document document, final String tag, final String targetTag) { protected static void replaceTag(Document document, final String tag, final String targetTag) {
NodeList nl = document.getElementsByTagName(tag); NodeList nl = document.getElementsByTagName(tag);
while(nl.getLength() > 0) { while(nl.getLength() > 0) {