KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > doc > javadoc > StoreWriter


1 /*
2  * Copyright (c) 1998-2003 Caucho Technology -- all rights reserved
3  *
4  * Caucho Technology permits redistribution, modification and use
5  * of this file in source and binary form ("the Software") under the
6  * Caucho Developer Source License ("the License"). The following
7  * conditions must be met:
8  *
9  * 1. Each copy or derived work of the Software must preserve the copyright
10  * notice and this notice unmodified.
11  *
12  * 2. Redistributions of the Software in source or binary form must include
13  * an unmodified copy of the License, normally in a plain ASCII text
14  *
15  * 3. The names "Resin" or "Caucho" are trademarks of Caucho Technology and
16  * may not be used to endorse products derived from this software.
17  * "Resin" or "Caucho" may not appear in the names of products derived
18  * from this software.
19  *
20  * This Software is provided "AS IS," without a warranty of any kind.
21  * ALL EXPRESS OR IMPLIED REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
22  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.
24  *
25  * CAUCHO TECHNOLOGY AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
26  * SUFFERED BY LICENSEE OR ANY THIRD PARTY AS A RESULT OF USING OR
27  * DISTRIBUTING SOFTWARE. IN NO EVENT WILL CAUCHO OR ITS LICENSORS BE LIABLE
28  * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
29  * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
30  * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
31  * INABILITY TO USE SOFTWARE, EVEN IF HE HAS BEEN ADVISED OF THE POSSIBILITY
32  * OF SUCH DAMAGES.
33  *
34  * @author Sam
35  */

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 JavaDoc;
46 import java.io.InputStream JavaDoc;
47
48 import java.sql.Connection JavaDoc;
49 import java.sql.PreparedStatement JavaDoc;
50 import java.sql.SQLException JavaDoc;
51 import java.sql.Statement JavaDoc;
52
53 import java.util.ArrayList JavaDoc;
54 import java.util.HashMap JavaDoc;
55 import java.util.logging.Level JavaDoc;
56 import java.util.logging.Logger JavaDoc;
57
58 /**
59  *
60  */

61 public class StoreWriter {
62   static protected final Logger JavaDoc log = Log.open(StoreWriter.class);
63   static final L10N L = new L10N(StoreWriter.class);
64
65   private PreparedStatement JavaDoc _stmtJavadocFile;
66   private PreparedStatement JavaDoc _stmtJavadocItem;
67
68   private HashMap JavaDoc<String JavaDoc,JavadocFile> _javadocFiles = new HashMap JavaDoc<String JavaDoc,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   /**
82    * Clear out the old database tables
83    */

84   public void clear(Connection JavaDoc conn)
85     throws SQLException JavaDoc
86   {
87     CharBuffer cb = CharBuffer.allocate();
88
89     conn.setAutoCommit(true);
90
91     Statement JavaDoc stmt = conn.createStatement();
92
93     // drop tables, allow errors
94
String JavaDoc 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 JavaDoc ex) {
111       log.log(Level.FINE,q,ex);
112     }
113
114     // create tables and indexes, allow errors
115

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 JavaDoc ex) {
155       log.log(Level.FINE,q,ex);
156     }
157
158     // XXX: resin db cannot do drop , must allow error on delete
159
//
160
// delete all rows from tables
161
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 JavaDoc ex) {
177         log.log(Level.FINE,q,ex);
178     }
179
180     stmt.close();
181     cb.free();
182   }
183
184   /**
185    * Add index entries to the database for the passed api.
186    *
187    * @return the number of index items added
188    */

189   public int add(Connection JavaDoc conn, Api api)
190     throws SQLException JavaDoc, IOException JavaDoc
191   {
192     _addCount = 0;
193
194     String JavaDoc q = null;
195
196     conn.setAutoCommit(true);
197
198     // PreparedStatement for updating each table
199
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 JavaDoc<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 JavaDoc(_addCount), api.getName()));
222       }
223     }
224
225     if (log.isLoggable(Level.INFO))
226       log.info(L.l(" added {0} entries for `{1}'", new Integer JavaDoc(_addCount), api.getName()));
227     
228     return _addCount;
229   }
230
231   private void parseFile(Path path)
232     throws IOException JavaDoc
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 JavaDoc path, String JavaDoc anchor, String JavaDoc name, String JavaDoc fullname, int typ, int modifier, String JavaDoc description)
241           {
242             _addCount++;
243
244             int jditype;
245
246             switch (typ) {
247
248               case IndexParser.TYPE_CONSTRUCTOR:
249                 return; // ignore constructors
250

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 JavaDoc(L.l("unknown typ {0}",new Integer JavaDoc(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 JavaDoc ex) {
285               throw new RuntimeException JavaDoc(ex);
286             }
287           }
288         });
289
290     indexParser.parse();
291   }
292
293   JavadocFile makeJavadocFile(Api api, String JavaDoc path)
294     throws SQLException JavaDoc
295   {
296     CharBuffer cbkey = CharBuffer.allocate();
297     cbkey.append(api.getId());
298     cbkey.append('|');
299     cbkey.append(path);
300     String JavaDoc 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 JavaDoc name, String JavaDoc fullname, int typ, String JavaDoc anchor, String JavaDoc description,JavadocFile file)
320     throws SQLException JavaDoc
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