1 16 package org.apache.cocoon.forms.transformation; 17 18 import org.apache.avalon.framework.parameters.Parameters; 19 20 import org.apache.cocoon.components.flow.FlowHelper; 21 import org.apache.cocoon.components.flow.WebContinuation; 22 import org.apache.cocoon.environment.ObjectModelHelper; 23 import org.apache.cocoon.environment.Request; 24 import org.apache.cocoon.environment.Session; 25 import org.apache.cocoon.forms.formmodel.Form; 26 import org.apache.cocoon.i18n.I18nUtils; 27 import org.apache.cocoon.util.Deprecation; 28 29 import org.apache.commons.jxpath.JXPathContext; 30 import org.apache.commons.jxpath.JXPathException; 31 import org.apache.commons.jxpath.Variables; 32 import org.xml.sax.Attributes ; 33 import org.xml.sax.SAXException ; 34 import org.xml.sax.helpers.AttributesImpl ; 35 36 import java.io.IOException ; 37 import java.io.StringReader ; 38 import java.util.ArrayList ; 39 import java.util.HashMap ; 40 import java.util.List ; 41 import java.util.Locale ; 42 import java.util.Map ; 43 44 47 public class FormsPipelineConfig { 48 49 52 public static final String CFORMSKEY = "CocoonFormsInstance"; 53 54 56 private final String attributeName; 57 58 61 private final Request request; 62 63 66 private final JXPathContext jxpathContext; 67 68 71 private final Locale localeParameter; 72 73 76 private Locale locale; 77 78 81 private String formAction; 82 83 86 private String formMethod; 87 88 89 private FormsPipelineConfig(JXPathContext jxpc, Request req, Locale localeParam, 90 String attName, String actionExpression, String method) { 91 this.attributeName = attName; 92 this.request = req; 93 this.jxpathContext =jxpc; 94 this.localeParameter = localeParam; 95 this.formAction = translateText(actionExpression); 96 this.formMethod = method; 97 } 98 99 108 public static FormsPipelineConfig createConfig(Map objectModel, Parameters parameters) { 109 Object flowContext = FlowHelper.getContextObject(objectModel); 111 WebContinuation wk = FlowHelper.getWebContinuation(objectModel); 112 JXPathContext jxpc = JXPathContext.newContext(flowContext); 113 Request request = ObjectModelHelper.getRequest(objectModel); 117 Session session = request.getSession(false); 118 final Map cocoonOM = new HashMap (); 119 cocoonOM.put("continuation", wk); 120 cocoonOM.put("request", request); 121 if ( session != null ) { 122 cocoonOM.put("session", session); 123 } 124 cocoonOM.put("parameters", parameters); 125 126 FormsVariables vars = new FormsVariables(); 127 vars.declareVariable("cocoon", cocoonOM); 128 vars.declareVariable("continuation", wk); 130 vars.declareVariable("request", request); 131 vars.declareVariable("session", session); 132 vars.declareVariable("parameters", parameters); 133 vars.addDeprecatedVariable("continuation"); 134 vars.addDeprecatedVariable("request"); 135 vars.addDeprecatedVariable("session"); 136 vars.addDeprecatedVariable("parameters"); 137 jxpc.setVariables(vars); 138 139 Locale localeParameter = null; 140 String localeStr = parameters.getParameter("locale", null); 141 if (localeStr != null) { 142 localeParameter = I18nUtils.parseLocale(localeStr); 143 } 144 145 String attributeName = parameters.getParameter("attribute-name", null); 146 String actionExpression = parameters.getParameter("form-action", null); 147 String formMethod = parameters.getParameter("form-method", null); 148 152 return new FormsPipelineConfig(jxpc, request, localeParameter, 153 attributeName, actionExpression, formMethod); 154 } 155 156 159 public Form findForm() throws SAXException { 160 return this.findForm(null); 161 } 162 163 182 public Form findForm(String jxpathExpression) throws SAXException { 183 Object form = null; 184 if (jxpathExpression != null) { 185 form = this.jxpathContext.getValue(jxpathExpression); 186 if (form == null) { 187 throw new SAXException ("No Cocoon Form found at location \"" + jxpathExpression + "\"."); 188 } else if (!(form instanceof Form)) { 189 throw new SAXException ("Object returned by expression \"" + jxpathExpression + "\" is not a Cocoon Form."); 190 } 191 } else if (this.attributeName != null) { form = this.request.getAttribute(this.attributeName); 193 if (form == null) { 194 throw new SAXException ("No Cocoon Form found in request attribute with name \"" + this.attributeName + "\""); 195 } else if (!(form instanceof Form)) { 196 throw new SAXException ("Object found in request (attribute = '" + this.attributeName + "') is not a Cocoon Form."); 197 } 198 } else { jxpathExpression = "/" + FormsPipelineConfig.CFORMSKEY; 200 try { 201 form = this.jxpathContext.getValue(jxpathExpression); 202 } catch (JXPathException e) { } 203 if (form == null) { 204 throw new SAXException ("No Cocoon Form found."); 205 } 206 } 207 return (Form)form; 208 } 209 210 217 public String translateText(String original) { 218 if (original==null) { 219 return null; 220 } 221 222 StringBuffer expression; 223 StringBuffer translated = new StringBuffer (); 224 StringReader in = new StringReader (original); 225 int chr; 226 try { 227 while ((chr = in.read()) != -1) { 228 char c = (char) chr; 229 if (c == '#') { 230 chr = in.read(); 231 if (chr != -1) { 232 c = (char) chr; 233 if (c == '{') { 234 expression = new StringBuffer (); 235 boolean more = true; 236 while ( more ) { 237 more = false; 238 if ((chr = in.read()) != -1) { 239 c = (char)chr; 240 if (c != '}') { 241 expression.append(c); 242 more = true; 243 } else { 244 translated.append(evaluateExpression(expression.toString()).toString()); 245 } 246 } else { 247 translated.append('#').append('{').append(expression); 248 } 249 } 250 } 251 } else { 252 translated.append((char) chr); 253 } 254 } else { 255 translated.append(c); 256 } 257 } 258 } catch (IOException ignored) { 259 ignored.printStackTrace(); 260 } 261 return translated.toString(); 262 } 263 264 275 public Object evaluateExpression(String expression) { 276 return this.jxpathContext.getValue(expression); 277 } 278 279 public Locale getLocale() { 280 return locale; 281 } 282 283 public void setLocale(Locale locale) { 284 this.locale = locale; 285 } 286 287 public Locale getLocaleParameter() { 288 return localeParameter; 289 } 290 291 298 public String getFormAction() { 299 return formAction; 300 } 301 302 309 public String getFormMethod() { 310 return formMethod; 311 } 312 313 314 319 public void setFormMethod(String method) { 320 this.formMethod = method; 321 } 322 323 330 public Attributes getFormAttributes() { 331 AttributesImpl attrs = new org.apache.cocoon.xml.AttributesImpl(); 332 addFormAttributes(attrs); 333 return attrs; 334 } 335 336 public void addFormAttributes(AttributesImpl attrs) { 337 if (getFormAction() != null) { 338 attrs.addAttribute("", "action", "action", "CDATA", getFormAction()); 339 } 340 if (getFormMethod() != null){ 341 attrs.addAttribute("", "method", "method", "CDATA", getFormMethod()); 342 } 343 } 344 345 public static final class FormsVariables implements Variables { 346 347 final Map vars = new HashMap (); 348 final List deprecatedNames = new ArrayList (); 349 350 public void addDeprecatedVariable(String name) { 351 this.deprecatedNames.add(name); 352 } 353 354 357 public void declareVariable(String name, Object value) { 358 this.vars.put(name, value); 359 } 360 361 364 public Object getVariable(String name) { 365 Object value = this.vars.get(name); 366 if ( deprecatedNames.contains(name) ) { 367 Deprecation.logger.warn("CForms: usage of the variable '" + name + "' is deprecated."+ 368 "Please use 'cocoon/" + name + "' instead. The usage of just '"+ 369 name+"' will be removed in Cocoon 2.2."); 370 } 371 return value; 372 } 373 374 377 public boolean isDeclaredVariable(String name) { 378 return this.vars.containsKey(name); 379 } 380 381 384 public void undeclareVariable(String name) { 385 this.vars.remove(name); 386 } 387 } 388 } 389 | Popular Tags |