1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.exolab.javasource;
17
18
19
20
21
22
23
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
39
40
41
42
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
83
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
100
101
102
103
104 private boolean isBreakable(final char ch) {
105 return (isWhitespace(ch) || isNewLine(ch));
106 }
107
108
109
110
111
112
113
114 private boolean isWhitespace(final char ch) {
115 return ((ch == ' ') || (ch == '\t'));
116 }
117
118
119
120
121
122
123
124 private boolean isNewLine(final char ch) {
125 return ((ch == '\n') || (ch == '\r'));
126 }
127
128
129 }