comparison flys-artifacts/src/main/java/org/dive4elements/river/themes/ThemeMapping.java @ 5831:bd047b71ab37

Repaired internal references
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 12:06:39 +0200
parents flys-artifacts/src/main/java/de/intevation/flys/themes/ThemeMapping.java@a5f65e8983be
children
comparison
equal deleted inserted replaced
5830:160f53ee0870 5831:bd047b71ab37
1 package org.dive4elements.river.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 org.dive4elements.river.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
92 if (m.matches()) {
93 logger.debug("Pattern matches: " + text);
94 return true;
95 }
96 else {
97 logger.debug(
98 "Pattern '"+ text + "' does not match: " + this.patternStr);
99 return false;
100 }
101 }
102
103
104 /**
105 * Inspects Artifacts data given the masterAttr-condition.
106 *
107 * The only condition implemented so far is 'key==value', for which
108 * the Artifacts data with name "key" has to be of value "value" in order
109 * for true to be returned.
110 *
111 * @param artifact Artifact of which to inspect data.
112 * @return true if no condition is specified or condition is met.
113 */
114 public boolean masterAttrMatches(FLYSArtifact artifact) {
115 if (masterAttr == null || masterAttr.length() == 0) {
116 return true;
117 }
118
119 // Operator split.
120 String[] parts = masterAttr.split("==");
121 if (parts.length != 2) {
122 logger.error("ThemeMapping could not parse masterAttr.-condition:_"
123 + masterAttr + "_");
124 return false;
125 }
126
127 // Test.
128 if (artifact.getDataAsString(parts[0]).equals(parts[1])) {
129 logger.debug("Matches master Attribute.");
130 return true;
131 }
132 else {
133 logger.debug("Does not match master Attribute.");
134 return false;
135 }
136 }
137
138
139 /**
140 * Returns true if no output condition exists, or the condition is met
141 * in parameter output.
142 */
143 public boolean outputMatches(String output) {
144 if (this.output == null || this.output.length() == 0) {
145 return true;
146 }
147
148 if (this.output.equals(output)) {
149 logger.debug("Output matches this mapping: " + output);
150 return true;
151 }
152 else {
153 logger.debug("Output '"+ output +"' does not match: "+ this.output);
154 return false;
155 }
156 }
157 }
158 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org