comparison flys-artifacts/src/main/java/de/intevation/flys/utils/ThemeUtil.java @ 3318:dbe2f85bf160

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

http://dive4elements.wald.intevation.org