1 17 18 19 20 package org.apache.lenya.util; 21 22 import java.io.BufferedInputStream ; 23 import java.io.FileInputStream ; 24 import java.io.FileOutputStream ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.io.OutputStream ; 28 29 import org.apache.log4j.Category; 30 31 32 37 public class SSI { 38 static Category log = Category.getInstance(SSI.class); 39 40 static String fileinc = "<!--#include file=\""; 41 static String virtinc = "<!--#include virtual=\""; 42 43 46 public static void main(String [] args) { 47 if (args.length == 0) { 48 System.out.println("Usage: java org.apache.lenya.util.SSI <file> -o file.out"); 49 50 return; 51 } 52 53 try { 54 OutputStream out = System.out; 55 out = new FileOutputStream ("ssi-out.html"); 56 new SSI().includeFile(args[0], false, out); 57 out.close(); 58 } catch (Exception e) { 59 System.err.println(".main(): " + e); 60 } 61 } 62 63 70 public void includeFile(String fileName, boolean virtual, OutputStream out) 71 throws IOException { 72 if (virtual) { 73 String documentRoot = ""; 75 if (documentRoot != null) { 76 fileName = documentRoot + fileName; 77 } 78 } 79 80 log.info("Including file: " + fileName); 81 82 BufferedInputStream bis = new BufferedInputStream (new FileInputStream (fileName)); 83 parseStream(bis, out); 84 } 85 86 91 public void parseStream(InputStream in, OutputStream out) 92 throws IOException { 93 int type = 0; 94 int count = 0; 95 int c = -1; 96 97 while ((c = in.read()) != -1) { 98 if ((count < 13) && (c == fileinc.charAt(count))) { 99 count++; 100 101 log.debug("Matched shared character("+count+"): "+(char) c); 102 continue; 103 } else if (count == 13) { 104 if (c == fileinc.charAt(count)) { 105 type = 19; count++; 107 108 log.debug("Matched file character(14): f"); 109 log.debug("Seems like a file include has been found"); 110 continue; 111 } else if (c == virtinc.charAt(count)) { 112 type = 22; count++; 114 115 log.debug("Matched virt character(14): v"); 116 log.debug("Seems like a virtual include has been found"); 117 continue; 118 } else { 119 out.write(fileinc.substring(0, count).getBytes()); 120 type = 0; 121 count = 0; 122 } 123 } else if (count > 13) { 124 int oldcount = count; 125 126 if ((type == 19) && (c == fileinc.charAt(count))) { 127 count++; 128 129 log.debug("Matched file character("+count+"): "+(char)c); 130 } else if ((type == 22) && (c == virtinc.charAt(count))) { 131 count++; 132 133 log.debug("Matched virt character("+count+"): "+(char)c); 134 } else { 135 String outs = (type == 19) ? fileinc : virtinc; 136 out.write(outs.substring(0, count).getBytes()); 137 count = 0; 138 type = 0; 139 } 140 141 if (count >= type) { 142 143 StringBuffer fName = new StringBuffer (); 145 while (((c = in.read()) != -1) && (c != '"')) { 146 fName.append((char) c); 147 } 148 149 while (((c = in.read()) != -1) && (c != '>')); 151 152 includeFile(fName.toString(), (type == 22), out); 154 count = 0; 155 type = 0; 156 157 continue; 158 } 159 160 if (oldcount != count) { 161 continue; 162 } 163 } else { 164 if (count > 0) { 165 out.write(fileinc.substring(0, count).getBytes()); 166 } 167 168 count = 0; 169 type = 0; 170 } 171 172 out.write(c); 173 } 174 } 175 } 176 | Popular Tags |