001: /*
002: * Portions Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: /*
027: *******************************************************************************
028: * (C) Copyright IBM Corp. 1996-2005 - All Rights Reserved *
029: * *
030: * The original version of this source code and documentation is copyrighted *
031: * and owned by IBM, These materials are provided under terms of a License *
032: * Agreement between IBM and Sun. This technology is protected by multiple *
033: * US and International patents. This notice and attribution to IBM may not *
034: * to removed. *
035: *******************************************************************************
036: */
037:
038: package sun.text.normalizer;
039:
040: import java.text.CharacterIterator;
041:
042: /**
043: * This class is a wrapper around CharacterIterator and implements the
044: * UCharacterIterator protocol
045: * @author ram
046: */
047:
048: public class CharacterIteratorWrapper extends UCharacterIterator {
049:
050: private CharacterIterator iterator;
051:
052: public CharacterIteratorWrapper(CharacterIterator iter) {
053: if (iter == null) {
054: throw new IllegalArgumentException();
055: }
056: iterator = iter;
057: }
058:
059: /**
060: * @see UCharacterIterator#current()
061: */
062: public int current() {
063: int c = iterator.current();
064: if (c == CharacterIterator.DONE) {
065: return DONE;
066: }
067: return c;
068: }
069:
070: /**
071: * @see UCharacterIterator#getLength()
072: */
073: public int getLength() {
074: return (iterator.getEndIndex() - iterator.getBeginIndex());
075: }
076:
077: /**
078: * @see UCharacterIterator#getIndex()
079: */
080: public int getIndex() {
081: return iterator.getIndex();
082: }
083:
084: /**
085: * @see UCharacterIterator#next()
086: */
087: public int next() {
088: int i = iterator.current();
089: iterator.next();
090: if (i == CharacterIterator.DONE) {
091: return DONE;
092: }
093: return i;
094: }
095:
096: /**
097: * @see UCharacterIterator#previous()
098: */
099: public int previous() {
100: int i = iterator.previous();
101: if (i == CharacterIterator.DONE) {
102: return DONE;
103: }
104: return i;
105: }
106:
107: /**
108: * @see UCharacterIterator#setIndex(int)
109: */
110: public void setIndex(int index) {
111: iterator.setIndex(index);
112: }
113:
114: //// for StringPrep
115: /**
116: * @see UCharacterIterator#getText(char[])
117: */
118: public int getText(char[] fillIn, int offset) {
119: int length = iterator.getEndIndex() - iterator.getBeginIndex();
120: int currentIndex = iterator.getIndex();
121: if (offset < 0 || offset + length > fillIn.length) {
122: throw new IndexOutOfBoundsException(Integer
123: .toString(length));
124: }
125:
126: for (char ch = iterator.first(); ch != CharacterIterator.DONE; ch = iterator
127: .next()) {
128: fillIn[offset++] = ch;
129: }
130: iterator.setIndex(currentIndex);
131:
132: return length;
133: }
134:
135: /**
136: * Creates a clone of this iterator. Clones the underlying character iterator.
137: * @see UCharacterIterator#clone()
138: */
139: public Object clone() {
140: try {
141: CharacterIteratorWrapper result = (CharacterIteratorWrapper) super
142: .clone();
143: result.iterator = (CharacterIterator) this .iterator.clone();
144: return result;
145: } catch (CloneNotSupportedException e) {
146: return null; // only invoked if bad underlying character iterator
147: }
148: }
149: }
|