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.filter;
021:
022: import java.io.IOException;
023:
024: import edu.umd.cs.findbugs.BugInstance;
025: import edu.umd.cs.findbugs.SystemProperties;
026: import edu.umd.cs.findbugs.xml.XMLOutput;
027:
028: /**
029: * Match bug instances having one of given codes or patterns.
030: *
031: * @author rafal@caltha.pl
032: */
033: public class BugMatcher implements Matcher {
034: private static final boolean DEBUG = SystemProperties
035: .getBoolean("filter.debug");
036:
037: private final StringSetMatch codes;
038:
039: private final StringSetMatch patterns;
040:
041: private final StringSetMatch categories;
042:
043: /**
044: * Constructor.
045: *
046: * @param codes
047: * comma-separated list of bug codes
048: * @param patterns
049: * coma-separated list of bug patterns.
050: * @param categories
051: * coma-separated list of bug categories.
052: */
053: public BugMatcher(String codes, String patterns, String categories) {
054: this .codes = new StringSetMatch(codes);
055: this .patterns = new StringSetMatch(patterns);
056: this .categories = new StringSetMatch(categories);
057: }
058:
059: public boolean match(BugInstance bugInstance) {
060: boolean result1 = codes.match(bugInstance.getAbbrev());
061: boolean result2 = patterns.match(bugInstance.getType());
062: boolean result3 = categories.match(bugInstance.getBugPattern()
063: .getCategory());
064: if (DEBUG)
065: System.out.println("Matching " + bugInstance.getAbbrev()
066: + "/" + bugInstance.getType() + "/"
067: + bugInstance.getBugPattern().getCategory()
068: + " with " + this + ", result = " + result1 + "/"
069: + result2 + "/" + result3);
070:
071: return result1 || result2 || result3;
072: }
073:
074: @Override
075: public int hashCode() {
076: return codes.hashCode() + patterns.hashCode()
077: + categories.hashCode();
078: }
079:
080: @Override
081: public boolean equals(Object o) {
082: if (!(o instanceof BugMatcher))
083: return false;
084: BugMatcher other = (BugMatcher) o;
085: return codes.equals(other.codes)
086: && patterns.equals(other.patterns)
087: && categories.equals(other.categories);
088: }
089:
090: public void writeXML(XMLOutput xmlOutput, boolean disabled)
091: throws IOException {
092: xmlOutput.startTag("Bug");
093: if (disabled)
094: xmlOutput.addAttribute("disabled", "true");
095:
096: addAttribute(xmlOutput, "code", codes);
097: addAttribute(xmlOutput, "pattern", patterns);
098: addAttribute(xmlOutput, "category", categories);
099: xmlOutput.stopTag(true);
100: }
101:
102: public void addAttribute(XMLOutput xmlOutput, String name,
103: StringSetMatch matches) throws IOException {
104: String value = matches.toString();
105: if (value.length() != 0)
106: xmlOutput.addAttribute(name, value);
107: }
108:
109: @Override
110: public String toString() {
111: StringBuffer buf = new StringBuffer("Bug(");
112: if (!codes.isEmpty())
113: buf.append("code = \"").append(codes).append("\" ");
114: if (!patterns.isEmpty())
115: buf.append("pattern = \"").append(patterns).append("\" ");
116: if (!categories.isEmpty())
117: buf.append("category = \"").append(categories)
118: .append("\" ");
119: buf.setLength(buf.length() - 1);
120: buf.append(")");
121: return buf.toString();
122: }
123:
124: }
|