/*
 * JSPTem - use jsp's for all your templating!
 * Copyright (c) 2006, Paul Tuckey
 * All rights reserved.
 *
 * JSPTem is based on GNUJSP (Copyright (C) 1999, Yaroslav Faybishenko <yaroslav@cs.berkeley.edu>)
 *
 * Each copy or derived work must preserve the copyright notice and this
 * notice unmodified.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
package org.tuckey.jsptem;

import java.io.CharArrayReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.Reader;
import java.net.URL;
import java.util.Stack;
import java.util.Vector;

class StackedReader extends Reader {
    private Stack readers;
    private Stack includes;
    // the names as the user knows them.
    private Vector visitedIncludes;
    // the resources as gnujsp knows them (e.g. file on disk)
    private Vector visitedResources;

    private LineNumberReader currentReader;
    private String currentInclude;
    // the use of StackedReader may store data here
    private Object contextInfo = null;

    StackedReader(Reader r, String o, URL resource) {
        readers = new Stack();
        includes = new Stack();
        visitedIncludes = new Vector();
        visitedResources = new Vector();
        pushReader(r, o, resource);
    }

    void setContextInfo(Object o) {
        contextInfo = o;
    }

    Object getContextInfo() {
        return contextInfo;
    }

    int getLineNumber() {
        return currentReader.getLineNumber();
    }

    /**
     * we are a StackedReader, so multiple files may be involved
     */
    Pos getPos() {
        return new Pos(getCurrentInclude(), currentReader.getLineNumber() + 1);
    }

    public void pushReader(Reader r, String o, URL resource) {
        if (currentReader != null) {
            readers.push(currentReader);
            includes.push(currentInclude);
        }
        currentReader = (r instanceof LineNumberReader) ? (LineNumberReader) r : new LineNumberReader(r);
        if (o != null) {
            visitedIncludes.addElement(o);
            currentInclude = o;
        }
        if (resource != null) {
            visitedResources.addElement(resource);
        }
    }

    private void pop() {
        currentReader = (LineNumberReader) readers.pop();
        currentInclude = (String) includes.pop();
    }

    public String getCurrentInclude() {
        return currentInclude;
    }

    public String[] getAllIncludes() {
        String[] all = new String [visitedIncludes.size()];
        visitedIncludes.copyInto(all);
        return all;
    }

    public URL[] getAllIncludesAsResources() {
        URL[] all = new URL [visitedResources.size()];
        visitedResources.copyInto(all);
        return all;
    }

    public final int read() throws IOException {
        if (currentReader == null) return -1;
        int c = currentReader.read();
        if (c == -1) {
            if (!readers.empty()) {
                currentReader.close();
                pop();
                return read();
            }
        }
        return c;
    }

    public final int read(char[] buf) throws IOException {
        return read(buf, 0, buf.length);
    }

    public final int read(char[] buf, int off, int len) throws IOException {
        int ret = currentReader.read(buf, off, len);

        if (ret == -1) {
            if (!readers.empty()) {
                currentReader.close();
                pop();
                return read(buf, off, len);
            }
        }
        return ret;
    }

    public boolean ready() throws IOException {
        return currentReader.ready();
    }

    public void reset() throws IOException {
        currentReader.reset();
    }

    public void close() throws IOException {
        if (currentReader != null) currentReader.close();
        if (!readers.empty()) {
            pop();
        }
    }

    public void mark(int readAheadLimit) throws IOException {
        currentReader.mark(readAheadLimit);
    }


    public boolean markSupported() {
        return currentReader.markSupported();
    }

    public long skip(long n) throws IOException {
        long skipped = currentReader.skip(n);

        if (skipped < n) {
            if (!readers.empty()) {
                currentReader.close();
                pop();
                return currentReader.skip(n - skipped);
            }
        }

        return skipped;
    }

    public void unread(char [] buf, int off, int len) {
        pushReader(new CharArrayReader(buf, off, len), null, null);
    }
}
