001: /*
002: * The Apache Software License, Version 1.1
003: *
004: * Copyright (c) 1999 The Apache Software Foundation. All rights
005: * reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The end-user documentation included with the redistribution, if
020: * any, must include the following acknowlegement:
021: * "This product includes software developed by the
022: * Apache Software Foundation (http://www.apache.org/)."
023: * Alternately, this acknowlegement may appear in the software itself,
024: * if and wherever such third-party acknowlegements normally appear.
025: *
026: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
027: * Foundation" must not be used to endorse or promote products derived
028: * from this software without prior written permission. For written
029: * permission, please contact apache@apache.org.
030: *
031: * 5. Products derived from this software may not be called "Apache"
032: * nor may "Apache" appear in their names without prior written
033: * permission of the Apache Group.
034: *
035: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: *
049: * This software consists of voluntary contributions made by many
050: * individuals on behalf of the Apache Software Foundation. For more
051: * information on the Apache Software Foundation, please see
052: * <http://www.apache.org/>.
053: *
054: */
055: package com.sun.portal.providers.jsp.jasper3.jasper.compiler;
056:
057: import java.util.Stack;
058:
059: /**
060: * Mark represents a point in the JSP input.
061: *
062: * @author Anil K. Vijendran
063: */
064: public final class Mark {
065: int cursor, line, col; // position within current stream
066: int fileid; // fileid of current stream
067: String fileName; // name of the current file
068: String baseDir; // directory of file for current stream
069: char[] stream = null; // current stream
070: Stack includeStack = null; // stack of stream and stream state of streams
071: // that have included current stream
072: String encoding = null; // encoding of current file
073: private JspReader reader; // reader that owns this mark
074:
075: // (so we can look up fileid's)
076:
077: /**
078: * Keep track of parser before parsing an included file.
079: * This class keeps track of the parser before we switch to parsing an
080: * included file. In other words, it's the parser's continuation to be
081: * reinstalled after the included file parsing is done.
082: */
083: class IncludeState {
084: int cursor, line, col;
085: int fileid;
086: String fileName;
087: String baseDir;
088: String encoding;
089: char[] stream = null;
090:
091: IncludeState(int inCursor, int inLine, int inCol, int inFileid,
092: String name, String inBaseDir, String inEncoding,
093: char[] inStream) {
094: cursor = inCursor;
095: line = inLine;
096: col = inCol;
097: fileid = inFileid;
098: fileName = name;
099: baseDir = inBaseDir;
100: encoding = inEncoding;
101: stream = inStream;
102: }
103: }
104:
105: /**
106: * Creates a new mark
107: * @param inReader JspReader this mark belongs to
108: * @param inStream current stream for this mark
109: * @param inFileid id of requested jsp file
110: * @param inEncoding encoding of current file
111: * @param inBaseDir base directory of requested jsp file
112: */
113: Mark(JspReader reader, char[] inStream, int fileid, String name,
114: String inBaseDir, String inEncoding) {
115: this .reader = reader;
116: this .stream = inStream;
117: this .cursor = this .line = this .col = 0;
118: this .fileid = fileid;
119: this .fileName = name;
120: this .baseDir = inBaseDir;
121: this .encoding = inEncoding;
122: this .includeStack = new Stack();
123: }
124:
125: Mark(Mark other) {
126: this .reader = other.reader;
127: this .stream = other.stream;
128: this .fileid = other.fileid;
129: this .fileName = other.fileName;
130: this .cursor = other.cursor;
131: this .line = other.line;
132: this .col = other.col;
133: this .baseDir = other.baseDir;
134: this .encoding = other.encoding;
135:
136: // clone includeStack without cloning contents
137: includeStack = new Stack();
138: for (int i = 0; i < other.includeStack.size(); i++) {
139: includeStack.addElement(other.includeStack.elementAt(i));
140: }
141: }
142:
143: /** Sets this mark's state to a new stream.
144: * It will store the current stream in it's includeStack.
145: * @param inStream new stream for mark
146: * @param inFileid id of new file from which stream comes from
147: * @param inBaseDir directory of file
148: * @param inEncoding encoding of new file
149: */
150: public void pushStream(char[] inStream, int inFileid, String name,
151: String inBaseDir, String inEncoding) {
152:
153: // store current state in stack
154: includeStack.push(new IncludeState(cursor, line, col, fileid,
155: fileName, baseDir, encoding, stream));
156:
157: // set new variables
158: cursor = 0;
159: line = 0;
160: col = 0;
161: fileid = inFileid;
162: fileName = name;
163: baseDir = inBaseDir;
164: encoding = inEncoding;
165: stream = inStream;
166: }
167:
168: /** Restores this mark's state to a previously stored stream.
169: */
170: public boolean popStream() {
171: // make sure we have something to pop
172: if (includeStack.size() <= 0)
173: return false;
174:
175: // get previous state in stack
176: IncludeState state = (IncludeState) includeStack.pop();
177:
178: // set new variables
179: cursor = state.cursor;
180: line = state.line;
181: col = state.col;
182: fileid = state.fileid;
183: fileName = state.fileName;
184: baseDir = state.baseDir;
185: stream = state.stream;
186: return true;
187: }
188:
189: public String toString() {
190: return getFile() + "(" + line + "," + col + ")";
191: }
192:
193: public String getFile() {
194: return this .fileName;
195: }
196:
197: public String toShortString() {
198: return "(" + line + "," + col + ")";
199: }
200:
201: public boolean equals(Object other) {
202: if (other instanceof Mark) {
203: Mark m = (Mark) other;
204: return this .reader == m.reader && this .fileid == m.fileid
205: && this .cursor == m.cursor && this .line == m.line
206: && this .col == m.col;
207: }
208: return false;
209: }
210: }
|