1 16 package org.apache.cocoon.woody.transformation; 17 18 import java.io.IOException ; 19 import java.io.StringReader ; 20 import java.util.Locale ; 21 import java.util.Map ; 22 23 import org.apache.avalon.framework.parameters.Parameters; 24 import org.apache.cocoon.components.flow.FlowHelper; 25 import org.apache.cocoon.components.flow.WebContinuation; 26 import org.apache.cocoon.environment.ObjectModelHelper; 27 import org.apache.cocoon.environment.Request; 28 import org.apache.cocoon.environment.Session; 29 import org.apache.cocoon.i18n.I18nUtils; 30 import org.apache.cocoon.woody.formmodel.Form; 31 import org.apache.cocoon.xml.AttributesImpl; 32 import org.apache.commons.jxpath.JXPathContext; 33 import org.apache.commons.jxpath.JXPathException; 34 import org.apache.commons.jxpath.Variables; 35 import org.xml.sax.Attributes ; 36 import org.xml.sax.SAXException ; 37 38 43 public class WoodyPipelineConfig { 44 45 48 public static final String WOODY_FORM = "woody-form"; 49 50 52 private final String attributeName; 53 54 56 private final Request request; 57 58 60 private final JXPathContext jxpathContext; 61 62 64 private final Locale localeParameter; 65 66 68 private Locale locale; 69 70 73 private String formAction; 74 75 78 private String formMethod; 79 80 private WoodyPipelineConfig(JXPathContext jxpc, Request req, Locale localeParam, 81 String attName, String actionExpression, String method) { 82 this.attributeName = attName; 83 this.request = req; 84 this.jxpathContext =jxpc; 85 this.localeParameter = localeParam; 86 this.formAction = translateText(actionExpression); 87 this.formMethod = method; 88 } 89 90 99 public static WoodyPipelineConfig createConfig(Map objectModel, Parameters parameters) { 100 Object flowContext = FlowHelper.getContextObject(objectModel); 102 WebContinuation wk = FlowHelper.getWebContinuation(objectModel); 103 JXPathContext jxpc = JXPathContext.newContext(flowContext); 104 Variables vars = jxpc.getVariables(); 105 vars.declareVariable("continuation", wk); 106 Request request = ObjectModelHelper.getRequest(objectModel); 107 vars.declareVariable("request", request); 108 Session session = request.getSession(false); 109 vars.declareVariable("session", session); 110 vars.declareVariable("parameters", parameters); 111 112 Locale localeParameter = null; 113 String localeStr = parameters.getParameter("locale", null); 114 if (localeStr != null) { 115 localeParameter = I18nUtils.parseLocale(localeStr); 116 } 117 118 String attributeName = parameters.getParameter("attribute-name", null); 119 String actionExpression = parameters.getParameter("form-action", null); 120 String formMethod = parameters.getParameter("form-method", "POST"); 121 125 return new WoodyPipelineConfig(jxpc, request, localeParameter, 126 attributeName, actionExpression, formMethod); 127 } 128 129 132 public Form findForm() throws SAXException { 133 return this.findForm(null); 134 } 135 136 155 public Form findForm(String jxpathExpression) throws SAXException { 156 Object form = null; 157 if (jxpathExpression != null) { 158 form = this.jxpathContext.getValue(jxpathExpression); 159 if (form == null) { 160 throw new SAXException ("No form found at location \"" + jxpathExpression + "\"."); 161 } else if (!(form instanceof Form)) { 162 throw new SAXException ("Object returned by expression \"" + jxpathExpression + "\" is not a Woody Form."); 163 } 164 } else if (this.attributeName != null) { form = this.request.getAttribute(this.attributeName); 166 if (form == null) { 167 throw new SAXException ("No form found in request attribute with name \"" + this.attributeName + "\""); 168 } else if (!(form instanceof Form)) { 169 throw new SAXException ("Object found in request (attribute = '" + this.attributeName + "') is not a Woody Form."); 170 } 171 } else { jxpathExpression = "/" + WoodyPipelineConfig.WOODY_FORM; 173 try { 174 form = this.jxpathContext.getValue(jxpathExpression); 175 } catch (JXPathException e) { } 176 if (form == null) { 177 throw new SAXException ("No Woody form found."); 178 } 179 } 180 return (Form)form; 181 } 182 183 190 public String translateText(String original) { 191 if (original==null) { 192 return null; 193 } 194 195 StringBuffer expression; 196 StringBuffer translated = new StringBuffer (); 197 StringReader in = new StringReader (original); 198 int chr; 199 try { 200 while ((chr = in.read()) != -1) { 201 char c = (char) chr; 202 if (c == '#') { 203 chr = in.read(); 204 if (chr != -1) { 205 c = (char) chr; 206 if (c == '{') { 207 expression = new StringBuffer (); 208 boolean more = true; 209 while ( more ) { 210 more = false; 211 if ((chr = in.read()) != -1) { 212 c = (char)chr; 213 if (c != '}') { 214 expression.append(c); 215 more = true; 216 } else { 217 translated.append(evaluateExpression(expression.toString()).toString()); 218 } 219 } else { 220 translated.append('#').append('{').append(expression); 221 } 222 } 223 } 224 } else { 225 translated.append((char) chr); 226 } 227 } else { 228 translated.append(c); 229 } 230 } 231 } catch (IOException ignored) { 232 ignored.printStackTrace(); 233 } 234 return translated.toString(); 235 } 236 237 248 public Object evaluateExpression(String expression) { 249 return this.jxpathContext.getValue(expression); 250 } 251 252 public Locale getLocale() { 253 return locale; 254 } 255 256 public void setLocale(Locale locale) { 257 this.locale = locale; 258 } 259 260 public Locale getLocaleParameter() { 261 return localeParameter; 262 } 263 264 271 public String getFormAction() { 272 return formAction; 273 } 274 275 282 public String getFormMethod() { 283 return formMethod; 284 } 285 286 293 public Attributes getFormAttributes() { 294 AttributesImpl formAtts = new AttributesImpl(); 295 if (getFormAction() != null) { 296 formAtts.addCDATAAttribute("action", getFormAction()); 297 } 298 formAtts.addCDATAAttribute("method", getFormMethod()); 299 return formAtts; 300 } 301 } 302 | Popular Tags |