001: /*
002: * Hammurapi
003: * Automated Java code review system.
004: * Copyright (C) 2004 Hammurapi Group
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: support@hammurapi.biz
022: */
023: package org.hammurapi.inspectors.samples;
024:
025: import java.io.FileWriter;
026: import java.io.IOException;
027: import java.io.Writer;
028: import java.sql.PreparedStatement;
029: import java.sql.ResultSet;
030: import java.sql.SQLException;
031: import java.util.Properties;
032:
033: import org.hammurapi.InspectorBase;
034: import org.hammurapi.HammurapiException;
035: import org.hammurapi.HammurapiRuntimeException;
036: import org.hammurapi.results.AnnotationContext;
037: import org.hammurapi.results.LinkedAnnotation;
038: import org.hammurapi.results.AnnotationContext.FileEntry;
039:
040: import com.pavelvlasov.jsel.Repository;
041: import com.pavelvlasov.jsel.expressions.StringConstant;
042: import com.pavelvlasov.review.SourceMarker;
043: import com.pavelvlasov.sql.Parameterizer;
044: import com.pavelvlasov.sql.RowProcessor;
045: import com.pavelvlasov.sql.SQLProcessor;
046:
047: /**
048: * This inspector demonstrates usage of annotations and persistency of inspector findings.
049: * It collects all string literals and then outputs them alphabetially sorted
050: * as an annotation in Summary.
051: * This inspector cleans results from previous run before scan. As such it will report string literals only from modified
052: * sources.
053: * @author Pavel Vlasov
054: * @version $Revision: 1.4 $
055: */
056: public class CollectStringLiterals extends InspectorBase {
057:
058: /**
059: * Unique table name
060: */
061: private String tableName = "STRING_LITERALS";
062:
063: /**
064: * Method to inject table name from parameters.
065: */
066: public void setTableName(String tableName) {
067: this .tableName = tableName;
068: }
069:
070: private String[] initSQL = {
071: "CREATE CACHED TABLE "
072: + tableName
073: + " (LITERAL VARCHAR(250), SOURCE VARCHAR(250), LINE INTEGER, COL INTEGER)",
074: "CREATE INDEX IX_" + tableName + " ON " + tableName
075: + " (LITERAL, SOURCE, LINE, COL)" };
076:
077: // private String[] destroySQL= {
078: // "DROP INDEX IX_"+tableName,
079: // "DROP TABLE "+tableName
080: // };
081:
082: /**
083: * Creating a table to store results
084: */
085: public void initDb(SQLProcessor processor, Properties dbProperties) {
086: for (int i = 0; i < initSQL.length; i++) {
087: try {
088: processor.processUpdate(initSQL[i], null);
089: } catch (SQLException e) {
090: throw new HammurapiRuntimeException(e);
091: }
092: }
093: }
094:
095: public void init() throws HammurapiException {
096: super .init();
097: SQLProcessor processor = getContext().getSession()
098: .getProcessor();
099: try {
100: processor.processUpdate("DELETE FROM " + tableName, null);
101: } catch (SQLException e) {
102: disable("Could not clean " + tableName + ": " + e);
103: }
104: }
105:
106: public void visit(final StringConstant constant) {
107: SQLProcessor processor = getContext().getSession()
108: .getProcessor();
109: try {
110: processor.processUpdate("INSERT INTO " + tableName
111: + " (LITERAL, SOURCE, LINE, COL) "
112: + "VALUES (?,?,?,?)", new Parameterizer() {
113: public void parameterize(PreparedStatement ps)
114: throws SQLException {
115: ps.setString(1, constant.getValue());
116: SourceMarker sourceMarker = (SourceMarker) constant;
117: ps.setString(2, sourceMarker.getSourceURL());
118: ps.setInt(3, sourceMarker.getLine());
119: ps.setInt(4, sourceMarker.getColumn());
120: }
121: });
122: } catch (SQLException e) {
123: context.warn((SourceMarker) constant, e);
124: }
125: }
126:
127: public void leave(Repository repo) {
128: final SQLProcessor processor = getContext().getSession()
129: .getProcessor();
130: if (processor != null) {
131: context.annotate(new LinkedAnnotation() {
132: String path;
133:
134: public String getPath() {
135: return path;
136: }
137:
138: public String getName() {
139: return "String literals";
140: }
141:
142: public void render(AnnotationContext context)
143: throws HammurapiException {
144: FileEntry a = context.getNextFile(".html");
145: path = a.getPath();
146: try {
147: final Writer w = new FileWriter(a.getFile());
148: try {
149: w
150: .write("<HTML><BODY><TABLE border=\"1\"><TR><TH>Literal</TH><TH>File</TH><TH>Line</TH><TH>Column</TH></TR>");
151: processor
152: .processSelect(
153: "SELECT * FROM "
154: + tableName
155: + " ORDER BY LITERAL, SOURCE, LINE, COL",
156: null, new RowProcessor() {
157: public boolean process(
158: ResultSet rs)
159: throws SQLException {
160: try {
161: w
162: .write("<TR><TD>");
163: w
164: .write(rs
165: .getString("LITERAL"));
166: w
167: .write("</TD><TD>");
168: w
169: .write(rs
170: .getString("SOURCE"));
171: w
172: .write("</TD><TD aligh=\"right\">");
173: w
174: .write(rs
175: .getString("LINE"));
176: w
177: .write("</TD><TD aligh=\"right\">");
178: w
179: .write(rs
180: .getString("COL"));
181: w
182: .write("</TD><TR>");
183: return true;
184: } catch (IOException e) {
185: throw new HammurapiRuntimeException(
186: e);
187: }
188: }
189: });
190:
191: w.write("</TABLE></BODY></HTML>");
192: } finally {
193: w.close();
194: }
195: } catch (SQLException e) {
196: throw new HammurapiException(e);
197: } catch (HammurapiRuntimeException e) {
198: throw new HammurapiException(e.getCause());
199: } catch (IOException e) {
200: throw new HammurapiException(e);
201: }
202: }
203:
204: public Properties getProperties() {
205: return null;
206: }
207: });
208: }
209:
210: }
211: }
|