1 | /*
|
---|
2 | * Copyright 2001-2002,2004 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 |
|
---|
18 | package org.apache.tools.ant.taskdefs.optional.sitraka;
|
---|
19 |
|
---|
20 | import java.util.Vector;
|
---|
21 | import org.apache.tools.ant.util.regexp.RegexpMatcher;
|
---|
22 | import org.apache.tools.ant.util.regexp.RegexpMatcherFactory;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Filters information from coverage, somewhat similar to a <tt>FileSet</tt>.
|
---|
26 | *
|
---|
27 | */
|
---|
28 | public class ReportFilters {
|
---|
29 |
|
---|
30 | /** user defined filters */
|
---|
31 | protected Vector filters = new Vector();
|
---|
32 |
|
---|
33 | /** cached matcher for each filter */
|
---|
34 | protected Vector matchers = null;
|
---|
35 |
|
---|
36 | public ReportFilters() {
|
---|
37 | }
|
---|
38 |
|
---|
39 | public void addInclude(Include incl) {
|
---|
40 | filters.addElement(incl);
|
---|
41 | }
|
---|
42 |
|
---|
43 | public void addExclude(Exclude excl) {
|
---|
44 | filters.addElement(excl);
|
---|
45 | }
|
---|
46 |
|
---|
47 | public int size() {
|
---|
48 | return filters.size();
|
---|
49 | }
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Check whether a given <classname><method>() is accepted by the list
|
---|
53 | * of filters or not.
|
---|
54 | * @param methodname the full method name in the format <classname><method>()
|
---|
55 | */
|
---|
56 | public boolean accept(String methodname) {
|
---|
57 | // I'm deferring matcher instantiations at runtime to avoid computing
|
---|
58 | // the filters at parsing time
|
---|
59 | if (matchers == null) {
|
---|
60 | createMatchers();
|
---|
61 | }
|
---|
62 | boolean result = false;
|
---|
63 | // assert filters.size() == matchers.size()
|
---|
64 | final int size = filters.size();
|
---|
65 | for (int i = 0; i < size; i++) {
|
---|
66 | FilterElement filter = (FilterElement) filters.elementAt(i);
|
---|
67 | RegexpMatcher matcher = (RegexpMatcher) matchers.elementAt(i);
|
---|
68 | if (filter instanceof Include) {
|
---|
69 | result = result || matcher.matches(methodname);
|
---|
70 | } else if (filter instanceof Exclude) {
|
---|
71 | result = result && !matcher.matches(methodname);
|
---|
72 | } else {
|
---|
73 | //not possible
|
---|
74 | throw new IllegalArgumentException("Invalid filter element: "
|
---|
75 | + filter.getClass().getName());
|
---|
76 | }
|
---|
77 | }
|
---|
78 | return result;
|
---|
79 | }
|
---|
80 |
|
---|
81 | /** should be called only once to cache matchers */
|
---|
82 | protected void createMatchers() {
|
---|
83 | RegexpMatcherFactory factory = new RegexpMatcherFactory();
|
---|
84 | final int size = filters.size();
|
---|
85 | matchers = new Vector();
|
---|
86 | for (int i = 0; i < size; i++) {
|
---|
87 | FilterElement filter = (FilterElement) filters.elementAt(i);
|
---|
88 | RegexpMatcher matcher = factory.newRegexpMatcher();
|
---|
89 | String pattern = filter.getAsPattern();
|
---|
90 | matcher.setPattern(pattern);
|
---|
91 | matchers.addElement(matcher);
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 |
|
---|
96 | /** default abstract filter element class */
|
---|
97 | public abstract static class FilterElement {
|
---|
98 | protected String clazz = "*"; // default is all classes
|
---|
99 | protected String method = "*"; // default is all methods
|
---|
100 |
|
---|
101 | public void setClass(String value) {
|
---|
102 | clazz = value;
|
---|
103 | }
|
---|
104 |
|
---|
105 | public void setMethod(String value) {
|
---|
106 | method = value;
|
---|
107 | }
|
---|
108 |
|
---|
109 | public String getAsPattern() {
|
---|
110 | StringBuffer buf = new StringBuffer(toString());
|
---|
111 | StringUtil.replace(buf, ".", "\\.");
|
---|
112 | StringUtil.replace(buf, "*", ".*");
|
---|
113 | StringUtil.replace(buf, "(", "\\(");
|
---|
114 | StringUtil.replace(buf, ")", "\\)");
|
---|
115 | return buf.toString();
|
---|
116 | }
|
---|
117 |
|
---|
118 | public String toString() {
|
---|
119 | return clazz + "." + method + "()";
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | /** concrete include class */
|
---|
124 | public static class Include extends FilterElement {
|
---|
125 | }
|
---|
126 |
|
---|
127 | /** concrete exclude class */
|
---|
128 | public static class Exclude extends FilterElement {
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|