comparison flys-artifacts/src/main/java/de/intevation/flys/themes/ThemeMapping.java @ 1822:6ed439ff61bf

Changed theme-mapping mechanism to include further condition (on master-artifacts attributes), added point themes for longitudinal.ws for calculations at locations. flys-artifacts/trunk@3151 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Thu, 03 Nov 2011 10:25:23 +0000
parents d2a17e990c70
children 9562ca537143
comparison
equal deleted inserted replaced
1821:e55f1a851923 1822:6ed439ff61bf
1 package de.intevation.flys.themes; 1 package de.intevation.flys.themes;
2
3 import org.apache.log4j.Logger;
2 4
3 import java.io.Serializable; 5 import java.io.Serializable;
4 import java.util.regex.Matcher; 6 import java.util.regex.Matcher;
5 import java.util.regex.Pattern; 7 import java.util.regex.Pattern;
6 8
9 import de.intevation.flys.artifacts.FLYSArtifact;
7 10
11 /**
12 * Represents mapping to a theme (including conditions).
13 */
8 public class ThemeMapping implements Serializable { 14 public class ThemeMapping implements Serializable {
9 15
16 /** The logger that is used in this class */
17 private static Logger logger = Logger.getLogger(ThemeMapping.class);
18
19 /** Name from which to map. */
10 protected String from; 20 protected String from;
21
22 /** Name to which to map. */
11 protected String to; 23 protected String to;
24
25 /** Given pattern (held against facet description). */
12 protected String patternStr; 26 protected String patternStr;
27
28 /** Given masterAttr pattern (held against masterartifacts attributes). */
29 protected String masterAttr;
13 30
14 protected Pattern pattern; 31 protected Pattern pattern;
15 32
16 33
17 public ThemeMapping(String from, String to) { 34 public ThemeMapping(String from, String to) {
18 this(from, to, null); 35 this(from, to, null, null);
19 } 36 }
20 37
21 38
22 public ThemeMapping(String from, String to, String patternStr) { 39 public ThemeMapping(
40 String from,
41 String to,
42 String patternStr,
43 String masterAttr)
44 {
23 this.from = from; 45 this.from = from;
24 this.to = to; 46 this.to = to;
25 this.patternStr = patternStr; 47 this.patternStr = patternStr;
48 this.masterAttr = masterAttr;
26 49
27 this.pattern = Pattern.compile(patternStr); 50 this.pattern = Pattern.compile(patternStr);
28 } 51 }
29 52
30 53
31 public String getFrom() { 54 public String getFrom() {
32 return from; 55 return from;
33 } 56 }
34 57
35 58
59 /**
60 * Get name of theme that is mapped to.
61 */
36 public String getTo() { 62 public String getTo() {
37 return to; 63 return to;
38 } 64 }
39 65
40 66
67 /**
68 * Get pattern.
69 */
41 public String getPatternStr() { 70 public String getPatternStr() {
42 return patternStr; 71 return patternStr;
43 } 72 }
44 73
45 74
75 /**
76 * Match regular expression against text.
77 *
78 * @param text string to be matched against.
79 * @return true if pattern matches text or pattern is empty.
80 */
46 public boolean applyPattern(String text) { 81 public boolean applyPattern(String text) {
82 if (patternStr == null || patternStr.equals("")) {
83 return true;
84 }
47 Matcher m = pattern.matcher(text); 85 Matcher m = pattern.matcher(text);
48 return m.matches(); 86 return m.matches();
49 } 87 }
88
89
90 /**
91 * Inspects Artifacts data given the masterAttr-condition.
92 *
93 * The only condition implemented so far is 'key==value', for which
94 * the Artifacts data with name "key" has to be of value "value" in order
95 * for true to be returned.
96 *
97 * @param artifact Artifact of which to inspect data.
98 * @return true if no condition is specified or condition is met.
99 */
100 public boolean masterAttrMatches(FLYSArtifact artifact) {
101 if (masterAttr == null || masterAttr.equals("")) {
102 return true;
103 }
104
105 // Operator split.
106 String[] parts = masterAttr.split("==");
107 if (parts.length != 2) {
108 logger.error("ThemeMapping could not parse masterAttr.-condition:_"
109 + masterAttr + "_");
110 return false;
111 }
112
113 // Test.
114 return artifact.getDataAsString(parts[0]).equals(parts[1]);
115 }
50 } 116 }
51 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 117 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org