KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > percederberg > grammatica > code > visualbasic > VisualBasicComment


1 /*
2  * VisualBasicComment.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) 2004 Adrian Moore. All rights reserved.
32  * Copyright (c) 2004 Per Cederberg. All rights reserved.
33  */

34
35 package net.percederberg.grammatica.code.visualbasic;
36
37 import java.io.PrintWriter JavaDoc;
38 import net.percederberg.grammatica.code.CodeElement;
39 import net.percederberg.grammatica.code.CodeStyle;
40
41 /**
42  * A class generating a Visual Basic comment.
43  *
44  * @author Adrian Moore, <adrianrob at hotmail dot com>
45  * @author Per Cederberg, <per at percederberg dot net>
46  * @version 1.5
47  * @since 1.5
48  */

49 public class VisualBasicComment extends CodeElement {
50
51     /**
52      * The documentation comment type. Note that this type may be used
53      * even if the comment spans several lines, as the ''' characters
54      * will be duplicated for each line.
55      */

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

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

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

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

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

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

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

121     public void print(PrintWriter JavaDoc out, CodeStyle style, int indent) {
122         String JavaDoc indentStr = style.getIndent(indent);
123         String JavaDoc firstLine;
124         String JavaDoc restLines;
125         int pos;
126
127         restLines = comment;
128         while ((pos = restLines.indexOf('\n')) >= 0) {
129             firstLine = restLines.substring(0, pos);
130             restLines = restLines.substring(pos + 1);
131             printLine(out, indentStr, firstLine);
132         }
133         printLine(out, indentStr, restLines);
134     }
135
136     /**
137      * Prints a single comment line.
138      *
139      * @param out the output stream
140      * @param indent the indentation string
141      * @param line the comment line to print
142      */

143     private void printLine(PrintWriter JavaDoc out, String JavaDoc indent, String JavaDoc line) {
144         if (type == DOCUMENTATION) {
145             out.println(indent + "'''" + line);
146         } else if (line.equals("")) {
147             out.println(indent + "'");
148         } else {
149             out.println(indent + "' " + line);
150         }
151     }
152 }
153
Popular Tags