001: /*
002: * Copyright (c) 1997-1999 The Java Apache Project. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions
006: * are met:
007: *
008: * 1. Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: *
011: * 2. Redistributions in binary form must reproduce the above copyright
012: * notice, this list of conditions and the following disclaimer in
013: * the documentation and/or other materials provided with the
014: * distribution.
015: *
016: * 3. All advertising materials mentioning features or use of this
017: * software must display the following acknowledgment:
018: * "This product includes software developed by the Java Apache
019: * Project for use in the Apache JServ servlet engine project
020: * (http://java.apache.org/)."
021: *
022: * 4. The names "Apache JServ", "Apache JServ Servlet Engine" and
023: * "Java Apache Project" must not be used to endorse or promote products
024: * derived from this software without prior written permission.
025: *
026: * 5. Products derived from this software may not be called "Apache JServ"
027: * nor may "Apache" nor "Apache JServ" appear in their names without
028: * prior written permission of the Java Apache Project.
029: *
030: * 6. Redistributions of any form whatsoever must retain the following
031: * acknowledgment:
032: * "This product includes software developed by the Java Apache
033: * Project for use in the Apache JServ servlet engine project
034: * (http://java.apache.org/)."
035: *
036: * THIS SOFTWARE IS PROVIDED BY THE JAVA APACHE PROJECT "AS IS" AND ANY
037: * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
038: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
039: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE JAVA APACHE PROJECT OR
040: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
042: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
043: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
044: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
045: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
046: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
047: * OF THE POSSIBILITY OF SUCH DAMAGE.
048: *
049: * This software consists of voluntary contributions made by many
050: * individuals on behalf of the Java Apache Group. For more information
051: * on the Java Apache Project and the Apache JServ Servlet Engine project,
052: * please see <http://java.apache.org/>.
053: */
054:
055: /*
056: * Copyright 2000,2005 wingS development team.
057: *
058: * This file is part of wingS (http://wingsframework.org).
059: *
060: * wingS is free software; you can redistribute it and/or modify
061: * it under the terms of the GNU Lesser General Public License
062: * as published by the Free Software Foundation; either version 2.1
063: * of the License, or (at your option) any later version.
064: *
065: * Please see COPYING for the complete licence.
066: */
067: package org.wings.template.parser;
068:
069: import java.io.IOException;
070: import java.io.Reader;
071:
072: /**
073: * PositionReader can be asked for the
074: * current position in the stream.
075: *
076: * @author <a href="mailto:zeller@think.de">Henner Zeller</a>
077: */
078: public class PositionReader extends Reader {
079:
080: private Reader reader = null;
081: private long position;
082: private long savePosition = -1;
083:
084: public PositionReader(Reader r) {
085: this .reader = r;
086: position = 0;
087: }
088:
089: /* ---- PositionReader ---- */
090: public long getPosition() {
091: return position;
092: }
093:
094: /* ---- Implementation of Reader ---- */
095:
096: public int read() throws IOException {
097: int c = reader.read();
098: if (c != -1)
099: position++;
100: return c;
101: }
102:
103: public int read(char cbuff[]) throws IOException {
104: int num = reader.read(cbuff);
105: if (num > 0)
106: position += num;
107: return num;
108: }
109:
110: public int read(char cbuff[], int off, int len) throws IOException {
111: int num = reader.read(cbuff, off, len);
112: if (num > 0)
113: position += num;
114: return num;
115: }
116:
117: public long skip(long n) throws IOException {
118: long skipped = reader.skip(n);
119: position += skipped;
120: return skipped;
121: }
122:
123: public boolean ready() throws IOException {
124: return reader.ready();
125: }
126:
127: public boolean markSupported() {
128: return reader.markSupported();
129: }
130:
131: public void mark(int readAheadLimit) throws IOException {
132: savePosition = position;
133: reader.mark(readAheadLimit);
134: }
135:
136: public void reset() throws IOException {
137: reader.reset();
138: // this should be noticed by the reader before
139: if (savePosition < 0)
140: throw new IOException("mark() not set before");
141: position = savePosition;
142: }
143:
144: public void close() throws IOException {
145: reader.close();
146: }
147: }
|