KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > sourceViewer > DocumentCharacterIterator


1 /*
2  * FindBugs - Find Bugs in Java programs
3  * Copyright (C) 2006, University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston MA 02111-1307, USA
18  */

19
20 package edu.umd.cs.findbugs.sourceViewer;
21
22 import java.text.CharacterIterator JavaDoc;
23
24 import javax.swing.text.BadLocationException JavaDoc;
25 import javax.swing.text.Document JavaDoc;
26 import javax.swing.text.Segment JavaDoc;
27
28 /**
29  * A CharacterIterator over a Document.
30  * Only a partial implementation.
31  */

32 public class DocumentCharacterIterator implements CharacterIterator JavaDoc {
33
34     private final Document JavaDoc doc;
35
36     private final Segment JavaDoc text;
37     
38     /** Position of iterator in document. */
39     private int docPos = 0;
40     
41     /** Index of end of current segment in document. */
42     private int segmentEnd;
43     
44     DocumentCharacterIterator(Document JavaDoc doc) {
45         this.doc = doc;
46         text = new Segment JavaDoc();
47         text.setPartialReturn(true);
48
49         try {
50             // FIXME: Findbugs warning
51
doc.getText(segmentEnd, doc.getLength(), text);
52         } catch (BadLocationException JavaDoc e) {
53             throw new RuntimeException JavaDoc(e);
54         }
55         segmentEnd = text.count;
56     }
57
58     public Object JavaDoc clone() {
59         throw new UnsupportedOperationException JavaDoc();
60     }
61
62     public char current() {
63         return text.current();
64     }
65
66     public char first() {
67         throw new UnsupportedOperationException JavaDoc();
68     }
69
70     public int getBeginIndex() {
71         throw new UnsupportedOperationException JavaDoc();
72     }
73
74     public int getEndIndex() {
75         throw new UnsupportedOperationException JavaDoc();
76     }
77
78     public int getIndex() {
79         return docPos;
80     }
81
82     public char last() {
83         throw new UnsupportedOperationException JavaDoc();
84     }
85
86     /** Increments the iterator's index by one and returns the character at the new index.
87      * @return the character at the new position, or DONE if the new position is off the end
88      */

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 JavaDoc e) {
97             throw new RuntimeException JavaDoc(e);
98         }
99         segmentEnd += text.count;
100         return text.current();
101     }
102
103     public char previous() {
104         throw new UnsupportedOperationException JavaDoc();
105     }
106
107     public char setIndex(int position) {
108         throw new UnsupportedOperationException JavaDoc();
109     }
110
111 }
112
Popular Tags