KickJava   Java API By Example, From Geeks To Geeks.

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


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.StringReader JavaDoc;
20 import java.io.UnsupportedEncodingException JavaDoc;
21 import java.util.Enumeration JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.apache.avalon.framework.CascadingRuntimeException;
25 import org.apache.avalon.framework.parameters.ParameterException;
26 import org.apache.avalon.framework.parameters.Parameterizable;
27 import org.apache.avalon.framework.parameters.Parameters;
28 import org.apache.cocoon.ProcessingException;
29 import org.apache.cocoon.environment.ObjectModelHelper;
30 import org.apache.cocoon.environment.Request;
31 import org.apache.cocoon.environment.SourceResolver;
32 import org.apache.cocoon.xml.XMLUtils;
33 import org.apache.cocoon.xml.IncludeXMLConsumer;
34 import org.apache.excalibur.xml.sax.SAXParser;
35 import org.xml.sax.InputSource JavaDoc;
36 import org.xml.sax.SAXException JavaDoc;
37 import org.xml.sax.helpers.AttributesImpl JavaDoc;
38
39 /**
40  * @cocoon.sitemap.component.documentation
41  * Generates an XML representation of the incoming request.
42  *
43  * @cocoon.sitemap.component.name request
44  * @cocoon.sitemap.component.label content
45  * @cocoon.sitemap.component.logger sitemap.generator.request
46  *
47  * @cocoon.sitemap.component.pooling.max 16
48  *
49  * <p>
50  * <b>Configuration options:</b>
51  * <dl>
52  * <dt> <i>container-encoding</i> (optional)
53  * <dd> The encoding used by container. Default value is ISO-8859-1.
54  * <dt> <i>form-encoding</i> (optional)
55  * <dd> The supposed encoding of the request parameter. Default is null.
56  * <dt> <i>generate-attributes</i> (optional)
57  * <dd> If true, request attributes were also included. Default is false.
58  * </dl>
59  * These configuration options are supported at both declaration and use time.
60  * The configuration at use time takes priority over declaration time.
61  *
62  * @author <a HREF="mailto:pier@apache.org">Pierpaolo Fumagalli</a>
63  * @author <a HREF="mailto:Giacomo.Pati@pwr.ch">Giacomo Pati</a>
64  * @author <a HREF="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
65  * @author <a HREF="mailto:stefano@apache.org">Stefano Mazzocchi</a>
66  * @version CVS $Id: RequestGenerator.java 225585 2005-07-27 19:30:05Z cziegeler $
67  */

