1 36 37 package com.caucho.doc.javadoc; 38 39 import com.caucho.log.Log; 40 import com.caucho.util.CharBuffer; 41 import com.caucho.util.L10N; 42 import com.caucho.vfs.Path; 43 import com.caucho.vfs.ReadStream; 44 45 import java.io.IOException ; 46 import java.io.InputStream ; 47 48 import java.sql.Connection ; 49 import java.sql.PreparedStatement ; 50 import java.sql.SQLException ; 51 import java.sql.Statement ; 52 53 import java.util.ArrayList ; 54 import java.util.HashMap ; 55 import java.util.logging.Level ; 56 import java.util.logging.Logger ; 57 58 61 public class StoreWriter { 62 static protected final Logger log = Log.open(StoreWriter.class); 63 static final L10N L = new L10N(StoreWriter.class); 64 65 private PreparedStatement _stmtJavadocFile; 66 private PreparedStatement _stmtJavadocItem; 67 68 private HashMap <String ,JavadocFile> _javadocFiles = new HashMap <String ,JavadocFile>(); 69 private int _javadocFileNextId = 1; 70 private int _javadocItemNextId = 1; 71 72 Store _store; 73 Api _currApi; 74 int _addCount; 75 76 StoreWriter(Store store) 77 { 78 _store = store; 79 } 80 81 84 public void clear(Connection conn) 85 throws SQLException 86 { 87 CharBuffer cb = CharBuffer.allocate(); 88 89 conn.setAutoCommit(true); 90 91 Statement stmt = conn.createStatement(); 92 93 String q = null; 95 96 try { 97 cb.clear(); 98 cb.append("DROP TABLE "); 99 cb.append(_store.getTableNameFile()); 100 q = cb.toString(); 101 log.fine(q); 102 stmt.executeUpdate(q); 103 104 cb.clear(); 105 cb.append("DROP TABLE "); 106 cb.append(_store.getTableNameItem()); 107 q = cb.toString(); 108 log.fine(q); 109 stmt.executeUpdate(q); 110 } catch (SQLException ex) { 111 log.log(Level.FINE,q,ex); 112 } 113 114 116 try { 117 cb.clear(); 118 cb.append("CREATE TABLE "); 119 cb.append(_store.getTableNameFile()); 120 cb.append(" (id INT NOT NULL, path VARCHAR(255), api VARCHAR(255), PRIMARY KEY (id))"); 121 q = cb.toString(); 122 log.fine(q); 123 stmt.executeUpdate(q); 124 125 cb.clear(); 126 cb.append("CREATE TABLE "); 127 cb.append(_store.getTableNameItem()); 128 cb.append(" (id INT NOT NULL, name VARCHAR(255), fullname VARCHAR(255), typ INT, file_id INT, anchor VARCHAR(255), description TEXT, PRIMARY KEY (id))"); 129 q = cb.toString(); 130 log.fine(q); 131 stmt.executeUpdate(q); 132 133 cb.clear(); 134 cb.append("CREATE INDEX "); 135 cb.append(_store.getTableNameItem()); 136 cb.append("NIdx ON "); 137 cb.append(_store.getTableNameItem()); 138 cb.append(" (name)"); 139 q = cb.toString(); 140 log.fine(q); 141 stmt.executeUpdate(q); 142 143 cb.clear(); 144 cb.append("CREATE INDEX "); 145 cb.append(_store.getTableNameItem()); 146 cb.append("FNIdx ON "); 147 cb.append(_store.getTableNameItem()); 148 cb.append(" (fullname)"); 149 q = cb.toString(); 150 log.fine(q); 151 stmt.executeUpdate(q); 152 153 } 154 catch (SQLException ex) { 155 log.log(Level.FINE,q,ex); 156 } 157 158 try { 162 cb.clear(); 163 cb.append("DELETE FROM "); 164 cb.append(_store.getTableNameFile()); 165 q = cb.toString(); 166 log.fine(q); 167 stmt.executeUpdate(q); 168 169 cb.clear(); 170 cb.append("DELETE FROM "); 171 cb.append(_store.getTableNameItem()); 172 q = cb.toString(); 173 log.fine(q); 174 stmt.executeUpdate(q); 175 } 176 catch (SQLException ex) { 177 log.log(Level.FINE,q,ex); 178 } 179 180 stmt.close(); 181 cb.free(); 182 } 183 184 189 public int add(Connection conn, Api api) 190 throws SQLException , IOException 191 { 192 _addCount = 0; 193 194 String q = null; 195 196 conn.setAutoCommit(true); 197 198 q = "INSERT INTO " + _store.getTableNameFile() + " (id,path,api) VALUES (?,?,?)"; 200 log.finer(q); 201 _stmtJavadocFile = conn.prepareStatement(q); 202 _stmtJavadocFile.setString(3,api.getId()); 203 204 q = "INSERT INTO " + _store.getTableNameItem() + " (id, name, fullname, typ, file_id, anchor, description) VALUES (?,?,?,?,?,?,?)"; 205 log.finer(q); 206 _stmtJavadocItem = conn.prepareStatement(q); 207 208 ArrayList <Path> paths = api.getIndexes(); 209 210 log.info(L.l(" adding entries for `{0}'", api.getName())); 211 212 for (int i = 0; i < paths.size(); i++) { 213 Path path = paths.get(i); 214 _currApi = api; 215 216 int addCount = _addCount; 217 218 parseFile(path); 219 220 if (_addCount - addCount == 0) { 221 log.warning(L.l(" {0} entries found for `{1}', is this not a javadoc index file?", new Integer (_addCount), api.getName())); 222 } 223 } 224 225 if (log.isLoggable(Level.INFO)) 226 log.info(L.l(" added {0} entries for `{1}'", new Integer (_addCount), api.getName())); 227 228 return _addCount; 229 } 230 231 private void parseFile(Path path) 232 throws IOException 233 { 234 if (log.isLoggable(Level.FINE)) 235 log.fine(L.l("reading from index file {0}", path)); 236 ReadStream in = path.openRead(); 237 238 IndexParser indexParser = new IndexParser(in, 239 new IndexParser.Callback() { 240 public void item(String path, String anchor, String name, String fullname, int typ, int modifier, String description) 241 { 242 _addCount++; 243 244 int jditype; 245 246 switch (typ) { 247 248 case IndexParser.TYPE_CONSTRUCTOR: 249 return; 251 case IndexParser.TYPE_PACKAGE: 252 253 jditype = JavadocItem.PACKAGE; 254 break; 255 256 case IndexParser.TYPE_CLASS: 257 case IndexParser.TYPE_ENUM: 258 case IndexParser.TYPE_INTERFACE: 259 case IndexParser.TYPE_EXCEPTION: 260 case IndexParser.TYPE_ERROR: 261 case IndexParser.TYPE_ANNOTATION: 262 263 jditype = JavadocItem.CLASS; 264 break; 265 266 case IndexParser.TYPE_METHOD: 267 268 jditype = JavadocItem.METHOD; 269 break; 270 271 case IndexParser.TYPE_VARIABLE: 272 273 jditype = JavadocItem.VARIABLE; 274 break; 275 276 default: 277 throw new RuntimeException (L.l("unknown typ {0}",new Integer (typ))); 278 } 279 280 try { 281 JavadocFile file = makeJavadocFile(_currApi,path); 282 JavadocItem item = makeJavadocItem(name, fullname, jditype, anchor, description,file); 283 } 284 catch (SQLException ex) { 285 throw new RuntimeException (ex); 286 } 287 } 288 }); 289 290 indexParser.parse(); 291 } 292 293 JavadocFile makeJavadocFile(Api api, String path) 294 throws SQLException 295 { 296 CharBuffer cbkey = CharBuffer.allocate(); 297 cbkey.append(api.getId()); 298 cbkey.append('|'); 299 cbkey.append(path); 300 String key = cbkey.close(); 301 302 JavadocFile file = _javadocFiles.get(key); 303 if (file == null) { 304 file = new JavadocFile(api,_javadocFileNextId++,path); 305 306 307 CharBuffer cb = CharBuffer.allocate(); 308 309 _stmtJavadocFile.setInt(1,file.getId()); 310 _stmtJavadocFile.setString(2,file.getPath()); 311 _stmtJavadocFile.executeUpdate(); 312 313 _javadocFiles.put(key,file); 314 } 315 316 return file; 317 } 318 319 private JavadocItem makeJavadocItem(String name, String fullname, int typ, String anchor, String description,JavadocFile file) 320 throws SQLException 321 { 322 JavadocItem item = new JavadocItem(name,fullname,typ,anchor,description,file); 323 _stmtJavadocItem.setInt(1,_javadocItemNextId++); 324 _stmtJavadocItem.setString(2,item.getName()); 325 _stmtJavadocItem.setString(3,item.getFullName()); 326 _stmtJavadocItem.setInt(4,item.getType()); 327 _stmtJavadocItem.setInt(5,item.getFile().getId()); 328 _stmtJavadocItem.setString(6,item.getAnchor()); 329 _stmtJavadocItem.setString(7,item.getDescription()); 330 _stmtJavadocItem.executeUpdate(); 331 return item; 332 } 333 334 } 335 336 | Popular Tags |