1 23 package org.hammurapi.inspectors.samples; 24 25 import java.io.FileWriter ; 26 import java.io.IOException ; 27 import java.io.Writer ; 28 import java.sql.PreparedStatement ; 29 import java.sql.ResultSet ; 30 import java.sql.SQLException ; 31 import java.util.Properties ; 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 56 public class CollectStringLiterals extends InspectorBase { 57 58 61 private String tableName="STRING_LITERALS"; 62 63 66 public void setTableName(String tableName) { 67 this.tableName=tableName; 68 } 69 70 private String [] 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 81 84 public void initDb(SQLProcessor processor, Properties dbProperties) { 85 for (int i=0; i<initSQL.length; i++) { 86 try { 87 processor.processUpdate(initSQL[i], null); 88 } catch (SQLException 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 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 ps) throws SQLException { 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 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 path; 130 131 public String getPath() { 132 return path; 133 } 134 135 public String 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 w=new FileWriter (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 rs) throws SQLException { 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 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 e) { 173 throw new HammurapiException(e); 174 } catch (HammurapiRuntimeException e) { 175 throw new HammurapiException(e.getCause()); 176 } catch (IOException e) { 177 throw new HammurapiException(e); 178 } 179 } 180 181 public Properties getProperties() { 182 return null; 183 } 184 }); 185 } 186 187 } 188 } 189 | Popular Tags |