Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/exports/DiagramAttributes.java @ 7048:a43137dfdcac generator-refactoring
Simple interpolation logic for diagram titles and subtitles.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 19 Sep 2013 10:57:47 +0200 |
parents | 599d3c48474c |
children | 165ea04b1545 |
comparison
equal
deleted
inserted
replaced
7044:6ab1464021ae | 7048:a43137dfdcac |
---|---|
16 | 16 |
17 import org.dive4elements.river.exports.process.Processor; | 17 import org.dive4elements.river.exports.process.Processor; |
18 | 18 |
19 import org.apache.log4j.Logger; | 19 import org.apache.log4j.Logger; |
20 | 20 |
21 import org.dive4elements.artifacts.CallContext; | |
22 | |
23 import org.dive4elements.river.artifacts.resources.Resources; | |
24 import org.dive4elements.river.artifacts.D4EArtifact; | |
25 | |
21 public class DiagramAttributes | 26 public class DiagramAttributes |
22 { | 27 { |
23 private static Logger log = Logger.getLogger(DiagramAttributes.class); | 28 private static Logger log = Logger.getLogger(DiagramAttributes.class); |
24 | 29 |
25 public class AxisAttributes { | 30 public static class AxisAttributes { |
26 // TODO: More Attributes | 31 // TODO: More Attributes |
27 private String name; | 32 private String name; |
28 | 33 |
29 public AxisAttributes() { | 34 public AxisAttributes() { |
30 } | 35 } |
36 public String getName() { | 41 public String getName() { |
37 return name; | 42 return name; |
38 } | 43 } |
39 } // class AxisAttributes | 44 } // class AxisAttributes |
40 | 45 |
41 public class AxisProcessor { | 46 public static class AxisProcessor { |
42 private Processor processor; | 47 private Processor processor; |
43 private String axisName; | 48 private String axisName; |
44 | 49 |
45 public AxisProcessor() { | 50 public AxisProcessor() { |
46 } | 51 } |
57 public String getAxisName() { | 62 public String getAxisName() { |
58 return axisName; | 63 return axisName; |
59 } | 64 } |
60 } // class AxisProcessor | 65 } // class AxisProcessor |
61 | 66 |
67 public static class Argument { | |
68 private String expression; | |
69 private String type; | |
70 | |
71 public Argument() { | |
72 } | |
73 | |
74 public Argument(String expression, String type) { | |
75 this.expression = expression; | |
76 this.type = type; | |
77 } | |
78 | |
79 public Object evaluate(D4EArtifact artifact, CallContext context) { | |
80 if (expression.startsWith("artifact.")) { | |
81 String value = artifact.getDataAsString( | |
82 expression.substring("artifact.".length())); | |
83 return convert(value); | |
84 } | |
85 if (expression.startsWith("context.")) { | |
86 return context.getContextValue( | |
87 expression.substring("context.".length())); | |
88 } | |
89 return expression; | |
90 } | |
91 | |
92 private Object convert(String value) { | |
93 if (value == null || type == null) { | |
94 return value; | |
95 } | |
96 if ("double".equals(type)) { | |
97 return Double.valueOf(value); | |
98 } | |
99 if ("int".equals(type)) { | |
100 return Integer.valueOf(value); | |
101 } | |
102 // TODO: more types | |
103 return value; | |
104 } | |
105 } // class Argument | |
106 | |
107 public static class Title { | |
108 | |
109 private String key; | |
110 private String def; | |
111 private List<Argument> arguments; | |
112 | |
113 public Title() { | |
114 arguments = new ArrayList<Argument>(5); | |
115 } | |
116 | |
117 public Title(String key) { | |
118 this(key, key); | |
119 } | |
120 | |
121 public Title(String key, String def) { | |
122 this(); | |
123 this.key = key; | |
124 this.def = def; | |
125 } | |
126 | |
127 public String getKey() { | |
128 return key; | |
129 } | |
130 | |
131 public void addArgument(Argument argument) { | |
132 arguments.add(argument); | |
133 } | |
134 | |
135 public String evalute(D4EArtifact artifact, CallContext context) { | |
136 if (key == null || key.isEmpty()) { | |
137 return def; | |
138 } | |
139 Object [] args = new Object[arguments.size()]; | |
140 for (int i = 0; i < args.length; ++i) { | |
141 args[i] = arguments.get(i).evaluate(artifact, context); | |
142 } | |
143 return Resources.getMsg(context.getMeta(), key, def, args); | |
144 } | |
145 } // class Title | |
146 | |
62 private List<AxisAttributes> axesAttrs; | 147 private List<AxisAttributes> axesAttrs; |
63 private List<AxisProcessor> axesProcessors; | 148 private List<AxisProcessor> axesProcessors; |
149 | |
150 private Title title; | |
151 private Title subtitle; | |
64 | 152 |
65 public DiagramAttributes() { | 153 public DiagramAttributes() { |
66 axesAttrs = new ArrayList<AxisAttributes>(5); | 154 axesAttrs = new ArrayList<AxisAttributes>(5); |
67 axesProcessors = new ArrayList<AxisProcessor>(5); | 155 axesProcessors = new ArrayList<AxisProcessor>(5); |
68 } | 156 } |
69 | 157 |
70 public DiagramAttributes(Element config) { | 158 public DiagramAttributes(Element config) { |
71 this(); | 159 this(); |
72 parseAxis(config); | 160 parseAxis(config); |
73 parseProcessors(config); | 161 parseProcessors(config); |
162 parseTitle(config); | |
74 } | 163 } |
75 | 164 |
76 private void parseAxis(Element config) { | 165 private void parseAxis(Element config) { |
77 NodeList axisNodes = config.getElementsByTagName("axis"); | 166 NodeList axisNodes = config.getElementsByTagName("axis"); |
78 | 167 |
85 } | 174 } |
86 } | 175 } |
87 | 176 |
88 public List<AxisProcessor> getAxesProcessors() { | 177 public List<AxisProcessor> getAxesProcessors() { |
89 return axesProcessors; | 178 return axesProcessors; |
179 } | |
180 | |
181 public Title getTitle() { | |
182 return title; | |
183 } | |
184 | |
185 public Title getSubtitle() { | |
186 return subtitle; | |
90 } | 187 } |
91 | 188 |
92 private void parseProcessors(Element config) { | 189 private void parseProcessors(Element config) { |
93 NodeList processorNodes = config.getElementsByTagName("processor"); | 190 NodeList processorNodes = config.getElementsByTagName("processor"); |
94 | 191 |
117 catch (ClassCastException cce) { | 214 catch (ClassCastException cce) { |
118 log.error(cce, cce); | 215 log.error(cce, cce); |
119 } | 216 } |
120 } | 217 } |
121 } | 218 } |
219 | |
220 private void parseTitle(Element config) { | |
221 title = extractTitle(config, "title"); | |
222 } | |
223 | |
224 private void parseSubtitle(Element config) { | |
225 subtitle = extractTitle(config, "subtitle"); | |
226 } | |
227 | |
228 private static Title extractTitle(Element config, String tagName) { | |
229 NodeList titleNodes = config.getElementsByTagName(tagName); | |
230 if (titleNodes.getLength() < 1) { | |
231 return null; | |
232 } | |
233 Element titleElement = (Element)titleNodes.item(0); | |
234 String key = titleElement.getAttribute("key"); | |
235 String def = titleElement.getAttribute("default"); | |
236 Title title = new Title(key, def); | |
237 NodeList argumentNodes = titleElement.getElementsByTagName("arg"); | |
238 for (int i = 0, N = argumentNodes.getLength(); i < N; ++i) { | |
239 Element argumentElement = (Element)argumentNodes.item(i); | |
240 String expression = argumentElement.getAttribute("expr"); | |
241 String type = argumentElement.getAttribute("type"); | |
242 title.addArgument(new Argument(expression, type)); | |
243 } | |
244 return title; | |
245 } | |
122 } | 246 } |
123 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : | 247 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |