1 20 package com.puppycrawl.tools.checkstyle.doclets; 21 22 import com.sun.javadoc.RootDoc; 23 import com.sun.javadoc.ClassDoc; 24 import com.sun.javadoc.Tag; 25 26 import java.io.IOException ; 27 import java.io.PrintWriter ; 28 import java.io.File ; 29 import java.io.FileWriter ; 30 import java.util.Arrays ; 31 import java.util.Comparator ; 32 33 40 public final class CheckDocsDoclet 41 { 42 43 private static final String DEST_DIR_OPT = "-d"; 44 45 49 private static class ClassDocByCheckNameComparator implements Comparator 50 { 51 52 public int compare(Object aObject1, Object aObject2) 53 { 54 final ClassDoc classDoc1 = (ClassDoc) aObject1; 55 final ClassDoc classDoc2 = (ClassDoc) aObject2; 56 final String checkName1 = getCheckName(classDoc1); 57 final String checkName2 = getCheckName(classDoc2); 58 return checkName1.compareTo(checkName2); 59 } 60 } 61 62 68 private static String getDescription(final ClassDoc aClassDoc) 69 { 70 final Tag[] tags = aClassDoc.firstSentenceTags(); 71 final StringBuffer buf = new StringBuffer (); 72 if (tags.length > 0) { 73 buf.append(tags[0].text()); 74 } 75 removeOpeningParagraphTag(buf); 76 return buf.toString(); 77 } 78 79 83 private static void removeOpeningParagraphTag(final StringBuffer aText) 84 { 85 final String openTag = "<p>"; 86 final int tagLen = openTag.length(); 87 if ((aText.length() > tagLen) 88 && aText.substring(0, tagLen).equals(openTag)) 89 { 90 aText.delete(0, tagLen); 91 } 92 } 93 94 101 private static String getCheckName(final ClassDoc aClassDoc) 102 { 103 final String strippedClassName = aClassDoc.typeName(); 104 final String checkName; 105 if (strippedClassName.endsWith("Check")) { 106 checkName = strippedClassName.substring( 107 0, strippedClassName.length() - "Check".length()); 108 } 109 else { 110 checkName = strippedClassName; 111 } 112 return checkName; 113 } 114 115 120 private static void writeXdocsHeader( 121 final PrintWriter aPrintWriter, 122 final String aTitle) 123 { 124 aPrintWriter.println("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>"); 125 aPrintWriter.println("<document>"); 126 aPrintWriter.println("<properties>"); 127 aPrintWriter.println("<title>" + aTitle + "</title>"); 128 aPrintWriter.println("<author " 129 + "email=\"checkstyle-devel@lists.sourceforge.net" 130 + "\">Checkstyle Development Team</author>"); 131 aPrintWriter.println("</properties>"); 132 aPrintWriter.println("<body>"); 133 aPrintWriter.flush(); 134 } 135 136 140 private static void writeXdocsFooter(final PrintWriter aPrintWriter) 141 { 142 aPrintWriter.println("</body>"); 143 aPrintWriter.println("</document>"); 144 aPrintWriter.flush(); 145 } 146 147 153 public static boolean start(RootDoc aRoot) throws IOException 154 { 155 final ClassDoc[] classDocs = aRoot.classes(); 156 157 final File destDir = new File (getDestDir(aRoot.options())); 158 159 final File checksIndexFile = new File (destDir, "availablechecks.xml"); 160 final PrintWriter fileWriter = new PrintWriter ( 161 new FileWriter (checksIndexFile)); 162 writeXdocsHeader(fileWriter, "Available Checks"); 163 164 fileWriter.println("<p>Checkstyle provides many checks that you can" 165 + " apply to your sourcecode. Below is an alphabetical" 166 + " reference, the site navigation menu provides a reference" 167 + " organized by functionality.</p>"); 168 fileWriter.println("<table>"); 169 170 Arrays.sort(classDocs, new ClassDocByCheckNameComparator()); 171 172 for (int i = 0; i < classDocs.length; i++) { 173 174 final ClassDoc classDoc = classDocs[i]; 175 176 if (classDoc.typeName().endsWith("Check") 179 && !classDoc.isAbstract()) 180 { 181 String pageName = getPageName(classDoc); 182 183 final Tag[] docPageTags = classDoc.tags("checkstyle-docpage"); 186 if ((docPageTags != null) && (docPageTags.length > 0)) { 187 pageName = docPageTags[0].text(); 188 } 189 190 final String descr = getDescription(classDoc); 191 final String checkName = getCheckName(classDoc); 192 193 194 fileWriter.println("<tr>" 195 + "<td><a HREF=\"" 196 + "config_" + pageName + ".html#" + checkName 197 + "\">" + checkName + "</a></td><td>" 198 + descr 199 + "</td></tr>"); 200 } 201 } 202 203 fileWriter.println("</table>"); 204 writeXdocsFooter(fileWriter); 205 fileWriter.close(); 206 207 return true; 208 } 209 210 216 private static String getPageName(ClassDoc aClassDoc) 217 { 218 final String packageName = aClassDoc.containingPackage().name(); 219 final String pageName = 220 packageName.substring(packageName.lastIndexOf('.') + 1); 221 if ("checks".equals(pageName)) { 222 return "misc"; 223 } 224 return pageName; 225 } 226 227 232 public static String getDestDir(String [][] aOptions) 233 { 234 for (int i = 0; i < aOptions.length; i++) { 235 final String [] opt = aOptions[i]; 236 if (DEST_DIR_OPT.equalsIgnoreCase(opt[0])) { 237 return opt[1]; 238 } 239 } 240 return null; } 242 243 248 public static int optionLength(String aOption) 249 { 250 if (DEST_DIR_OPT.equals(aOption)) { 251 return 2; 252 } 253 return 0; 254 } 255 256 } 257 | Popular Tags |