001: package org.andromda.core.configuration;
002:
003: import java.io.Serializable;
004:
005: import java.util.ArrayList;
006: import java.util.Collection;
007: import java.util.Iterator;
008: import java.util.regex.PatternSyntaxException;
009:
010: import org.andromda.core.metafacade.MetafacadeConstants;
011: import org.apache.commons.lang.StringUtils;
012:
013: /**
014: * Stores information about all {@link Filter} instances that should or should not be applied
015: * to a model.
016: *
017: * @author Chad Brandon
018: * @see org.andromda.core.configuration.Filter
019: */
020: public class Filters implements Serializable {
021: /**
022: * The flag indicating whether or not all filter
023: * should be applied.
024: */
025: private boolean applyAll = true;
026:
027: /**
028: * Sets whether or not AndroMDA should apply all filters on a model. If this is set to true, then filter values should be
029: * specified if you want to keep certain filters from being being applied. If this is set to false, then you would want
030: * to define filter elements to specify which filters <strong>SHOULD BE</strong> applied.
031: *
032: * @param applyAll whether or not we should apply all filters true/false
033: */
034: public void setApplyAll(final boolean applyAll) {
035: this .applyAll = applyAll;
036: }
037:
038: /**
039: * Stores the filters as they're added.
040: */
041: private final Collection filters = new ArrayList();
042:
043: /**
044: * Adds the filter to the underlying filters store.
045: *
046: * @param filter the Filter instance.
047: */
048: public void addFilter(final Filter filter) {
049: this .filters.add(filter);
050: }
051:
052: /**
053: * Adds all {@link Filter} instances in the given <code>filters</code>
054: * to this instance.
055: *
056: * @param filters the Filters instance to add.
057: */
058: public void addFilters(final Filters filters) {
059: this .filters.addAll(filters.filters);
060: }
061:
062: /**
063: * Gets the actual filters that belong to this filters instance.
064: *
065: * @return the filters to retrieve.
066: */
067: public Filter[] getFilters() {
068: return (Filter[]) this .filters.toArray(new Filter[0]);
069: }
070:
071: /**
072: * Determines whether or not the <code>value</code> should be applied. If
073: * <code>applyAll</code> is true, then this method will return false only if the Filter
074: * corresponding to the <code>value</code> has apply set to false.
075: *
076: * @param value the name of the model filter to check.
077: * @return true/false
078: */
079: public boolean isApply(final String value) {
080: boolean shouldApply = this .applyAll;
081: for (final Iterator iterator = this .filters.iterator(); iterator
082: .hasNext();) {
083: final Filter filter = (Filter) iterator.next();
084: if (match(value, filter.getValue())) {
085: shouldApply = filter.isApply();
086: break;
087: }
088: }
089: return shouldApply;
090: }
091:
092: /**
093: * Indicates whether or not this model filters instance
094: * has any filtering defined.
095: *
096: * @return true/false
097: */
098: public boolean isEmpty() {
099: return this .filters.isEmpty();
100: }
101:
102: /**
103: * The double star constant.
104: */
105: private static final String DOUBLE_STAR = "**";
106:
107: /**
108: * Provides matching of simple wildcards. (i.e. '*.java' etc.)
109: *
110: * @param value the value to match against.
111: * @param pattern the pattern to check if the path matches.
112: * @return true if the <code>value</code> matches the given <code>pattern</code>, false otherwise.
113: */
114: public boolean match(String value, String pattern) {
115: value = StringUtils.trimToEmpty(value);
116: boolean matches = false;
117: if (value != null) {
118: final String scopeOperator = MetafacadeConstants.NAMESPACE_SCOPE_OPERATOR;
119: pattern = StringUtils.replace(pattern, ".", "\\.");
120: boolean matchAll = pattern.indexOf(DOUBLE_STAR) != -1;
121: pattern = pattern.replaceAll("\\*{1}", "\\.\\*");
122: if (matchAll) {
123: pattern = StringUtils.replace(pattern, DOUBLE_STAR,
124: ".*");
125: }
126: try {
127: matches = value.matches(pattern);
128: } catch (final PatternSyntaxException exception) {
129: matches = false;
130: }
131: if (!matchAll) {
132: matches = matches
133: && StringUtils.countMatches(pattern,
134: scopeOperator) == StringUtils
135: .countMatches(value, scopeOperator);
136: }
137: }
138: return matches;
139: }
140: }
|