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
13
14
15
16
17
18 public class Filters
19 implements Serializable
20 {
21 private static final long serialVersionUID = 34L;
22
23
24
25
26
27 private boolean applyAll = true;
28
29
30
31
32
33
34
35
36 public void setApplyAll(final boolean applyAll)
37 {
38 this.applyAll = applyAll;
39 }
40
41
42
43
44 private final Collection<Filter> filters = new ArrayList<Filter>();
45
46
47
48
49
50
51 public void addFilter(final Filter filter)
52 {
53 this.filters.add(filter);
54 }
55
56
57
58
59
60
61
62 public void addFilters(final Filters filters)
63 {
64 this.filters.addAll(filters.filters);
65 }
66
67
68
69
70
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
81
82
83
84
85
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
104
105
106
107
108 public boolean isEmpty()
109 {
110 return this.filters.isEmpty();
111 }
112
113
114
115
116 private static final String DOUBLE_STAR = "**";
117
118
119
120
121
122
123
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 }