KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > go > beandoc > teadoc > PropertyDoc


1 /* ====================================================================
2  * BeanDoc - Copyright (c) 1997-2000 GO.com
3  * ====================================================================
4  * The Tea Software License, Version 1.0
5  *
6  * Copyright (c) 2000 GO.com. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by GO.com
23  * (http://opensource.go.com/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Tea", "TeaServlet", "Kettle", "Trove", BeanDoc and "GO.com"
28  * must not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact opensource@go.com.
31  *
32  * 5. Products derived from this software may not be called "Tea",
33  * "TeaServlet", "Kettle", "Trove" or BeanDoc, nor may "Tea", "TeaServlet",
34  * "Kettle", "Trove" or BeanDoc appear in their name, without prior written
35  * permission of GO.com.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL GO.COM OR ITS CONTRIBUTORS BE LIABLE
41  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
42  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
43  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
44  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
45  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
46  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
47  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48  * ====================================================================
49  *
50  * For more information about Tea, please see http://opensource.go.com/.
51  */

52
53 package com.go.beandoc.teadoc;
54
55 import java.beans.Introspector JavaDoc;
56
57 import java.util.Vector JavaDoc;
58 import java.util.HashMap JavaDoc;
59
60 /******************************************************************************
61  * There is no Doclet equivalent for this class. It was created from
62  * scratch to make documenting JavaBean properties easier.
63  *
64  * @author Mark Masse
65  * @version
66  * <!--$$Revision:--> 7 <!-- $-->, <!--$$JustDate:--> 8/23/00 <!-- $-->
67  */

