001: /*
002: * FindBugs - Find Bugs in Java programs
003: * Copyright (C) 2006, University of Maryland
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019:
020: package edu.umd.cs.findbugs.gui2;
021:
022: import java.util.Collection;
023:
024: import edu.umd.cs.findbugs.BugInstance;
025: import edu.umd.cs.findbugs.filter.AndMatcher;
026: import edu.umd.cs.findbugs.filter.BugMatcher;
027: import edu.umd.cs.findbugs.filter.ClassMatcher;
028: import edu.umd.cs.findbugs.filter.DesignationMatcher;
029: import edu.umd.cs.findbugs.filter.FirstVersionMatcher;
030: import edu.umd.cs.findbugs.filter.LastVersionMatcher;
031: import edu.umd.cs.findbugs.filter.Matcher;
032: import edu.umd.cs.findbugs.filter.OrMatcher;
033: import edu.umd.cs.findbugs.filter.PriorityMatcher;
034: import edu.umd.cs.findbugs.filter.RelationalOp;
035: import edu.umd.cs.findbugs.gui2.BugAspects.SortableValue;
036:
037: /**
038: * @author pugh
039: */
040: public class FilterFactory {
041:
042: public static Matcher makeOrMatcher(
043: Collection<SortableValue> sortables) {
044: return makeMatcher(sortables, false);
045: }
046:
047: public static Matcher makeAndMatcher(
048: Collection<SortableValue> sortables) {
049: return makeMatcher(sortables, true);
050: }
051:
052: private static Matcher makeMatcher(
053: Collection<SortableValue> sortables, boolean andOr) {
054: if (sortables.size() == 1) {
055: for (SortableValue s : sortables)
056: return makeMatcher(s);
057: }
058: edu.umd.cs.findbugs.filter.CompoundMatcher matcher;
059: if (andOr == true)
060: matcher = new AndMatcher();
061: else
062: matcher = new OrMatcher();
063: for (SortableValue s : sortables)
064: matcher.addChild(makeMatcher(s));
065: return matcher;
066: }
067:
068: public static Matcher makeMatcher(Collection<Sortables> sortables,
069: BugInstance bug) {
070: if (sortables.size() == 1) {
071: for (Sortables s : sortables)
072: return makeMatcher(s, bug);
073: }
074: AndMatcher matcher = new AndMatcher();
075: for (Sortables s : sortables)
076: matcher.addChild(makeMatcher(s, bug));
077: return matcher;
078: }
079:
080: /**
081: * @param s
082: * @param bug
083: * @return
084: */
085: private static Matcher makeMatcher(Sortables s, BugInstance bug) {
086: switch (s) {
087: case BUGCODE:
088: return new BugMatcher(s.getFrom(bug), null, null);
089: case CATEGORY:
090: return new BugMatcher(null, null, s.getFrom(bug));
091: case CLASS:
092: return new ClassMatcher(s.getFrom(bug));
093: case DESIGNATION:
094: return new DesignationMatcher(s.getFrom(bug));
095:
096: case FIRSTVERSION:
097: return new FirstVersionMatcher(s.getFrom(bug),
098: RelationalOp.EQ);
099: case LASTVERSION:
100: return new LastVersionMatcher(s.getFrom(bug),
101: RelationalOp.EQ);
102: case PACKAGE:
103: String p = Sortables.CLASS.getFrom(bug);
104: int lastDot = p.lastIndexOf('.');
105: if (lastDot > 0)
106: p = p.substring(0, lastDot);
107: return new ClassMatcher("~" + p + "\\.[^.]+");
108: case PRIORITY:
109: return new PriorityMatcher(Integer.toString(bug
110: .getPriority()));
111:
112: case TYPE:
113: return new BugMatcher(null, s.getFrom(bug), null);
114:
115: case DIVIDER:
116: default:
117: throw new IllegalArgumentException();
118:
119: }
120:
121: }
122:
123: private static Matcher makeMatcher(SortableValue sv) {
124: Sortables s = sv.key;
125: String value = sv.value;
126: switch (s) {
127: case BUGCODE:
128: return new BugMatcher(value, null, null);
129: case CATEGORY:
130: return new BugMatcher(null, null, value);
131: case CLASS:
132: return new ClassMatcher(value);
133: case DESIGNATION:
134: return new DesignationMatcher(value);
135:
136: case FIRSTVERSION:
137: return new FirstVersionMatcher(value, RelationalOp.EQ);
138: case LASTVERSION:
139: return new LastVersionMatcher(value, RelationalOp.EQ);
140: case PACKAGE:
141: return new ClassMatcher("~" + value + "\\.[^.]+");
142: case PRIORITY:
143: return new PriorityMatcher(value);
144:
145: case TYPE:
146: return new BugMatcher(null, value, null);
147:
148: case DIVIDER:
149: default:
150: throw new IllegalArgumentException();
151:
152: }
153:
154: }
155:
156: }
|