KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > snow > texteditor > DocumentWordTokenizer


1 package snow.texteditor;
2
3 import java.util.*;
4 import javax.swing.text.*;
5
6 public class DocumentWordTokenizer
7 {
8   final private SimpleDocument doc;
9   private int actualStartPosition ;
10   private final int endPosition;
11
12   public DocumentWordTokenizer(SimpleDocument doc)
13   {
14      this(doc, 0, doc.getLength());
15   } // Constructor
16

17
18   public DocumentWordTokenizer(SimpleDocument doc, int from, int to)
19   {
20      this.doc = doc;
21      this.actualStartPosition = from;
22      this.endPosition = to;
23
24   } // Constructor
25

26
27
28   /** @return null if none
29   */

30   public Word getNextWord()
31   {
32     int n=0;
33     //readNextLine(); // first line
34

35     while(true) // read all lines until end of document
36
{
37       n++;
38       if(n>100)
39       {
40         System.out.println("too much words: "+n);
41         return null;
42       }
43
44       // search next starting char
45
int start = -1;
46       StringBuilder JavaDoc word = new StringBuilder JavaDoc();
47       for(int i=actualStartPosition; i<endPosition; i++)
48       {
49          char ci = doc.getCharAt(i);
50          if(Character.isJavaIdentifierStart(ci))
51          {
52            start = i;
53            word.append(ci);
54            break;
55          }
56       }
57       if(start==-1) return null;
58
59       boolean endReached = false;
60       for(int i=start+1; i<endPosition; i++)
61       {
62          char ci = doc.getCharAt(i);
63          if(Character.isJavaIdentifierPart(ci))
64          {
65            word.append(ci);
66          }
67          else
68          {
69            break;
70          }
71       }
72       actualStartPosition = start + word.length()+1;
73
74       return new Word(word.toString(), start);
75     }
76   }
77   /*
78   private void readNextLine()
79   {
80     actualLineNumber++;
81     if(actualLineNumber >= doc.getDefaultRootElement().getElementCount())
82     {
83       actualElement = null;
84       return;
85     }
86
87     actualElement = doc.getDefaultRootElement().getElement(actualLineNumber);
88     actualLine = doc.getTextFromTo(actualElement.getStartOffset(), actualElement.getEndOffset());
89     actualPositionInLine = 0;
90
91   }*/

92
93
94   /** inner static class representing a word
95   */

96   public static class Word
97   {
98     final public int positionInDocument;
99     final public String JavaDoc word;
100
101     public Word(String JavaDoc w, int pos)
102     {
103       this.word = w;
104       this.positionInDocument = pos;
105     }
106
107     public String JavaDoc toString() { return word+" ("+ positionInDocument+")"; }
108   }
109
110   public static void main(String JavaDoc[] args)
111   {
112     SimpleDocument doc = new SimpleDocument();
113     doc.append(" Hallo hello \r\ne\r\nimport");
114     DocumentWordTokenizer dwt = new DocumentWordTokenizer(doc,0,10);
115     System.out.println("1 '"+dwt.getNextWord());
116     System.out.println("2 '"+dwt.getNextWord());
117     System.out.println("3 '"+dwt.getNextWord());
118     System.out.println("4 '"+dwt.getNextWord());
119   }
120
121 }
Popular Tags