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. package org.apache.tools.ant.taskdefs.optional.sitraka;
  18. import java.util.Vector;
  19. import org.apache.tools.ant.util.regexp.RegexpMatcher;
  20. import org.apache.tools.ant.util.regexp.RegexpMatcherFactory;
  21. /**
  22. * Filters information from coverage, somewhat similar to a <tt>FileSet</tt>.
  23. *
  24. */
  25. public class ReportFilters {
  26. /** user defined filters */
  27. protected Vector filters = new Vector();
  28. /** cached matcher for each filter */
  29. protected Vector matchers = null;
  30. public ReportFilters() {
  31. }
  32. public void addInclude(Include incl) {
  33. filters.addElement(incl);
  34. }
  35. public void addExclude(Exclude excl) {
  36. filters.addElement(excl);
  37. }
  38. public int size() {
  39. return filters.size();
  40. }
  41. /**
  42. * Check whether a given <classname><method>() is accepted by the list
  43. * of filters or not.
  44. * @param methodname the full method name in the format <classname><method>()
  45. */
  46. public boolean accept(String methodname) {
  47. // I'm deferring matcher instantiations at runtime to avoid computing
  48. // the filters at parsing time
  49. if (matchers == null) {
  50. createMatchers();
  51. }
  52. boolean result = false;
  53. // assert filters.size() == matchers.size()
  54. final int size = filters.size();
  55. for (int i = 0; i < size; i++) {
  56. FilterElement filter = (FilterElement) filters.elementAt(i);
  57. RegexpMatcher matcher = (RegexpMatcher) matchers.elementAt(i);
  58. if (filter instanceof Include) {
  59. result = result || matcher.matches(methodname);
  60. } else if (filter instanceof Exclude) {
  61. result = result && !matcher.matches(methodname);
  62. } else {
  63. //not possible
  64. throw new IllegalArgumentException("Invalid filter element: "
  65. + filter.getClass().getName());
  66. }
  67. }
  68. return result;
  69. }
  70. /** should be called only once to cache matchers */
  71. protected void createMatchers() {
  72. RegexpMatcherFactory factory = new RegexpMatcherFactory();
  73. final int size = filters.size();
  74. matchers = new Vector();
  75. for (int i = 0; i < size; i++) {
  76. FilterElement filter = (FilterElement) filters.elementAt(i);
  77. RegexpMatcher matcher = factory.newRegexpMatcher();
  78. String pattern = filter.getAsPattern();
  79. matcher.setPattern(pattern);
  80. matchers.addElement(matcher);
  81. }
  82. }
  83. /** default abstract filter element class */
  84. public abstract static class FilterElement {
  85. protected String clazz = "*"; // default is all classes
  86. protected String method = "*"; // default is all methods
  87. public void setClass(String value) {
  88. clazz = value;
  89. }
  90. public void setMethod(String value) {
  91. method = value;
  92. }
  93. public String getAsPattern() {
  94. StringBuffer buf = new StringBuffer(toString());
  95. StringUtil.replace(buf, ".", "\\.");
  96. StringUtil.replace(buf, "*", ".*");
  97. StringUtil.replace(buf, "(", "\\(");
  98. StringUtil.replace(buf, ")", "\\)");
  99. return buf.toString();
  100. }
  101. public String toString() {
  102. return clazz + "." + method + "()";
  103. }
  104. }
  105. /** concrete include class */
  106. public static class Include extends FilterElement {
  107. }
  108. /** concrete exclude class */
  109. public static class Exclude extends FilterElement {
  110. }
  111. }