001: /*
002: * Hammurapi
003: * Automated Java code review system.
004: * Copyright (C) 2005 Johannes Bellert
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.org
021: * e-Mail: CraftOfObjects@gmail.com
022: */
023: package org.hammurapi.inspectors;
024:
025: import java.util.ArrayList;
026: import java.util.Iterator;
027:
028: import org.apache.oro.text.GlobCompiler;
029: import org.apache.oro.text.regex.MalformedPatternException;
030: import org.apache.oro.text.regex.Pattern;
031: import org.apache.oro.text.regex.Perl5Matcher;
032: import org.hammurapi.InspectorBase;
033:
034: import com.pavelvlasov.config.ConfigurationException;
035: import com.pavelvlasov.config.Parameterizable;
036: import com.pavelvlasov.jsel.impl.JavaTokenTypes;
037: import com.pavelvlasov.jsel.impl.Token;
038: import com.pavelvlasov.review.SourceMarker;
039:
040: /**
041: * ER-209 as a variant of ER-100
042: * Copyrights information should be present in each file.
043: * @author Johannes Bellert
044: * @version $Revision: 1.3 $
045: */
046: public class VendorNameViolation extends InspectorBase implements
047: Parameterizable {
048: private Perl5Matcher matcher = new Perl5Matcher();
049:
050: /**
051: * Stores the setting from the configuration for the mandatory
052: * copyright text.
053: */
054: private ArrayList vendorNames = new ArrayList();
055: private ArrayList patterns = new ArrayList();
056:
057: public void visit(Token token) {
058: if (token.getType() == JavaTokenTypes.SL_COMMENT
059: || token.getType() == JavaTokenTypes.ML_COMMENT) {
060: context.info(token, token.getText()); // + " ---> " + token.getOwner() + " " + token.getOwner().getLocation());
061: checkForViolations(token, token.getText());
062: }
063: }
064:
065: public void checkForViolations(SourceMarker scrMrk, String text) {
066: Iterator it = patterns.iterator();
067:
068: while (it.hasNext()) {
069: Pattern pt = (Pattern) it.next();
070: if (matcher.contains(text, pt)) {
071: context.reportViolation(scrMrk, "Found Pattern "
072: + pt.getPattern().toString());
073: }
074: }
075: }
076:
077: /**
078: * Configures the rule. Reads in the values of the parameter vendor-name.
079: *
080: * @param name the name of the parameter being loaded from Hammurapi configuration
081: * @param parameter the value of the parameter being loaded from Hammurapi configuration
082: * @exception ConfigurationException in case of a not supported parameter
083: */
084: public boolean setParameter(String name, Object parameter)
085: throws ConfigurationException {
086: if ("vendor-name".equals(name)) {
087: // System.out.println("+ " + parameter.toString());
088:
089: if (!vendorNames.contains(parameter.toString())) {
090: vendorNames.add(parameter.toString());
091:
092: GlobCompiler compiler = new GlobCompiler();
093: try {
094: patterns.add(compiler.compile(parameter.toString(),
095: GlobCompiler.CASE_INSENSITIVE_MASK));
096: } catch (MalformedPatternException e) {
097: throw new ConfigurationException(
098: "Malformed pattern: " + parameter, e);
099: }
100: }
101: return true;
102: }
103:
104: throw new ConfigurationException("Parameter '" + name
105: + "' is not supported by " + getClass().getName());
106: }
107:
108: /**
109: * Gives back the preconfigured values.
110: */
111: public String getConfigInfo() {
112: StringBuffer ret = new StringBuffer(
113: "Configured Vendor Names text:\n");
114: Iterator it = vendorNames.iterator();
115:
116: while (it.hasNext()) {
117: ret.append(" " + it.next() + "\n");
118: }
119: return ret.toString();
120: }
121: }
|