KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > codegen > jet > JETMark


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2002-2004 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: JETMark.java,v 1.4 2005/06/08 06:15:56 nickb Exp $
16  */

17 package org.eclipse.emf.codegen.jet;
18
19
20 import java.util.Stack JavaDoc;
21
22 import org.eclipse.core.resources.IFile;
23 import org.eclipse.core.resources.IWorkspaceRoot;
24 import org.eclipse.core.resources.ResourcesPlugin;
25 import org.eclipse.core.runtime.Path;
26
27 import org.eclipse.emf.codegen.CodeGenPlugin;
28
29
30 /**
31  * A mark represents a point in the JET input.
32  */

33 public final class JETMark
34 {
35   /**
36    * This is the character offset.
37    */

38   protected int cursor;
39
40   /**
41    * This is the line index.
42    */

43   protected int line;
44
45   /**
46    * This is the column index.
47    */

48   protected int col;
49
50   /**
51    * This is the id of the file.
52    */

53   protected int fileid;
54
55   /**
56    * This is the base URI for relative paths.
57    */

58   protected String JavaDoc baseDir;
59
60   /**
61    * This is the stream of characters.
62    */

63   protected char[] stream = null;
64
65   /**
66    * This is the stack of inclusions.
67    */

68   protected Stack JavaDoc includeStack = null;
69
70   /**
71    * This is the encoding of the stream.
72    */

73   protected String JavaDoc encoding = null;
74
75   /**
76    * This is the reader that owns this mark.
77    */

78   protected JETReader reader;
79                              
80   /**
81    * Keep track of parser before parsing an included file.
82    * This class keeps track of the parser before we switch to parsing an
83    * included file. In other words, it's the parser's continuation to be
84    * reinstalled after the included file parsing is done.
85    */

86   class IncludeState
87   {
88     int cursor;
89     int line;
90     int col;
91     int fileid;
92     String JavaDoc baseDir;
93     String JavaDoc encoding;
94     char[] stream = null;
95
96     IncludeState(int inCursor, int inLine, int inCol, int inFileid, String JavaDoc inBaseDir, String JavaDoc inEncoding, char[] inStream)
97     {
98       cursor = inCursor;
99       line = inLine;
100       col = inCol;
101       fileid = inFileid;
102       baseDir = inBaseDir;
103       encoding = inEncoding;
104       stream = inStream;
105     }
106   }
107
108   /**
109    * Creates a new mark
110    * @param reader JETReader this mark belongs to
111    * @param inStream current stream for this mark
112    * @param fileid id of requested jet file
113    * @param inEncoding encoding of current file
114    * @param inBaseDir base directory of requested jet file
115    */

116   JETMark(JETReader reader, char[] inStream, int fileid, String JavaDoc inBaseDir, String JavaDoc inEncoding)
117   {
118     this.reader = reader;
119     this.stream = inStream;
120     this.cursor = this.line = this.col = 0;
121     this.fileid = fileid;
122     this.baseDir = inBaseDir;
123     this.encoding = inEncoding;
124     this.includeStack = new Stack JavaDoc();
125   }
126
127   JETMark(JETMark other)
128   {
129     this.reader = other.reader;
130     this.stream = other.stream;
131     this.fileid = other.fileid;
132     this.cursor = other.cursor;
133     this.line = other.line;
134     this.col = other.col;
135     this.baseDir = other.baseDir;
136     this.encoding = other.encoding;
137
138     // clone includeStack without cloning contents
139
//
140
includeStack = new Stack JavaDoc();
141     for (int i = 0; i < other.includeStack.size(); ++i)
142     {
143       includeStack.addElement(other.includeStack.elementAt(i));
144     }
145   }
146
147   /**
148    * Sets this mark's state to a new stream.
149    * It will store the current stream in it's includeStack.
150    * @param inStream new stream for mark
151    * @param inFileid id of new file from which stream comes from
152    * @param inBaseDir directory of file
153    * @param inEncoding encoding of new file
154    */

155   public void pushStream(char[] inStream, int inFileid, String JavaDoc inBaseDir, String JavaDoc inEncoding)
156   {
157     // Store the current state in stack.
158
//
159
includeStack.push(new IncludeState(cursor, line, col, fileid, baseDir, encoding, stream) );
160
161     // Set the new variables.
162
//
163
cursor = 0;
164     line = 0;
165     col = 0;
166     fileid = inFileid;
167     baseDir = inBaseDir;
168     encoding = inEncoding;
169     stream = inStream;
170   }
171
172   /**
173    * Restores this mark's state to a previously stored stream.
174    */

175   public boolean popStream()
176   {
177     // Make sure we have something to pop.
178
//
179
if (includeStack.size() <= 0)
180     {
181       return false;
182     }
183
184     // Get previous state in stack.
185
//
186
IncludeState state = (IncludeState) includeStack.pop( );
187
188     // Set the new variables.
189
//
190
cursor = state.cursor;
191     line = state.line;
192     col = state.col;
193     fileid = state.fileid;
194     baseDir = state.baseDir;
195     stream = state.stream;
196     return true;
197   }
198
199   public String JavaDoc getFile()
200   {
201     return reader.getFile(fileid);
202   }
203   
204   public String JavaDoc getBaseURI()
205   {
206     return reader.getBaseURI(fileid);
207   }
208
209   public String JavaDoc getLocalFile()
210   {
211     String JavaDoc file = reader.getFile(fileid);
212     if (file.startsWith("file:/"))
213     {
214       IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
215       IFile iFile = workspaceRoot.getFileForLocation(new Path(file.substring(6)));
216       file = iFile.getFullPath().toString();
217     }
218
219     return file;
220   }
221
222   public int getFileId()
223   {
224     return fileid;
225   }
226
227   public int getCursor()
228   {
229     return cursor;
230   }
231
232   public String JavaDoc toShortString()
233   {
234     return "(" + line + "," + col + ")";
235   }
236
237   public String JavaDoc toString()
238   {
239     return getLocalFile() + "(" + line + "," + col + ")";
240   }
241
242   public String JavaDoc format(String JavaDoc key)
243   {
244     return
245       CodeGenPlugin.getPlugin().getString
246         (key,
247          new Object JavaDoc []
248          {
249            getLocalFile(),
250            new Integer JavaDoc(line + 1),
251            new Integer JavaDoc(col + 1),
252            new Integer JavaDoc(cursor)
253          });
254   }
255
256   public boolean equals(Object JavaDoc other)
257   {
258     if (other instanceof JETMark)
259     {
260       JETMark m = (JETMark) other;
261       return
262         this.reader == m.reader &&
263           this.fileid == m.fileid &&
264           this.cursor == m.cursor &&
265           this.line == m.line &&
266           this.col == m.col;
267     }
268     return false;
269   }
270 }
271
Popular Tags