KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > percederberg > grammatica > code > java > JavaComment


1 /*
2  * JavaComment.java
3  *
4  * This work is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published
6  * by the Free Software Foundation; either version 2 of the License,
7  * or (at your option) any later version.
8  *
9  * This work is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA
18  *
19  * As a special exception, the copyright holders of this library give
20  * you permission to link this library with independent modules to
21  * produce an executable, regardless of the license terms of these
22  * independent modules, and to copy and distribute the resulting
23  * executable under terms of your choice, provided that you also meet,
24  * for each linked independent module, the terms and conditions of the
25  * license of that module. An independent module is a module which is
26  * not derived from or based on this library. If you modify this
27  * library, you may extend this exception to your version of the
28  * library, but you are not obligated to do so. If you do not wish to
29  * do so, delete this exception statement from your version.
30  *
31  * Copyright (c) 2003-2004 Per Cederberg. All rights reserved.
32  */

33
34 package net.percederberg.grammatica.code.java;
35
36 import java.io.PrintWriter JavaDoc;
37
38 import net.percederberg.grammatica.code.CodeElement;
39 import net.percederberg.grammatica.code.CodeStyle;
40
41 /**
42  * A class generating a Java comment.
43  *
44  * @author Per Cederberg, <per at percederberg dot net>
45  * @version 1.5
46  */

47 public class JavaComment extends CodeElement {
48
49     /**
50      * The JavaDoc documentation comment type.
51      */

52     public static final int DOCUMENTATION = 0;
53
54     /**
55      * The block comment type.
56      */

57     public static final int BLOCK = 1;
58
59     /**
60      * The single line comment type. Note that this may be used even
61      * if the comment spans several lines, as the // characters will
62      * be duplicated for each line.
63      */

64     public static final int SINGLELINE = 2;
65
66     /**
67      * The comment type.
68      */

69     private int type;
70
71     /**
72      * The comment text.
73      */

74     private String JavaDoc comment;
75
76     /**
77      * Creates a new documentation comment with no indentation.
78      *
79      * @param comment the comment text
80      */

81     public JavaComment(String JavaDoc comment) {
82         this(DOCUMENTATION, comment);
83     }
84
85     /**
86      * Creates a new comment of the specified type.
87      *
88      * @param type the comment type
89      * @param comment the comment text
90      *
91      * @see #DOCUMENTATION
92      * @see #BLOCK
93      * @see #SINGLELINE
94      */

95     public JavaComment(int type, String JavaDoc comment) {
96         if (type == BLOCK || type == SINGLELINE) {
97             this.type = type;
98         } else {
99             this.type = DOCUMENTATION;
100         }
101         this.comment = comment;
102     }
103
104     /**
105      * Returns a numeric category number for the code element. A lower
106      * category number implies that the code element should be placed
107      * before code elements with a higher category number within a
108      * declaration.
109      *
110      * @return the category number
111      */

112     public int category() {
113         return 0;
114     }
115
116     /**
117      * Prints the code element to the specified output stream.
118      *
119      * @param out the output stream
120      * @param style the code style to use
121      * @param indent the indentation level
122      */

123     public void print(PrintWriter JavaDoc out, CodeStyle style, int indent) {
124         String JavaDoc indentStr = style.getIndent(indent);
125         String JavaDoc firstLine;
126         String JavaDoc restLines;
127         int pos;
128
129         // Comment head
130
if (type == DOCUMENTATION) {
131             out.println(indentStr + "/**");
132         } else if (type == BLOCK) {
133             out.println(indentStr + "/*");
134         }
135
136         // Comment body
137
restLines = comment;
138         while ((pos = restLines.indexOf('\n')) >= 0) {
139             firstLine = restLines.substring(0, pos);
140             restLines = restLines.substring(pos + 1);
141             printLine(out, indentStr, firstLine);
142         }
143         printLine(out, indentStr, restLines);
144
145         // Comment tail
146
if (type != SINGLELINE) {
147             out.println(indentStr + " */");
148         }
149     }
150
151     /**
152      * Prints a single comment line.
153      *
154      * @param out the output stream
155      * @param indent the indentation string
156      * @param line the comment line to print
157      */

158     private void printLine(PrintWriter JavaDoc out, String JavaDoc indent, String JavaDoc line) {
159         if (type == SINGLELINE) {
160             out.print(indent + "//");
161         } else {
162             out.print(indent + " *");
163         }
164         if (line.equals("")) {
165             out.println();
166         } else {
167             out.println(" " + line);
168         }
169     }
170 }
171
Popular Tags