1 5 package org.h2.tools.doclet; 6 7 import java.io.BufferedWriter ; 8 import java.io.File ; 9 import java.io.FileWriter ; 10 import java.io.IOException ; 11 import java.io.PrintWriter ; 12 import java.util.Arrays ; 13 import java.util.Comparator ; 14 15 import com.sun.javadoc.ClassDoc; 16 import com.sun.javadoc.FieldDoc; 17 import com.sun.javadoc.MethodDoc; 18 import com.sun.javadoc.ParamTag; 19 import com.sun.javadoc.Parameter; 20 import com.sun.javadoc.RootDoc; 21 import com.sun.javadoc.Tag; 22 import com.sun.javadoc.ThrowsTag; 23 import com.sun.javadoc.Type; 24 25 public class Doclet { 26 public static boolean start(RootDoc root) throws IOException { 27 ClassDoc[] classes = root.classes(); 28 String [][] options = root.options(); 29 String destDir = "docs/javadoc"; 30 for(int i=0; i<options.length; i++) { 31 if(options[i][0].equals("destdir")) { 32 destDir = options[i][1]; 33 } 34 } 35 for (int i = 0; i < classes.length; ++i) { 36 ClassDoc clazz = classes[i]; 37 processClass(destDir, clazz); 38 } 39 return true; 40 } 41 42 private static String getClass(String name) { 43 if(name.startsWith("Jdbc")) { 44 return name.substring(4); 45 } 46 return name; 47 } 48 49 private static void processClass(String destDir, ClassDoc clazz) throws IOException { 50 String packageName = clazz.containingPackage().name(); 51 String dir = destDir +"/"+ packageName.replace('.', '/'); 52 (new File (dir)).mkdirs(); 53 String fileName = dir + "/" + clazz.name() + ".html"; 54 String className = getClass(clazz.name()); 55 FileWriter out = new FileWriter (fileName); 56 PrintWriter writer = new PrintWriter (new BufferedWriter (out)); 57 writer.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">"); 58 writer.println("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\"><title>"); 59 writer.println(className); 60 writer.println("</title><link rel=\"stylesheet\" type=\"text/css\" HREF=\"../../../stylesheet.css\"></head><body>"); 61 writer.println("<table class=\"content\"><tr class=\"content\"><td class=\"content\"><div class=\"contentDiv\">"); 62 63 writer.println("<h1>"+className+"</h1>"); 64 writer.println(clazz.commentText()+"<br><br>"); 65 66 MethodDoc[] methods = clazz.methods(); 67 Arrays.sort(methods, new Comparator () { 68 public int compare(Object a, Object b) { 69 return ((MethodDoc)a).name().compareTo(((MethodDoc)b).name()); 70 } 71 }); 72 writer.println("<table><tr><th colspan=\"2\">Methods</th></tr>"); 73 for(int i=0; i<methods.length; i++) { 74 MethodDoc method = methods[i]; 75 String name = method.name(); 76 if(skipMethod(method)) { 77 continue; 78 } 79 String type = getTypeName(method.isStatic(), method.returnType()); 80 writer.println("<tr><td class=\"return\">" + type + "</td><td class=\"method\">"); 81 Parameter[] params = method.parameters(); 82 StringBuffer buff = new StringBuffer (); 83 buff.append('('); 84 for(int j=0; j<params.length; j++) { 85 if(j>0) { 86 buff.append(", "); 87 } 88 buff.append(getTypeName(false, params[j].type())); 89 buff.append(' '); 90 buff.append(params[j].name()); 91 } 92 buff.append(')'); 93 if(isDeprecated(method)) { 94 name = "<span class=\"deprecated\">" + name + "</span>"; 95 } 96 writer.println("<a HREF=\"#r" + i + "\">" + name + "</a>"+buff.toString()); 97 String firstSentence = getFirstSentence(method.firstSentenceTags()); 98 if(firstSentence != null) { 99 writer.println("<div class=\"methodText\">"+firstSentence+"</div>"); 100 } 101 writer.println("</td></tr>"); 102 } 103 writer.println("</table>"); 104 FieldDoc[] fields = clazz.fields(); 105 if(clazz.interfaces().length > 0) { 106 fields = clazz.interfaces()[0].fields(); 107 } 108 Arrays.sort(fields, new Comparator () { 109 public int compare(Object a, Object b) { 110 return ((FieldDoc)a).name().compareTo(((FieldDoc)b).name()); 111 } 112 }); 113 int fieldId=0; 114 for(int i=0; i<fields.length; i++) { 115 FieldDoc field = fields[i]; 116 if(!field.isFinal() || !field.isStatic() || !field.isPublic()) { 117 continue; 118 } 119 if(fieldId==0) { 120 writer.println("<br><table><tr><th colspan=\"2\">Fields</th></tr>"); 121 } 122 String name = field.name(); 123 String type = getTypeName(true, field.type()); 124 writer.println("<tr><td class=\"return\">" + type + "</td><td class=\"method\">"); 125 writer.println(name + " = " + field.constantValueExpression()); 127 String firstSentence = getFirstSentence(field.firstSentenceTags()); 128 if(firstSentence != null) { 129 writer.println("<div class=\"methodText\">"+firstSentence+"</div>"); 130 } 131 writer.println("</td></tr>"); 132 fieldId++; 133 } 134 if(fieldId > 0) { 135 writer.println("</table>"); 136 } 137 138 for(int i=0; i<methods.length; i++) { 139 MethodDoc method = methods[i]; 140 String name = method.name(); 141 if(skipMethod(method)) { 142 continue; 143 } 144 String type = getTypeName(method.isStatic(), method.returnType()); 145 writer.println("<a name=\"r"+i+"\"></a>"); 146 Parameter[] params = method.parameters(); 147 StringBuffer buff = new StringBuffer (); 148 buff.append('('); 149 for(int j=0; j<params.length; j++) { 150 if(j>0) { 151 buff.append(", "); 152 } 153 buff.append(getTypeName(false, params[j].type())); 154 buff.append(' '); 155 buff.append(params[j].name()); 156 } 157 buff.append(')'); 158 ClassDoc[] exceptions = method.thrownExceptions(); 159 if(exceptions.length>0) { 160 buff.append(" throws "); 161 for(int k=0; k<exceptions.length; k++) { 162 if(k>0) { 163 buff.append(", "); 164 } 165 buff.append(exceptions[k].typeName()); 166 } 167 } 168 if(isDeprecated(method)) { 169 name = "<span class=\"deprecated\">" + name + "</span>"; 170 } 171 writer.println("<h4>"+ type + " <span class=\"methodName\">"+name+"</span>" + buff.toString()+"</h4>"); 172 writer.println(method.commentText()); 173 ParamTag[] paramTags = method.paramTags(); 174 boolean space = false; 175 for(int j=0; j<paramTags.length; j++) { 176 if(!space) { 177 writer.println("<br><br >"); 178 space = true; 179 } 180 String p = paramTags[j].parameterName() + " - " + paramTags[j].parameterComment(); 181 if(j==0) { 182 writer.println("<div class=\"itemTitle\">Parameters:</div>"); 183 } 184 writer.println("<div class=\"item\">"+p+"</div>"); 185 } 186 Tag[] returnTags = method.tags("return"); 187 if(returnTags != null && returnTags.length>0) { 188 if(!space) { 189 writer.println("<br><br >"); 190 space = true; 191 } 192 writer.println("<div class=\"itemTitle\">Returns:</div>"); 193 writer.println("<div class=\"item\">"+returnTags[0].text()+"</div>"); 194 } 195 ThrowsTag[] throwsTags = method.throwsTags(); 196 if(throwsTags != null && throwsTags.length > 0) { 197 if(!space) { 198 writer.println("<br><br >"); 199 space = true; 200 } 201 writer.println("<div class=\"itemTitle\">Throws:</div>"); 202 for(int j=0; j<throwsTags.length; j++) { 203 String p = throwsTags[j].exceptionName(); 204 String c = throwsTags[j].exceptionComment(); 205 if(c.length() > 0) { 206 p += " - " + c; 207 } 208 writer.println("<div class=\"item\">"+p+"</div>"); 209 } 210 } 211 writer.println("<hr>"); 212 } 213 writer.println("</div></td></tr></table></body></html>"); 214 writer.close(); 215 out.close(); 216 } 217 218 private static boolean skipMethod(MethodDoc method) { 219 String name = method.name(); 220 if(!method.isPublic() || name.equals("finalize")) { 221 return true; 222 } 223 if(method.getRawCommentText().startsWith("@deprecated INTERNAL")) { 224 return true; 225 } 226 String firstSentence = getFirstSentence(method.firstSentenceTags()); 227 if(firstSentence==null || firstSentence.trim().length()==0) { 228 throw new Error ("undocumented method? " + name+ " " + method.containingClass().name()+" "+method.getRawCommentText()); 229 } 230 if(firstSentence.startsWith("INTERNAL")) { 231 return true; 232 } 233 return false; 234 } 235 236 private static String getFirstSentence(Tag[] tags) { 237 238 String firstSentence = null; 239 if(tags.length>0) { 240 Tag first = tags[0]; 241 firstSentence = first.text(); 242 } 243 return firstSentence; 244 } 245 246 private static String getTypeName(boolean isStatic, Type type) { 247 String s = type.typeName() + type.dimension(); 248 if(isStatic) { 249 s = "static " + s; 250 } 251 return s; 252 } 253 254 private static boolean isDeprecated(MethodDoc method) { 255 Tag[] tags = method.tags(); 256 boolean deprecated = false; 257 for(int j=0; j<tags.length; j++) { 258 Tag t = tags[j]; 259 if(t.kind().equals("@deprecated")) { 260 deprecated = true; 261 } 262 } 263 return deprecated; 264 } 265 } 266 | Popular Tags |