KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ashkelon > JPackage


1 package org.ashkelon;
2 /**
3  * Ashkelon
4  * Copyright UptoData Inc. 2001
5  * March 2001
6  */

7
8 import java.io.Serializable JavaDoc;
9 import java.sql.Connection JavaDoc;
10 import java.sql.PreparedStatement JavaDoc;
11 import java.sql.ResultSet JavaDoc;
12 import java.sql.SQLException JavaDoc;
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.ashkelon.db.DBMgr;
19 import org.ashkelon.db.DBUtils;
20 import org.ashkelon.db.PKManager;
21 import org.ashkelon.util.JDocUtil;
22 import org.ashkelon.util.Logger;
23 import org.ashkelon.util.StringUtils;
24 import org.ashkelon.util.TreeNode;
25
26 import com.sun.javadoc.ClassDoc;
27 import com.sun.javadoc.PackageDoc;
28
29 /**
30  * Part of Persistable javadoc object model
31  * Analog of com.sun.javadoc.PackageDoc
32  *
33  * @author Eitan Suez
34  * @version 2.0
35  */

36 public class JPackage implements JDoc, Serializable JavaDoc
37 {
38    private String JavaDoc name;
39    private DocInfo doc;
40    private List JavaDoc classes;
41    
42    private List JavaDoc ordinaryClasses;
43    private List JavaDoc exceptionClasses;
44    private List JavaDoc errorClasses;
45    private List JavaDoc interfaces;
46
47    private static String JavaDoc SEQUENCE = "PKG_SEQ";
48    private static String JavaDoc TABLENAME = "PACKAGE";
49    
50    private int id;
51    private boolean idSet = false;
52    
53    private API api;
54    
55    private transient Logger log;
56    
57    public JPackage(String JavaDoc name)
58    {
59       log = Logger.getInstance();
60       setName(name);
61       setClasses(new ArrayList JavaDoc());
62       setDoc(new DocInfo());
63    }
64    
65    public JPackage(PackageDoc packageDoc, boolean recurseClasses, API api)
66    {
67       this(packageDoc.name());
68       this.api = api;
69       setDoc(new DocInfo(packageDoc));
70       if (recurseClasses)
71       {
72          addClasses(packageDoc.allClasses());
73       }
74    }
75    
76    public void store(Connection JavaDoc conn) throws SQLException JavaDoc
77    {
78       String JavaDoc prefix = log.getPrefix();
79       log.setPrefix("JPackage");
80       if (exists(conn))
81       {
82          //log.traceln("Skipping package " + getName() + " (already in repository)");
83
//log.traceln("(to update " + getName() + ", remove then add)");
84
return;
85       }
86
87       Map JavaDoc fieldInfo = new HashMap JavaDoc(5);
88       fieldInfo.put("ID", new Integer JavaDoc(getId(conn)));
89       fieldInfo.put("NAME", StringUtils.truncate(name, 60));
90       fieldInfo.put("DOCID", new Integer JavaDoc(getDoc().getId(conn)));
91       fieldInfo.put("API_ID", new Integer JavaDoc(api.getId(conn)));
92
93       DBUtils.insert(conn, TABLENAME, fieldInfo);
94       getDoc().store(conn);
95       for (int i=0; i<classes.size(); i++)
96       {
97          ClassType cls = (ClassType) classes.get(i);
98          log.verbose("about to store class: "+cls.getQualifiedName());
99          (cls).store(conn);
100       }
101       log.setPrefix(prefix);
102    }
103    
104    public boolean exists(Connection JavaDoc conn) throws SQLException JavaDoc
105    {
106       Map JavaDoc constraints = new HashMap JavaDoc();
107       constraints.put("NAME", getName());
108       constraints.put("API_ID", new Integer JavaDoc(api.getId(conn)));
109       Object JavaDoc obj = DBUtils.getObject(conn, TABLENAME, "ID", constraints);
110       if (obj == null)
111          return false;
112       
113       if (!idSet)
114       {
115          id = ((Number JavaDoc) obj).intValue();
116          idSet = true;
117       }
118       return true;
119    }
120    
121    /**
122     * @param pkgName the package to remove
123     * @return whether pkgName was found in the repository
124     */

125    public static boolean delete(Connection JavaDoc conn, String JavaDoc pkgName) throws SQLException JavaDoc
126    {
127       Logger log = Logger.getInstance();
128       
129       String JavaDoc sql = "select p.id, p.docid, c.id, c.docid " +
130                    "from PACKAGE p, CLASSTYPE c " +
131                    "where p.name=? and c.packageid=p.id";
132       PreparedStatement JavaDoc pstmt = conn.prepareStatement(sql);
133       pstmt.setString(1, pkgName);
134       ResultSet JavaDoc rset = pstmt.executeQuery();
135
136       if (!rset.next())
137       {
138          log.traceln("Skipping Package " + pkgName + " (not in repository)");
139          rset.close();
140          pstmt.close();
141
142          // see if package exists with no child classes
143
return (deleteNoChildren(conn, pkgName));
144       }
145
146       int pid = rset.getInt(1);
147       int docid = rset.getInt(2);
148       int classid = rset.getInt(3);
149       int class_docid = rset.getInt(4);
150
151       // delete classes
152
ClassType.delete(conn, classid, class_docid);
153
154       while (rset.next())
155       {
156          classid = rset.getInt(3);
157          class_docid = rset.getInt(4);
158          ClassType.delete(conn, classid, class_docid);
159       }
160
161       rset.close();
162       pstmt.close();
163
164       // delete doc
165
DocInfo.delete(conn, docid);
166
167       // sever references to reference
168
sql = "update REFERENCE set refdoc_id = null where refdoc_id = ?";
169       pstmt = conn.prepareStatement(sql);
170       pstmt.setInt(1, pid);
171       pstmt.executeUpdate();
172       pstmt.close();
173
174       // delete self
175
HashMap JavaDoc constraint = new HashMap JavaDoc();
176       constraint.put("ID", new Integer JavaDoc(pid));
177       DBUtils.delete(conn, TABLENAME, constraint);
178
179       return true;
180    }
181
182    public static boolean deleteNoChildren(Connection JavaDoc conn, String JavaDoc pkgName)
183        throws SQLException JavaDoc
184    {
185       HashMap JavaDoc constraint = new HashMap JavaDoc();
186       constraint.put("NAME", pkgName);
187       int num_deleted = DBUtils.delete(conn, TABLENAME, constraint);
188       return (num_deleted > 0);
189    }
190    
191    
192    /**
193     * uses the primary key manager to assign an id to this object for/before
194     * insertion into db
195     */

196    public int getId(Connection JavaDoc conn) throws SQLException JavaDoc
197    {
198       if (!idSet)
199       {
200          //id = DBUtils.getNextVal(conn, SEQUENCE);
201
id = PKManager.getInstance().nextVal(SEQUENCE);
202          idSet = true;
203       }
204       return id;
205    }
206    
207    public int getId()
208    {
209       return id;
210    }
211    
212    public void setId(int id)
213    {
214       this.id = id;
215       idSet = true;
216    }
217    
218    // accessor methods..
219
public String JavaDoc getName()
220    {
221       return name;
222    }
223    public void setName(String JavaDoc name)
224    {
225       this.name = name;
226    }
227    
228    public DocInfo getDoc()
229    {
230       return doc;
231    }
232    public void setDoc(DocInfo doc)
233    {
234       this.doc = doc;
235    }
236
237    public List JavaDoc getClasses()
238    {
239       return classes;
240    }
241    public void setClasses(List JavaDoc classes)
242    {
243       this.classes = new ArrayList JavaDoc(35);
244       ordinaryClasses = new ArrayList JavaDoc(12);
245       interfaces = new ArrayList JavaDoc(12);
246       exceptionClasses = new ArrayList JavaDoc(12);
247       errorClasses = new ArrayList JavaDoc(12);
248       
249       for (int i=0; i<classes.size(); i++)
250       {
251          addClass((ClassType)classes.get(i));
252       }
253    }
254
255    public void addClasses(ClassDoc[] classes)
256    {
257       for (int i=0; i<classes.length; i++)
258       {
259          // algorithm: innerclasses will be added as children of parent classes
260
// therefore skip over them here.
261
if (classes[i].containingClass() == null)
262             addClass(classes[i]);
263       }
264    }
265    
266    public void addClass(ClassDoc classdoc)
267    {
268       log.verbose(classdoc.qualifiedName());
269       addClass(new ClassType(classdoc, this, getAPI()));
270    }
271    
272    public void addClass(ClassType classtype)
273    {
274       classes.add(classtype);
275       switch (classtype.getClassType())
276       {
277          case ClassType.ORDINARY_CLASS:
278          {
279             ordinaryClasses.add(classtype);
280             break;
281          }
282          case ClassType.INTERFACE:
283          {
284             interfaces.add(classtype);
285             break;
286          }
287          case ClassType.ERROR_CLASS:
288          {
289             errorClasses.add(classtype);
290             break;
291          }
292          case ClassType.EXCEPTION_CLASS:
293          {
294             exceptionClasses.add(classtype);
295             break;
296          }
297       }
298    }
299    
300    public List JavaDoc getOrdinaryClasses() { return ordinaryClasses; }
301    public List JavaDoc getExceptionClasses() { return exceptionClasses; }
302    public List JavaDoc getInterfaces() { return interfaces; }
303    public List JavaDoc getErrorClasses() { return errorClasses; }
304    
305    public boolean hasOrdinaryClasses() { return !ordinaryClasses.isEmpty(); }
306    public boolean hasExceptionClasses() { return !exceptionClasses.isEmpty(); }
307    public boolean hasInterfaces() { return !interfaces.isEmpty(); }
308    public boolean hasErrorClasses() { return !errorClasses.isEmpty(); }
309
310    public API getAPI() { return api; }
311    public void setAPI(API api) { this.api = api; }
312
313    public static JPackage makePackageFor(Connection JavaDoc conn, int pkgId) throws SQLException JavaDoc
314    {
315       /*
316       String sql =
317          "select p.id, p.name " +
318          " d.summarydescription, d.description, d.since, d.deprecated, d.id " +
319          " from package p, doc d " +
320          " where p.id=? " +
321          " and p.docid=d.id " +
322          " order by p.name ";
323        */

324       String JavaDoc sql = DBMgr.getInstance().getStatement("makepackage");
325
326       PreparedStatement JavaDoc pstmt = conn.prepareStatement(sql);
327       pstmt.setInt(1, pkgId);
328       ResultSet JavaDoc rset = pstmt.executeQuery();
329       
330       if (!rset.next())
331          return null;
332       
333       JPackage pkg = new JPackage(rset.getString(2));
334       pkg.setId(rset.getInt(1));
335       pkg.setDoc(new DocInfo(rset.getString(3), rset.getString(5), rset.getString(6), rset.getString(4)));
336       pkg.getDoc().setId(rset.getInt(7));
337       String JavaDoc apiname = rset.getString(8);
338       if (!StringUtils.isBlank(apiname))
339       {
340          API api = new API(rset.getString(8));
341          api.setId(rset.getInt(9));
342          pkg.setAPI(api);
343       }
344       
345       rset.close();
346       pstmt.close();
347       
348       pkg.getClassInfo(conn);
349       pkg.getDoc().fetchRefs(conn);
350       
351       return pkg;
352    }
353    
354    public void getClassInfo(Connection JavaDoc conn) throws SQLException JavaDoc
355    {
356       /*
357       String sql =
358          "select c.qualifiedname, c.type, c.id, c.superclassname, s.superclassid, " +
359          " d.summarydescription, d.since, d.deprecated, " +
360          " c.isstatic, c.isfinal, c.isabstract, c.accessibility, c.modifier " +
361          " from classtype c, doc d, superclass s " +
362          " where c.packageid=? and c.docid=d.id and c.id=s.classid (+)" +
363          " order by c.type, c.name, isstatic, isabstract";
364        */

365       String JavaDoc sql = DBMgr.getInstance().getStatement("classinfo");
366       
367       PreparedStatement JavaDoc pstmt = conn.prepareStatement(sql);
368       pstmt.setInt(1, getId());
369       ResultSet JavaDoc rset = pstmt.executeQuery();
370       
371       ClassType classtype;
372       while (rset.next())
373       {
374          classtype = new ClassType(rset.getString(1));
375          classtype.setClassType(rset.getInt(2));
376          classtype.setId(rset.getInt(3));
377          classtype.setDoc(new DocInfo(rset.getString(6), rset.getString(7), rset.getString(8)));
378          classtype.setSuperClassName(rset.getString(4));
379          classtype.setStatic(rset.getBoolean(9));
380          classtype.setFinal(rset.getBoolean(10));
381          classtype.setAbstract(rset.getBoolean(11));
382          classtype.setAccessibility(rset.getInt(12));
383          classtype.setModifiers(rset.getString(13));
384          
385          int superid = rset.getInt(5);
386          if (superid > 0)
387          {
388               classtype.setSuperClass(new ClassType(classtype.getSuperClassName()));
389               classtype.getSuperClass().setId(superid);
390          }
391          addClass(classtype);
392       }
393       
394       rset.close();
395       pstmt.close();
396    }
397    
398    
399    public TreeNode buildTree(Connection JavaDoc conn) throws SQLException JavaDoc
400    {
401       List JavaDoc classes = getClasses();
402       List JavaDoc clstreelist = new ArrayList JavaDoc();
403       ClassType c;
404       
405       for (int i=0; i<classes.size(); i++)
406       {
407          c = (ClassType) classes.get(i);
408          //log.debug("Target class: "+c.getQualifiedName());
409

410          TreeNode clstree = c.getSuperclasses(conn);
411          
412          //log.debug(TreeNode.printTree(clstree,0));
413
clstreelist.add(clstree);
414       }
415       
416       TreeNode pkgNode = new TreeNode(this);
417
418       TreeNode clstree;
419
420       log.debug(clstreelist.size()+" little trees to merge");
421
422       for (int i=0; i<clstreelist.size(); i++)
423       {
424          clstree = (TreeNode) clstreelist.get(i);
425          String JavaDoc key = ((ClassType) clstree.getValue()).getQualifiedName();
426          //log.debug(key);
427

428          TreeNode node = pkgNode;
429          TreeNode targetnode = clstree;
430          String JavaDoc targetkey = key;
431          
432          while(node.getChild(targetkey)!=null)
433          {
434             //log.debug(targetkey + " already in tree..");
435
node = node.getChild(targetkey);
436             targetnode = targetnode.getOnlyChild();
437             if (targetnode == null)
438             {
439                continue;
440             }
441             targetkey = ((ClassType) targetnode.getValue()).getQualifiedName();
442          }
443          if (targetnode == null)
444          {
445             continue;
446          }
447          
448          if (node.getValue() instanceof ClassType)
449          {
450             log.debug(targetkey + " not in tree\n" +
451                            " adding to : "+((ClassType) node.getValue()).getQualifiedName()+ "\n" +
452                            " ( should be : "+((ClassType)targetnode.getValue()).getSuperClassName()+")");
453          }
454
455          String JavaDoc clsname = ((ClassType)targetnode.getValue()).getQualifiedName();
456          String JavaDoc supercls = ((ClassType)targetnode.getValue()).getSuperClassName();
457          if (!StringUtils.isBlank(supercls) && !clsname.equals("java.lang.Object") &&
458              node.getValue() instanceof JPackage)
459          {
460             if (node.getChild("java.lang.Object")==null)
461                continue;
462             
463             TreeNode newnode = null;
464             
465             if (node.getChild("java.lang.Object").getChild(supercls)==null)
466             {
467                newnode = new TreeNode();
468             } else
469             {
470                newnode = node.getChild("java.lang.Object").getChild(supercls);
471             }
472             //construct classtype for absent superclass:
473
ClassType absentparent = new ClassType(supercls);
474             absentparent.setClassType(JDocUtil.UNKNOWN_TYPE);
475             absentparent.setPackage(this);
476             absentparent.setLevel(2);
477             absentparent.setDoc(new DocInfo("","",""));
478             absentparent.setSuperClassName("java.lang.Object");
479             absentparent.setSuperClass((ClassType) node.getChild("java.lang.Object").getValue());
480             
481             newnode.setValue(absentparent);
482             
483             ClassType targetCls = (ClassType) targetnode.getValue();
484             targetCls.setLevel(3);
485
486             targetnode.setParent(newnode);
487             newnode.addChild(targetkey, targetnode);
488             
489             node.getChild("java.lang.Object").addChild(supercls, newnode);
490          }
491          else
492          {
493             node.addChild(targetkey, targetnode);
494          }
495          
496          //log.debug(TreeNode.printTree(pkgNode,0));
497
//if (i > 3) { break; }
498
}
499       
500       return pkgNode;
501    }
502    
503    private TreeNode makeSimpleClassTree(List JavaDoc ctree)
504    {
505       TreeNode root = new TreeNode(ctree.get(0));
506       ClassType c = null;
507       TreeNode n = root;
508       for (int i=1; i<ctree.size(); i++)
509       {
510          c = (ClassType) ctree.get(i);
511          
512          n.addChild(c.getQualifiedName(), new TreeNode(c));
513          n = n.getOnlyChild();
514       }
515       return root;
516    }
517    
518    public String JavaDoc toString()
519    {
520       return getName();
521    }
522    
523    public static void main(String JavaDoc[] args)
524    {
525       DBMgr mgr = null;
526       Connection JavaDoc conn = null;
527       Logger.getInstance().setTraceLevel(Logger.DEBUG);
528       
529       try
530       {
531          mgr = DBMgr.getInstance();
532          conn = mgr.getConnection();
533          JPackage pkg = JPackage.makePackageFor(conn, 102);
534          TreeNode pkgTree = pkg.buildTree(conn);
535          System.out.println(TreeNode.printTree(pkgTree,0));
536       } catch (SQLException JavaDoc ex)
537       {
538          ex.printStackTrace();
539          DBUtils.logSQLException(ex);
540       }
541       finally
542       {
543          mgr.releaseConnection(conn);
544       }
545    }
546    
547
548    public String JavaDoc getSummaryDescription()
549    {
550       return getDoc().getSummaryDescription();
551    }
552    public String JavaDoc getDescription()
553    {
554       return getDoc().getDescription();
555    }
556    public String JavaDoc getSince()
557    {
558       return getDoc().getSince();
559    }
560    public String JavaDoc getDeprecatedDescr()
561    {
562       return getDoc().getDeprecated();
563    }
564    public boolean isDeprecated()
565    {
566       return getDoc().isDeprecated();
567    }
568    public List JavaDoc getReferences()
569    {
570       return getDoc().getReferences();
571    }
572
573    public static String JavaDoc getTableName()
574    {
575      return TABLENAME;
576    }
577    
578    public String JavaDoc getStyle() { return "package"; }
579
580  
581 }
582
Popular Tags