KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jasper > compiler > Mark


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.jasper.compiler;
18
19 import java.util.Stack JavaDoc;
20 import java.net.URL JavaDoc;
21 import java.net.MalformedURLException JavaDoc;
22 import org.apache.jasper.JspCompilationContext;
23
24 /**
25  * Mark represents a point in the JSP input.
26  *
27  * @author Anil K. Vijendran
28  */

29 final class Mark {
30
31     // position within current stream
32
int cursor, line, col;
33
34     // directory of file for current stream
35
String JavaDoc baseDir;
36
37     // current stream
38
char[] stream = null;
39
40     // fileid of current stream
41
private int fileId;
42
43     // name of the current file
44
private String JavaDoc fileName;
45
46     /*
47      * stack of stream and stream state of streams that have included
48      * current stream
49      */

50     private Stack JavaDoc includeStack = null;
51
52     // encoding of current file
53
private String JavaDoc encoding = null;
54
55     // reader that owns this mark (so we can look up fileid's)
56
private JspReader reader;
57
58     private JspCompilationContext ctxt;
59
60     /**
61      * Constructor
62      *
63      * @param reader JspReader this mark belongs to
64      * @param inStream current stream for this mark
65      * @param fileId id of requested jsp file
66      * @param name JSP file name
67      * @param inBaseDir base directory of requested jsp file
68      * @param inEncoding encoding of current file
69      */

70     Mark(JspReader reader, char[] inStream, int fileId, String JavaDoc name,
71          String JavaDoc inBaseDir, String JavaDoc inEncoding) {
72
73         this.reader = reader;
74         this.ctxt = reader.getJspCompilationContext();
75         this.stream = inStream;
76         this.cursor = 0;
77         this.line = 1;
78         this.col = 1;
79         this.fileId = fileId;
80         this.fileName = name;
81         this.baseDir = inBaseDir;
82         this.encoding = inEncoding;
83         this.includeStack = new Stack JavaDoc();
84     }
85
86
87     /**
88      * Constructor
89      */

90     Mark(Mark other) {
91
92         this.reader = other.reader;
93         this.ctxt = other.reader.getJspCompilationContext();
94         this.stream = other.stream;
95         this.fileId = other.fileId;
96         this.fileName = other.fileName;
97         this.cursor = other.cursor;
98         this.line = other.line;
99         this.col = other.col;
100         this.baseDir = other.baseDir;
101         this.encoding = other.encoding;
102
103         // clone includeStack without cloning contents
104
includeStack = new Stack JavaDoc();
105         for ( int i=0; i < other.includeStack.size(); i++ ) {
106             includeStack.addElement( other.includeStack.elementAt(i) );
107         }
108     }
109
110
111     /**
112      * Constructor
113      */

114     Mark(JspCompilationContext ctxt, String JavaDoc filename, int line, int col) {
115
116         this.reader = null;
117         this.ctxt = ctxt;
118         this.stream = null;
119         this.cursor = 0;
120         this.line = line;
121         this.col = col;
122         this.fileId = -1;
123         this.fileName = filename;
124         this.baseDir = "le-basedir";
125         this.encoding = "le-endocing";
126         this.includeStack = null;
127     }
128
129
130     /**
131      * Sets this mark's state to a new stream.
132      * It will store the current stream in it's includeStack.
133      *
134      * @param inStream new stream for mark
135      * @param inFileId id of new file from which stream comes from
136      * @param inBaseDir directory of file
137      * @param inEncoding encoding of new file
138      */

139     public void pushStream(char[] inStream, int inFileId, String JavaDoc name,
140                            String JavaDoc inBaseDir, String JavaDoc inEncoding)
141     {
142         // store current state in stack
143
includeStack.push(new IncludeState(cursor, line, col, fileId,
144                                            fileName, baseDir,
145                        encoding, stream) );
146
147         // set new variables
148
cursor = 0;
149         line = 1;
150         col = 1;
151         fileId = inFileId;
152         fileName = name;
153         baseDir = inBaseDir;
154         encoding = inEncoding;
155         stream = inStream;
156     }
157
158
159     /**
160      * Restores this mark's state to a previously stored stream.
161      * @return The previous Mark instance when the stream was pushed, or null
162      * if there is no previous stream
163      */

164     public Mark popStream() {
165         // make sure we have something to pop
166
if ( includeStack.size() <= 0 ) {
167             return null;
168         }
169
170         // get previous state in stack
171
IncludeState state = (IncludeState) includeStack.pop( );
172
173         // set new variables
174
cursor = state.cursor;
175         line = state.line;
176         col = state.col;
177         fileId = state.fileId;
178         fileName = state.fileName;
179         baseDir = state.baseDir;
180         stream = state.stream;
181         return this;
182     }
183
184
185     // -------------------- Locator interface --------------------
186

187     public int getLineNumber() {
188         return line;
189     }
190
191     public int getColumnNumber() {
192         return col;
193     }
194
195     public String JavaDoc getSystemId() {
196         return getFile();
197     }
198
199     public String JavaDoc getPublicId() {
200         return null;
201     }
202
203     public String JavaDoc toString() {
204     return getFile()+"("+line+","+col+")";
205     }
206
207     public String JavaDoc getFile() {
208         return this.fileName;
209     }
210
211     /**
212      * Gets the URL of the resource with which this Mark is associated
213      *
214      * @return URL of the resource with which this Mark is associated
215      *
216      * @exception MalformedURLException if the resource pathname is incorrect
217      */

218     public URL JavaDoc getURL() throws MalformedURLException JavaDoc {
219         return ctxt.getResource(getFile());
220     }
221
222     public String JavaDoc toShortString() {
223         return "("+line+","+col+")";
224     }
225
226     public boolean equals(Object JavaDoc other) {
227     if (other instanceof Mark) {
228         Mark m = (Mark) other;
229         return this.reader == m.reader && this.fileId == m.fileId
230         && this.cursor == m.cursor && this.line == m.line
231         && this.col == m.col;
232     }
233     return false;
234     }
235
236     /**
237      * @return true if this Mark is greather than the <code>other</code>
238      * Mark, false otherwise.
239      */

240     public boolean isGreater(Mark other) {
241
242         boolean greater = false;
243
244         if (this.line > other.line) {
245             greater = true;
246         } else if (this.line == other.line && this.col > other.col) {
247             greater = true;
248         }
249
250         return greater;
251     }
252
253     /**
254      * Keep track of parser before parsing an included file.
255      * This class keeps track of the parser before we switch to parsing an
256      * included file. In other words, it's the parser's continuation to be
257      * reinstalled after the included file parsing is done.
258      */

259     class IncludeState {
260         int cursor, line, col;
261         int fileId;
262         String JavaDoc fileName;
263         String JavaDoc baseDir;
264         String JavaDoc encoding;
265         char[] stream = null;
266
267         IncludeState(int inCursor, int inLine, int inCol, int inFileId,
268                      String JavaDoc name, String JavaDoc inBaseDir, String JavaDoc inEncoding,
269                      char[] inStream) {
270             cursor = inCursor;
271             line = inLine;
272             col = inCol;
273             fileId = inFileId;
274             fileName = name;
275             baseDir = inBaseDir;
276             encoding = inEncoding;
277             stream = inStream;
278         }
279     }
280
281 }
282
283
Popular Tags