Update rendering constants

This commit is contained in:
Victor Shcherb 2012-10-07 14:38:19 +02:00
parent d2b65e4a0f
commit c66c916d18
2 changed files with 77 additions and 34 deletions

View file

@ -68,7 +68,11 @@ public class RenderingRulesStorage {
this.renderingName = name; this.renderingName = name;
} }
public RenderingRulesStorage(String name, Map<String, String> renderingConstants){
if(renderingConstants != null) {
this.renderingConstants.putAll(renderingConstants);
}
}
public int getDictionaryValue(String val) { public int getDictionaryValue(String val) {
if(dictionaryMap.containsKey(val)){ if(dictionaryMap.containsKey(val)){
@ -320,7 +324,9 @@ public class RenderingRulesStorage {
} }
PROPS.registerRule(prop); PROPS.registerRule(prop);
} else if("renderingConstant".equals(name)){ //$NON-NLS-1$ } else if("renderingConstant".equals(name)){ //$NON-NLS-1$
renderingConstants.put(attributes.getValue("name"), attributes.getValue("value")); if(!renderingConstants.containsKey(attributes.getValue("name"))){
renderingConstants.put(attributes.getValue("name"), attributes.getValue("value"));
}
} else if("renderingStyle".equals(name)){ //$NON-NLS-1$ } else if("renderingStyle".equals(name)){ //$NON-NLS-1$
String depends = attributes.getValue("depends"); String depends = attributes.getValue("depends");
if(depends != null && depends.length()> 0){ if(depends != null && depends.length()> 0){

View file

@ -2,20 +2,26 @@ package net.osmand.plus.render;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.Collection; import java.util.Collection;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.Map; import java.util.Map;
import java.util.Set;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import net.osmand.LogUtil; import net.osmand.LogUtil;
import net.osmand.render.RenderingRulesStorage; import net.osmand.render.RenderingRulesStorage;
import net.osmand.render.RenderingRulesStorage.RenderingRulesStorageResolver; import net.osmand.render.RenderingRulesStorage.RenderingRulesStorageResolver;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class RendererRegistry { public class RendererRegistry {
@ -53,16 +59,8 @@ public class RendererRegistry {
if(!hasRender(name)){ if(!hasRender(name)){
return null; return null;
} }
return getRenderer(name, new LinkedHashSet<String>());
}
private boolean hasRender(String name) {
return externalRenderers.containsKey(name) || internalRenderers.containsKey(name);
}
private RenderingRulesStorage getRenderer(String name, Set<String> loadedRenderers) {
try { try {
return loadRenderer(name, loadedRenderers); return loadRenderer(name, new LinkedHashMap<String, RenderingRulesStorage>(), new LinkedHashMap<String, String>());
} catch (IOException e) { } catch (IOException e) {
log.error("Error loading renderer", e); //$NON-NLS-1$ log.error("Error loading renderer", e); //$NON-NLS-1$
} catch (SAXException e) { } catch (SAXException e) {
@ -71,7 +69,66 @@ public class RendererRegistry {
return null; return null;
} }
private RenderingRulesStorage loadRenderer(String name, final Set<String> loadedRenderers) throws IOException, SAXException { private boolean hasRender(String name) {
return externalRenderers.containsKey(name) || internalRenderers.containsKey(name);
}
private RenderingRulesStorage loadRenderer(String name, final Map<String, RenderingRulesStorage> loadedRenderers,
final Map<String, String> renderingConstants) throws IOException, SAXException {
InputStream is = getInputStream(name);
try {
final SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
saxParser.parse(is, new DefaultHandler() {
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
String tagName = saxParser.isNamespaceAware() ? localName : qName;
if ("renderingConstant".equals(tagName)) { //$NON-NLS-1$
if (!renderingConstants.containsKey(attributes.getValue("name"))) {
renderingConstants.put(attributes.getValue("name"), attributes.getValue("value"));
}
}
}
});
} catch (ParserConfigurationException e1) {
throw new IllegalStateException(e1);
} finally {
is.close();
}
// parse content
is = getInputStream(name);
final RenderingRulesStorage main = new RenderingRulesStorage(name, renderingConstants);
loadedRenderers.put(name, main);
try {
main.parseRulesFromXmlInputStream(is, new RenderingRulesStorageResolver() {
@Override
public RenderingRulesStorage resolve(String name, RenderingRulesStorageResolver ref) throws SAXException {
// reload every time to propogate rendering constants
if (loadedRenderers.containsKey(name)) {
log.warn("Circular dependencies found " + name); //$NON-NLS-1$
}
RenderingRulesStorage dep = null;
try {
dep = loadRenderer(name, loadedRenderers, renderingConstants);
} catch (IOException e) {
log.warn("Dependent renderer not found : " + e.getMessage(), e); //$NON-NLS-1$
}
if (dep == null) {
log.warn("Dependent renderer not found : " + name); //$NON-NLS-1$
}
return dep;
}
});
} finally {
is.close();
}
renderers.put(name, main);
return main;
}
private InputStream getInputStream(String name) throws FileNotFoundException {
InputStream is = null; InputStream is = null;
if(externalRenderers.containsKey(name)){ if(externalRenderers.containsKey(name)){
is = new FileInputStream(externalRenderers.get(name)); is = new FileInputStream(externalRenderers.get(name));
@ -80,27 +137,7 @@ public class RendererRegistry {
} else { } else {
throw new IllegalArgumentException("Not found " + name); //$NON-NLS-1$ throw new IllegalArgumentException("Not found " + name); //$NON-NLS-1$
} }
RenderingRulesStorage main = new RenderingRulesStorage(name); return is;
loadedRenderers.add(name);
main.parseRulesFromXmlInputStream(is, new RenderingRulesStorageResolver() {
@Override
public RenderingRulesStorage resolve(String name, RenderingRulesStorageResolver ref) throws SAXException {
if(renderers.containsKey(name)){
return renderers.get(name);
}
if (loadedRenderers.contains(name)) {
log.warn("Circular dependencies found " + name); //$NON-NLS-1$
}
RenderingRulesStorage dep = getRenderer(name, loadedRenderers);
if (dep == null) {
log.warn("Dependent renderer not found : " + name); //$NON-NLS-1$
}
return dep;
}
});
renderers.put(name, main);
return main;
} }