KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > js > JavaComment


1 /*
2  * Copyright 2003, 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15
16  */

17 package org.apache.ws.jaxme.js;
18
19 import java.io.IOException JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23
24
25 /** <p>A class representing a Java comment.</p>
26  *
27  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
28  */

29 public class JavaComment {
30   boolean forJavaDoc;
31
32   /** <p>Creates a new JavaComment which ought to be included into JavaDoc.
33    * Use JavaComment(false) for comments which should be excluded.</p>
34    */

35   JavaComment() { forJavaDoc = true; }
36
37   /** <p>Creates a new JavaComment.</p>
38    *
39    * @param javaDoc True, if this comment should appear in JavaDoc, false otherwise
40    */

41   JavaComment(boolean javaDoc) { forJavaDoc = javaDoc; }
42
43   private List JavaDoc authors;
44   /** <p>Returns the JavaDoc author field or null, if there is no
45    * author field.</p>
46    *
47    * @see #addAuthor
48    */

49   public List JavaDoc getAuthors() { return authors; }
50   /** <p>Sets the JavaDoc author field. Use null to disable the author field.</p>
51    *
52    * @see #getAuthors
53    */

54   public void addAuthor(String JavaDoc author) {
55      if (authors == null) {
56         authors = new ArrayList JavaDoc();
57      }
58     authors.add(author);
59   }
60
61   private String JavaDoc version;
62   /** <p>Returns the JavaDoc version field or null, if there is no
63    * version field.</p>
64    *
65    * @see #setVersion
66    */

67   public String JavaDoc getVersion() { return version; }
68   /** <p>Sets the JavaDoc version field. Use null to disable the version field.</p>
69    *
70    * @see #getVersion
71    */

72   public void setVersion(String JavaDoc pVersion) {
73     version = (pVersion != null && pVersion.length() > 0) ? pVersion : null;
74   }
75
76   private String JavaDoc returns;
77   /** <p>Returns the JavaDoc return field or null, if there is no
78    * return field.</p>
79    *
80    * @see #setReturn
81    */

82   public String JavaDoc getReturn() { return returns; }
83   /** <p>Sets the JavaDoc return field. Use null to disable the return field.</p>
84    *
85    * @see #getReturn
86    */

87   public void setReturn(String JavaDoc pReturns) {
88     returns = (pReturns != null && pReturns.length() > 0) ? pReturns : null;
89   }
90
91   private List JavaDoc see = new ArrayList JavaDoc();
92   /** <p>Returns an array list of Strings which should be used for "see" fields.</p>
93    *
94    * @see #addSee
95    */

96   public List JavaDoc getSee() { return see; }
97   /** <p>Adds an element to the list of Strings which should be used for
98    * "see" fields.</p>
99    *
100    * @see #getSee
101    */

102   public void addSee(String JavaDoc pSee) { see.add(pSee); }
103
104   private ArrayList JavaDoc lines = new ArrayList JavaDoc();
105   /** <p>Returns an array of lines being the comments content.</p>
106    *
107    * @see #addLine
108    */

109   public ArrayList JavaDoc getLines() { return lines; }
110   /** <p>Adds a line to the comments content.</p>
111    *
112    * @see #getLines
113    */

114   public void addLine(String JavaDoc s) {
115     if (s == null) s = "";
116     boolean done = false;
117     for (java.util.StringTokenizer JavaDoc st = new java.util.StringTokenizer JavaDoc(s, "\n");
118          st.hasMoreTokens(); ) {
119       String JavaDoc t = st.nextToken();
120       if (t.length() > 0 && t.charAt(t.length()-1) == '\r') {
121         t = t.substring(0, t.charAt(t.length()-1));
122       }
123       lines.add(t);
124       done = true;
125     }
126     if (!done) lines.add("");
127   }
128
129   private List JavaDoc params = new ArrayList JavaDoc();
130   /** <p>Returns an array of values for the JavaDoc param field.</p>
131    *
132    * @see #addParam
133    */

134   public List JavaDoc getParams() { return params; }
135   /** <p>Adds a JavaDoc "param" field.</p>
136    *
137    * @see #getParams
138    */

139   public void addParam(String JavaDoc s) { params.add(s); }
140
141   private List JavaDoc throwsList = new ArrayList JavaDoc();
142   /** <p>Returns an array of values for the JavaDoc throw field.</p>
143    *
144    * @see #addThrows
145    */

146   public List JavaDoc getThrows() { return throwsList; }
147   /** <p>Adds a JavaDoc "throw" field.</p>
148    *
149    * @see #getThrows
150    */

151   public void addThrows(String JavaDoc s) { throwsList.add(s); }
152
153   /** <p>Returns a string representation of this comment.</p>
154    */

155   public void write(IndentationTarget pTarget) throws IOException JavaDoc {
156     String JavaDoc sep = "/*" + (forJavaDoc ? "* " : " ");
157     String JavaDoc othersep = " * ";
158     if (lines.size() > 0) {
159       for (int i = 0; i < lines.size(); i++) {
160         if (i > 0) pTarget.indent(0);
161         pTarget.write(sep);
162         pTarget.write(lines.get(i).toString());
163         pTarget.write();
164         sep = othersep;
165       }
166     } else {
167       pTarget.write(sep);
168       pTarget.write();
169       sep = othersep;
170     }
171     pTarget.indent(0);
172     pTarget.write(sep);
173     pTarget.write();
174
175     for (int i = 0; i < params.size(); i++) {
176       pTarget.indent(0);
177       pTarget.write(sep);
178       pTarget.write("@param ");
179       pTarget.write(params.get(i).toString());
180       pTarget.write();
181     }
182     if (returns != null) {
183       pTarget.indent(0);
184       pTarget.write(sep);
185       pTarget.write("@return ");
186       pTarget.write(returns);
187       pTarget.write();
188     }
189     for (int i = 0; i < throwsList.size(); i++) {
190       pTarget.indent(0);
191       pTarget.write(sep);
192       pTarget.write("@throws ");
193       pTarget.write(throwsList.get(i).toString());
194       pTarget.write();
195     }
196     for (int i = 0; i < see.size(); i++) {
197       pTarget.indent(0);
198       pTarget.write(sep);
199       pTarget.write("@see ");
200       pTarget.write(see.get(i).toString());
201       pTarget.write();
202     }
203     List JavaDoc myAuthors = getAuthors();
204     if (myAuthors != null) {
205         for (Iterator JavaDoc iter = myAuthors.iterator(); iter.hasNext(); ) {
206         pTarget.indent(0);
207         pTarget.write(sep);
208         pTarget.write("@author ");
209             pTarget.write((String JavaDoc) iter.next());
210         pTarget.write();
211       }
212     }
213     if (version != null) {
214       pTarget.indent(0);
215       pTarget.write(sep);
216       pTarget.write("@version ");
217       pTarget.write(version);
218       pTarget.write();
219     }
220     pTarget.indent(0);
221     pTarget.write(" */");
222     pTarget.write();
223   }
224 }
225
Popular Tags