1 package com.puppycrawl.tools.checkstyle.doclets; 20 21 import java.io.FileNotFoundException ; 22 import java.io.FileOutputStream ; 23 import java.io.PrintStream ; 24 25 import com.sun.javadoc.ClassDoc; 26 import com.sun.javadoc.DocErrorReporter; 27 import com.sun.javadoc.FieldDoc; 28 import com.sun.javadoc.RootDoc; 29 30 36 public class TokenTypesDoclet 37 { 38 39 private static final String DEST_FILE_OPT = "-destfile"; 40 41 48 public static boolean start(RootDoc aRoot) throws FileNotFoundException 49 { 50 final String fileName = getDestFileName(aRoot.options()); 51 final FileOutputStream fos = new FileOutputStream (fileName); 52 final PrintStream ps = new PrintStream (fos); 53 final ClassDoc[] classes = aRoot.classes(); 54 if ((classes.length != 1) || !classes[0].name().equals("TokenTypes")) { 55 final String message = 56 "The doclet should be used for TokenTypes only"; 57 throw new IllegalArgumentException (message); 58 } 59 60 final FieldDoc[] fields = classes[0].fields(); 61 for (int j = 0; j < fields.length; j++) { 62 final FieldDoc field = fields[j]; 63 if (field.isStatic() && field.isPublic() && field.isFinal() 64 && "int".equals((field.type().qualifiedTypeName()))) 65 { 66 if (field.firstSentenceTags().length != 1) { 67 final String message = "Should be only one tag."; 68 throw new IllegalArgumentException (message); 69 } 70 ps.println(field.name() + "=" 71 + field.firstSentenceTags()[0].text()); 72 } 73 } 74 75 ps.close(); 76 return true; 77 } 78 79 84 public static int optionLength(String aOption) 85 { 86 if (DEST_FILE_OPT.equals(aOption)) { 87 return 2; 88 } 89 return 0; 90 } 91 92 98 public static boolean validOptions(String aOptions[][], 99 DocErrorReporter aReporter) 100 { 101 boolean foundDestFileOption = false; 102 for (int i = 0; i < aOptions.length; i++) { 103 final String [] opt = aOptions[i]; 104 if (DEST_FILE_OPT.equals(opt[0])) { 105 if (foundDestFileOption) { 106 aReporter.printError("Only one -destfile option allowed."); 107 return false; 108 } 109 foundDestFileOption = true; 110 } 111 } 112 if (!foundDestFileOption) { 113 final String message = 114 "Usage: javadoc -destfile file -doclet TokenTypesDoclet ..."; 115 aReporter.printError(message); 116 } 117 return foundDestFileOption; 118 } 119 120 125 private static String getDestFileName(String [][] aOptions) 126 { 127 String fileName = null; 128 for (int i = 0; i < aOptions.length; i++) { 129 final String [] opt = aOptions[i]; 130 if (DEST_FILE_OPT.equals(opt[0])) { 131 fileName = opt[1]; 132 } 133 } 134 return fileName; 135 } 136 } 137 | Popular Tags |