0N/A/*
2362N/A * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A *******************************************************************************
0N/A * (C) Copyright IBM Corp. 1996-2005 - All Rights Reserved *
0N/A * *
0N/A * The original version of this source code and documentation is copyrighted *
0N/A * and owned by IBM, These materials are provided under terms of a License *
0N/A * Agreement between IBM and Sun. This technology is protected by multiple *
0N/A * US and International patents. This notice and attribution to IBM may not *
0N/A * to removed. *
0N/A *******************************************************************************
0N/A */
0N/A
0N/A/*
0N/A **********************************************************************
0N/A * Author: Alan Liu
0N/A * Created: September 23 2003
0N/A * Since: ICU 2.8
0N/A **********************************************************************
0N/A */
0N/A
0N/Apackage sun.text.normalizer;
0N/A
0N/Aimport java.text.ParsePosition;
0N/A
0N/A/**
0N/A * An iterator that returns 32-bit code points. This class is deliberately
0N/A * <em>not</em> related to any of the JDK or ICU4J character iterator classes
0N/A * in order to minimize complexity.
0N/A * @author Alan Liu
0N/A * @since ICU 2.8
0N/A */
0N/Apublic class RuleCharacterIterator {
0N/A
0N/A // TODO: Ideas for later. (Do not implement if not needed, lest the
0N/A // code coverage numbers go down due to unused methods.)
0N/A // 1. Add a copy constructor, equals() method, clone() method.
0N/A // 2. Rather than return DONE, throw an exception if the end
0N/A // is reached -- this is an alternate usage model, probably not useful.
0N/A // 3. Return isEscaped from next(). If this happens,
0N/A // don't keep an isEscaped member variable.
0N/A
0N/A /**
0N/A * Text being iterated.
0N/A */
0N/A private String text;
0N/A
0N/A /**
0N/A * Position of iterator.
0N/A */
0N/A private ParsePosition pos;
0N/A
0N/A /**
0N/A * Symbol table used to parse and dereference variables. May be null.
0N/A */
0N/A private SymbolTable sym;
0N/A
0N/A /**
0N/A * Current variable expansion, or null if none.
0N/A */
0N/A private char[] buf;
0N/A
0N/A /**
0N/A * Position within buf[]. Meaningless if buf == null.
0N/A */
0N/A private int bufPos;
0N/A
0N/A /**
0N/A * Flag indicating whether the last character was parsed from an escape.
0N/A */
0N/A private boolean isEscaped;
0N/A
0N/A /**
0N/A * Value returned when there are no more characters to iterate.
0N/A */
0N/A public static final int DONE = -1;
0N/A
0N/A /**
0N/A * Bitmask option to enable parsing of variable names. If (options &
0N/A * PARSE_VARIABLES) != 0, then an embedded variable will be expanded to
0N/A * its value. Variables are parsed using the SymbolTable API.
0N/A */
0N/A public static final int PARSE_VARIABLES = 1;
0N/A
0N/A /**
0N/A * Bitmask option to enable parsing of escape sequences. If (options &
0N/A * PARSE_ESCAPES) != 0, then an embedded escape sequence will be expanded
0N/A * to its value. Escapes are parsed using Utility.unescapeAt().
0N/A */
0N/A public static final int PARSE_ESCAPES = 2;
0N/A
0N/A /**
0N/A * Bitmask option to enable skipping of whitespace. If (options &
0N/A * SKIP_WHITESPACE) != 0, then whitespace characters will be silently
0N/A * skipped, as if they were not present in the input. Whitespace
0N/A * characters are defined by UCharacterProperty.isRuleWhiteSpace().
0N/A */
0N/A public static final int SKIP_WHITESPACE = 4;
0N/A
0N/A /**
0N/A * Constructs an iterator over the given text, starting at the given
0N/A * position.
0N/A * @param text the text to be iterated
0N/A * @param sym the symbol table, or null if there is none. If sym is null,
0N/A * then variables will not be deferenced, even if the PARSE_VARIABLES
0N/A * option is set.
0N/A * @param pos upon input, the index of the next character to return. If a
0N/A * variable has been dereferenced, then pos will <em>not</em> increment as
0N/A * characters of the variable value are iterated.
0N/A */
0N/A public RuleCharacterIterator(String text, SymbolTable sym,
0N/A ParsePosition pos) {
0N/A if (text == null || pos.getIndex() > text.length()) {
0N/A throw new IllegalArgumentException();
0N/A }
0N/A this.text = text;
0N/A this.sym = sym;
0N/A this.pos = pos;
0N/A buf = null;
0N/A }
0N/A
0N/A /**
0N/A * Returns true if this iterator has no more characters to return.
0N/A */
0N/A public boolean atEnd() {
0N/A return buf == null && pos.getIndex() == text.length();
0N/A }
0N/A
0N/A /**
0N/A * Returns the next character using the given options, or DONE if there
0N/A * are no more characters, and advance the position to the next
0N/A * character.
0N/A * @param options one or more of the following options, bitwise-OR-ed
0N/A * together: PARSE_VARIABLES, PARSE_ESCAPES, SKIP_WHITESPACE.
0N/A * @return the current 32-bit code point, or DONE
0N/A */
0N/A public int next(int options) {
0N/A int c = DONE;
0N/A isEscaped = false;
0N/A
0N/A for (;;) {
0N/A c = _current();
0N/A _advance(UTF16.getCharCount(c));
0N/A
0N/A if (c == SymbolTable.SYMBOL_REF && buf == null &&
0N/A (options & PARSE_VARIABLES) != 0 && sym != null) {
0N/A String name = sym.parseReference(text, pos, text.length());
0N/A // If name == null there was an isolated SYMBOL_REF;
0N/A // return it. Caller must be prepared for this.
0N/A if (name == null) {
0N/A break;
0N/A }
0N/A bufPos = 0;
0N/A buf = sym.lookup(name);
0N/A if (buf == null) {
0N/A throw new IllegalArgumentException(
0N/A "Undefined variable: " + name);
0N/A }
0N/A // Handle empty variable value
0N/A if (buf.length == 0) {
0N/A buf = null;
0N/A }
0N/A continue;
0N/A }
0N/A
0N/A if ((options & SKIP_WHITESPACE) != 0 &&
0N/A UCharacterProperty.isRuleWhiteSpace(c)) {
0N/A continue;
0N/A }
0N/A
0N/A if (c == '\\' && (options & PARSE_ESCAPES) != 0) {
0N/A int offset[] = new int[] { 0 };
0N/A c = Utility.unescapeAt(lookahead(), offset);
0N/A jumpahead(offset[0]);
0N/A isEscaped = true;
0N/A if (c < 0) {
0N/A throw new IllegalArgumentException("Invalid escape");
0N/A }
0N/A }
0N/A
0N/A break;
0N/A }
0N/A
0N/A return c;
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the last character returned by next() was
0N/A * escaped. This will only be the case if the option passed in to
0N/A * next() included PARSE_ESCAPED and the next character was an
0N/A * escape sequence.
0N/A */
0N/A public boolean isEscaped() {
0N/A return isEscaped;
0N/A }
0N/A
0N/A /**
0N/A * Returns true if this iterator is currently within a variable expansion.
0N/A */
0N/A public boolean inVariable() {
0N/A return buf != null;
0N/A }
0N/A
0N/A /**
0N/A * Returns an object which, when later passed to setPos(), will
0N/A * restore this iterator's position. Usage idiom:
0N/A *
0N/A * RuleCharacterIterator iterator = ...;
0N/A * Object pos = iterator.getPos(null); // allocate position object
0N/A * for (;;) {
0N/A * pos = iterator.getPos(pos); // reuse position object
0N/A * int c = iterator.next(...);
0N/A * ...
0N/A * }
0N/A * iterator.setPos(pos);
0N/A *
0N/A * @param p a position object previously returned by getPos(),
0N/A * or null. If not null, it will be updated and returned. If
0N/A * null, a new position object will be allocated and returned.
0N/A * @return a position object which may be passed to setPos(),
0N/A * either `p,' or if `p' == null, a newly-allocated object
0N/A */
0N/A public Object getPos(Object p) {
0N/A if (p == null) {
0N/A return new Object[] {buf, new int[] {pos.getIndex(), bufPos}};
0N/A }
0N/A Object[] a = (Object[]) p;
0N/A a[0] = buf;
0N/A int[] v = (int[]) a[1];
0N/A v[0] = pos.getIndex();
0N/A v[1] = bufPos;
0N/A return p;
0N/A }
0N/A
0N/A /**
0N/A * Restores this iterator to the position it had when getPos()
0N/A * returned the given object.
0N/A * @param p a position object previously returned by getPos()
0N/A */
0N/A public void setPos(Object p) {
0N/A Object[] a = (Object[]) p;
0N/A buf = (char[]) a[0];
0N/A int[] v = (int[]) a[1];
0N/A pos.setIndex(v[0]);
0N/A bufPos = v[1];
0N/A }
0N/A
0N/A /**
0N/A * Skips ahead past any ignored characters, as indicated by the given
0N/A * options. This is useful in conjunction with the lookahead() method.
0N/A *
0N/A * Currently, this only has an effect for SKIP_WHITESPACE.
0N/A * @param options one or more of the following options, bitwise-OR-ed
0N/A * together: PARSE_VARIABLES, PARSE_ESCAPES, SKIP_WHITESPACE.
0N/A */
0N/A public void skipIgnored(int options) {
0N/A if ((options & SKIP_WHITESPACE) != 0) {
0N/A for (;;) {
0N/A int a = _current();
0N/A if (!UCharacterProperty.isRuleWhiteSpace(a)) break;
0N/A _advance(UTF16.getCharCount(a));
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns a string containing the remainder of the characters to be
0N/A * returned by this iterator, without any option processing. If the
0N/A * iterator is currently within a variable expansion, this will only
0N/A * extend to the end of the variable expansion. This method is provided
0N/A * so that iterators may interoperate with string-based APIs. The typical
0N/A * sequence of calls is to call skipIgnored(), then call lookahead(), then
0N/A * parse the string returned by lookahead(), then call jumpahead() to
0N/A * resynchronize the iterator.
0N/A * @return a string containing the characters to be returned by future
0N/A * calls to next()
0N/A */
0N/A public String lookahead() {
0N/A if (buf != null) {
0N/A return new String(buf, bufPos, buf.length - bufPos);
0N/A } else {
0N/A return text.substring(pos.getIndex());
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Advances the position by the given number of 16-bit code units.
0N/A * This is useful in conjunction with the lookahead() method.
0N/A * @param count the number of 16-bit code units to jump over
0N/A */
0N/A public void jumpahead(int count) {
0N/A if (count < 0) {
0N/A throw new IllegalArgumentException();
0N/A }
0N/A if (buf != null) {
0N/A bufPos += count;
0N/A if (bufPos > buf.length) {
0N/A throw new IllegalArgumentException();
0N/A }
0N/A if (bufPos == buf.length) {
0N/A buf = null;
0N/A }
0N/A } else {
0N/A int i = pos.getIndex() + count;
0N/A pos.setIndex(i);
0N/A if (i > text.length()) {
0N/A throw new IllegalArgumentException();
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the current 32-bit code point without parsing escapes, parsing
0N/A * variables, or skipping whitespace.
0N/A * @return the current 32-bit code point
0N/A */
0N/A private int _current() {
0N/A if (buf != null) {
0N/A return UTF16.charAt(buf, 0, buf.length, bufPos);
0N/A } else {
0N/A int i = pos.getIndex();
0N/A return (i < text.length()) ? UTF16.charAt(text, i) : DONE;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Advances the position by the given amount.
0N/A * @param count the number of 16-bit code units to advance past
0N/A */
0N/A private void _advance(int count) {
0N/A if (buf != null) {
0N/A bufPos += count;
0N/A if (bufPos == buf.length) {
0N/A buf = null;
0N/A }
0N/A } else {
0N/A pos.setIndex(pos.getIndex() + count);
0N/A if (pos.getIndex() > text.length()) {
0N/A pos.setIndex(text.length());
0N/A }
0N/A }
0N/A }
0N/A}