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