1 /*******************************************************************************
2 * Copyright (c) 2000, 2005 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.text;
13 import java.text.CharacterIterator;
16 //import org.eclipse.jface.text.Assert;
17 import org.eclipse.core.runtime.Assert;
18 import org.eclipse.jface.text.BadLocationException;
19 import org.eclipse.jface.text.IDocument;
22 * An <code>IDocument</code> based implementation of
23 * <code>CharacterIterator</code> and <code>CharSequence</code>. Note that
24 * the supplied document is not copied; if the document is modified during the
25 * lifetime of a <code>DocumentCharacterIterator</code>, the methods
26 * returning document content may not always return the same values. Also, if
27 * accessing the document fails with a {@link BadLocationException}, any of
28 * <code>CharacterIterator</code> methods as well as <code>charAt</code>may
29 * return {@link CharacterIterator#DONE}.
33 public class DocumentCharacterIterator implements CharacterIterator,
36 private int fIndex = -1;
38 private final IDocument fDocument;
40 private final int fFirst;
42 private final int fLast;
44 private void invariant() {
45 Assert.isTrue(fIndex >= fFirst);
46 Assert.isTrue(fIndex <= fLast);
50 * Creates an iterator for the entire document.
53 * the document backing this iterator
55 public DocumentCharacterIterator(IDocument document) {
60 * Creates an iterator, starting at offset <code>first</code>.
63 * the document backing this iterator
65 * the first character to consider
66 * @throws IllegalArgumentException
67 * if the indices are out of bounds
69 public DocumentCharacterIterator(IDocument document, int first)
70 throws IllegalArgumentException {
71 this(document, first, document.getLength());
75 * Creates an iterator for the document contents from <code>first</code>
76 * (inclusive) to <code>last</code> (exclusive).
79 * the document backing this iterator
81 * the first character to consider
83 * the last character index to consider
84 * @throws IllegalArgumentException
85 * if the indices are out of bounds
87 public DocumentCharacterIterator(IDocument document, int first, int last)
88 throws IllegalArgumentException {
90 throw new NullPointerException();
91 if (first < 0 || first > last)
92 throw new IllegalArgumentException();
93 if (last > document.getLength())
94 throw new IllegalArgumentException();
103 * @see java.text.CharacterIterator#first()
105 public char first() {
106 return setIndex(getBeginIndex());
110 * @see java.text.CharacterIterator#last()
114 return setIndex(getEndIndex());
116 return setIndex(getEndIndex() - 1);
120 * @see java.text.CharacterIterator#current()
122 public char current() {
123 if (fIndex >= fFirst && fIndex < fLast)
125 return fDocument.getChar(fIndex);
126 } catch (BadLocationException e) {
133 * @see java.text.CharacterIterator#next()
136 return setIndex(Math.min(fIndex + 1, getEndIndex()));
140 * @see java.text.CharacterIterator#previous()
142 public char previous() {
143 if (fIndex > getBeginIndex()) {
144 return setIndex(fIndex - 1);
151 * @see java.text.CharacterIterator#setIndex(int)
153 public char setIndex(int position) {
154 if (position >= getBeginIndex() && position <= getEndIndex())
157 throw new IllegalArgumentException();
164 * @see java.text.CharacterIterator#getBeginIndex()
166 public int getBeginIndex() {
171 * @see java.text.CharacterIterator#getEndIndex()
173 public int getEndIndex() {
178 * @see java.text.CharacterIterator#getIndex()
180 public int getIndex() {
185 * @see java.text.CharacterIterator#clone()
187 public Object clone() {
189 return super.clone();
190 } catch (CloneNotSupportedException e) {
191 throw new InternalError();
196 * @see java.lang.CharSequence#length()
198 public int length() {
199 return getEndIndex() - getBeginIndex();
205 * Note that, if the document is modified concurrently, this method may
206 * return {@link CharacterIterator#DONE} if a {@link BadLocationException}
207 * was thrown when accessing the backing document.
212 * @return {@inheritDoc}
214 public char charAt(int index) {
215 if (index >= 0 && index < length())
217 return fDocument.getChar(getBeginIndex() + index);
218 } catch (BadLocationException e) {
219 // ignore and return DONE
223 throw new IndexOutOfBoundsException();
227 * @see java.lang.CharSequence#subSequence(int, int)
229 public CharSequence subSequence(int start, int end) {
231 throw new IndexOutOfBoundsException();
233 throw new IndexOutOfBoundsException();
235 throw new IndexOutOfBoundsException();
236 return new DocumentCharacterIterator(fDocument,
237 getBeginIndex() + start, getBeginIndex() + end);