OsmAnd/OsmAnd-java/src/net/osmand/render/RenderingRulesTransformer.java

134 lines
4.2 KiB
Java
Raw Normal View History

2014-09-03 02:35:14 +02:00
package net.osmand.render;
import java.io.File;
import java.io.IOException;
2014-09-03 19:04:08 +02:00
import java.util.HashMap;
import java.util.Map;
2014-09-03 02:35:14 +02:00
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class RenderingRulesTransformer {
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, TransformerException {
if(args.length == 0) {
System.out.println("Please specify source and target file path.");
return;
}
String srcFile = args[0];
String targetFile = args[1];
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = db.parse(new File(srcFile));
transform(document);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(document.getDocumentElement());
StreamResult streamResult = new StreamResult(new File(targetFile));
transformer.transform(source, streamResult);
}
2014-09-03 19:04:08 +02:00
2014-09-03 02:35:14 +02:00
2014-09-03 19:04:08 +02:00
static Map<String, Element> patterns = new HashMap<String, Element>();
2014-09-03 02:35:14 +02:00
public static void transform(Document document) {
2014-09-03 19:04:08 +02:00
collectPatterns(document);
applyPatterns(document);
combineAllApplyTags(document);
2014-09-03 11:33:30 +02:00
replaceTag(document, "ifelse", "filter");
replaceTag(document, "check", "filter");
replaceTag(document, "check_and_apply", "filter");
}
2014-09-03 19:04:08 +02:00
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);
}
}
2014-09-03 11:33:30 +02:00
protected static void replaceTag(Document document, final String tag, final String targetTag) {
NodeList nl = document.getElementsByTagName(tag);
2014-09-03 02:35:14 +02:00
while(nl.getLength() > 0) {
2014-09-03 11:33:30 +02:00
Element newElement = document.createElement(targetTag);
2014-09-03 02:35:14 +02:00
Element old = (Element) nl.item(0);
2014-09-03 11:33:30 +02:00
copyAndReplaceElement(old, newElement);
}
}
protected static void copyAndReplaceElement(Element oldElement, Element newElement) {
while(oldElement.getChildNodes().getLength() > 0) {
newElement.appendChild(oldElement.getChildNodes().item(0));
}
NamedNodeMap attrs = oldElement.getAttributes();
for(int i = 0; i < attrs.getLength(); i++) {
Node ns = attrs.item(i);
newElement.setAttribute(ns.getNodeName(), ns.getNodeValue());
2014-09-03 02:35:14 +02:00
}
2014-09-03 11:33:30 +02:00
((Element)oldElement.getParentNode()).replaceChild(newElement, oldElement);
2014-09-03 02:35:14 +02:00
}
}