KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hammurapi > inspectors > samples > CollectStringLiterals


1 /*
2   * Hammurapi
3  * Automated Java code review system.
4  * Copyright (C) 2004 Hammurapi Group
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * URL: http://www.hammurapi.org
21  * e-Mail: support@hammurapi.biz
22  */

23 package org.hammurapi.inspectors.samples;
24
25 import java.io.FileWriter JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.Writer JavaDoc;
28 import java.sql.PreparedStatement JavaDoc;
29 import java.sql.ResultSet JavaDoc;
30 import java.sql.SQLException JavaDoc;
31 import java.util.Properties JavaDoc;
32
33 import org.hammurapi.InspectorBase;
34 import org.hammurapi.HammurapiException;
35 import org.hammurapi.HammurapiRuntimeException;
36 import org.hammurapi.results.AnnotationContext;
37 import org.hammurapi.results.LinkedAnnotation;
38 import org.hammurapi.results.AnnotationContext.FileEntry;
39
40 import com.pavelvlasov.jsel.Repository;
41 import com.pavelvlasov.jsel.expressions.StringConstant;
42 import com.pavelvlasov.review.SourceMarker;
43 import com.pavelvlasov.sql.Parameterizer;
44 import com.pavelvlasov.sql.RowProcessor;
45 import com.pavelvlasov.sql.SQLProcessor;
46
47 /**
48  * This inspector demonstrates usage of annotations and persistency of inspector findings.
49  * It collects all string literals and then outputs them alphabetially sorted
50  * as an annotation in Summary.
51  * This inspector cleans results from previous run before scan. As such it will report string literals only from modified
52  * sources.
53  * @author Pavel Vlasov
54  * @version $Revision: 1.4 $
55  */

56 public class CollectStringLiterals extends InspectorBase {
57     
58     /**
59      * Unique table name
60      */

61     private String JavaDoc tableName="STRING_LITERALS";
62     
63     /**
64      * Method to inject table name from parameters.
65      */

66     public void setTableName(String JavaDoc tableName) {
67         this.tableName=tableName;
68     }
69     
70     private String JavaDoc[] initSQL= {
71             "CREATE CACHED TABLE "+tableName+
72             " (LITERAL VARCHAR(250), SOURCE VARCHAR(250), LINE INTEGER, COL INTEGER)",
73             "CREATE INDEX IX_"+tableName+" ON "+tableName+" (LITERAL, SOURCE, LINE, COL)"
74     };
75     
76 // private String[] destroySQL= {
77
// "DROP INDEX IX_"+tableName,
78
// "DROP TABLE "+tableName
79
// };
80

81     /**
82      * Creating a table to store results
83      */

84     public void initDb(SQLProcessor processor, Properties JavaDoc dbProperties) {
85         for (int i=0; i<initSQL.length; i++) {
86             try {
87                 processor.processUpdate(initSQL[i], null);
88             } catch (SQLException JavaDoc e) {
89                 throw new HammurapiRuntimeException(e);
90             }
91         }
92     }
93     
94     public void init() throws HammurapiException {
95         super.init();
96         SQLProcessor processor=getContext().getSession().getProcessor();
97         try {
98             processor.processUpdate("DELETE FROM "+tableName, null);
99         } catch (SQLException JavaDoc e) {
100             disable("Could not clean "+tableName+": "+e);
101         }
102     }
103         
104     public void visit(final StringConstant constant) {
105         SQLProcessor processor=getContext().getSession().getProcessor();
106         try {
107             processor.processUpdate(
108                     "INSERT INTO "+tableName+
109                     " (LITERAL, SOURCE, LINE, COL) " +
110                     "VALUES (?,?,?,?)",
111                     new Parameterizer() {
112                         public void parameterize(PreparedStatement JavaDoc ps) throws SQLException JavaDoc {
113                             ps.setString(1, constant.getValue());
114                             SourceMarker sourceMarker = (SourceMarker) constant;
115                             ps.setString(2, sourceMarker.getSourceURL());
116                             ps.setInt(3, sourceMarker.getLine());
117                             ps.setInt(4, sourceMarker.getColumn());
118                         }
119                     });
120         } catch (SQLException JavaDoc e) {
121             context.warn((SourceMarker) constant, e);
122         }
123     }
124     
125     public void leave(Repository repo) {
126         final SQLProcessor processor=getContext().getSession().getProcessor();
127         if (processor!=null) {
128             context.annotate(new LinkedAnnotation() {
129                 String JavaDoc path;
130
131                 public String JavaDoc getPath() {
132                     return path;
133                 }
134
135                 public String JavaDoc getName() {
136                     return "String literals";
137                 }
138
139                 public void render(AnnotationContext context) throws HammurapiException {
140                     FileEntry a = context.getNextFile(".html");
141                     path=a.getPath();
142                     try {
143                         final Writer JavaDoc w=new FileWriter JavaDoc(a.getFile());
144                         try {
145                             w.write("<HTML><BODY><TABLE border=\"1\"><TR><TH>Literal</TH><TH>File</TH><TH>Line</TH><TH>Column</TH></TR>");
146                             processor.processSelect(
147                                     "SELECT * FROM "+tableName+" ORDER BY LITERAL, SOURCE, LINE, COL",
148                                     null,
149                                     new RowProcessor() {
150                                         public boolean process(ResultSet JavaDoc rs) throws SQLException JavaDoc {
151                                             try {
152                                                 w.write("<TR><TD>");
153                                                 w.write(rs.getString("LITERAL"));
154                                                 w.write("</TD><TD>");
155                                                 w.write(rs.getString("SOURCE"));
156                                                 w.write("</TD><TD aligh=\"right\">");
157                                                 w.write(rs.getString("LINE"));
158                                                 w.write("</TD><TD aligh=\"right\">");
159                                                 w.write(rs.getString("COL"));
160                                                 w.write("</TD><TR>");
161                                                 return true;
162                                             } catch (IOException JavaDoc e) {
163                                                 throw new HammurapiRuntimeException(e);
164                                             }
165                                         }
166                                     });
167                             
168                             w.write("</TABLE></BODY></HTML>");
169                         } finally {
170                             w.close();
171                         }
172                     } catch (SQLException JavaDoc e) {
173                         throw new HammurapiException(e);
174                     } catch (HammurapiRuntimeException e) {
175                         throw new HammurapiException(e.getCause());
176                     } catch (IOException JavaDoc e) {
177                         throw new HammurapiException(e);
178                     }
179                 }
180
181                 public Properties JavaDoc getProperties() {
182                     return null;
183                 }
184             });
185         }
186         
187     }
188 }
189
Popular Tags