1 16 package org.apache.cocoon.generation; 17 18 import java.io.IOException ; 19 import java.io.StringReader ; 20 import java.io.UnsupportedEncodingException ; 21 import java.util.Enumeration ; 22 import java.util.Map ; 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 ; 36 import org.xml.sax.SAXException ; 37 import org.xml.sax.helpers.AttributesImpl ; 38 39 68 public class RequestGenerator extends ServiceableGenerator implements Parameterizable { 69 70 71 private final static String PREFIX = "h"; 72 73 private final static String URI = "http://apache.org/cocoon/request/2.0"; 74 75 76 private String global_container_encoding; 77 78 private String container_encoding; 79 80 81 private String global_form_encoding; 82 83 private String form_encoding; 84 85 86 private boolean global_generate_attributes; 87 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 objectModel, String src, Parameters parameters) 98 throws ProcessingException, SAXException , IOException { 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 108 public void generate() 109 throws SAXException { 110 final Request request = ObjectModelHelper.getRequest(objectModel); 111 final AttributesImpl attr = new AttributesImpl (); 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 headers = request.getHeaderNames(); 123 if ( headers != null ) { 124 while (headers.hasMoreElements()) { 125 String header = (String )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 parameters = request.getParameterNames(); 136 while (parameters.hasMoreElements()) { 137 String parameter = (String )parameters.nextElement(); 138 attribute(attr, "name", parameter); 139 start("parameter", attr); 140 String 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 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 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 attributes = request.getAttributeNames(); 169 while (attributes.hasMoreElements()) { 170 String attribute = (String )attributes.nextElement(); 171 attribute(attr, "name", attribute); 172 start("attribute", attr); 173 Object 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 [] 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 attr, String name, String value) { 201 attr.addAttribute("", name, name, "CDATA", value); 202 } 203 204 private void start(String name, AttributesImpl attr) 205 throws SAXException { 206 super.contentHandler.startElement(URI, name, PREFIX + ":" + name, attr); 207 attr.clear(); 208 } 209 210 private void end(String name) 211 throws SAXException { 212 super.contentHandler.endElement(URI, name, PREFIX + ":" + name); 213 } 214 215 private void data(String data) 216 throws SAXException { 217 super.contentHandler.characters(data.toCharArray(), 0, data.length()); 218 } 219 220 private void data(String data, String container_encoding, String form_encoding) 221 throws SAXException , UnsupportedEncodingException { 222 this.data(new String (data.getBytes(container_encoding), form_encoding)); 223 } 224 225 private void parse(String data) 226 throws Exception { 227 SAXParser parser = null; 228 try { 229 parser = (SAXParser) manager.lookup(SAXParser.ROLE); 230 InputSource is = new InputSource (new StringReader (data)); 231 parser.parse(is, new IncludeXMLConsumer(super.xmlConsumer)); 232 } finally { 233 manager.release(parser); 234 } 235 } 236 } 237 | Popular Tags |