View Javadoc
1   package org.andromda.core.configuration;
2   
3   import java.io.Serializable;
4   import java.util.ArrayList;
5   import java.util.Collection;
6   import java.util.regex.PatternSyntaxException;
7   import org.andromda.core.metafacade.MetafacadeConstants;
8   import org.andromda.core.metafacade.MetafacadeFactory;
9   import org.apache.commons.lang.StringUtils;
10  
11  /**
12   * Stores information about all {@link Filter} instances that should or should not be applied
13   * to a model.
14   *
15   * @author Chad Brandon
16   * @see org.andromda.core.configuration.Filter
17   */
18  public class Filters
19      implements Serializable
20  {
21      private static final long serialVersionUID = 34L;
22  
23      /**
24       * The flag indicating whether or not all filter
25       * should be applied.
26       */
27      private boolean applyAll = true;
28  
29      /**
30       * Sets whether or not AndroMDA should apply all filters on a model. If this is set to true, then filter values should be
31       * specified if you want to keep certain filters from being being applied. If this is set to false, then you would want
32       * to define filter elements to specify which filters <strong>SHOULD BE</strong> applied.
33       *
34       * @param applyAll whether or not we should apply all filters true/false
35       */
36      public void setApplyAll(final boolean applyAll)
37      {
38          this.applyAll = applyAll;
39      }
40  
41      /**
42       * Stores the filters as they're added.
43       */
44      private final Collection<Filter> filters = new ArrayList<Filter>();
45  
46      /**
47       * Adds the filter to the underlying filters store.
48       *
49       * @param filter the Filter instance.
50       */
51      public void addFilter(final Filter filter)
52      {
53          this.filters.add(filter);
54      }
55  
56      /**
57       * Adds all {@link Filter} instances in the given <code>filters</code>
58       * to this instance.
59       *
60       * @param filters the Filters instance to add.
61       */
62      public void addFilters(final Filters filters)
63      {
64          this.filters.addAll(filters.filters);
65      }
66  
67      /**
68       * Gets the actual filters that belong to this filters instance.
69       *
70       * @return the filters to retrieve.
71       */
72      public Filter[] getFilters()
73      {
74          return this.filters.toArray(new Filter[this.filters.size()]);
75      }
76  
77      private final MetafacadeFactory factory = MetafacadeFactory.getInstance();
78  
79      /**
80       * Determines whether or not the <code>value</code> should be applied. If
81       * <code>applyAll</code> is true, then this method will return false only if the Filter
82       * corresponding to the <code>value</code> has apply set to false.
83       *
84       * @param value the name of the model filter to check.
85       * @return true/false
86       */
87      public boolean isApply(final String value)
88      {
89          boolean shouldApply = this.applyAll;
90          for (Filter filter : this.filters)
91          {
92              if (this.match(value, filter.getValue()))
93              {
94                  shouldApply = filter.isApply() && (filter.getNamespaceList().isEmpty() ||
95                      filter.getNamespaceList().contains(StringUtils.trim(factory.getNamespace())));
96                  break;
97              }
98          }
99          return shouldApply;
100     }
101 
102     /**
103      * Indicates whether or not this model filters instance
104      * has any filtering defined.
105      *
106      * @return true/false
107      */
108     public boolean isEmpty()
109     {
110         return this.filters.isEmpty();
111     }
112 
113     /**
114      * The double star constant.
115      */
116     private static final String DOUBLE_STAR = "**";
117 
118     /**
119      * Provides matching of simple wildcards. (i.e. '*.java' etc.)
120      *
121      * @param value the value to match against.
122      * @param pattern the pattern to check if the path matches.
123      * @return true if the <code>value</code> matches the given <code>pattern</code>, false otherwise.
124      */
125     public boolean match(
126         String value,
127         String pattern)
128     {
129         value = StringUtils.trimToEmpty(value);
130         boolean matches = false;
131         if (value != null)
132         {
133             final String scopeOperator = MetafacadeConstants.NAMESPACE_SCOPE_OPERATOR;
134             pattern = StringUtils.replace(
135                     pattern,
136                     ".",
137                     "\\.");
138             boolean matchAll = pattern.contains(DOUBLE_STAR);
139             pattern = pattern.replaceAll(
140                     "\\*{1}",
141                     "\\.\\*");
142             if (matchAll)
143             {
144                 pattern = StringUtils.replace(
145                         pattern,
146                         DOUBLE_STAR,
147                         ".*");
148             }
149             try
150             {
151                 matches = value.matches(pattern);
152             }
153             catch (final PatternSyntaxException exception)
154             {
155                 matches = false;
156             }
157             if (!matchAll)
158             {
159                 matches =
160                     matches &&
161                     StringUtils.countMatches(
162                         pattern,
163                         scopeOperator) == StringUtils.countMatches(
164                         value,
165                         scopeOperator);
166             }
167         }
168         return matches;
169     }
170 }