Mercurial > dive4elements > river
view artifacts/src/main/java/org/dive4elements/river/exports/DiagramAttributes.java @ 7062:4310c612a986 generator-refactoring
Added 'include-zero' attribute to <axis> tag in diagram configuration.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Fri, 20 Sep 2013 12:35:14 +0200 |
parents | 4f6b3ba542de |
children | 726d998dce29 |
line wrap: on
line source
/* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde * Software engineering by Intevation GmbH * * This file is Free Software under the GNU AGPL (>=v3) * and comes with ABSOLUTELY NO WARRANTY! Check out the * documentation coming with Dive4Elements River for details. */ package org.dive4elements.river.exports; import java.util.ArrayList; import java.util.List; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.dive4elements.river.exports.process.Processor; import org.apache.log4j.Logger; import org.dive4elements.artifacts.CallContext; import org.dive4elements.river.artifacts.resources.Resources; import org.dive4elements.river.artifacts.D4EArtifact; public class DiagramAttributes { private static Logger log = Logger.getLogger(DiagramAttributes.class); public static class AxisAttributes { private String name; private boolean isLeftAlign; private boolean forceAlign; private boolean includeZero; public AxisAttributes() { } public AxisAttributes( String name, boolean isLeftAlign, boolean forceAlign, boolean includeZero ) { this.name = name; this.isLeftAlign = isLeftAlign; this.forceAlign = forceAlign; this.includeZero = includeZero; } public String getName() { return name; } public boolean isLeftAlign() { return isLeftAlign; } public boolean forceAlign() { return forceAlign; } public boolean includeZero() { return includeZero; } } // class AxisAttributes public static class Argument { private String expression; private String type; public Argument() { } public Argument(String expression, String type) { this.expression = expression; this.type = type; } public Object evaluate(D4EArtifact artifact, CallContext context) { if (expression.startsWith("artifact.")) { String value = artifact.getDataAsString( expression.substring("artifact.".length())); return convert(value); } if (expression.startsWith("context.")) { return context.getContextValue( expression.substring("context.".length())); } return expression; } private Object convert(String value) { if (value == null || type == null) { return value; } if ("double".equals(type)) { return Double.valueOf(value); } if ("int".equals(type)) { return Integer.valueOf(value); } // TODO: more types return value; } } // class Argument public static class Title { private String key; private String def; private List<Argument> arguments; public Title() { arguments = new ArrayList<Argument>(5); } public Title(String key) { this(key, key); } public Title(String key, String def) { this(); this.key = key; this.def = def; } public String getKey() { return key; } public void addArgument(Argument argument) { arguments.add(argument); } public String evaluate(D4EArtifact artifact, CallContext context) { if (key == null || key.isEmpty()) { return def; } Object [] args = new Object[arguments.size()]; for (int i = 0; i < args.length; ++i) { args[i] = arguments.get(i).evaluate(artifact, context); } return Resources.getMsg(context.getMeta(), key, def, args); } } // class Title private List<AxisAttributes> axesAttrs; private List<Processor> processors; private Title title; private Title subtitle; private Title domainAxisTitle; public DiagramAttributes() { axesAttrs = new ArrayList<AxisAttributes>(5); processors = new ArrayList<Processor>(5); } public DiagramAttributes(Element config) { this(); parseAxis(config); parseProcessors(config); parseTitle(config); parseSubtitle(config); parseDomainAxisTitle(config); } public List<AxisAttributes> getAxesAttributes() { return axesAttrs; } private void parseAxis(Element config) { NodeList axisNodes = config.getElementsByTagName("axis"); for (int i = 0, N = axisNodes.getLength(); i < N; ++i) { Element axisElement = (Element)axisNodes.item(i); String name = axisElement.getAttribute("name").trim(); String align = axisElement.getAttribute("align").trim(); String includeZero = axisElement.getAttribute("include-zero").trim(); if (name.isEmpty()) { continue; } boolean isleftAlign = false; boolean forceAlign = false; for (String part: align.split("[\\s,]")) { part = part.trim(); if ("left" .equals(part)) isleftAlign = true; else if ("right".equals(part)) isleftAlign = false; else if ("force".equals(part)) forceAlign = true; } axesAttrs.add(new AxisAttributes( name, isleftAlign, forceAlign, includeZero.equals("true"))); } } public List<Processor> getProcessors() { return processors; } public Title getTitle() { return title; } public Title getSubtitle() { return subtitle; } public Title getDomainAxisTitle() { return domainAxisTitle; } private void parseProcessors(Element config) { NodeList processorNodes = config.getElementsByTagName("processor"); for (int i = 0, N = processorNodes.getLength(); i < N; ++i) { Element processorElement = (Element)processorNodes.item(i); String className = processorElement.getAttribute("class").trim(); String axisName = processorElement.getAttribute("axis").trim(); if (className.isEmpty() || axisName.isEmpty()) { continue; } try { Processor processor = (Processor)Class.forName(className).newInstance(); processor.setAxisName(axisName); processors.add(processor); } catch (ClassNotFoundException cnfe) { log.error(cnfe, cnfe); } catch (InstantiationException ie) { log.error(ie, ie); } catch (IllegalAccessException ia) { log.error(ia, ia); } catch (ClassCastException cce) { log.error(cce, cce); } } } private void parseTitle(Element config) { title = extractTitle(config, "title"); } private void parseSubtitle(Element config) { subtitle = extractTitle(config, "subtitle"); } private void parseDomainAxisTitle(Element config) { domainAxisTitle = extractTitle(config, "domain-axis"); } private static Title extractTitle(Element config, String tagName) { NodeList titleNodes = config.getElementsByTagName(tagName); if (titleNodes.getLength() < 1) { return null; } Element titleElement = (Element)titleNodes.item(0); String key = titleElement.getAttribute("key"); String def = titleElement.getAttribute("default"); Title title = new Title(key, def); NodeList argumentNodes = titleElement.getElementsByTagName("arg"); for (int i = 0, N = argumentNodes.getLength(); i < N; ++i) { Element argumentElement = (Element)argumentNodes.item(i); String expression = argumentElement.getAttribute("expr"); String type = argumentElement.getAttribute("type"); title.addArgument(new Argument(expression, type)); } return title; } public int getAxisIndex(String axisName) { for (int i = axesAttrs.size()-1; i >= 0; --i) { if (axesAttrs.get(i).getName().equals(axisName)) { return i; } } return -1; } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :