1 19 20 package edu.umd.cs.findbugs.sourceViewer; 21 22 import java.text.CharacterIterator ; 23 24 import javax.swing.text.BadLocationException ; 25 import javax.swing.text.Document ; 26 import javax.swing.text.Segment ; 27 28 32 public class DocumentCharacterIterator implements CharacterIterator { 33 34 private final Document doc; 35 36 private final Segment text; 37 38 39 private int docPos = 0; 40 41 42 private int segmentEnd; 43 44 DocumentCharacterIterator(Document doc) { 45 this.doc = doc; 46 text = new Segment (); 47 text.setPartialReturn(true); 48 49 try { 50 doc.getText(segmentEnd, doc.getLength(), text); 52 } catch (BadLocationException e) { 53 throw new RuntimeException (e); 54 } 55 segmentEnd = text.count; 56 } 57 58 public Object clone() { 59 throw new UnsupportedOperationException (); 60 } 61 62 public char current() { 63 return text.current(); 64 } 65 66 public char first() { 67 throw new UnsupportedOperationException (); 68 } 69 70 public int getBeginIndex() { 71 throw new UnsupportedOperationException (); 72 } 73 74 public int getEndIndex() { 75 throw new UnsupportedOperationException (); 76 } 77 78 public int getIndex() { 79 return docPos; 80 } 81 82 public char last() { 83 throw new UnsupportedOperationException (); 84 } 85 86 89 public char next() { 90 ++docPos; 91 if (docPos < segmentEnd || segmentEnd >= doc.getLength()) { 92 return text.next(); 93 } 94 try { 95 doc.getText(segmentEnd, doc.getLength() - segmentEnd, text); 96 } catch (BadLocationException e) { 97 throw new RuntimeException (e); 98 } 99 segmentEnd += text.count; 100 return text.current(); 101 } 102 103 public char previous() { 104 throw new UnsupportedOperationException (); 105 } 106 107 public char setIndex(int position) { 108 throw new UnsupportedOperationException (); 109 } 110 111 } 112 | Popular Tags |