001: package snow.texteditor;
002:
003: import java.util.*;
004: import javax.swing.text.*;
005:
006: public class DocumentWordTokenizer {
007: final private SimpleDocument doc;
008: private int actualStartPosition;
009: private final int endPosition;
010:
011: public DocumentWordTokenizer(SimpleDocument doc) {
012: this (doc, 0, doc.getLength());
013: } // Constructor
014:
015: public DocumentWordTokenizer(SimpleDocument doc, int from, int to) {
016: this .doc = doc;
017: this .actualStartPosition = from;
018: this .endPosition = to;
019:
020: } // Constructor
021:
022: /** @return null if none
023: */
024: public Word getNextWord() {
025: int n = 0;
026: //readNextLine(); // first line
027:
028: while (true) // read all lines until end of document
029: {
030: n++;
031: if (n > 100) {
032: System.out.println("too much words: " + n);
033: return null;
034: }
035:
036: // search next starting char
037: int start = -1;
038: StringBuilder word = new StringBuilder();
039: for (int i = actualStartPosition; i < endPosition; i++) {
040: char ci = doc.getCharAt(i);
041: if (Character.isJavaIdentifierStart(ci)) {
042: start = i;
043: word.append(ci);
044: break;
045: }
046: }
047: if (start == -1)
048: return null;
049:
050: boolean endReached = false;
051: for (int i = start + 1; i < endPosition; i++) {
052: char ci = doc.getCharAt(i);
053: if (Character.isJavaIdentifierPart(ci)) {
054: word.append(ci);
055: } else {
056: break;
057: }
058: }
059: actualStartPosition = start + word.length() + 1;
060:
061: return new Word(word.toString(), start);
062: }
063: }
064:
065: /*
066: private void readNextLine()
067: {
068: actualLineNumber++;
069: if(actualLineNumber >= doc.getDefaultRootElement().getElementCount())
070: {
071: actualElement = null;
072: return;
073: }
074:
075: actualElement = doc.getDefaultRootElement().getElement(actualLineNumber);
076: actualLine = doc.getTextFromTo(actualElement.getStartOffset(), actualElement.getEndOffset());
077: actualPositionInLine = 0;
078:
079: }*/
080:
081: /** inner static class representing a word
082: */
083: public static class Word {
084: final public int positionInDocument;
085: final public String word;
086:
087: public Word(String w, int pos) {
088: this .word = w;
089: this .positionInDocument = pos;
090: }
091:
092: @Override
093: public String toString() {
094: return word + " (" + positionInDocument + ")";
095: }
096: }
097:
098: public static void main(String[] args) {
099: SimpleDocument doc = new SimpleDocument();
100: doc.append(" Hallo hello \r\ne\r\nimport");
101: DocumentWordTokenizer dwt = new DocumentWordTokenizer(doc, 0,
102: 10);
103: System.out.println("1 '" + dwt.getNextWord());
104: System.out.println("2 '" + dwt.getNextWord());
105: System.out.println("3 '" + dwt.getNextWord());
106: System.out.println("4 '" + dwt.getNextWord());
107: }
108:
109: }
|