KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > woody > transformation > WoodyPipelineConfig


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.woody.transformation;
17
18 import java.io.IOException JavaDoc;
19 import java.io.StringReader JavaDoc;
20 import java.util.Locale JavaDoc;
21 import java.util.Map JavaDoc;
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 JavaDoc;
36 import org.xml.sax.SAXException JavaDoc;
37
38 /**
39  * WoodyPipeLineConfig
40  *
41  * @version CVS $Id: WoodyPipelineConfig.java 30932 2004-07-29 17:35:38Z vgritsenko $
42  */

43 public class WoodyPipelineConfig {
44
45     /**
46      * Default key under which the woody form is stored in the JXPath context.
47      */

48     public static final String JavaDoc WOODY_FORM = "woody-form";
49
50     /**
51      * Name of the request attribute under which the Woody form is stored (optional). */

52     private final String JavaDoc attributeName;
53
54     /**
55      * Pointer to the current request object. */

56     private final Request request;
57
58     /**
59      * Initialized jxpathcontext to evaluate passed expressions with. */

60     private final JXPathContext jxpathContext;
61
62     /**
63      * Containts locale specified as a parameter to the transformer, if any. */

64     private final Locale JavaDoc localeParameter;
65
66     /**
67      * The locale currently used by the transformer. */

68     private Locale JavaDoc locale;
69
70     /**
71      * Value for the action attribute of the form.
72      */

73     private String JavaDoc formAction;
74
75     /**
76      * Value for the method attribute of the form.
77      */

78     private String JavaDoc formMethod;
79
80     private WoodyPipelineConfig(JXPathContext jxpc, Request req, Locale JavaDoc localeParam,
81             String JavaDoc attName, String JavaDoc actionExpression, String JavaDoc 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     /**
91      * Creates and initializes a WoodyPipelineConfig object based on the passed
92      * arguments of the setup() of the specific Pipeline-component.
93      *
94      * @param objectModel the objectmodel as passed in the setup()
95      * @param parameters the parameters as passed in the setup()
96      * @return an instance of WoodyPipelineConfig initialized according to the
97      * settings in the sitemap.
98      */

99     public static WoodyPipelineConfig createConfig(Map JavaDoc objectModel, Parameters parameters) {
100         // create and set the jxpathContext...
101
Object JavaDoc 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 JavaDoc localeParameter = null;
113         String JavaDoc localeStr = parameters.getParameter("locale", null);
114         if (localeStr != null) {
115             localeParameter = I18nUtils.parseLocale(localeStr);
116         }
117
118         String JavaDoc attributeName = parameters.getParameter("attribute-name", null);
119         String JavaDoc actionExpression = parameters.getParameter("form-action", null);
120         String JavaDoc formMethod = parameters.getParameter("form-method", "POST");
121         //TODO (20031223 mpo)think about adding form-encoding for the Generator.
122
// Note generator will also need some text to go on the submit-button?
123
// Alternative to adding more here is to apply xinclude ?
124

125         return new WoodyPipelineConfig(jxpc, request, localeParameter,
126                 attributeName, actionExpression, formMethod);
127     }
128
129     /**
130      * Overloads {@link #findForm(String)} by setting the jxpath-expression to null
131      */

132     public Form findForm() throws SAXException JavaDoc {
133         return this.findForm(null);
134     }
135
136     /**
137      * Finds the form from the current request-context based on the settings of
138      * this configuration object. The fall-back search-procedure is as follows:
139      * <ol><li>Use the provided jxpathExpression (if not null)</li>
140      * <li>Use the setting of the 'attribute-name' parameter on the request</li>
141      * <li>Obtain the form from it's default location in the flow context</li>
142      * </ol>
143      *
144      * @param jxpathExpression that should be pointing to the form
145      * @return the found form if found
146      * @throws SAXException in any of the following cases:
147      * <ul><li>The provided jxpathExpression (if not null) not point to a
148      * {@link Form} instance.</li>
149      * <li>The request is not holding a {@link Form} instance under the key
150      * specified by 'attribute-name' (if specified)</li>
151      * <li>Both jxpathExpression and 'attribute-name' were not specified AND
152      * also the default location was not holding a valid {@link Form} instance.</li>
153      * </ol>
154      */

155     public Form findForm(String JavaDoc jxpathExpression) throws SAXException JavaDoc {
156         Object JavaDoc form = null;
157         if (jxpathExpression != null) {
158             form = this.jxpathContext.getValue(jxpathExpression);
159             if (form == null) {
160                 throw new SAXException JavaDoc("No form found at location \"" + jxpathExpression + "\".");
161             } else if (!(form instanceof Form)) {
162                 throw new SAXException JavaDoc("Object returned by expression \"" + jxpathExpression + "\" is not a Woody Form.");
163             }
164         } else if (this.attributeName != null) { // then see if an attribute-name was specified
165
form = this.request.getAttribute(this.attributeName);
166             if (form == null) {
167                 throw new SAXException JavaDoc("No form found in request attribute with name \"" + this.attributeName + "\"");
168             } else if (!(form instanceof Form)) {
169                 throw new SAXException JavaDoc("Object found in request (attribute = '" + this.attributeName + "') is not a Woody Form.");
170             }
171         } else { // and then see if we got a form from the flow
172
jxpathExpression = "/" + WoodyPipelineConfig.WOODY_FORM;
173             try {
174                 form = this.jxpathContext.getValue(jxpathExpression);
175             } catch (JXPathException e) { /* do nothing */ }
176             if (form == null) {
177                 throw new SAXException JavaDoc("No Woody form found.");
178             }
179         }
180         return (Form)form;
181     }
182
183     /**
184      * Replaces JXPath expressions embedded inside #{ and } by their value.
185      * This will parse the passed String looking for #{} occurences and then
186      * uses the {@link #evaluateExpression(String)} to evaluate the found expression.
187      *
188      * @return the original String with it's #{}-parts replaced by the evaulated results.
189      */

190     public String JavaDoc translateText(String JavaDoc original) {
191         if (original==null) {
192             return null;
193         }
194
195         StringBuffer JavaDoc expression;
196         StringBuffer JavaDoc translated = new StringBuffer JavaDoc();
197         StringReader JavaDoc in = new StringReader JavaDoc(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 JavaDoc();
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 JavaDoc ignored) {
232             ignored.printStackTrace();
233         }
234         return translated.toString();
235     }
236
237     /**
238      * Evaluates the passed xpath expression using the internal jxpath context
239      * holding the declared variables:
240      * <ol><li>continuation: as made available by flowscript</li>
241      * <li>request: as present in the cocoon processing environment</li>
242      * <li>session: as present in the cocoon processing environment</li>
243      * <li>parameters: as present in the cocoon sitemap node of the pipeline component</li></ol>
244      *
245      * @param expression
246      * @return the object-value resulting the expression evaluation.
247      */

248     public Object JavaDoc evaluateExpression(String JavaDoc expression) {
249         return this.jxpathContext.getValue(expression);
250     }
251
252     public Locale JavaDoc getLocale() {
253         return locale;
254     }
255
256     public void setLocale(Locale JavaDoc locale) {
257         this.locale = locale;
258     }
259
260     public Locale JavaDoc getLocaleParameter() {
261         return localeParameter;
262     }
263
264     /**
265      * The value for the wi:form-generated/@action.
266      * Note: wi:form-template copies this from its wt:form-template counterpart.
267      *
268      * @return the {@link #translateText(String)} result of the 'form-action' sitemap
269      * parameter to the pipeline component, or null if that parameter was not set.
270      */

271     public String JavaDoc getFormAction() {
272         return formAction;
273     }
274
275     /**
276      * The value for the wi:form-generated/@method.
277      * Note: wi:form-template copies this from its wt:form-template counterpart.
278      *
279      * @return the value of the 'form-method' sitemap parameter to the pipeline
280      * component. Defaults to 'POST' if it was not set.
281      */

282     public String JavaDoc getFormMethod() {
283         return formMethod;
284     }
285
286     /**
287      * The grouped attributes to set on the wi:form-generated element.
288      * Note: wi:form-template copies this from its wt:form-template counterpart.
289      *
290      * @see #getFormAction()
291      * @see #getFormMethod()
292      */

293     public Attributes JavaDoc 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