KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ashkelon > util > JDocUtil


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

6
7 import java.io.BufferedReader JavaDoc;
8 import java.io.FileReader JavaDoc;
9 import java.io.IOException JavaDoc;
10 import java.util.ArrayList JavaDoc;
11 import java.util.HashMap JavaDoc;
12 import java.util.List JavaDoc;
13 import java.util.Map JavaDoc;
14
15 import org.ashkelon.DocInfo;
16 import org.ashkelon.pages.ConfigInfo;
17
18 import com.sun.javadoc.ParamTag;
19 import com.sun.javadoc.ProgramElementDoc;
20 import com.sun.javadoc.Tag;
21 import com.sun.javadoc.Type;
22
23 /**
24  * A class containing various utility methods for dealing with the javadoc object
25  * model com.sun.javadoc
26  *
27  * @author Eitan Suez
28  */

29 public class JDocUtil
30 {
31    public static final int UNKNOWN_TYPE = -123;
32    
33    /** programElement accessibility constants */
34    public static final int PUBLIC = 1;
35     public static final int PROTECTED = 2;
36     public static final int PRIVATE = 3;
37     public static final int PACKAGEPRIVATE = 4;
38
39     public static final String JavaDoc[] ACCESSIBILITIES = {"public", "protected", "private", "packageprivate"};
40    
41    /**
42     * @param dim number of array dimensions
43     * @return "[]" x dim (in perl speak)
44     */

45    public static String JavaDoc getDimension(int dim)
46    {
47       return StringUtils.join("[]", "", dim);
48    }
49    
50    /**
51     * @return number of array dimensions for Type
52     * @param type a javadoc type
53     */

54    public static int getDimension(Type type)
55    {
56       if (type.dimension() == null) return 0;
57       return (type.dimension().length()/2);
58    }
59
60    /**
61     * @param qualifiedName qualified name of a program element doc
62     * @return unqualified portion of qualifiedName
63     */

64    public static String JavaDoc unqualify(String JavaDoc qualifiedName)
65    {
66       return unqualify(qualifiedName, false);
67    }
68    
69    public static String JavaDoc unqualify(String JavaDoc qualifiedName, boolean isNestedClass)
70    {
71       if (qualifiedName == null) { return ""; }
72       
73       int idx = qualifiedName.indexOf("(");
74       boolean isexecmember = (idx > -1);
75       if (isexecmember)
76       {
77          String JavaDoc end = qualifiedName.substring(idx);
78          String JavaDoc start = qualifiedName.substring(0,idx);
79          int index = start.lastIndexOf(".");
80          if (index < 0) { return qualifiedName; }
81          return start.substring(index+1) + end;
82       }
83       else
84       {
85          int index = qualifiedName.lastIndexOf(".");
86          if (index < 0) { return qualifiedName; }
87
88          if (isNestedClass)
89          {
90             String JavaDoc firstPart = qualifiedName.substring(0, index);
91             index = firstPart.lastIndexOf(".");
92             if (index < 0) { return qualifiedName; }
93          }
94          
95          return qualifiedName.substring(index+1);
96       }
97    }
98
99    /**
100     * potential problems with this method: e.g. pkgName = java.awt
101     * and name = java.awt.event.MouseMotionListener
102     * even though name is in different package, the java.awt. part
103     * will be truncated. need to rethink this.
104     */

105    public static String JavaDoc conditionalQualify(String JavaDoc name, String JavaDoc pkgName)
106    {
107       if (name.startsWith(pkgName+"."))
108       {
109          return name.substring(pkgName.length()+1);
110       }
111       else if (name.startsWith("java.lang."))
112       {
113          return name.substring("java.lang.".length());
114       }
115       else
116       {
117          return name;
118       }
119    }
120    
121    public static boolean isQualified(String JavaDoc name)
122    {
123       return (name.indexOf(".")>=0);
124    }
125    
126    /**
127     * returns the contents of a sequence of tag objects as
128     * one text string
129     */

130    public static String JavaDoc getTagText(Tag tags[])
131    {
132       String JavaDoc text = "";
133       for (int i=0; i<tags.length; i++)
134       {
135          text += tags[i].text() + " ";
136       }
137       return text.trim();
138    }
139    
140    /**
141     * resolves all inline tags in a description into html links
142     * @param tags text represented as an array of tags, as returned by doc.inlineTags()
143     * @return resolved text
144     */

145    public static String JavaDoc resolveDescription(DocInfo sourcedoc, Tag tags[])
146    {
147       return ConfigInfo.getResolver().resolveDescription(sourcedoc, tags);
148    }
149    
150    /**
151     * returns a list of tags as an array of text strings
152     * (assumes that within one tag's text, there may be > 1 entry
153     * where entries are separated with a comma -- the author tag
154     * behaves this way)
155     */

156    public static String JavaDoc[] getTagList(Tag tags[])
157    {
158       List JavaDoc list = new ArrayList JavaDoc();
159       for (int i=0; i<tags.length; i++)
160       {
161          String JavaDoc[] vals = StringUtils.split(tags[i].text(), ",");
162          for (int j=0; j<vals.length; j++)
163          {
164             list.add(vals[j]);
165          }
166       }
167       String JavaDoc[] stringlist = new String JavaDoc[list.size()];
168       list.toArray(stringlist);
169       return stringlist;
170    }
171    
172    /**
173     * @return whether type is a primitive type (int, short, etc..)
174     */

175    public static boolean isPrimitive(Type type)
176    {
177       return (type.asClassDoc() == null);
178    }
179    
180    /**
181     * @return accessibility of programElement (PUBLIC, PRIVATE, etc..)
182     */

183    public static int getAccessibility(ProgramElementDoc programElement)
184    {
185       if (programElement.isPublic())
186       {
187          return PUBLIC;
188       }
189       else if (programElement.isProtected())
190       {
191          return PROTECTED;
192       }
193       else if (programElement.isPrivate())
194       {
195          return PRIVATE;
196       }
197       else
198       {
199          return PACKAGEPRIVATE;
200       }
201    }
202    
203    /**
204     * @return parameter name to comment map
205     */

206     public static Map JavaDoc makeParamMap(DocInfo sourcedoc, ParamTag[] paramTags)
207     {
208        Map JavaDoc paramInfo = new HashMap JavaDoc();
209        String JavaDoc paramdescription = "";
210        for (int i=0; i<paramTags.length; i++)
211        {
212           //paramInfo.put(paramTags[i].parameterName(), paramTags[i].parameterComment());
213
paramdescription = resolveDescription(sourcedoc, paramTags[i].inlineTags());
214           paramInfo.put(paramTags[i].parameterName(), paramdescription);
215        }
216        return paramInfo;
217     }
218     
219     /**
220      * provides ability to specify package list to process indirectly by providing
221      * file name that contains a list of packages sited one per line
222      * (as in javadoc @pkgnamesfile)
223      */

224     public static List JavaDoc getPackageListFromFileName(String JavaDoc filename)
225     {
226        List JavaDoc pkgs = new ArrayList JavaDoc(12);
227        if (filename==null) return pkgs;
228        if (filename.startsWith("@"))
229           filename = filename.substring(1);
230        try
231        {
232           BufferedReader JavaDoc br = new BufferedReader JavaDoc(new FileReader JavaDoc(filename));
233           String JavaDoc pkg = null;
234           while ((pkg=br.readLine()) != null)
235           {
236              pkgs.add(pkg);
237           }
238        }
239        catch (IOException JavaDoc ex)
240        {
241        }
242        return pkgs;
243     }
244     
245 }
246
Popular Tags