comparison flys-artifacts/src/main/java/de/intevation/flys/utils/ThemeUtil.java @ 3651:06a65baae494

merged flys-artifacts/2.9
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:43 +0200
parents 84a19de5f16a
children 34da25796c21
comparison
equal deleted inserted replaced
3549:6a8f83c538e3 3651:06a65baae494
1 package de.intevation.flys.utils;
2
3 import de.intevation.artifacts.common.utils.XMLUtils;
4 import de.intevation.flys.artifacts.model.MapserverStyle;
5 import de.intevation.flys.artifacts.model.MapserverStyle.Clazz;
6 import de.intevation.flys.artifacts.model.MapserverStyle.Expression;
7 import de.intevation.flys.artifacts.model.MapserverStyle.Label;
8 import de.intevation.flys.artifacts.model.MapserverStyle.Style;
9
10 import java.awt.Color;
11 import java.awt.Font;
12
13 import javax.xml.xpath.XPathConstants;
14
15 import org.apache.log4j.Logger;
16 import org.w3c.dom.Document;
17 import org.w3c.dom.Element;
18 import org.w3c.dom.NodeList;
19
20
21 /**
22 * Utility to deal with themes and their representations.
23 */
24 public class ThemeUtil {
25
26 /** Private logger. */
27 private static Logger logger =
28 Logger.getLogger(ThemeUtil.class);
29
30 public final static String XPATH_FILL_COLOR =
31 "/theme/field[@name='fillcolor']/@default";
32
33 public final static String XPATH_LINE_COLOR =
34 "/theme/field[@name='linecolor']/@default";
35
36 public static final String XPATH_LINE_SIZE =
37 "/theme/field[@name='linesize']/@default";
38
39 public static final String XPATH_LINE_STYLE =
40 "/theme/field[@name='linetype']/@default";
41
42 public static final String XPATH_POINT_SIZE =
43 "/theme/field[@name='pointsize']/@default";
44
45 public static final String XPATH_POINT_COLOR =
46 "/theme/field[@name='pointcolor']/@default";
47
48 public final static String XPATH_SHOW_BORDER =
49 "/theme/field[@name='showborder']/@default";
50
51 public final static String XPATH_SHOW_POINTS =
52 "/theme/field[@name='showpoints']/@default";
53
54 public final static String XPATH_SHOW_LINE =
55 "/theme/field[@name='showlines']/@default";
56
57 public final static String XPATH_SHOW_VERTICAL_LINE =
58 "/theme/field[@name='showverticalline']/@default";
59
60 public final static String XPATH_SHOW_HORIZONTAL_LINE =
61 "/theme/field[@name='showhorizontalline']/@default";
62
63 public final static String XPATH_SHOW_LINE_LABEL =
64 "/theme/field[@name='showlinelabel']/@default";
65
66 public final static String XPATH_SHOW_POINT_LABEL =
67 "/theme/field[@name='showpointlabel']/@default";
68
69 public final static String XPATH_SHOW_WIDTH =
70 "/theme/field[@name='showwidth']/@default";
71
72 public final static String XPATH_SHOW_LEVEL =
73 "/theme/field[@name='showlevel']/@default";
74
75 public final static String XPATH_TRANSPARENCY =
76 "/theme/field[@name='transparent']/@default";
77
78 public final static String XPATH_TRANSPARENCY_ALPHA =
79 "/theme/field[@name='alpha']/@default";
80
81 public final static String XPATH_SHOW_AREA =
82 "/theme/field[@name='showarea']/@default";
83
84 public final static String XPATH_SHOW_MIDDLE_HEIGHT =
85 "/theme/field[@name='showmiddleheight']/@default";
86
87 public final static String XPATH_LABEL_FONT_COLOR =
88 "/theme/field[@name='labelfontcolor']/@default";
89
90 public final static String XPATH_LABEL_FONT_SIZE =
91 "/theme/field[@name='labelfontsize']/@default";
92
93 public final static String XPATH_LABEL_FONT_FACE =
94 "/theme/field[@name='labelfontface']/@default";
95
96 public final static String XPATH_LABEL_FONT_STYLE =
97 "/theme/field[@name='labelfontstyle']/@default";
98
99 public final static String XPATH_TEXT_ORIENTATION =
100 "/theme/field[@name='textorientation']/@default";
101
102 public final static String XPATH_LABEL_BGCOLOR =
103 "/theme/field[@name='labelbgcolor']/@default";
104
105 public final static String XPATH_LABEL_SHOW_BACKGROUND =
106 "/theme/field[@name='labelshowbg']/@default";
107
108 public final static String XPATH_BACKGROUND_COLOR =
109 "/theme/field[@name='backgroundcolor']/@default";
110
111 public final static String XPATH_SYMBOL =
112 "/theme/field[@name='symbol']/@default";
113
114 public final static String XPATH_SHOW_MINIMUM =
115 "/theme/field[@name='showminimum']/@default";
116
117 public final static String XPATH_SHOW_MAXIMUM =
118 "/theme/field[@name='showmaximum']/@default";
119
120 public final static String XPATH_WSPLGEN_FIELDS =
121 "/theme[@name='WSPLGEN']/field";
122
123 /** XPATH to bandwidth field. */
124 public final static String XPATH_BANDWIDTH =
125 "/theme/field[@name='bandwidth']/@default";
126
127
128 /** Parse string to be boolean with default if empty or unrecognized. */
129 public static boolean parseBoolean(String value, boolean defaultsTo) {
130 if (value == null || value.length() == 0) {
131 return defaultsTo;
132 }
133 if (value.equals("false")) {
134 return false;
135 }
136 else if (value.equals("true")) {
137 return true;
138 }
139 else {
140 return defaultsTo;
141 }
142 }
143
144
145 /**
146 * Attempt converting \param value to an integer, in failing cases,
147 * return \param defaultsTo.
148 * @param value String to be converted to integer.
149 * @param defaultsTo Default to return if conversion failed.
150 * @return \param value as integer or defaultsto if conversion failed.
151 */
152 public static int parseInteger(String value, int defaultsTo) {
153 if (value == null || value.length() == 0) {
154 return defaultsTo;
155 }
156
157 try {
158 return Integer.parseInt(value);
159 }
160 catch (NumberFormatException nfe) {
161 // do nothing
162 }
163
164 return defaultsTo;
165 }
166
167
168 /**
169 * Attempt converting \param value to a double, in failing cases,
170 * return \param defaultsTo.
171 * @param value String to be converted to double.
172 * @param defaultsTo Default to return if conversion failed.
173 * @return \param value as integer or defaultsto if conversion failed.
174 */
175 public static double parseDouble(String value, double defaultsTo) {
176 if (value == null || value.length() == 0) {
177 return defaultsTo;
178 }
179
180 try {
181 return Double.parseDouble(value);
182 }
183 catch (NumberFormatException nfe) {
184 // do nothing
185 }
186
187 return defaultsTo;
188 }
189
190
191 /**
192 * Parses line width, defaulting to 0.
193 * @param theme the theme
194 */
195 public static int parseLineWidth(Document theme) {
196 String size = XMLUtils.xpathString(theme, XPATH_LINE_SIZE, null);
197 if (size == null || size.length() == 0) {
198 return 0;
199 }
200
201 try {
202 return Integer.parseInt(size);
203 }
204 catch (NumberFormatException nfe) {
205 logger.warn("Unable to set line size from string: '" + size + "'");
206 }
207 return 0;
208 }
209
210
211 /**
212 * Parse band width, defaulting to 0.
213 * @param theme the theme.
214 */
215 public static double parseBandWidth(Document theme) {
216 String bandWidth = XMLUtils.xpathString(theme, XPATH_BANDWIDTH, null);
217
218 return parseDouble(bandWidth, 0);
219 }
220
221
222 public static int parsePointWidth(Document theme) {
223 String width = XMLUtils.xpathString(theme, XPATH_POINT_SIZE, null);
224
225 return parseInteger(width, 3);
226 }
227
228
229 public static Color parsePointColor(Document theme) {
230 String color = XMLUtils.xpathString(theme, XPATH_POINT_COLOR, null);
231 logger.debug("parsePointColor(): color = " + color);
232 return parseColor(color);
233 }
234
235
236 /**
237 * Parses the line style, defaulting to '10'.
238 * @param theme The theme.
239 */
240 public static float[] parseLineStyle(Document theme) {
241 String dash = XMLUtils.xpathString(theme, XPATH_LINE_STYLE, null);
242
243 float[] def = {10};
244 if (dash == null || dash.length() == 0) {
245 return def;
246 }
247
248 String[] pattern = dash.split(",");
249 if(pattern.length == 1) {
250 return def;
251 }
252
253 try {
254 float[] dashes = new float[pattern.length];
255 for (int i = 0; i < pattern.length; i++) {
256 dashes[i] = Float.parseFloat(pattern[i]);
257 }
258 return dashes;
259 }
260 catch(NumberFormatException nfe) {
261 logger.warn("Unable to set dash from string: '" + dash + "'");
262 return def;
263 }
264 }
265
266
267 /**
268 * Parses text size, defaulting to 10.
269 * @param theme The theme.
270 */
271 public static int parseTextSize(Document theme, String path) {
272 String size = XMLUtils.xpathString(theme, path, null);
273 if (size == null || size.length() == 0) {
274 return 10;
275 }
276
277 try {
278 return Integer.parseInt(size);
279 }
280 catch (NumberFormatException nfe) {
281 }
282 return 10;
283 }
284
285
286 public static int parseTextSize(Document theme) {
287 return parseTextSize(theme, XPATH_LABEL_FONT_SIZE);
288 }
289
290
291 /**
292 * Parses the attribute 'showpoints', defaults to false.
293 * @param theme The theme.
294 */
295 public static boolean parseShowPoints(Document theme) {
296 String show = XMLUtils.xpathString(theme, XPATH_SHOW_POINTS, null);
297 return parseBoolean(show, false);
298 }
299
300 /**
301 * Parses the attribute 'showmiddleheight', defaults to false.
302 * @param theme The theme.
303 */
304 public static boolean parseShowMiddleHeight(Document theme) {
305 String show = XMLUtils.xpathString(theme, XPATH_SHOW_MIDDLE_HEIGHT, null);
306 return parseBoolean(show, false);
307 }
308
309 /**
310 * Parses the attribute 'showarea', defaults to false.
311 * @param theme The theme.
312 */
313 public static boolean parseShowArea(Document theme) {
314 String show = XMLUtils.xpathString(theme, XPATH_SHOW_AREA, null);
315 return parseBoolean(show, false);
316 }
317
318 /**
319 * Parses the attribute 'showverticalline', defaults to true.
320 * @param theme The theme.
321 */
322 public static boolean parseShowVerticalLine(Document theme) {
323 String show = XMLUtils.xpathString(theme, XPATH_SHOW_VERTICAL_LINE, null);
324 return parseBoolean(show, true);
325 }
326
327 /**
328 * Parses the attribute 'showhorizontalline', defaults to true.
329 * @param theme The theme.
330 */
331 public static boolean parseShowHorizontalLine(Document theme) {
332 String show = XMLUtils.xpathString(theme, XPATH_SHOW_HORIZONTAL_LINE, null);
333 return parseBoolean(show, true);
334 }
335
336 /**
337 * Parses the attribute 'showlines', defaults to true.
338 * @param theme The theme.
339 */
340 public static boolean parseShowLine(Document theme) {
341 String show = XMLUtils.xpathString(theme, XPATH_SHOW_LINE, null);
342 return parseBoolean(show, true);
343 }
344
345 /**
346 * Parses the attribute 'showlinelabel', defaults to true.
347 * @param theme The theme.
348 */
349 public static boolean parseShowLineLabel(Document theme) {
350 String show = XMLUtils.xpathString(theme, XPATH_SHOW_LINE_LABEL, null);
351 return parseBoolean(show, false);
352 }
353
354 public static boolean parseShowPointLabel(Document theme) {
355 String show = XMLUtils.xpathString(theme, XPATH_SHOW_POINT_LABEL, null);
356 return parseBoolean(show, false);
357 }
358
359 /**
360 * Parses text color.
361 * @param theme The theme.
362 */
363 public static Color parseTextColor(Document theme) {
364 return parseRGB(getTextColorString(theme));
365 }
366
367
368 /**
369 * Parses the font.
370 * @param theme The theme.
371 */
372 public static Font parseTextFont(Document theme) {
373 String font = XMLUtils.xpathString(theme, XPATH_LABEL_FONT_FACE, null);
374 if (font == null || font.length() == 0) {
375 return null;
376 }
377
378 int size = parseTextSize(theme);
379 int style = parseTextStyle(theme);
380 Font f = new Font (font, style, size);
381 return f;
382 }
383
384
385 /**
386 * Parses the text style, defaults to 'Font.PLAIN'.
387 * @param theme The theme.
388 */
389 public static int parseTextStyle(Document theme, String path) {
390 String style = XMLUtils.xpathString(theme, path, null);
391 if (style == null || style.length() == 0) {
392 return Font.PLAIN;
393 }
394
395 if (style.equals("italic")) {
396 return Font.ITALIC;
397 }
398 else if (style.equals("bold")) {
399 return Font.BOLD;
400 }
401 else {
402 return Font.PLAIN;
403 }
404 }
405
406
407 public static int parseTextStyle(Document theme) {
408 return parseTextStyle(theme, XPATH_LABEL_FONT_STYLE);
409 }
410
411
412 public static boolean parseShowWidth(Document theme) {
413 String show = XMLUtils.xpathString(theme, XPATH_SHOW_WIDTH, null);
414 return parseBoolean(show, false);
415 }
416
417
418 public static boolean parseShowLevel(Document theme) {
419 String show = XMLUtils.xpathString(theme, XPATH_SHOW_LEVEL, null);
420 return parseBoolean(show, false);
421 }
422
423 /**
424 * Parses the textorientation, defaults to 'vertical'.
425 * @param theme The theme.
426 */
427 public static String parseTextOrientation(Document theme) {
428 String o = XMLUtils.xpathString(theme, XPATH_TEXT_ORIENTATION, null);
429 if ("true".equals(o)) {
430 return "horizontal";
431 }
432 else {
433 return "vertical";
434 }
435 }
436
437
438 /**
439 * Parses the text background color, defaults to white.
440 * @param theme The theme.
441 */
442 public static Color parseTextBackground(Document theme) {
443 String color = getLabelBackgroundColorString(theme);
444 if (color == null || color.length() == 0) {
445 return Color.WHITE;
446 }
447 return parseRGB(color);
448 }
449
450
451 /**
452 * Parses the attribute whether to show background or not, defaults to
453 * false.
454 * @param theme The theme.
455 */
456 public static boolean parseLabelShowBackground(Document theme) {
457 String show = XMLUtils.xpathString(theme, XPATH_LABEL_SHOW_BACKGROUND, null);
458 return parseBoolean(show, false);
459 }
460
461
462 public static Color parseColor(String colorString) {
463 if (colorString == null || colorString.length() == 0) {
464 return null;
465 }
466 else if (colorString.indexOf("#") == 0) {
467 return parseHexColor(colorString);
468 }
469 else if (colorString.indexOf(",") >= 0) {
470 return parseRGB(colorString);
471 }
472
473 return null;
474 }
475
476
477 /**
478 * Parse a string like "#00CC22" and return the corresponding color.
479 *
480 * @param hex The hex color value.
481 *
482 * @return a Color or null, if <i>hex</i> is empty.
483 */
484 public static Color parseHexColor(String hex) {
485 if (hex == null) {
486 return null;
487 }
488
489 return Color.decode(hex);
490 }
491
492 /**
493 * Parse a string like "103, 100, 0" and return a corresping color.
494 * @param rgbtext Color as string representation, e.g. "255,0,20".
495 * @return Color, null in case of issues.
496 */
497 public static Color parseRGB(String rgbtext) {
498 if (rgbtext == null) {
499 return null;
500 }
501 String rgb[] = rgbtext.split(",");
502 Color c = null;
503 try {
504 c = new Color(
505 Integer.parseInt(rgb[0].trim()),
506 Integer.parseInt(rgb[1].trim()),
507 Integer.parseInt(rgb[2].trim()));
508 }
509 catch (NumberFormatException nfe) {
510 c = null;
511 }
512 return c;
513 }
514
515
516 public static String getLineColorString(Document theme) {
517 return XMLUtils.xpathString(theme, XPATH_LINE_COLOR, null);
518 }
519
520
521 /** Get show border as string. */
522 public static String getShowBorderString(Document theme) {
523 return XMLUtils.xpathString(theme, XPATH_SHOW_BORDER, null);
524 }
525
526
527 /** Get fill color as string. */
528 public static String getFillColorString(Document theme) {
529 return XMLUtils.xpathString(theme, XPATH_FILL_COLOR, null);
530 }
531
532
533 public static String getLabelBackgroundColorString(Document theme) {
534 return XMLUtils.xpathString(theme, XPATH_LABEL_BGCOLOR, null);
535 }
536
537
538 public static String getBackgroundColorString(Document theme) {
539 return XMLUtils.xpathString(theme, XPATH_BACKGROUND_COLOR, null);
540 }
541
542
543 public static String getTextColorString(Document theme) {
544 String textColor = XMLUtils.xpathString(theme, XPATH_LABEL_FONT_COLOR, null);
545 return textColor;
546 }
547
548
549 public static String getSymbol(Document theme) {
550 return XMLUtils.xpathString(theme, XPATH_SYMBOL, null);
551 }
552
553
554 public static String getTransparencyString(Document theme) {
555 return XMLUtils.xpathString(theme, XPATH_TRANSPARENCY, null);
556 }
557
558
559 public static String getTransparencyAlpha(Document theme) {
560 return XMLUtils.xpathString(theme, XPATH_TRANSPARENCY_ALPHA, null);
561 }
562
563
564 public static String getShowMinimum(Document theme) {
565 return XMLUtils.xpathString(theme, XPATH_SHOW_MINIMUM, null);
566 }
567
568
569 public static String getShowMaximum(Document theme) {
570 return XMLUtils.xpathString(theme, XPATH_SHOW_MAXIMUM, null);
571 }
572
573
574 /**
575 * Gets color from color field.
576 * @param theme the theme document.
577 * @return color.
578 */
579 public static Color parseFillColorField(Document theme) {
580 return parseRGB(getFillColorString(theme));
581 }
582
583
584 public static boolean parseShowBorder(Document theme) {
585 return parseBoolean(getShowBorderString(theme), false);
586 }
587
588
589 public static boolean parseTransparency(Document theme) {
590 return parseBoolean(getTransparencyString(theme), false);
591 }
592
593
594 /**
595 * Gets color from color field.
596 * @param theme the theme document.
597 * @return color.
598 */
599 public static Color parseLineColorField(Document theme) {
600 String lineColorStr = getLineColorString(theme);
601 logger.debug("parseLineColorField: lineColorStr = " +
602 (lineColorStr == null ? "null" : lineColorStr));
603 return parseRGB(lineColorStr);
604 }
605
606
607 public static boolean parseShowMinimum(Document theme) {
608 return parseBoolean(getShowMinimum(theme), false);
609 }
610
611
612 public static boolean parseShowMaximum(Document theme) {
613 return parseBoolean(getShowMaximum(theme), false);
614 }
615
616
617 public static String createWSPLGENStyle(Document theme) {
618 NodeList categories = (NodeList) XMLUtils.xpath(
619 theme,
620 XPATH_WSPLGEN_FIELDS,
621 XPathConstants.NODESET);
622
623 return createWSPLGENStyle(categories).toString();
624 }
625
626 protected static MapserverStyle createWSPLGENStyle(NodeList categories) {
627 MapserverStyle ms = new MapserverStyle();
628
629 for (int i = 0, n = categories.getLength(); i < n; i++) {
630 Element cat = (Element) categories.item(i);
631 String color = cat.getAttribute("default");
632 String name = cat.getAttribute("name");
633
634 String expr;
635
636 try {
637 int length = name.length();
638 int idx = Integer.parseInt(name.substring(length-1, length));
639
640 expr = createWSPLGENExpression(idx);
641 }
642 catch (NumberFormatException nfe) {
643 logger.warn("Error while parsing WSPLGEN category.", nfe);
644 continue;
645 }
646
647 Clazz c = new Clazz(expr);
648 Style s = new Style();
649 s.setColor(color.replace(",", ""));
650 s.setSize(5);
651
652 c.addItem(new Expression("(" + expr + ")"));
653 c.addItem(s);
654
655 ms.addClazz(c);
656 }
657
658 return ms;
659 }
660
661
662 protected static String createWSPLGENExpression(int idx) {
663 if (idx < 5) {
664 int lower = idx - 1;
665 return "[DIFF] >= " + lower + " AND [DIFF] < " + idx;
666 }
667 else {
668 return "[DIFF] >= 4";
669 }
670 }
671
672
673 public static String createMapserverStyle(Document theme) {
674 String symbol = getSymbol(theme);
675 String backcolor = getLabelBackgroundColorString(theme);
676 String linecolor = getLineColorString(theme);
677
678 int linewidth = parseLineWidth(theme);
679
680 MapserverStyle ms = new MapserverStyle();
681
682 Clazz c = new Clazz(" ");
683
684 Style s = new Style();
685 s.setOutlineColor(linecolor.replace(",", ""));
686
687 if (backcolor != null && backcolor.length() > 0) {
688 s.setColor(backcolor.replace(",", ""));
689 }
690
691 s.setSize(linewidth);
692 s.setSymbol(symbol);
693 c.addItem(s);
694
695 String textcolor = getTextColorString(theme);
696 int textsize = parseTextSize(theme);
697
698 if (textcolor != null && textcolor.length() > 0 && textsize > 0) {
699 Label l = new Label();
700 l.setColor(textcolor.replace(",", ""));
701 l.setSize(textsize);
702 c.addItem(l);
703 }
704
705 ms.addClazz(c);
706
707 return ms.toString();
708 }
709 }
710 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org