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 } 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 } 26 27 28 30 public Word getNextWord() 31 { 32 int n=0; 33 35 while(true) { 37 n++; 38 if(n>100) 39 { 40 System.out.println("too much words: "+n); 41 return null; 42 } 43 44 int start = -1; 46 StringBuilder word = new StringBuilder (); 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 92 93 94 96 public static class Word 97 { 98 final public int positionInDocument; 99 final public String word; 100 101 public Word(String w, int pos) 102 { 103 this.word = w; 104 this.positionInDocument = pos; 105 } 106 107 public String toString() { return word+" ("+ positionInDocument+")"; } 108 } 109 110 public static void main(String [] 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 |