KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > generation > PhpGenerator


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 package org.apache.cocoon.generation;
17
18 import java.io.IOException JavaDoc;
19 import java.io.OutputStream JavaDoc;
20 import java.io.PipedInputStream JavaDoc;
21 import java.io.PipedOutputStream JavaDoc;
22 import java.util.Enumeration JavaDoc;
23
24 import javax.servlet.ServletConfig JavaDoc;
25 import javax.servlet.ServletContext JavaDoc;
26 import javax.servlet.ServletException JavaDoc;
27 import javax.servlet.http.HttpServletRequest JavaDoc;
28 import javax.servlet.http.HttpServletResponse JavaDoc;
29
30 import org.apache.avalon.framework.component.Component;
31 import org.apache.avalon.framework.CascadingRuntimeException;
32 import org.apache.avalon.framework.logger.Logger;
33
34 import org.apache.cocoon.ProcessingException;
35 import org.apache.cocoon.environment.http.HttpEnvironment;
36
37 import org.apache.excalibur.source.Source;
38 import org.apache.excalibur.xml.sax.SAXParser;
39
40 import org.xml.sax.InputSource JavaDoc;
41 import org.xml.sax.SAXException JavaDoc;
42
43 /**
44  * Allows PHP to be used as a generator. Builds upon the PHP servlet
45  * functionallity - overrides the output method in order to pipe the
46  * results into SAX events.
47  *
48  * @author <a HREF="mailto:rubys@us.ibm.com">Sam Ruby</a>
49  * @version CVS $Id: PhpGenerator.java 30932 2004-07-29 17:35:38Z vgritsenko $
50  */

51 public class PhpGenerator extends ServletGenerator {
52
53     /**
54      * Stub implementation of Servlet Config
55      */

56     class config implements ServletConfig JavaDoc {
57         ServletContext JavaDoc c;
58         public config(ServletContext JavaDoc c) {this.c = c; }
59
60         public String JavaDoc getServletName() { return "PhpGenerator"; }
61         public Enumeration JavaDoc getInitParameterNames()
62                { return c.getInitParameterNames(); }
63         public ServletContext JavaDoc getServletContext() { return c; }
64         public String JavaDoc getInitParameter(String JavaDoc name) { return null; }
65     }
66
67     /**
68      * Subclass the PHP servlet implementation, overriding the method
69      * that produces the output.
70      */

71     public class PhpServlet extends net.php.servlet implements Runnable JavaDoc {
72
73         String JavaDoc input;
74         OutputStream JavaDoc output;
75         HttpServletRequest JavaDoc request;
76         HttpServletResponse JavaDoc response;
77         ServletException JavaDoc exception = null;
78         Logger logger;
79
80         protected PhpServlet( Logger logger ) {
81             this.logger = logger;
82         }
83         public void setInput(String JavaDoc input) {
84             this.input = input;
85         }
86
87         public void setOutput(OutputStream JavaDoc output) {
88             this.output = output;
89         }
90
91         public void setRequest(HttpServletRequest JavaDoc request) {
92             this.request = request;
93         }
94
95         public void setResponse(HttpServletResponse JavaDoc response) {
96             this.response = response;
97         }
98
99         public void write(String JavaDoc data) {
100             try {
101                 output.write(data.getBytes());
102             } catch (IOException JavaDoc e) {
103                 logger.debug("PhpGenerator.write()", e);
104                 throw new CascadingRuntimeException("PhpGenerator.write()", e);
105             }
106         }
107
108         /******************************************************************/
109         /* runnable interface */
110         /******************************************************************/
111
112         public void run() {
113             try {
114                 service(request, response, input);
115             } catch (ServletException JavaDoc e) {
116                 logger.error("PhpGenerator.run()", e);
117                 this.exception = e;
118             }
119
120             try {
121                 output.close();
122             } catch (IOException JavaDoc e) {
123                 logger.error("PhpGenerator.run():SHOULD NEVER HAPPEN", e);
124                 // should never happen
125
}
126         }
127
128     }
129
130     /**
131      * Generate XML data from PHP.
132      */

133     public void generate() throws IOException JavaDoc, SAXException JavaDoc, ProcessingException {
134
135         // ensure that we are running in a servlet environment
136
HttpServletResponse JavaDoc httpResponse =
137             (HttpServletResponse JavaDoc)this.objectModel.get(HttpEnvironment.HTTP_RESPONSE_OBJECT);
138         HttpServletRequest JavaDoc httpRequest =
139             (HttpServletRequest JavaDoc)this.objectModel.get(HttpEnvironment.HTTP_REQUEST_OBJECT);
140         if (httpResponse == null || httpRequest == null) {
141             throw new ProcessingException("HttpServletRequest or HttpServletResponse object not available");
142         }
143
144         // ensure that we are serving a file...
145
Source inputSource = null;
146         SAXParser parser = null;
147         try {
148             inputSource = this.resolver.resolveURI(this.source);
149             String JavaDoc systemId = inputSource.getURI();
150             if (!systemId.startsWith("file:/"))
151                 throw new IOException JavaDoc("protocol not supported: " + systemId);
152
153             // construct both ends of the pipe
154
PipedInputStream JavaDoc input = new PipedInputStream JavaDoc();
155
156             // start PHP producing results into the pipe
157
PhpServlet php = new PhpServlet( getLogger() );
158             php.init(new config((ServletContext JavaDoc)this.objectModel.get(HttpEnvironment.HTTP_SERVLET_CONTEXT)));
159             php.setInput(systemId.substring(6));
160             php.setOutput(new PipedOutputStream JavaDoc(input));
161             php.setRequest(httpRequest);
162             php.setResponse(httpResponse);
163             new Thread JavaDoc(php).start();
164
165             // pipe the results into the parser
166
parser = (SAXParser)this.manager.lookup(SAXParser.ROLE);
167             parser.parse(new InputSource JavaDoc(input), this.xmlConsumer);
168
169             // clean up
170
php.destroy();
171         } catch (IOException JavaDoc e) {
172             throw e;
173         } catch (Exception JavaDoc e) {
174             throw new ProcessingException(e.toString(), e);
175         } finally {
176             this.resolver.release( inputSource );
177             this.manager.release( (Component)parser );
178         }
179     }
180 }
181
Popular Tags