KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > percederberg > grammatica > code > csharp > CSharpComment


1 /*
2  * CSharpComment.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.csharp;
35
36 import java.io.PrintWriter JavaDoc;
37 import net.percederberg.grammatica.code.CodeElement;
38 import net.percederberg.grammatica.code.CodeStyle;
39
40 /**
41  * A class generating a C# comment.
42  *
43  * @author Per Cederberg, <per at percederberg dot net>
44  * @version 1.5
45  */

46 public class CSharpComment extends CodeElement {
47
48     /**
49      * The block documentation comment type.
50      */

51     public static final int DOCUMENTATION = 0;
52
53     /**
54      * The single line documentation comment type. Note that this type
55      * may be used even if the comment spans several lines, as the
56      * /// characters will be duplicated for each line.
57      */

58     public static final int DOCUMENTATION_SINGLE = 1;
59
60     /**
61      * The block comment type.
62      */

63     public static final int BLOCK = 2;
64
65     /**
66      * The single line comment type. Note that this type may be used
67      * even if the comment spans several lines, as the // characters
68      * will be duplicated for each line.
69      */

70     public static final int SINGLELINE = 3;
71
72     /**
73      * The comment type.
74      */

75     private int type;
76
77     /**
78      * The comment text.
79      */

80     private String JavaDoc comment;
81
82     /**
83      * Creates a new documentation comment with no indentation.
84      *
85      * @param comment the comment text
86      */

87     public CSharpComment(String JavaDoc comment) {
88         this(DOCUMENTATION, comment);
89     }
90
91     /**
92      * Creates a new comment of the specified type.
93      *
94      * @param type the comment type
95      * @param comment the comment text
96      *
97      * @see #DOCUMENTATION
98      * @see #BLOCK
99      * @see #SINGLELINE
100      */

101     public CSharpComment(int type, String JavaDoc comment) {
102         if (DOCUMENTATION <= type && type <= SINGLELINE) {
103             this.type = type;
104         } else {
105             this.type = DOCUMENTATION;
106         }
107         this.comment = comment;
108     }
109
110     /**
111      * Returns a numeric category number for the code element. A lower
112      * category number implies that the code element should be placed
113      * before code elements with a higher category number within a
114      * declaration.
115      *
116      * @return the category number
117      */

118     public int category() {
119         return 0;
120     }
121
122     /**
123      * Prints the comment to the specified stream.
124      *
125      * @param out the output stream
126      * @param style the code style to use
127      * @param indent the indentation level
128      */

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

165     private void printLine(PrintWriter JavaDoc out, String JavaDoc indent, String JavaDoc line) {
166         if (type == DOCUMENTATION || type == BLOCK) {
167             out.print(indent + " *");
168         } else if (type == DOCUMENTATION_SINGLE) {
169             out.print(indent + "///");
170         } else {
171             out.print(indent + "//");
172         }
173         if (line.equals("")) {
174             out.println();
175         } else {
176             out.println(" " + line);
177         }
178     }
179 }
180
Popular Tags