001: /*
002: * Copyright 2006 Ralf Joachim
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package org.exolab.javasource;
017:
018: /**
019: * A class to format comments.
020: *
021: * @author <a href="mailto:ralf DOT joachim AT syscon DOT eu">Ralf Joachim</a>
022: * @version $Revision: 6668 $ $Date: 2005-05-08 12:32:06 -0600 (Sun, 08 May 2005) $
023: * @since 1.1
024: */
025: public final class JCommentFormatter {
026: //--------------------------------------------------------------------------
027:
028: private String _comment = null;
029: private int _maxLength = JComment.MAX_LENGTH;
030: private int _offset = 0;
031: private int _length = 0;
032: private String _prefix = null;
033: private StringBuffer _sb = null;
034:
035: //--------------------------------------------------------------------------
036:
037: /**
038: * Creates a new LineFormatter for the given comment.
039: *
040: * @param comment The String to format.
041: * @param maxLength The maximum number of characters per line.
042: * @param prefix A prefix to append to the beginning of each line.
043: */
044: public JCommentFormatter(final String comment, final int maxLength,
045: final String prefix) {
046: _comment = comment;
047: if (comment != null) {
048: _length = comment.length();
049: }
050: _sb = new StringBuffer();
051: _maxLength = maxLength;
052: _prefix = prefix;
053: }
054:
055: //--------------------------------------------------------------------------
056:
057: public boolean hasMoreLines() {
058: if (_comment == null) {
059: return false;
060: }
061: return (_offset < _length);
062: }
063:
064: public String nextLine() {
065: if (_comment == null) {
066: return null;
067: }
068: if (_offset >= _length) {
069: return null;
070: }
071:
072: _sb.setLength(0);
073: if (_prefix != null) {
074: _sb.append(_prefix);
075: }
076:
077: int max = _offset + _maxLength;
078: if (max > this ._length) {
079: max = this ._length;
080: }
081:
082: int index = _offset;
083: int breakable = _offset;
084: for (; index < max; index++) {
085: char ch = _comment.charAt(index);
086: if (isNewLine(ch)) {
087: _sb.append(_comment.substring(_offset, index));
088: _offset = index + 1;
089: return _sb.toString();
090: }
091: if (isWhitespace(ch)) {
092: breakable = index;
093: }
094: }
095:
096: if (index < _length - 1) {
097: //-- if we could not find a breakable character, we must look
098: //-- ahead
099: if (_offset == breakable) {
100: while (index < _length) {
101: if (isBreakable(_comment.charAt(index))) {
102: break;
103: }
104: ++index;
105: }
106: } else {
107: index = breakable;
108: }
109: }
110: _sb.append(_comment.substring(_offset, index));
111: _offset = index + 1;
112: return _sb.toString();
113: }
114:
115: /**
116: * Returns true if we can break a line at this character.
117: *
118: * @param ch Character to examine.
119: * @return True if we can break a line at this character.
120: */
121: private boolean isBreakable(final char ch) {
122: return (isWhitespace(ch) || isNewLine(ch));
123: }
124:
125: /**
126: * Returns true if this character is whitespace.
127: *
128: * @param ch Character to examine.
129: * @return True if this character is whitespace.
130: */
131: private boolean isWhitespace(final char ch) {
132: return ((ch == ' ') || (ch == '\t'));
133: }
134:
135: /**
136: * Returns true if this character is a new line character.
137: *
138: * @param ch Character to examine.
139: * @return True if this character is a new line character.
140: */
141: private boolean isNewLine(final char ch) {
142: return ((ch == '\n') || (ch == '\r'));
143: }
144:
145: //--------------------------------------------------------------------------
146: }
|