teichmann@7040: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@7040: * Software engineering by Intevation GmbH teichmann@7040: * teichmann@7040: * This file is Free Software under the GNU AGPL (>=v3) teichmann@7040: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@7040: * documentation coming with Dive4Elements River for details. teichmann@7040: */ teichmann@7040: teichmann@7040: package org.dive4elements.river.exports; teichmann@7040: teichmann@7040: import java.util.ArrayList; teichmann@7040: import java.util.List; teichmann@7040: teichmann@7040: import org.w3c.dom.Element; teichmann@7040: import org.w3c.dom.NodeList; teichmann@7040: teichmann@7040: import org.dive4elements.river.exports.process.Processor; teichmann@7040: teichmann@7040: import org.apache.log4j.Logger; teichmann@7040: teichmann@7048: import org.dive4elements.artifacts.CallContext; teichmann@7048: teichmann@7048: import org.dive4elements.river.artifacts.resources.Resources; teichmann@7048: import org.dive4elements.river.artifacts.D4EArtifact; teichmann@7048: teichmann@7040: public class DiagramAttributes teichmann@7040: { teichmann@7040: private static Logger log = Logger.getLogger(DiagramAttributes.class); teichmann@7040: teichmann@7048: public static class AxisAttributes { teichmann@7040: // TODO: More Attributes teichmann@7040: private String name; teichmann@7040: teichmann@7040: public AxisAttributes() { teichmann@7040: } teichmann@7040: teichmann@7040: public AxisAttributes(String name) { teichmann@7040: this.name = name; teichmann@7040: } teichmann@7040: teichmann@7040: public String getName() { teichmann@7040: return name; teichmann@7040: } teichmann@7040: } // class AxisAttributes teichmann@7040: teichmann@7048: public static class AxisProcessor { teichmann@7040: private Processor processor; teichmann@7040: private String axisName; teichmann@7040: teichmann@7040: public AxisProcessor() { teichmann@7040: } teichmann@7040: teichmann@7040: public AxisProcessor(Processor processor, String axisName) { teichmann@7040: this.processor = processor; teichmann@7040: this.axisName = axisName; teichmann@7040: } teichmann@7040: teichmann@7040: public Processor getProcessor() { teichmann@7040: return processor; teichmann@7040: } teichmann@7040: teichmann@7042: public String getAxisName() { teichmann@7040: return axisName; teichmann@7040: } teichmann@7040: } // class AxisProcessor teichmann@7040: teichmann@7048: public static class Argument { teichmann@7048: private String expression; teichmann@7048: private String type; teichmann@7048: teichmann@7048: public Argument() { teichmann@7048: } teichmann@7048: teichmann@7048: public Argument(String expression, String type) { teichmann@7048: this.expression = expression; teichmann@7048: this.type = type; teichmann@7048: } teichmann@7048: teichmann@7048: public Object evaluate(D4EArtifact artifact, CallContext context) { teichmann@7048: if (expression.startsWith("artifact.")) { teichmann@7048: String value = artifact.getDataAsString( teichmann@7048: expression.substring("artifact.".length())); teichmann@7048: return convert(value); teichmann@7048: } teichmann@7048: if (expression.startsWith("context.")) { teichmann@7048: return context.getContextValue( teichmann@7048: expression.substring("context.".length())); teichmann@7048: } teichmann@7048: return expression; teichmann@7048: } teichmann@7048: teichmann@7048: private Object convert(String value) { teichmann@7048: if (value == null || type == null) { teichmann@7048: return value; teichmann@7048: } teichmann@7048: if ("double".equals(type)) { teichmann@7048: return Double.valueOf(value); teichmann@7048: } teichmann@7048: if ("int".equals(type)) { teichmann@7048: return Integer.valueOf(value); teichmann@7048: } teichmann@7048: // TODO: more types teichmann@7048: return value; teichmann@7048: } teichmann@7048: } // class Argument teichmann@7048: teichmann@7048: public static class Title { teichmann@7048: teichmann@7048: private String key; teichmann@7048: private String def; teichmann@7048: private List arguments; teichmann@7048: teichmann@7048: public Title() { teichmann@7048: arguments = new ArrayList(5); teichmann@7048: } teichmann@7048: teichmann@7048: public Title(String key) { teichmann@7048: this(key, key); teichmann@7048: } teichmann@7048: teichmann@7048: public Title(String key, String def) { teichmann@7048: this(); teichmann@7048: this.key = key; teichmann@7048: this.def = def; teichmann@7048: } teichmann@7048: teichmann@7048: public String getKey() { teichmann@7048: return key; teichmann@7048: } teichmann@7048: teichmann@7048: public void addArgument(Argument argument) { teichmann@7048: arguments.add(argument); teichmann@7048: } teichmann@7048: teichmann@7048: public String evalute(D4EArtifact artifact, CallContext context) { teichmann@7048: if (key == null || key.isEmpty()) { teichmann@7048: return def; teichmann@7048: } teichmann@7048: Object [] args = new Object[arguments.size()]; teichmann@7048: for (int i = 0; i < args.length; ++i) { teichmann@7048: args[i] = arguments.get(i).evaluate(artifact, context); teichmann@7048: } teichmann@7048: return Resources.getMsg(context.getMeta(), key, def, args); teichmann@7048: } teichmann@7048: } // class Title teichmann@7048: teichmann@7040: private List axesAttrs; teichmann@7040: private List axesProcessors; teichmann@7040: teichmann@7048: private Title title; teichmann@7048: private Title subtitle; teichmann@7048: teichmann@7040: public DiagramAttributes() { teichmann@7040: axesAttrs = new ArrayList(5); teichmann@7040: axesProcessors = new ArrayList(5); teichmann@7040: } teichmann@7040: teichmann@7040: public DiagramAttributes(Element config) { teichmann@7040: this(); teichmann@7040: parseAxis(config); teichmann@7040: parseProcessors(config); teichmann@7048: parseTitle(config); teichmann@7040: } teichmann@7040: teichmann@7040: private void parseAxis(Element config) { teichmann@7040: NodeList axisNodes = config.getElementsByTagName("axis"); teichmann@7040: teichmann@7040: for (int i = 0, N = axisNodes.getLength(); i < N; ++i) { teichmann@7040: Element axisElement = (Element)axisNodes.item(i); teichmann@7040: String name = axisElement.getAttribute("name").trim(); teichmann@7040: if (!name.isEmpty()) { teichmann@7040: axesAttrs.add(new AxisAttributes(name)); teichmann@7040: } teichmann@7040: } teichmann@7040: } teichmann@7040: teichmann@7040: public List getAxesProcessors() { teichmann@7040: return axesProcessors; teichmann@7040: } teichmann@7040: teichmann@7048: public Title getTitle() { teichmann@7048: return title; teichmann@7048: } teichmann@7048: teichmann@7048: public Title getSubtitle() { teichmann@7048: return subtitle; teichmann@7048: } teichmann@7048: teichmann@7040: private void parseProcessors(Element config) { teichmann@7040: NodeList processorNodes = config.getElementsByTagName("processor"); teichmann@7040: teichmann@7040: for (int i = 0, N = processorNodes.getLength(); i < N; ++i) { teichmann@7040: Element processorElement = (Element)processorNodes.item(i); teichmann@7040: String className = processorElement.getAttribute("class").trim(); teichmann@7040: String axisName = processorElement.getAttribute("axis").trim(); teichmann@7040: if (className.isEmpty() || axisName.isEmpty()) { teichmann@7040: continue; teichmann@7040: } teichmann@7040: teichmann@7040: try { teichmann@7040: Processor processor = teichmann@7040: (Processor)Class.forName(className).newInstance(); teichmann@7040: axesProcessors.add(new AxisProcessor(processor, axisName)); teichmann@7040: } teichmann@7040: catch (ClassNotFoundException cnfe) { teichmann@7040: log.error(cnfe, cnfe); teichmann@7040: } teichmann@7040: catch (InstantiationException ie) { teichmann@7040: log.error(ie, ie); teichmann@7040: } teichmann@7040: catch (IllegalAccessException ia) { teichmann@7040: log.error(ia, ia); teichmann@7040: } teichmann@7040: catch (ClassCastException cce) { teichmann@7040: log.error(cce, cce); teichmann@7040: } teichmann@7040: } teichmann@7040: } teichmann@7048: teichmann@7048: private void parseTitle(Element config) { teichmann@7048: title = extractTitle(config, "title"); teichmann@7048: } teichmann@7048: teichmann@7048: private void parseSubtitle(Element config) { teichmann@7048: subtitle = extractTitle(config, "subtitle"); teichmann@7048: } teichmann@7048: teichmann@7048: private static Title extractTitle(Element config, String tagName) { teichmann@7048: NodeList titleNodes = config.getElementsByTagName(tagName); teichmann@7048: if (titleNodes.getLength() < 1) { teichmann@7048: return null; teichmann@7048: } teichmann@7048: Element titleElement = (Element)titleNodes.item(0); teichmann@7048: String key = titleElement.getAttribute("key"); teichmann@7048: String def = titleElement.getAttribute("default"); teichmann@7048: Title title = new Title(key, def); teichmann@7048: NodeList argumentNodes = titleElement.getElementsByTagName("arg"); teichmann@7048: for (int i = 0, N = argumentNodes.getLength(); i < N; ++i) { teichmann@7048: Element argumentElement = (Element)argumentNodes.item(i); teichmann@7048: String expression = argumentElement.getAttribute("expr"); teichmann@7048: String type = argumentElement.getAttribute("type"); teichmann@7048: title.addArgument(new Argument(expression, type)); teichmann@7048: } teichmann@7048: return title; teichmann@7048: } teichmann@7040: } teichmann@7040: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :