1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.doclet;
17
18 import com.sun.javadoc.AnnotationDesc;
19 import com.sun.javadoc.ClassDoc;
20 import com.sun.javadoc.MethodDoc;
21 import com.sun.javadoc.ProgramElementDoc;
22 import com.sun.javadoc.RootDoc;
23 import com.sun.tools.javadoc.Main;
24
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.util.Arrays;
28 import java.util.Collections;
29 import java.util.Enumeration;
30 import java.util.Properties;
31 import java.util.Vector;
32
33
34
35
36
37
38
39 public class KRADLibraryPropertiesDoclet {
40
41
42 public static void main(String[] args) {
43 Main.execute(new String[]{"-doclet", "org.kuali.rice.krad.demo.uif.library.tools.KRADLibraryPropertiesDoclet",
44 "-sourcepath", "C:/Java/Projects/Rice/Trunk/krad/krad-web-framework/src/main/java", "-subpackages",
45 "org.kuali.rice.krad.uif:org.kuali.rice.krad.datadictionary.validation.constraint"});
46 }
47
48
49
50
51
52
53
54 public static boolean start(RootDoc root) {
55 storeToPropertyFile(root);
56 return true;
57 }
58
59
60
61
62
63
64 private static void storeToPropertyFile(RootDoc root) {
65 ClassDoc[] classes = root.classes();
66 SortedProperties prop = new SortedProperties();
67
68 for (ClassDoc classDoc : classes) {
69
70
71 if (isAnnotatedWith(classDoc, "org.kuali.rice.krad.datadictionary.parse.BeanTag",
72 "org.kuali.rice.krad.datadictionary.parse.BeanTags")) {
73
74 String className = classDoc.qualifiedName();
75 String classDescription = classDoc.commentText();
76 prop.setProperty(className, classDescription);
77
78 MethodDoc[] methods = classDoc.methods();
79
80 for (MethodDoc methodDoc : methods) {
81
82
83 if (methodDoc.parameters().length == 0 &&
84 (methodDoc.name().startsWith("get") || methodDoc.name().startsWith("is")) &&
85 !(methodDoc.name().equals("get") || methodDoc.name().equals("is")) &&
86 isAnnotatedWith(methodDoc, "org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute")) {
87 String methodName;
88
89 if (methodDoc.name().startsWith("get")) {
90 methodName = methodDoc.name().replaceFirst("get", "");
91 } else {
92 methodName = methodDoc.name().replaceFirst("is", "");
93 }
94 String propertyName = Character.toLowerCase(methodName.charAt(0)) + (methodName.length() > 1 ?
95 methodName.substring(1) : "");
96 String propertyType = methodDoc.returnType().typeName();
97 String propertyDescription = getDocText(methodDoc, root);
98
99 prop.setProperty(className + "|" + propertyName + "|" + propertyType, propertyDescription);
100 }
101 }
102 }
103 }
104
105
106 try {
107 prop.store(new FileOutputStream("C:/Java/Projects/Rice/Trunk/sampleapp/src/main/resources/"
108 + "org/kuali/rice/devtools/krad/documentation/PropertiesDescription.properties"), null);
109 } catch (IOException e) {
110 e.printStackTrace();
111 }
112 }
113
114
115
116
117
118
119
120
121 private static String getDocText(MethodDoc method, RootDoc root) {
122
123 if (method.commentText() != null && !method.commentText().equals("")) {
124 return method.commentText();
125 }
126
127 String docText = method.getRawCommentText();
128
129 if (docText != null && docText.contains("@see")) {
130 String classMethodName = docText.replace("@see", "").replace("\n", "").replace(" ", "");
131 String className = classMethodName.substring(0, classMethodName.indexOf("#"));
132 String methodName = classMethodName.substring(classMethodName.indexOf("#") + 1).replace("()", "");
133 ClassDoc classDoc = root.classNamed(className);
134
135 if (classDoc == null) {
136 System.err.println("warning - Comment on "
137 + method.toString()
138 + " does not have valid fully qualified "
139 + "method in @see annotation.\n"
140 + docText);
141 return "";
142 }
143 MethodDoc methodDoc = getNoParamMethodFromClassDocByName(classDoc, methodName);
144 return methodDoc.commentText();
145 }
146
147 return "";
148 }
149
150
151
152
153
154
155
156
157 private static MethodDoc getNoParamMethodFromClassDocByName(ClassDoc classDoc, String methodName) {
158 MethodDoc[] methods = classDoc.methods();
159
160 for (MethodDoc methodDoc : methods) {
161
162 if (methodDoc.name().equals(methodName) && methodDoc.parameters().length == 0) {
163 return methodDoc;
164 }
165 }
166 return null;
167 }
168
169
170
171
172
173
174
175
176 private static boolean isAnnotatedWith(ProgramElementDoc elementDoc, String... tagString) {
177 AnnotationDesc[] annotations = elementDoc.annotations();
178
179 for (AnnotationDesc annotation : annotations) {
180
181 if (Arrays.asList(tagString).contains(annotation.annotationType().toString())) {
182 return true;
183 }
184 }
185
186 return false;
187 }
188
189
190
191
192 static class SortedProperties extends Properties {
193
194 public Enumeration keys() {
195 Enumeration keysEnum = super.keys();
196 Vector<String> keyList = new Vector<String>();
197
198 while (keysEnum.hasMoreElements()) {
199 keyList.add((String) keysEnum.nextElement());
200 }
201 Collections.sort(keyList);
202 return keyList.elements();
203 }
204 }
205
206 }