KickJava   Java API By Example, From Geeks To Geeks.

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


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.ByteArrayInputStream JavaDoc;
19 import java.io.IOException JavaDoc;
20
21 import javax.servlet.ServletContext JavaDoc;
22 import javax.servlet.ServletException JavaDoc;
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24 import javax.servlet.http.HttpServletResponse JavaDoc;
25
26 import org.apache.cocoon.ProcessingException;
27 import org.apache.cocoon.components.jsp.JSPEngine;
28 import org.apache.cocoon.environment.http.HttpEnvironment;
29 import org.apache.excalibur.source.Source;
30 import org.apache.excalibur.xml.sax.SAXParser;
31 import org.xml.sax.InputSource JavaDoc;
32 import org.xml.sax.SAXException JavaDoc;
33
34 /**
35  * Allows Servlets and JSPs to be used as a generator.
36  *
37  * @author <a HREF="mailto:dims@yahoo.com">Davanum Srinivas</a>
38  * @version CVS $Id: JSPGenerator.java 30932 2004-07-29 17:35:38Z vgritsenko $
39  */

40 public class JSPGenerator extends ServiceableGenerator {
41
42     /**
43      * Generate XML data from JSPEngine output.
44      */

45     public void generate() throws ProcessingException {
46
47         final HttpServletResponse JavaDoc servletResponse =
48             (HttpServletResponse JavaDoc) this.objectModel.get(HttpEnvironment.HTTP_RESPONSE_OBJECT);
49         final HttpServletRequest JavaDoc servletRequest =
50             (HttpServletRequest JavaDoc) this.objectModel.get(HttpEnvironment.HTTP_REQUEST_OBJECT);
51         final ServletContext JavaDoc servletContext =
52             (ServletContext JavaDoc) this.objectModel.get(HttpEnvironment.HTTP_SERVLET_CONTEXT);
53
54         // ensure that we are running in a servlet environment
55
if (servletResponse == null || servletRequest == null || servletContext == null) {
56             throw new ProcessingException("JSPGenerator can only be used from within a Servlet environment.");
57         }
58
59         JSPEngine engine = null;
60         SAXParser parser = null;
61         Source inputSource = null;
62         Source contextSource = null;
63         try {
64             inputSource = this.resolver.resolveURI(this.source);
65             contextSource = this.resolver.resolveURI("context:/");
66
67             String JavaDoc inputSourceURI = inputSource.getURI();
68             String JavaDoc contextSourceURI = contextSource.getURI();
69
70             if (!inputSourceURI.startsWith(contextSourceURI)) {
71                 throw new ProcessingException("You must not reference a file "
72                         + "outside of the servlet context at " + contextSourceURI + ".");
73             }
74
75             String JavaDoc url = inputSourceURI.substring(contextSourceURI.length());
76             if (url.charAt(0) != '/') {
77                 url = "/" + url;
78             }
79
80             if (getLogger().isDebugEnabled()) {
81                 getLogger().debug("JSPGenerator executing:" + url);
82             }
83
84             engine = (JSPEngine) super.manager.lookup(JSPEngine.ROLE);
85             byte[] bytes = engine.executeJSP(url, servletRequest, servletResponse, servletContext);
86
87             InputSource JavaDoc input = new InputSource JavaDoc(new ByteArrayInputStream JavaDoc(bytes));
88             // utf-8 is default encoding; specified explicitely here as a reminder.
89
input.setEncoding("utf-8");
90
91             // pipe the results into the parser
92
parser = (SAXParser) super.manager.lookup(SAXParser.ROLE);
93             parser.parse(input, super.xmlConsumer);
94         } catch (ServletException JavaDoc e) {
95             throw new ProcessingException("ServletException while executing JSPEngine", e);
96         } catch (SAXException JavaDoc e) {
97             throw new ProcessingException("SAXException while parsing JSPEngine output", e);
98         } catch (IOException JavaDoc e) {
99             throw new ProcessingException("IOException JSPGenerator.generate()", e);
100         } catch (ProcessingException e) {
101             throw e;
102         } catch (Exception JavaDoc e) {
103             throw new ProcessingException("Exception JSPGenerator.generate()", e);
104         } finally {
105             super.manager.release(parser);
106             super.manager.release(engine);
107             this.resolver.release(inputSource);
108             this.resolver.release(contextSource);
109         }
110     }
111 }
112
Popular Tags