KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > util > SSI


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 /* $Id: SSI.java 126545 2005-01-26 22:12:27Z michi $ */
19
20 package org.apache.lenya.util;
21
22 import java.io.BufferedInputStream JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.FileOutputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.OutputStream JavaDoc;
28
29 import org.apache.log4j.Category;
30
31
32 /**
33  * SSI - Server Side Include, which can be used within a ResourceReader supporting SSI
34  *
35  * @deprecated
36  */

37 public class SSI {
38     static Category log = Category.getInstance(SSI.class);
39
40     static String JavaDoc fileinc = "<!--#include file=\"";
41     static String JavaDoc virtinc = "<!--#include virtual=\"";
42
43     /**
44      *
45      */

46     public static void main(String JavaDoc[] 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 JavaDoc out = System.out;
55             out = new FileOutputStream JavaDoc("ssi-out.html");
56             new SSI().includeFile(args[0], false, out);
57             out.close();
58         } catch (Exception JavaDoc e) {
59             System.err.println(".main(): " + e);
60         }
61     }
62
63     /**
64      * Read the specified file and parse server side include instructions
65      *
66      * @param fileName The file path to read in
67      * @param virtual Whether the fileName parameter is absolut or relativ to the document root of the web server
68      * @param out The OutputStream where to write output to
69      */

70     public void includeFile(String JavaDoc fileName, boolean virtual, OutputStream JavaDoc out)
71         throws IOException JavaDoc {
72         if (virtual) {
73             String JavaDoc documentRoot = ""; //(String) r_request.get("Request.DOCUMENT_ROOT");
74

75             if (documentRoot != null) {
76                 fileName = documentRoot + fileName;
77             }
78         }
79
80         log.info("Including file: " + fileName);
81
82         BufferedInputStream JavaDoc bis = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(fileName));
83         parseStream(bis, out);
84     }
85
86     /** Parse server side include instruction in the input stream and put the input
87      * stream together with the includes to the output stream
88      * @param in The input stream with instruction inline
89      * @param out The output stream
90      */

91     public void parseStream(InputStream JavaDoc in, OutputStream JavaDoc out)
92         throws IOException JavaDoc {
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; // file include
106
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; // virtual include
113
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 JavaDoc 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                     // Read the filename of the file to be included
144
StringBuffer JavaDoc fName = new StringBuffer JavaDoc();
145                     while (((c = in.read()) != -1) && (c != '"')) {
146                         fName.append((char) c);
147                     }
148
149                     // Read to the end of the include statement
150
while (((c = in.read()) != -1) && (c != '>'));
151
152                     // Include the file
153
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