ingo@1747: package de.intevation.flys.themes; ingo@1747: felix@1822: import org.apache.log4j.Logger; felix@1822: ingo@1747: import java.io.Serializable; ingo@1747: import java.util.regex.Matcher; ingo@1747: import java.util.regex.Pattern; ingo@1747: felix@1822: import de.intevation.flys.artifacts.FLYSArtifact; ingo@1747: felix@1822: /** felix@1822: * Represents mapping to a theme (including conditions). felix@1822: */ ingo@1747: public class ThemeMapping implements Serializable { ingo@1747: felix@1822: /** The logger that is used in this class */ felix@1822: private static Logger logger = Logger.getLogger(ThemeMapping.class); felix@1822: felix@1822: /** Name from which to map. */ ingo@1747: protected String from; felix@1822: felix@1822: /** Name to which to map. */ ingo@1747: protected String to; felix@1822: felix@1822: /** Given pattern (held against facet description). */ ingo@1747: protected String patternStr; ingo@1747: felix@1822: /** Given masterAttr pattern (held against masterartifacts attributes). */ felix@1822: protected String masterAttr; felix@1822: ingo@1747: protected Pattern pattern; ingo@1747: ingo@1747: ingo@1747: public ThemeMapping(String from, String to) { felix@1822: this(from, to, null, null); ingo@1747: } ingo@1747: ingo@1747: felix@1822: public ThemeMapping( felix@1822: String from, felix@1822: String to, felix@1822: String patternStr, felix@1822: String masterAttr) felix@1822: { ingo@1747: this.from = from; ingo@1747: this.to = to; ingo@1747: this.patternStr = patternStr; felix@1822: this.masterAttr = masterAttr; ingo@1747: ingo@1747: this.pattern = Pattern.compile(patternStr); ingo@1747: } ingo@1747: ingo@1747: ingo@1747: public String getFrom() { ingo@1747: return from; ingo@1747: } ingo@1747: ingo@1747: felix@1822: /** felix@1822: * Get name of theme that is mapped to. felix@1822: */ ingo@1747: public String getTo() { ingo@1747: return to; ingo@1747: } ingo@1747: ingo@1747: felix@1822: /** felix@1822: * Get pattern. felix@1822: */ ingo@1747: public String getPatternStr() { ingo@1747: return patternStr; ingo@1747: } ingo@1747: ingo@1747: felix@1822: /** felix@1822: * Match regular expression against text. felix@1822: * felix@1822: * @param text string to be matched against. felix@1822: * @return true if pattern matches text or pattern is empty. felix@1822: */ ingo@1747: public boolean applyPattern(String text) { felix@1822: if (patternStr == null || patternStr.equals("")) { felix@1822: return true; felix@1822: } ingo@1747: Matcher m = pattern.matcher(text); ingo@1747: return m.matches(); ingo@1747: } felix@1822: felix@1822: felix@1822: /** felix@1822: * Inspects Artifacts data given the masterAttr-condition. felix@1822: * felix@1822: * The only condition implemented so far is 'key==value', for which felix@1822: * the Artifacts data with name "key" has to be of value "value" in order felix@1822: * for true to be returned. felix@1822: * felix@1822: * @param artifact Artifact of which to inspect data. felix@1822: * @return true if no condition is specified or condition is met. felix@1822: */ felix@1822: public boolean masterAttrMatches(FLYSArtifact artifact) { felix@1822: if (masterAttr == null || masterAttr.equals("")) { felix@1822: return true; felix@1822: } felix@1822: felix@1822: // Operator split. felix@1822: String[] parts = masterAttr.split("=="); felix@1822: if (parts.length != 2) { felix@1822: logger.error("ThemeMapping could not parse masterAttr.-condition:_" felix@1822: + masterAttr + "_"); felix@1822: return false; felix@1822: } felix@1822: felix@1822: // Test. felix@1822: return artifact.getDataAsString(parts[0]).equals(parts[1]); felix@1822: } ingo@1747: } ingo@1747: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :