comparison flys-artifacts/src/main/java/de/intevation/flys/themes/ThemeMapping.java @ 3468:f37e7e8907cb

merged flys-artifacts/2.8.1
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:39 +0200
parents e74e707ff650
children 8e713e9bb4d7
comparison
equal deleted inserted replaced
3387:5ffad8bde8ad 3468:f37e7e8907cb
1 package de.intevation.flys.themes;
2
3 import org.apache.log4j.Logger;
4
5 import java.io.Serializable;
6 import java.util.regex.Matcher;
7 import java.util.regex.Pattern;
8
9 import de.intevation.flys.artifacts.FLYSArtifact;
10
11 /**
12 * Represents mapping to a theme (including conditions).
13 */
14 public class ThemeMapping implements Serializable {
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. */
20 protected String from;
21
22 /** Name to which to map. */
23 protected String to;
24
25 /** Given pattern (held against facet description). */
26 protected String patternStr;
27
28 /** Given masterAttr pattern (held against masterartifacts attributes). */
29 protected String masterAttr;
30
31 /** Given output for which mapping is valid. */
32 protected String output;
33
34 protected Pattern pattern;
35
36
37 public ThemeMapping(String from, String to) {
38 this(from, to, null, null, null);
39 }
40
41
42 public ThemeMapping(
43 String from,
44 String to,
45 String patternStr,
46 String masterAttr,
47 String output)
48 {
49 this.from = from;
50 this.to = to;
51 this.patternStr = patternStr;
52 this.masterAttr = masterAttr;
53 this.output = output;
54
55 this.pattern = Pattern.compile(patternStr);
56 }
57
58
59 public String getFrom() {
60 return from;
61 }
62
63
64 /**
65 * Get name of theme that is mapped to.
66 */
67 public String getTo() {
68 return to;
69 }
70
71
72 /**
73 * Get pattern.
74 */
75 public String getPatternStr() {
76 return patternStr;
77 }
78
79
80 /**
81 * Match regular expression against text.
82 *
83 * @param text string to be matched against.
84 * @return true if pattern matches text or pattern is empty.
85 */
86 public boolean applyPattern(String text) {
87 if (patternStr == null || patternStr.length() == 0) {
88 return true;
89 }
90 Matcher m = pattern.matcher(text);
91 return m.matches();
92 }
93
94
95 /**
96 * Inspects Artifacts data given the masterAttr-condition.
97 *
98 * The only condition implemented so far is 'key==value', for which
99 * the Artifacts data with name "key" has to be of value "value" in order
100 * for true to be returned.
101 *
102 * @param artifact Artifact of which to inspect data.
103 * @return true if no condition is specified or condition is met.
104 */
105 public boolean masterAttrMatches(FLYSArtifact artifact) {
106 if (masterAttr == null || masterAttr.length() == 0) {
107 return true;
108 }
109
110 // Operator split.
111 String[] parts = masterAttr.split("==");
112 if (parts.length != 2) {
113 logger.error("ThemeMapping could not parse masterAttr.-condition:_"
114 + masterAttr + "_");
115 return false;
116 }
117
118 // Test.
119 return artifact.getDataAsString(parts[0]).equals(parts[1]);
120 }
121
122
123 /**
124 * Returns true if no output condition exists, or the condition is met
125 * in parameter output.
126 */
127 public boolean outputMatches(String output) {
128 if (this.output == null || this.output.length() == 0) {
129 return true;
130 }
131
132 return this.output.equals(output);
133 }
134 }
135 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org