001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.tools.common.toolspec.parser;
019:
020: import java.util.logging.Level;
021: import java.util.logging.Logger;
022:
023: import org.apache.cxf.common.logging.LogUtils;
024:
025: public class TokenInputStream {
026:
027: private static final Logger LOG = LogUtils
028: .getL7dLogger(TokenInputStream.class);
029: private final String[] tokens;
030: private int pos;
031:
032: public TokenInputStream(String[] t) {
033: this .tokens = t;
034: }
035:
036: public String read() {
037: if (LOG.isLoggable(Level.FINE)) {
038: LOG.fine("Reading token " + tokens[pos]);
039: }
040: return tokens[pos++];
041: }
042:
043: public String readNext() {
044: return read(pos++);
045: }
046:
047: public String read(int position) {
048: if (position < 0) {
049: pos = 0;
050: }
051: if (position > tokens.length) {
052: pos = tokens.length - 1;
053: }
054: return tokens[pos];
055: }
056:
057: public String readPre() {
058: if (pos != 0) {
059: pos--;
060: }
061: return tokens[pos];
062: }
063:
064: public String peek() {
065: if (LOG.isLoggable(Level.FINE)) {
066: LOG.fine("Peeking token " + tokens[pos]);
067: }
068: return tokens[pos];
069: }
070:
071: public String peekPre() {
072: if (pos == 0) {
073: return tokens[pos];
074: }
075: return tokens[pos - 1];
076: }
077:
078: public String peek(int position) {
079: if (position < 0) {
080: return tokens[0];
081: }
082: if (position > tokens.length) {
083: return tokens[tokens.length - 1];
084: }
085: return tokens[position];
086: }
087:
088: public int getPosition() {
089: return pos;
090: }
091:
092: public void setPosition(int p) {
093: this .pos = p;
094: }
095:
096: public int available() {
097: return tokens.length - pos;
098: }
099:
100: public boolean hasNext() {
101: return available() > 1;
102: }
103:
104: public boolean isOutOfBound() {
105: return pos >= tokens.length;
106: }
107:
108: public String toString() {
109: StringBuffer sb = new StringBuffer("[ ");
110:
111: for (int i = pos; i < tokens.length; i++) {
112: sb.append(tokens[i]);
113: if (i < tokens.length - 1) {
114: sb.append(" ");
115: }
116: }
117: sb.append(" ]");
118: return sb.toString();
119: }
120:
121: }
|