KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > go > beandoc > BeanDocContext


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;
54
55 import com.go.beandoc.teadoc.*;
56
57 import java.io.*;
58
59 /******************************************************************************
60  * Context class used by the beandoc Tea templates.
61  *
62  * @author Mark Masse
63  * @version
64  * <!--$$Revision:--> 8 <!-- $-->, <!--$$JustDate:--> 8/23/00 <!-- $-->
65  */

66 public class BeanDocContext extends com.go.tea.runtime.DefaultContext {
67
68     /** The BeanDoc instance */
69     private BeanDoc mBeanDoc;
70
71     /** The "BeanInfo.java" file to write to */
72     private File mDest;
73
74     /** The Writer that writes the file */
75     private Writer mOut;
76
77     private MethodFinder mCommentedMethodFinder;
78     private CommentedParamFinder mCommentedParamFinder;
79
80     /**
81      * Creates a new BeanDocContext for use with a Tea template.
82      *
83      * @param beandoc the BeanDoc instance
84      * @param dest the "BeanInfo.java" file to write to.
85      */

86     public BeanDocContext(BeanDoc beandoc, File dest) throws IOException {
87         super();
88
89         mBeanDoc = beandoc;
90         
91         String JavaDoc parentDir = dest.getParent();
92         if (parentDir != null) {
93             File dir = new File(parentDir);
94             if (!dir.exists() || !dir.isDirectory()) {
95                 dir.mkdirs();
96             }
97         }
98
99         mOut = new BufferedWriter(new FileWriter(dest));
100
101         mCommentedMethodFinder = new MethodFinder() {
102             public boolean checkMethod(MethodDoc md) {
103                 return (md != null && md.getCommentText() != null);
104             }
105         };
106
107         mCommentedParamFinder = new CommentedParamFinder();
108
109     }
110
111     /**
112      * The standard context method, implemented to write to the file.
113      */

114     public void print(Object JavaDoc obj) throws IOException {
115
116         if (mOut != null) {
117             // static Tea!
118
mOut.write(toString(obj));
119         }
120     }
121
122     /**
123      * Closes the FileWriter
124      */

125     public void close() throws IOException {
126
127         if (mOut != null) {
128             mOut.close();
129             mOut = null;
130         }
131     }
132
133
134     public String JavaDoc getMethodComment(ClassDoc classDoc, MethodDoc md) {
135
136         String JavaDoc comment = md.getCommentText();
137         if (comment == null) {
138             md = classDoc.findMatchingMethod(md, mCommentedMethodFinder);
139             if (md != null) {
140                 comment = md.getCommentText();
141             }
142         }
143
144         if (comment != null) {
145             comment = formatForLiteral(comment.trim());
146         }
147
148         return comment;
149     }
150
151     public String JavaDoc getMethodParamComment(ClassDoc classDoc,
152                                         MethodDoc md,
153                                         String JavaDoc paramName) {
154
155         if (paramName == null) {
156             return null;
157         }
158
159         mCommentedParamFinder.paramName = paramName;
160         
161         classDoc.getMatchingMethod(md, mCommentedParamFinder);
162         String JavaDoc comment = mCommentedParamFinder.comment;
163
164         if (comment == null) {
165             classDoc.findMatchingMethod(md, mCommentedParamFinder);
166             comment = mCommentedParamFinder.comment;
167         }
168
169         if (comment != null) {
170             comment = formatForLiteral(comment.trim());
171         }
172
173         return comment;
174     }
175
176     /**
177      * Returns a String that is same as the specified String except that
178      * all special/escape characters are formatted as if they are escaped.
179      * <p>
180      * So, for example a tab character ('\t') becomes the string "\\t"
181      *
182      */

183     public String JavaDoc formatForLiteral(String JavaDoc s) {
184
185         if (s == null) {
186             return "";
187         }
188
189         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(s.length());
190         char[] chars = s.toCharArray();
191         for (int i = 0; i < chars.length; i++) {
192             char c = chars[i];
193             switch (c) {
194             case '\"':
195                 sb.append("\\\"");
196                 break;
197             case '\'':
198                 sb.append("\\\'");
199                 break;
200             case '\t':
201                 sb.append("\\t");
202                 break;
203             case '\r':
204                 sb.append("\\r");
205                 break;
206             case '\n':
207                 sb.append("\\n");
208                 break;
209             case '\f':
210                 sb.append("\\f");
211                 break;
212             case '\b':
213                 sb.append("\\b");
214                 break;
215             case '\\':
216                 sb.append("\\\\");
217                 break;
218             default:
219                 sb.append(c);
220                 break;
221             }
222         }
223
224         return sb.toString();
225     }
226
227     /**
228      * Prints an error message. Can be used to print an error message from
229      * the Tea template.
230      */

231     public void printError(String JavaDoc msg) {
232         mBeanDoc.printError(msg);
233     }
234
235     /**
236      * Prints an warning message. Can be used to print an warning message
237      * from the Tea template.
238      */

239     public void printWarning(String JavaDoc msg) {
240         mBeanDoc.printWarning(msg);
241     }
242
243     /**
244      * Prints an notice message. Can be used to print an notice message from
245      * the Tea template.
246      */

247     public void printNotice(String JavaDoc msg) {
248         mBeanDoc.printNotice(msg);
249     }
250
251
252     private static class CommentedParamFinder implements MethodFinder {
253         
254         public String JavaDoc paramName;
255         public String JavaDoc comment;
256
257         public boolean checkMethod(MethodDoc md) {
258             comment = null;
259
260             ParamTag[] paramTags = md.getParamTags();
261             if (paramTags != null) {
262                 for (int i = 0; i < paramTags.length; i++) {
263                     if (paramName.equals(paramTags[i].getParameterName())) {
264                         comment = paramTags[i].getParameterComment();
265                         if (comment != null) {
266                             return true;
267                         }
268                     }
269                 }
270             }
271
272             return false;
273         }
274     }
275
276 }
277
Popular Tags