001: /*
002: *Author: Mike Atkinson
003: *
004: *This software has been developed under the copyleft
005: *rules of the GNU General Public License. Please
006: *consult the GNU General Public License for more
007: *details about use and distribution of this software.
008: */
009: package org.acm.seguin.pretty;
010:
011: import net.sourceforge.jrefactory.parser.Token;
012:
013: /**
014: * Parses a XDoclet javadoc comment tag
015: *
016: * @author Mike Atkinson
017: * @created June 22, 2003
018: * @since JRefactory 2.7.02
019: */
020: public class Tokenizer {
021: /**Description of the Field */
022: protected StringBuffer buffer;
023: /**Description of the Field */
024: protected int index;
025: /**Description of the Field */
026: protected int last;
027: /**Description of the Field */
028: protected String value;
029:
030: /** Represents spaces */
031: public final static int SPACE = 0;
032:
033: /** Represents newline */
034: public final static int NEWLINE = 1;
035:
036: /** Represents a word */
037: public final static int WORD = 2;
038:
039: /**
040: * Constructor for the JavadocTokenizer object
041: *
042: * @paraminit Description of Parameter
043: * @paraminit Description of Parameter
044: */
045: public Tokenizer(String init) {
046: value = init;
047: index = 0;
048: buffer = new StringBuffer();
049: last = value.length();
050: }
051:
052: /**
053: * Description of the Method
054: *
055: * @return Description of the Returned Value
056: */
057: public boolean hasNext() {
058: return index < last;
059: }
060:
061: /**
062: * Description of the Method
063: *
064: * @return Description of the Returned Value
065: */
066: public Token next() {
067: Token result = new Token();
068:
069: if (index == last) {
070: result.kind = SPACE;
071: result.image = " ";
072: return result;
073: }
074:
075: buffer.setLength(0);
076: if (((index == 0) && (value.charAt(index) == '*'))
077: || ((index == 0) && (value.charAt(index) == '/'))
078: || (value.charAt(index) == '\r')
079: || (value.charAt(index) == '\n')) {
080: if (value.charAt(index) == '/') {
081: index++;
082: }
083:
084: loadNewline();
085: result.kind = NEWLINE;
086: result.image = buffer.toString();
087:
088: //System.out.println("Found a newline: [" + result.image + "]");
089: } else if (Character.isWhitespace(value.charAt(index))) {
090: loadSpace();
091: result.kind = SPACE;
092: result.image = buffer.toString();
093: //System.out.println("Found a space: [" + result.image + "]");
094: } else {
095: loadWord();
096: result.kind = WORD;
097: result.image = checkEnd(buffer.toString());
098: //System.out.println("Found a word: [" + result.image + "]");
099: }
100:
101: return result;
102: }
103:
104: /** Loads a particular word. */
105: protected void loadWord() {
106: int start = index;
107:
108: while ((index < last)
109: && !Character.isWhitespace(value.charAt(index))
110: && ((value.charAt(index) != '<') || (index == start))) {
111: buffer.append(value.charAt(index));
112: index++;
113:
114: if (value.charAt(index - 1) == '>')
115: return;
116: }
117: }
118:
119: /**
120: * Checks that the end of the word doesn't end with end of comment
121: *
122: * @paramvalue Description of Parameter
123: * @return the revised value
124: * @paramvalue the value to check
125: */
126: private String checkEnd(String value) {
127: if (value.endsWith("*/")) {
128: return value.substring(0, value.length() - 2);
129: }
130: return value;
131: }
132:
133: /**
134: * Moves from the end of one line past the * on the next line and possibly
135: * to the end of the comment.
136: */
137: private void loadNewline() {
138: while ((index < last)
139: && Character.isWhitespace(value.charAt(index))) {
140: buffer.append(value.charAt(index));
141: index++;
142: }
143:
144: while ((index < last) && (value.charAt(index) == '*')) {
145: buffer.append(value.charAt(index));
146: index++;
147: }
148:
149: if ((index < last)
150: && (value.charAt(index - 1) == '*' && value
151: .charAt(index) == '/')) {
152: buffer.append(value.charAt(index));
153: index++;
154: }
155: }
156:
157: /** Description of the Method */
158: private void loadSpace() {
159: while ((index < last)
160: && Character.isWhitespace(value.charAt(index))
161: && (value.charAt(index) != '\n')
162: && (value.charAt(index) != '\r')) {
163: buffer.append(value.charAt(index));
164: index++;
165: }
166: }
167:
168: /**
169: * Description of the Method
170: *
171: * @paramvalue Description of Parameter
172: * @return Description of the Return Value
173: * @paramvalue Description of the Parameter
174: */
175: public static boolean hasContent(String value) {
176: if (value == null) {
177: return false;
178: }
179:
180: int valueLength = value.length();
181:
182: if (valueLength == 0) {
183: return false;
184: }
185:
186: int start = 0;
187:
188: if (value.charAt(0) == '/') {
189: start++;
190: }
191:
192: int last = valueLength - 1;
193:
194: for (int ndx = start; ndx < last; ndx++) {
195: char ch = value.charAt(ndx);
196:
197: if (!(Character.isWhitespace(ch) || (ch == '*'))) {
198: return true;
199: }
200: }
201:
202: char ch = value.charAt(last);
203:
204: return !(Character.isWhitespace(ch) || (ch == '*') || (ch == '/'));
205: }
206: }
|