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: felix@1828: /** Given output for which mapping is valid. */ felix@1828: protected String output; felix@1828: ingo@1747: protected Pattern pattern; ingo@1747: ingo@1747: ingo@1747: public ThemeMapping(String from, String to) { felix@1828: this(from, to, null, 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@1828: String masterAttr, felix@1828: String output) felix@1822: { ingo@1747: this.from = from; ingo@1747: this.to = to; ingo@1747: this.patternStr = patternStr; felix@1822: this.masterAttr = masterAttr; felix@1828: this.output = output; 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) { sascha@3453: if (patternStr == null || patternStr.length() == 0) { felix@1822: return true; felix@1822: } ingo@1747: Matcher m = pattern.matcher(text); ingo@3785: ingo@3781: if (m.matches()) { ingo@3781: logger.debug("Pattern matches: " + text); ingo@3781: return true; ingo@3781: } ingo@3781: else { ingo@3781: logger.debug( ingo@3781: "Pattern '"+ text + "' does not match: " + this.patternStr); ingo@3781: return false; ingo@3781: } 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) { sascha@3453: if (masterAttr == null || masterAttr.length() == 0) { 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. ingo@3781: if (artifact.getDataAsString(parts[0]).equals(parts[1])) { ingo@3781: logger.debug("Matches master Attribute."); ingo@3781: return true; ingo@3781: } ingo@3781: else { ingo@3781: logger.debug("Does not match master Attribute."); ingo@3781: return false; ingo@3781: } felix@1822: } felix@1828: felix@1828: felix@1828: /** felix@1828: * Returns true if no output condition exists, or the condition is met felix@1828: * in parameter output. felix@1828: */ felix@1828: public boolean outputMatches(String output) { sascha@3453: if (this.output == null || this.output.length() == 0) { felix@1828: return true; felix@1828: } felix@1828: ingo@3781: if (this.output.equals(output)) { ingo@3781: logger.debug("Output matches this mapping: " + output); ingo@3781: return true; ingo@3781: } ingo@3781: else { ingo@3781: logger.debug("Output '"+ output +"' does not match: "+ this.output); ingo@3781: return false; ingo@3781: } felix@1828: } ingo@1747: } ingo@1747: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :