1 52 53 package com.go.beandoc; 54 55 import com.go.beandoc.teadoc.*; 56 57 import java.io.*; 58 59 import com.go.tea.runtime.TemplateLoader; 60 61 74 public class BeanDoc extends com.sun.javadoc.Doclet { 75 76 77 private static final String DEFAULT_TEMPLATE_CLASS_NAME = 78 "com.go.beandoc.template.BeanInfo"; 79 80 84 87 public static boolean start(com.sun.javadoc.RootDoc root) { 88 try { 89 BeanDoc doclet = new BeanDoc(root); 90 doclet.start(); 91 } 92 catch (Exception e) { 93 e.printStackTrace(); 94 return false; 95 } 96 97 return true; 98 } 99 100 101 public static int optionLength(String option) { 102 if (option.equals("-d")) { 103 return 2; 104 } 105 else { 106 return 0; 107 } 108 } 109 110 114 private RootDoc mRootDoc; 115 private File mDest; 116 private String mTemplateClassName; 117 private TemplateLoader.Template mTemplate; 118 119 123 126 public BeanDoc(com.sun.javadoc.RootDoc root) throws Exception { 127 128 String [][] options = root.options(); 129 String dest = "."; 130 131 for (int i = 0; i < options.length; i++) { 132 String [] values = options[i]; 133 String key = values[0]; 134 135 if (key.equals("-d") && values.length > 1) { 136 dest = values[1]; 137 } 138 } 139 140 init(root, dest, DEFAULT_TEMPLATE_CLASS_NAME); 142 } 143 144 148 public void start() { 149 150 ClassDoc[] classDocs = mRootDoc.getClasses(); 151 152 for (int i = 0; i < classDocs.length; i++) { 153 154 ClassDoc classDoc = classDocs[i]; 155 if (!accept(classDoc)) { 156 continue; 157 } 158 159 try { 160 generateBeanInfo(classDoc); 161 } 162 catch (RuntimeException e) { 163 throw e; 164 } 165 catch (Exception e) { 166 e.printStackTrace(); 167 throw new RuntimeException (e.toString()); 168 } 169 } 170 } 171 172 175 public RootDoc getRootDoc() { 176 return mRootDoc; 177 } 178 179 180 183 public void printError(String msg) { 184 mRootDoc.printError(msg); 185 } 186 187 190 public void printWarning(String msg) { 191 mRootDoc.printWarning(msg); 192 } 193 194 197 public void printNotice(String msg) { 198 mRootDoc.printNotice(msg); 199 } 200 201 205 206 209 protected void init(com.sun.javadoc.RootDoc root, 210 String dest, 211 String templateClassName) throws Exception { 212 213 mRootDoc = new RootDoc(root); 214 215 mDest = new File(dest); 216 mDest.mkdirs(); 217 218 mTemplateClassName = templateClassName; 219 if (mTemplateClassName == null) { 220 printWarning("No template name"); 221 return; 222 } 223 224 String [] templatePath = ClassDoc.parseClassName(mTemplateClassName); 225 226 230 TemplateLoader loader = 231 new TemplateLoader(getClass().getClassLoader(), 232 templatePath[0]); 233 234 mTemplate = loader.getTemplate(templatePath[1]); 235 236 Class [] params = mTemplate.getParameterTypes(); 237 if (params.length != 1 || params[0] != ClassDoc.class) { 238 printError("Template has incorrect param signature"); 239 } 240 } 241 242 245 protected boolean accept(ClassDoc classDoc) { 246 return (classDoc.isPublic() && 247 !classDoc.getTypeName().endsWith("BeanInfo")); 248 } 249 250 251 255 private void generateBeanInfo(ClassDoc classDoc) throws Exception { 256 257 String beanInfoJavaFileName = 258 classDoc.getTypeNameForFile() + "BeanInfo.java"; 259 260 String beanInfoJavaFilePath = beanInfoJavaFileName; 261 262 String packageName = classDoc.getPackageName(); 263 264 if (packageName != null) { 265 beanInfoJavaFilePath = 267 packageName.replace('.', '/') + "/" + beanInfoJavaFileName; 268 } 269 270 File dest = null; 271 272 if (mDest != null) { 273 dest = new File(mDest, beanInfoJavaFilePath); 274 } 275 else { 276 dest = new File(beanInfoJavaFilePath); 277 } 278 279 if (dest.exists()) { 280 if (dest.canWrite()) { 281 printWarning("File exists: " + beanInfoJavaFileName); 282 } 283 else { 284 printWarning("File exists and cannot be written: " + 285 beanInfoJavaFileName); 286 return; 287 } 288 } 289 290 BeanDocContext context = null; 291 try { 292 293 context = new BeanDocContext(this, dest); 294 295 if (mTemplate != null) { 296 printNotice("Creating BeanInfo: " + beanInfoJavaFilePath); 297 mTemplate.execute(context, new Object [] { classDoc }); 298 } 299 else { 300 printWarning("No template"); 301 } 302 } 303 finally { 304 if (context != null) { 305 context.close(); 306 } 307 } 308 } 309 310 } 311 312 | Popular Tags |