KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > idl > GlobalInputStream


1 /*
2  * JacORB - a free Java ORB
3  *
4  * Copyright (C) 1997-2004 Gerald Brose.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */

20
21 package org.jacorb.idl;
22
23 import java.io.*;
24 import java.util.Stack JavaDoc;
25
26 /**
27  * This class deals with IDL input files and their inclusion relationships.
28  *
29  * @author Gerald Brose <mailto:gerald.brose@acm.org>
30  * @version $Id: GlobalInputStream.java,v 1.22 2004/05/06 12:39:58 nicolas Exp $
31  */

32
33 public class GlobalInputStream
34 {
35     private static InputStream stream;
36     private static Stack JavaDoc lookahead_stack;
37     private static boolean included;
38     private static StringBuffer JavaDoc expandedText;
39     private static int pos;
40     private static boolean eof;
41     private static File currentFile;
42     private static String JavaDoc[] path_names;
43     private static org.apache.log.Logger logger;
44
45     /** stack of information for lexical scopes */
46     static java.util.Stack JavaDoc positions;
47
48     public static void init()
49     {
50         lookahead_stack = new Stack JavaDoc();
51         included = false;
52         expandedText = new StringBuffer JavaDoc();
53         pos = 0;
54         eof = false;
55         currentFile = null;
56         positions = new java.util.Stack JavaDoc();
57         logger = parser.getLogger();
58     }
59
60     public static void setInput(String JavaDoc fname)
61         throws java.io.IOException JavaDoc
62     {
63         currentFile = new File(fname);
64         stream = new java.io.FileInputStream JavaDoc(currentFile);
65     }
66
67     /**
68      * Test if this input stream (or rather the underlying IDL file)
69      * is more recent (was modified at a later time than) another
70      * file. (Used, e.g., to determine if code has been previosuly
71      * generated from an IDL file).
72      *
73      * @param other the file to compare this stream against
74      * @return true, if this stream's IDL file is more recent than the other file .
75      */

76
77     public static boolean isMoreRecentThan(File other)
78     {
79         return (parser.forceOverwrite ||
80                  (other.lastModified() < currentFile.lastModified())) ;
81     }
82
83     public static boolean includeState()
84     {
85         return included;
86     }
87
88     public static void insert(String JavaDoc str)
89     {
90         expandedText.insert(pos, str);
91     }
92
93     public static void include(String JavaDoc fname, int lookahead, boolean useIncludePath)
94         throws FileNotFoundException
95     {
96         included = true;
97         PositionInfo position = lexer.getPosition();
98         position.file = currentFile();
99         position.stream = stream;
100
101         stream = find(fname, useIncludePath);
102
103         positions.push(position);
104         lookahead_stack.push(new Integer JavaDoc(lookahead));
105
106         if (logger.isInfoEnabled())
107             logger.info("Including " + fname);
108         /* files form their own scopes, so we have to open a new one here */
109     }
110
111     public static void setIncludePath(String JavaDoc path)
112     {
113         java.util.StringTokenizer JavaDoc strtok =
114                 new java.util.StringTokenizer JavaDoc(path, File.pathSeparator);
115
116         int i;
117         if (path_names == null)
118         {
119             path_names = new String JavaDoc[ strtok.countTokens() ];
120             i = 0;
121         }
122         else
123         {
124             i = path_names.length;
125
126             String JavaDoc[] _path_names = new String JavaDoc[ strtok.countTokens() + path_names.length ];
127             for (int j = 0; j < path_names.length; j++)
128                 _path_names[ j ] = path_names[ j ];
129             path_names = _path_names;
130         }
131
132         while(strtok.hasMoreTokens())
133         {
134             path_names[ i++ ] = strtok.nextToken();
135         }
136     }
137
138     /**
139      * tries to locate and open a new file for "fname",
140      * updates currentFile if successful
141      */

142
143     private static FileInputStream find(String JavaDoc fname, boolean useIncludePath)
144             throws FileNotFoundException
145     {
146         if (!useIncludePath)
147         {
148             if (fname.indexOf(File.separator) != 0)
149             {
150                 String JavaDoc dir = null;
151                 try
152                 {
153                     dir = currentFile.getCanonicalPath();
154                     if (dir.indexOf(File.separator) > -1)
155                     {
156                         dir = dir.substring(0, dir.lastIndexOf(File.separator));
157                     }
158                 }
159                 catch(java.io.IOException JavaDoc ioe)
160                 {
161                     logger.error("Caught error finding file ", ioe);
162                 }
163
164                 if (logger.isInfoEnabled())
165                     logger.info("opening " + dir + File.separator + fname);
166                 currentFile = new File(dir + File.separator + fname);
167             }
168             else
169                 currentFile = new File(fname);
170
171             try
172             {
173                 return new FileInputStream(currentFile);
174             }
175             catch(java.io.IOException JavaDoc iof)
176             {
177                 return find(fname, true);
178             }
179         }
180         else
181         {
182             if (path_names == null)
183             {
184                 org.jacorb.idl.parser.fatal_error("File " + fname +
185                         " not found in include path", null);
186             }
187             else
188             {
189                 for (int i = 0; i < path_names.length; i++)
190                 {
191                     try
192                     {
193                         if (logger.isInfoEnabled())
194                             logger.info("opening " + path_names[ i ] + File.separator + fname);
195
196                         currentFile = new File(path_names[ i ] + File.separator + fname);
197                         return new FileInputStream(currentFile);
198                     }
199                     catch(FileNotFoundException fnfex)
200                     {
201                     }
202                 }
203             }
204
205             org.jacorb.idl.parser.fatal_error("File " + fname +
206                     " not found in include path", null);
207             return null;
208         }
209     }
210
211     public static File currentFile()
212     {
213         return currentFile;
214     }
215
216     public static InputStream currentStream()
217     {
218         return stream;
219     }
220
221     public static int read()
222         throws IOException
223     {
224         int ch = 0;
225
226         // Might be the end of file for main file but still have an included file
227
// to process.
228
if (eof && positions.size() == 0)
229         {
230             return -1;
231         }
232
233         if (expandedText.length() > 0)
234         {
235             if (pos < expandedText.length())
236                 ch = (int)expandedText.charAt(pos++);
237             if (pos == expandedText.length())
238             {
239                 expandedText = new StringBuffer JavaDoc();
240                 pos = 0;
241             }
242         }
243         else
244         {
245             ch = currentStream().read();
246
247             /*
248              * if eof is reached, see whether we were reading
249              * from the main input stream or from an include file.
250              * If the latter, switch back to the main stream
251              */

252
253             if (ch == -1)
254             {
255                 // the following line was moved here to fix bug #385
256
currentStream().close();
257                 if (included)
258                 {
259
260                     // undo effects of inhibition pragma
261
parser.setInhibitionState(false);
262
263                     // return to last position in previous file
264
PositionInfo positionInfo = (PositionInfo)positions.pop();
265                     stream = positionInfo.stream;
266                     currentFile = positionInfo.file;
267                     ch = ((Integer JavaDoc)lookahead_stack.pop()).intValue();
268
269                     included = !(positions.empty());
270
271                     if (logger.isInfoEnabled())
272                         logger.info("returning to " + currentFile + " included: " + included);
273
274                     lexer.restorePosition(positionInfo);
275                 }
276                 else
277                 {
278                     eof = true;
279                 }
280             }
281         }
282         return ch;
283     }
284
285 }
286
Popular Tags