1 /*
2 * Copyright 2006 Ralf Joachim
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
12 * the License.
13 */
14 package org.exolab.javasource;
15
16 /**
17 * A class to format comments.
18 *
19 * @author <a href="mailto:ralf DOT joachim AT syscon DOT eu">Ralf Joachim</a>
20 * @version $Revision: 6668 $ $Date: 2005-05-08 12:32:06 -0600 (Sun, 08 May 2005) $
21 * @since 1.1
22 */
23 public final class JCommentFormatter {
24 // --------------------------------------------------------------------------
25
26 private String _comment = null;
27 private int _maxLength = JComment.MAX_LENGTH;
28 private int _offset = 0;
29 private int _length = 0;
30 private String _prefix = null;
31 private StringBuffer _sb = null;
32
33 // --------------------------------------------------------------------------
34
35 /**
36 * Creates a new LineFormatter for the given comment.
37 *
38 * @param comment The String to format.
39 * @param maxLength The maximum number of characters per line.
40 * @param prefix A prefix to append to the beginning of each line.
41 */
42 public JCommentFormatter(final String comment, final int maxLength, final String prefix) {
43 _comment = comment;
44 if (comment != null) {
45 _length = comment.length();
46 }
47 _sb = new StringBuffer();
48 _maxLength = maxLength;
49 _prefix = prefix;
50 }
51
52 // --------------------------------------------------------------------------
53
54 public boolean hasMoreLines() {
55 if (_comment == null) {
56 return false;
57 }
58 return (_offset < _length);
59 }
60
61 public String nextLine() {
62 if (_comment == null) {
63 return null;
64 }
65 if (_offset >= _length) {
66 return null;
67 }
68
69 _sb.setLength(0);
70 if (_prefix != null) {
71 _sb.append(_prefix);
72 }
73
74 int max = _offset + _maxLength;
75 if (max > this._length) {
76 max = this._length;
77 }
78
79 int index = _offset;
80 int breakable = _offset;
81 for (; index < max; index++) {
82 char ch = _comment.charAt(index);
83 if (isNewLine(ch)) {
84 _sb.append(_comment.substring(_offset, index));
85 _offset = index + 1;
86 return _sb.toString();
87 }
88 if (isWhitespace(ch)) {
89 breakable = index;
90 }
91 }
92
93 if (index < _length - 1) {
94 // -- if we could not find a breakable character, we must look
95 // -- ahead
96 if (_offset == breakable) {
97 while (index < _length) {
98 if (isBreakable(_comment.charAt(index))) {
99 break;
100 }
101 ++index;
102 }
103 } else {
104 index = breakable;
105 }
106 }
107 _sb.append(_comment.substring(_offset, index));
108 _offset = index + 1;
109 return _sb.toString();
110 }
111
112 /**
113 * Returns true if we can break a line at this character.
114 *
115 * @param ch Character to examine.
116 * @return True if we can break a line at this character.
117 */
118 private boolean isBreakable(final char ch) {
119 return (isWhitespace(ch) || isNewLine(ch));
120 }
121
122 /**
123 * Returns true if this character is whitespace.
124 *
125 * @param ch Character to examine.
126 * @return True if this character is whitespace.
127 */
128 private boolean isWhitespace(final char ch) {
129 return ((ch == ' ') || (ch == '\t'));
130 }
131
132 /**
133 * Returns true if this character is a new line character.
134 *
135 * @param ch Character to examine.
136 * @return True if this character is a new line character.
137 */
138 private boolean isNewLine(final char ch) {
139 return ((ch == '\n') || (ch == '\r'));
140 }
141
142 // --------------------------------------------------------------------------
143 }