1 20 21 package org.jacorb.idl; 22 23 import java.io.*; 24 import java.util.Stack ; 25 26 32 33 public class GlobalInputStream 34 { 35 private static InputStream stream; 36 private static Stack lookahead_stack; 37 private static boolean included; 38 private static StringBuffer expandedText; 39 private static int pos; 40 private static boolean eof; 41 private static File currentFile; 42 private static String [] path_names; 43 private static org.apache.log.Logger logger; 44 45 46 static java.util.Stack positions; 47 48 public static void init() 49 { 50 lookahead_stack = new Stack (); 51 included = false; 52 expandedText = new StringBuffer (); 53 pos = 0; 54 eof = false; 55 currentFile = null; 56 positions = new java.util.Stack (); 57 logger = parser.getLogger(); 58 } 59 60 public static void setInput(String fname) 61 throws java.io.IOException 62 { 63 currentFile = new File(fname); 64 stream = new java.io.FileInputStream (currentFile); 65 } 66 67 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 str) 89 { 90 expandedText.insert(pos, str); 91 } 92 93 public static void include(String 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 (lookahead)); 105 106 if (logger.isInfoEnabled()) 107 logger.info("Including " + fname); 108 109 } 110 111 public static void setIncludePath(String path) 112 { 113 java.util.StringTokenizer strtok = 114 new java.util.StringTokenizer (path, File.pathSeparator); 115 116 int i; 117 if (path_names == null) 118 { 119 path_names = new String [ strtok.countTokens() ]; 120 i = 0; 121 } 122 else 123 { 124 i = path_names.length; 125 126 String [] _path_names = new String [ 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 142 143 private static FileInputStream find(String fname, boolean useIncludePath) 144 throws FileNotFoundException 145 { 146 if (!useIncludePath) 147 { 148 if (fname.indexOf(File.separator) != 0) 149 { 150 String 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 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 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 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 (); 240 pos = 0; 241 } 242 } 243 else 244 { 245 ch = currentStream().read(); 246 247 252 253 if (ch == -1) 254 { 255 currentStream().close(); 257 if (included) 258 { 259 260 parser.setInhibitionState(false); 262 263 PositionInfo positionInfo = (PositionInfo)positions.pop(); 265 stream = positionInfo.stream; 266 currentFile = positionInfo.file; 267 ch = ((Integer )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 |