1 23 package com.sun.enterprise.tools.jsfext.util; 24 25 import java.io.BufferedInputStream ; 26 import java.io.File ; 27 import java.io.FileInputStream ; 28 import java.io.FileNotFoundException ; 29 import java.io.FilterInputStream ; 30 import java.io.IOException ; 31 import java.io.InputStream ; 32 import java.lang.reflect.InvocationTargetException ; 33 import java.lang.reflect.Method ; 34 35 36 44 public class IncludeInputStream extends FilterInputStream { 45 46 49 public IncludeInputStream(InputStream input) { 50 super(input); 51 } 52 53 58 public int read() throws IOException { 59 int intChar = -1; 60 if (redirStream != null) { 61 intChar = redirStream.read(); 63 if (intChar != -1) { 64 return intChar; 65 } 66 67 redirStream = null; 69 } 70 71 intChar = super.read(); 73 char ch = (char) intChar; 74 75 if (eol) { 77 if (ch == '#') { 79 intChar = startInclude(); 80 } else { 81 eol = false; 82 } 83 } 84 85 if ((ch == 0x0A) || (ch == 0x0D)) { 87 eol = true; 88 } 89 90 return intChar; 91 } 92 93 public int available() throws IOException { 94 return 0; 95 } 96 97 public boolean markSupported() { 98 return false; 99 } 100 101 public int read(byte[] bytes, int off, int len) throws IOException { 102 if (bytes == null) { 103 throw new NullPointerException (); 104 } else if ((off < 0) || (off > bytes.length) || (len < 0) 105 || ((off + len) > bytes.length) || ((off + len) < 0)) { 106 throw new IndexOutOfBoundsException (); 107 } else if (len == 0) { 108 return 0; 109 } 110 111 int c = read(); 112 if (c == -1) { 113 return -1; 114 } 115 bytes[off] = (byte) c; 116 117 int i = 1; 118 try { 119 for (; i < len; i++) { 120 c = read(); 121 if (c == -1) { 122 break; 123 } 124 if (bytes != null) { 125 bytes[off + i] = (byte) c; 126 } 127 } 128 } catch (IOException ee) { 129 ee.printStackTrace(); 130 } 131 return i; 132 } 133 134 137 private int startInclude() throws IOException { 138 char ch; 140 for (int count = 0; count < INCLUDE_LEN; count++) { 141 ch = (char) super.read(); 143 if (Character.toLowerCase(ch) != INCLUDE.charAt(count)) { 144 throw new RuntimeException ( 145 "\"#include\" expected in " 146 + "IncludeInputStream."); 147 } 148 } 149 150 ch = (char) super.read(); 152 while ((ch == ' ') || (ch == '\t')) { 153 ch = (char) super.read(); 154 } 155 156 if ((ch == '"') || (ch == '\'')) { 158 ch = (char) super.read(); 159 } 160 161 StringBuffer buf = new StringBuffer (""); 163 while ((ch != '"') 164 && (ch != '\'') 165 && (ch != 0x0A) 166 && (ch != 0x0D) 167 && (ch != -1)) { 168 buf.append(ch); 169 ch = (char) super.read(); 170 } 171 172 if ((ch == '"') || (ch == '\'')) { 174 ch = (char) super.read(); 175 } 176 177 String filename = buf.toString(); 179 180 if (FACES_CONTEXT != null) { 182 filename = convertRelativePath(filename); 184 } 185 File file = new File (filename); 186 if (file.exists()) { 188 redirStream = new IncludeInputStream( 190 new BufferedInputStream (new FileInputStream (file))); 191 } else { 192 ClassLoader loader = Util.getClassLoader(this); 194 InputStream stream = loader.getResourceAsStream(filename); 195 if (stream == null) { 196 stream = loader.getResourceAsStream("/" + filename); 197 if (stream == null) { 198 stream = loader.getResourceAsStream( 199 "META-INF/" + filename); 200 if (stream == null) { 201 throw new FileNotFoundException (filename); 202 } 203 } 204 } 205 redirStream = new IncludeInputStream( 206 new BufferedInputStream (stream)); 207 } 208 209 return redirStream.read(); 211 } 212 213 225 protected String convertRelativePath(String filename) { 226 230 Method method = null; 232 Object ctx = null; 233 String newFilename = null; 234 try { 235 method = FACES_CONTEXT.getMethod( 238 "getCurrentInstance", (Class []) null); 239 ctx = method.invoke((Object ) null, (Object []) null); 240 241 method = ctx.getClass().getMethod( 243 "getExternalContext", (Class []) null); 244 ctx = method.invoke(ctx, (Object []) null); 245 246 method = ctx.getClass().getMethod( 248 "getContext", (Class []) null); 249 ctx = method.invoke(ctx, (Object []) null); 250 251 method = ctx.getClass().getMethod( 253 "getRealPath", GET_REAL_PATH_ARGS); 254 newFilename = (String ) method.invoke(ctx, new Object [] {filename}); 255 if (!(new File (newFilename)).exists()) { 256 newFilename = filename; 258 } 259 } catch (NoSuchMethodException ex) { 260 throw new RuntimeException (ex); 261 } catch (IllegalAccessException ex) { 262 throw new RuntimeException (ex); 263 } catch (InvocationTargetException ex) { 264 throw new RuntimeException (ex); 265 } 266 return newFilename; 267 } 268 269 272 public static void main(String [] args) { 273 try { 274 IncludeInputStream stream = 275 new IncludeInputStream(new FileInputStream (args[0])); 276 int ch = '\n'; 277 while (ch != -1) { 278 System.out.print((char) ch); 279 ch = stream.read(); 280 } 281 } catch (Exception ex) { 282 ex.printStackTrace(); 283 } 284 } 285 286 private boolean eol = true; 287 private IncludeInputStream redirStream = null; 288 289 private static final Class [] GET_REAL_PATH_ARGS = 290 new Class [] {String .class}; 291 292 private static final String INCLUDE = "include"; 293 private static final int INCLUDE_LEN = INCLUDE.length(); 294 295 private static Class FACES_CONTEXT; 296 297 static { 298 try { 299 FACES_CONTEXT = Class.forName("javax.faces.context.FacesContext"); 300 } catch (Exception ex) { 301 FACES_CONTEXT = null; 303 } 304 } 305 } 306 | Popular Tags |