68 public class PropertyDoc extends MemberDoc {
69
70     /**
71      * Creates a set of PropertyDoc objects from the specified MemberDocs.
72      * A single PropertyDoc object will be created for each pair of get/set
73      * methods.
74      */

75     public static PropertyDoc[] create(RootDoc root,
76                                        MethodDoc[] docs) {
77
78         if (docs == null) {
79             return null;
80         }
81
82         // Vector of the PropertyDocs to be returned by this method
83
Vector JavaDoc propertyDocVector = new Vector JavaDoc();
84
85         // HashMap that maps propertyName to PropertyDoc object. This is
86
// used to ensure that a single PropertyDoc instance contains
87
// both the "get" and the "set" MethodDoc (if both exist).
88
HashMap JavaDoc propertyMap = new HashMap JavaDoc();
89
90         for (int i = 0; i < docs.length; i++) {
91
92             MethodDoc method = docs[i];
93
94             if (!method.isPublic() ||
95                 method.isStatic() || method.isExcluded()) {
96                 continue;
97             }
98
99             String JavaDoc methodName = method.getName();
100             Parameter[] params = method.getParameters();
101             Type returnType = method.getReturnType();
102             String JavaDoc typeName = returnType.getQualifiedTypeName();
103             if (typeName != null) {
104                 String JavaDoc dimension = returnType.getDimension();
105                 if (dimension != null && dimension.length() > 0) {
106                     typeName += dimension;
107                 }
108             }
109
110             //
111
// Look for JavaBeans naming patterns
112
//
113

114             String JavaDoc propertyName = null;
115             boolean isReadMethod = true;
116
117             if (params == null || (params.length == 0)) {
118                 // Accepts no params, might be a "get" method
119

120                 if (methodName.startsWith("get") && methodName.length() > 3) {
121
122                     propertyName =
123                         Introspector.decapitalize(methodName.substring(3));
124                 }
125                 else if (methodName.startsWith("is") &&
126                          methodName.length() > 2) {
127
128                     if ("boolean".equalsIgnoreCase(typeName)) {
129                         propertyName =
130                             Introspector.decapitalize(methodName.substring(2));
131                     }
132                 }
133             }
134             else if (params != null && (params.length == 1)) {
135                 // Accepts a single param, might be a "set" method
136

137                 if (methodName.startsWith("set") && methodName.length() > 3) {
138
139                     propertyName =
140                         Introspector.decapitalize(methodName.substring(3));
141                     isReadMethod = false;
142                 }
143             }
144
145             if (propertyName == null) {
146                 // Was neither a "get" nor a "set"
147
continue;
148             }
149
150             PropertyDoc pd = (PropertyDoc) propertyMap.get(propertyName);
151             if (pd != null) {
152                 MethodDoc readMethod = pd.getReadMethod();
153                 if (readMethod == null) {
154                     pd.setReadMethod(method);
155                 }
156                 else {
157                     MethodDoc writeMethod = pd.getWriteMethod();
158                     if (writeMethod == null) {
159                         pd.setWriteMethod(method);
160                     }
161                 }
162             }
163             else {
164                 pd = new PropertyDoc(root, method, isReadMethod);
165                 pd.setName(propertyName);
166                 propertyMap.put(propertyName, pd);
167                 propertyDocVector.addElement(pd);
168             }
169
170         }
171
172         PropertyDoc[] propertyDocs = new PropertyDoc[propertyDocVector.size()];
173         propertyDocVector.copyInto(propertyDocs);
174
175         return propertyDocs;
176     }
177
178     /**
179      * Trims off the first word of the specified string and capitalizes the
180      * new first word
181      */

182     private static final String JavaDoc trimFirstWord(String JavaDoc s) {
183         s = s.trim();
184         char[] chars = s.toCharArray();
185         int firstSpace = -1;
186
187         for (int i = 0; i < chars.length; i++) {
188             char c = chars[i];
189             if (Character.isWhitespace(c)) {
190                 firstSpace = i;
191                 break;
192             }
193         }
194
195         if (firstSpace == -1) {
196             return s;
197         }
198
199         s = s.substring(firstSpace).trim();
200         s = capitalize(s);
201
202         return s;
203     }
204
205     private static final String JavaDoc capitalize(String JavaDoc s) {
206
207         if (s != null && s.length() > 1) {
208             s = Character.toUpperCase(s.charAt(0)) + s.substring(1);
209         }
210         return s;
211     }
212
213
214     private String JavaDoc mName;
215     private MethodDoc mReadMethod;
216     private MethodDoc mWriteMethod;
217     private String JavaDoc mCommentText;
218
219     public PropertyDoc(RootDoc root,
220                        MethodDoc methodDoc, boolean isReadMethod) {
221
222         super(root, (com.sun.javadoc.MethodDoc) methodDoc.getInnerDoc());
223
224         if (isReadMethod) {
225             setReadMethod(methodDoc);
226         }
227         else {
228             setWriteMethod(methodDoc);
229         }
230     }
231
232
233     public String JavaDoc getName() {
234         return mName;
235     }
236
237     public void setName(String JavaDoc name) {
238         mName = name;
239     }
240
241     public MethodDoc getReadMethod() {
242         return mReadMethod;
243     }
244
245     public void setReadMethod(MethodDoc doc) {
246         mReadMethod = doc;
247     }
248
249
250     public MethodDoc getWriteMethod() {
251         return mWriteMethod;
252     }
253
254     public void setWriteMethod(MethodDoc doc) {
255         mWriteMethod = doc;
256     }
257
258
259     public String JavaDoc getCommentText() {
260         if (mCommentText != null) {
261             return mCommentText;
262         }
263
264         String JavaDoc text = getTagValue("property");
265
266         if (text == null) {
267             text = getMethodCommentText();
268
269             if (text != null) {
270                 String JavaDoc t = text.toLowerCase();
271
272                 if (t.startsWith("return ") ||
273                     t.startsWith("returns ") ||
274                     t.startsWith("get ") ||
275                     t.startsWith("gets ") ||
276                     t.startsWith("set ") ||
277                     t.startsWith("sets ")) {
278
279                     // Make it appear as if the comment is referring to the
280
// property instead of the method
281
text = trimFirstWord(text);
282                 }
283             }
284         }
285
286         if (text != null) {
287             text = text.trim();
288         }
289         mCommentText = capitalize(text);
290
291         return mCommentText;
292     }
293
294     public boolean isBound() {
295         return isTagPresent("bound");
296     }
297
298     public boolean isConstrained() {
299         return isTagPresent("constrained");
300     }
301
302     public boolean isDefault() {
303         return isTagPresent("default");
304     }
305
306     public String JavaDoc getPropertyEditorClassName() {
307         return getTagValue("editor");
308     }
309
310     public boolean isMethod() {
311         return false;
312     }
313
314     /**
315      * Checks to see if the specified tag exists
316      */

317     public boolean isTagPresent(String JavaDoc tagName) {
318
319         boolean tagPresent = false;
320
321         if (mReadMethod != null) {
322             tagPresent = mReadMethod.isTagPresent(tagName);
323         }
324         if (!tagPresent && mWriteMethod != null) {
325             tagPresent = mWriteMethod.isTagPresent(tagName);
326         }
327
328         return tagPresent;
329     }
330
331
332     // Overridden to check both the read and write MethodDocs for the tag
333
public String JavaDoc getTagValue(String JavaDoc tagName) {
334         String JavaDoc value = null;
335
336         if (mReadMethod != null) {
337             value = mReadMethod.getTagValue(tagName);
338         }
339         if (value == null && mWriteMethod != null) {
340             value = mWriteMethod.getTagValue(tagName);
341         }
342
343         return value;
344     }
345
346
347
348     //
349
// Non-public interface
350
//
351

352
353     // Checks both the read and write MethodDocs for a comment
354
private String JavaDoc getMethodCommentText() {
355
356         String JavaDoc text = null;
357
358         // Check the read method first
359
if (mReadMethod != null) {
360             text = mReadMethod.getCommentText();
361         }
362         if (text == null && mWriteMethod != null) {
363             // Try the write method
364
text = mWriteMethod.getCommentText();
365         }
366
367         if (text == null || text.trim().length() == 0) {
368             // Try the "@return" tag
369
text = getTagValue("return");
370         }
371
372         return text;
373     }
374
375
376 }
377
Popular Tags