1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.exolab.javasource;
15
16
17
18
19
20
21
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
37
38
39
40
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
95
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
114
115
116
117
118 private boolean isBreakable(final char ch) {
119 return (isWhitespace(ch) || isNewLine(ch));
120 }
121
122
123
124
125
126
127
128 private boolean isWhitespace(final char ch) {
129 return ((ch == ' ') || (ch == '\t'));
130 }
131
132
133
134
135
136
137
138 private boolean isNewLine(final char ch) {
139 return ((ch == '\n') || (ch == '\r'));
140 }
141
142
143 }