1 /**
2 * Redistribution and use of this software and associated documentation
3 * ("Software"), with or without modification, are permitted provided
4 * that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain copyright
7 * statements and notices. Redistributions must also contain a
8 * copy of this document.
9 *
10 * 2. Redistributions in binary form must reproduce the
11 * above copyright notice, this list of conditions and the
12 * following disclaimer in the documentation and/or other
13 * materials provided with the distribution.
14 *
15 * 3. The name "Exolab" must not be used to endorse or promote
16 * products derived from this Software without prior written
17 * permission of Intalio, Inc. For written permission,
18 * please contact info@exolab.org.
19 *
20 * 4. Products derived from this Software may not be called "Exolab"
21 * nor may "Exolab" appear in their names without prior written
22 * permission of Intalio, Inc. Exolab is a registered
23 * trademark of Intalio, Inc.
24 *
25 * 5. Due credit should be given to the Exolab Project
26 * (http://www.exolab.org/).
27 *
28 * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32 * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 * OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Copyright 1999 (C) Intalio, Inc. All Rights Reserved.
42 */
43 package org.exolab.javasource;
44
45 /**
46 * A class that represents a Java comment.
47 *
48 * @author <a href="mailto:keith AT kvisco DOT com">Keith Visco</a>
49 * @version $Revision$ $Date: 2005-02-26 17:30:28 -0700 (Sat, 26 Feb 2005) $
50 */
51 public final class JComment {
52 //--------------------------------------------------------------------------
53
54 /** The auto style, allows this JComment to automatically choose a style for
55 * this comment. */
56 public static final short AUTO_STYLE = 0;
57
58 /** The block comment style. \/* *\/ */
59 public static final short BLOCK_STYLE = 1;
60
61 /** The line comment style. \/\/ */
62 public static final short LINE_STYLE = 2;
63
64 /** The header style, similiar to block, but with an '*' at the start of each line. */
65 public static final short HEADER_STYLE = 3;
66
67 /** Similiar to HEADER_STYLE. But starts with: \/** */
68 public static final short JAVADOC_STYLE = 4;
69
70 private static final String START_BLOCK = "/*";
71 private static final String END_BLOCK = " */";
72
73 private static final String START_JAVADOC = "/**";
74 private static final String END_JAVADOC = " */";
75
76 private static final String ASTERIX_PREFIX = " * ";
77 private static final String LINE_COMMENT_PREFIX = "// ";
78 private static final String SPACE_PREFIX = " ";
79
80 //--------------------------------------------------------------------------
81
82 /** The style of this comment. */
83 private short _style = AUTO_STYLE;
84
85 /** The main comment for this JDocComment. */
86 private StringBuffer _comment = null;
87
88 /** The maximum number of characters per line. */
89 protected static final int MAX_LENGTH = 65;
90
91 /** The maximum number of characters to indent comments. */
92 protected static final int MAX_INDENT = 17;
93
94 //--------------------------------------------------------------------------
95
96 /**
97 * Creates a new Java Comment.
98 */
99 public JComment() {
100 super();
101
102 _comment = new StringBuffer();
103 }
104
105 /**
106 * Creates a new Java comment with the given style.
107 *
108 * @param style The desired style.
109 */
110 public JComment(final short style) {
111 this();
112
113 _style = style;
114 }
115
116 //--------------------------------------------------------------------------
117
118 /**
119 * Appends the comment String to this JDocComment.
120 *
121 * @param comment The comment to append.
122 */
123 public void appendComment(final String comment) {
124 _comment.append(comment);
125 }
126
127 /**
128 * Sets the comment String of this JDocComment.
129 *
130 * @param comment The comment String of this JDocComment.
131 */
132 public void setComment(final String comment) {
133 _comment.setLength(0);
134 _comment.append(comment);
135 }
136
137 /**
138 * Sets the style for this JComment.
139 *
140 * @param style The style to use for this JComment.
141 */
142 public void setStyle(final short style) {
143 _style = style;
144 }
145
146 //--------------------------------------------------------------------------
147
148 /**
149 * Prints this JComment using the given JSourceWriter.
150 *
151 * @param jsw The JSourceWriter to print to.
152 */
153 public void print(final JSourceWriter jsw) {
154 if (jsw == null) { return; }
155
156 JCommentFormatter formatter = null;
157
158 //-- calculate comment length
159 short currentIndent = jsw.getIndentSize();
160 int maxLength = MAX_LENGTH - currentIndent;
161
162 //-- a simple check to make sure we have some room to print the comment
163 if (maxLength <= MAX_INDENT) { maxLength = MAX_LENGTH / 2; }
164
165 short resolvedStyle = _style;
166
167 if (_style == AUTO_STYLE) {
168 //-- estimation of number of lines
169 int nbrLines = _comment.length() / maxLength;
170
171 if (nbrLines > 2) {
172 resolvedStyle = BLOCK_STYLE;
173 } else {
174 resolvedStyle = LINE_STYLE;
175 }
176 }
177
178 //-- start comment
179 String prefix = null;
180 String start = null;
181 String end = null;
182
183 switch(resolvedStyle) {
184 case BLOCK_STYLE:
185 start = START_BLOCK;
186 end = END_BLOCK;
187 prefix = SPACE_PREFIX;
188 break;
189 case HEADER_STYLE:
190 start = START_BLOCK;
191 end = END_BLOCK;
192 prefix = ASTERIX_PREFIX;
193 break;
194 case JAVADOC_STYLE:
195 start = START_JAVADOC;
196 end = END_JAVADOC;
197 prefix = ASTERIX_PREFIX;
198 break;
199 default: //-- LINE
200 prefix = LINE_COMMENT_PREFIX;
201 break;
202 }
203
204 if (start != null) { jsw.writeln(start); }
205 //-- print main comment
206 formatter = new JCommentFormatter(_comment.toString(), maxLength, prefix);
207 while (formatter.hasMoreLines()) {
208 jsw.writeln(formatter.nextLine());
209 }
210 if (end != null) { jsw.writeln(end); }
211 jsw.flush();
212 }
213
214 /**
215 * {@inheritDoc}
216 */
217 public String toString() {
218 return "";
219 }
220
221 //--------------------------------------------------------------------------
222 }