68 public class RequestGenerator extends ServiceableGenerator implements Parameterizable {
69
70     /** The namespace prefix of this generator. */
71     private final static String JavaDoc PREFIX = "h";
72     /** The namespace URI of this generator. */
73     private final static String JavaDoc URI = "http://apache.org/cocoon/request/2.0";
74
75     /** The configured container encoding at declaration time. */
76     private String JavaDoc global_container_encoding;
77     /** The configured container encoding at use time. */
78     private String JavaDoc container_encoding;
79
80     /** The configured form encoding at declaration time. */
81     private String JavaDoc global_form_encoding;
82     /** The configured form encoding at use time. */
83     private String JavaDoc form_encoding;
84
85     /** The configuration for including request attributes at declaration time. */
86     private boolean global_generate_attributes;
87     /** The configuration for including request attributes at use time. */
88     private boolean generate_attributes;
89
90     public void parameterize(Parameters parameters)
91     throws ParameterException {
92         global_container_encoding = parameters.getParameter("container-encoding", "ISO-8859-1");
93         global_form_encoding = parameters.getParameter("form-encoding", null);
94         global_generate_attributes = parameters.getParameterAsBoolean("generate-attributes", false);
95     }
96
97     public void setup(SourceResolver resolver, Map JavaDoc objectModel, String JavaDoc src, Parameters parameters)
98     throws ProcessingException, SAXException JavaDoc, IOException JavaDoc {
99         super.setup(resolver, objectModel, src, parameters);
100         container_encoding = parameters.getParameter("container-encoding", global_container_encoding);
101         form_encoding = parameters.getParameter("form-encoding", global_form_encoding);
102         generate_attributes = parameters.getParameterAsBoolean("generate-attributes", global_generate_attributes);
103     }
104
105     /**
106      * Generate XML data.
107      */

108     public void generate()
109     throws SAXException JavaDoc {
110         final Request request = ObjectModelHelper.getRequest(objectModel);
111         final AttributesImpl JavaDoc attr = new AttributesImpl JavaDoc();
112
113         this.contentHandler.startDocument();
114         this.contentHandler.startPrefixMapping(PREFIX, URI);
115
116         attribute(attr, "target", request.getRequestURI());
117         attribute(attr, "sitemap", request.getSitemapURI());
118         attribute(attr, "source", (this.source != null ? this.source : ""));
119         start("request", attr);
120
121         start("requestHeaders", attr);
122         Enumeration JavaDoc headers = request.getHeaderNames();
123         if ( headers != null ) {
124             while (headers.hasMoreElements()) {
125                 String JavaDoc header = (String JavaDoc)headers.nextElement();
126                 attribute(attr, "name", header);
127                 start("header", attr);
128                 data(request.getHeader(header));
129                 end("header");
130             }
131         }
132         end("requestHeaders");
133
134         start("requestParameters", attr);
135         Enumeration JavaDoc parameters = request.getParameterNames();
136         while (parameters.hasMoreElements()) {
137             String JavaDoc parameter = (String JavaDoc)parameters.nextElement();
138             attribute(attr, "name", parameter);
139             start("parameter", attr);
140             String JavaDoc values[] = request.getParameterValues(parameter);
141             if (values != null) {
142                 for (int x = 0; x < values.length; x++) {
143                     start("value", attr);
144                     if (form_encoding != null) {
145                         try {
146                             data(values[x], container_encoding, form_encoding);
147                         } catch (UnsupportedEncodingException JavaDoc uee) {
148                             throw new CascadingRuntimeException("The suggested encoding is not supported.", uee);
149                         }
150                     } else if (parameter.startsWith("xml:")) {
151                         try {
152                             parse(values[x]);
153                         } catch (Exception JavaDoc e) {
154                             throw new CascadingRuntimeException("Could not parse the xml parameter", e);
155                         }
156                     } else {
157                         data(values[x]);
158                     }
159                     end("value");
160                 }
161             }
162             end("parameter");
163         }
164         end("requestParameters");
165
166         if (generate_attributes) {
167             start("requestAttributes", attr);
168             Enumeration JavaDoc attributes = request.getAttributeNames();
169             while (attributes.hasMoreElements()) {
170                 String JavaDoc attribute = (String JavaDoc)attributes.nextElement();
171                 attribute(attr, "name", attribute);
172                 start("attribute", attr);
173                 Object JavaDoc value = request.getAttribute(attribute);
174                 if (value != null) {
175                     start("value", attr);
176                     XMLUtils.valueOf(this.contentHandler, value);
177                     end("value");
178                 }
179                 end("attribute");
180             }
181             end("requestAttributes");
182         }
183
184         this.start("configurationParameters", attr);
185         String JavaDoc[] confparams = super.parameters.getNames();
186         for (int i = 0; i < confparams.length; i++) {
187             attribute(attr, "name", confparams[i]);
188             start("parameter", attr);
189             data(super.parameters.getParameter(confparams[i], ""));
190             end("parameter");
191         }
192         end("configurationParameters");
193
194         end("request");
195
196         this.contentHandler.endPrefixMapping(PREFIX);
197         this.contentHandler.endDocument();
198     }
199
200     private void attribute(AttributesImpl JavaDoc attr, String JavaDoc name, String JavaDoc value) {
201         attr.addAttribute("", name, name, "CDATA", value);
202     }
203
204     private void start(String JavaDoc name, AttributesImpl JavaDoc attr)
205     throws SAXException JavaDoc {
206         super.contentHandler.startElement(URI, name, PREFIX + ":" + name, attr);
207         attr.clear();
208     }
209
210     private void end(String JavaDoc name)
211     throws SAXException JavaDoc {
212         super.contentHandler.endElement(URI, name, PREFIX + ":" + name);
213     }
214
215     private void data(String JavaDoc data)
216     throws SAXException JavaDoc {
217         super.contentHandler.characters(data.toCharArray(), 0, data.length());
218     }
219     
220     private void data(String JavaDoc data, String JavaDoc container_encoding, String JavaDoc form_encoding)
221     throws SAXException JavaDoc, UnsupportedEncodingException JavaDoc {
222         this.data(new String JavaDoc(data.getBytes(container_encoding), form_encoding));
223     }
224     
225     private void parse(String JavaDoc data)
226     throws Exception JavaDoc {
227         SAXParser parser = null;
228         try {
229             parser = (SAXParser) manager.lookup(SAXParser.ROLE);
230             InputSource JavaDoc is = new InputSource JavaDoc(new StringReader JavaDoc(data));
231             parser.parse(is, new IncludeXMLConsumer(super.xmlConsumer));
232         } finally {
233             manager.release(parser);
234         }
235     }
236 }
237
Popular Tags