KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > transformation > helpers > FormValidatorHelper


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.transformation.helpers;
17
18 import org.apache.avalon.framework.configuration.Configuration;
19 import org.apache.avalon.framework.configuration.ConfigurationException;
20 import org.apache.avalon.framework.configuration.SAXConfigurationHandler;
21 import org.apache.excalibur.source.Source;
22
23 import org.apache.cocoon.Constants;
24 import org.apache.cocoon.acting.ConfigurationHelper;
25 import org.apache.cocoon.acting.ValidatorActionResult;
26 import org.apache.cocoon.components.source.SourceUtil;
27 import org.apache.cocoon.environment.ObjectModelHelper;
28 import org.apache.cocoon.environment.Request;
29 import org.apache.cocoon.environment.SourceResolver;
30
31 import org.apache.avalon.framework.logger.Logger;
32
33 import java.util.HashMap JavaDoc;
34 import java.util.Map JavaDoc;
35
36 /**
37  * The <code>ValidatorActionResult</code> object helper
38  *
39  * @author <a HREF="mailto:haul@apache.org">Christian Haul</a>
40  * @version CVS $Id: FormValidatorHelper.java 30932 2004-07-29 17:35:38Z vgritsenko $
41  */

42 public class FormValidatorHelper {
43
44     private static Map JavaDoc configurations = new HashMap JavaDoc();
45
46     /**
47      * these make it easier for the xsl
48      */

49
50     String JavaDoc current_descriptor = null;
51     boolean current_reloadable = true;
52     Logger current_logger = null;
53     String JavaDoc current_constraint_set = null;
54     String JavaDoc current_parameter = null;
55     SourceResolver current_resolver = null;
56
57     public FormValidatorHelper(String JavaDoc descriptor, boolean reloadable,
58                                   Logger logger, SourceResolver resolver) {
59         current_descriptor = descriptor;
60         current_reloadable = reloadable;
61         current_logger = logger;
62         current_resolver = resolver;
63     }
64
65     public FormValidatorHelper(String JavaDoc descriptor, boolean reloadable,
66                                   Logger logger, SourceResolver resolver,
67                                   String JavaDoc constraintset) {
68         current_descriptor = descriptor;
69         current_reloadable = reloadable;
70         current_logger = logger;
71         current_resolver = resolver;
72         current_constraint_set = constraintset;
73     }
74
75     /**
76      * keep track of current parameter context
77      */

78     public void setParameter(String JavaDoc parameter) {
79         current_parameter = parameter;
80     }
81
82     /**
83      * keep track of current constraint-set context
84      * (probably this is not needed?)
85      */

86     public void setConstraintSet(String JavaDoc constraintset) {
87         current_constraint_set = constraintset;
88     }
89
90     /**
91      * Get the specified attribute
92      *
93      * @param objectModel The Map objectModel
94      * @param name The parameter name
95      */

96     public static Object JavaDoc getAttribute(Map JavaDoc objectModel, String JavaDoc name) {
97         Request request = ObjectModelHelper.getRequest(objectModel);
98         return request.getAttribute(name);
99     }
100
101     /**
102      * Extracts the validation results from the request attribute
103      *
104      * @param objectModel The Map objectModel
105      * @return Map with ValidatorActionResults
106      * @see org.apache.cocoon.acting.ValidatorActionResult
107      */

108     public static Map JavaDoc getResults(Map JavaDoc objectModel) {
109         Request request = ObjectModelHelper.getRequest(objectModel);
110         return (Map JavaDoc) request.getAttribute(Constants.XSP_FORMVALIDATOR_PATH);
111     }
112
113
114     /**
115      * Extracts the validation results from the request attribute
116      * for a specific request parameter
117      *
118      * @param objectModel The Map objectModel
119      * @param name Request parameter's name
120      * @see org.apache.cocoon.acting.ValidatorActionResult
121      */

122     public static ValidatorActionResult getParamResult(Map JavaDoc objectModel,
123                                                        String JavaDoc name) {
124         ValidatorActionResult result = ValidatorActionResult.NOTPRESENT;
125         Map JavaDoc param_result = getResults(objectModel);
126         if (param_result != null) {
127             result = (ValidatorActionResult) param_result.get(name);
128         }
129         return (result != null? result : ValidatorActionResult.NOTPRESENT);
130     }
131
132     /**
133      * Extracts the validation results from the request attribute
134      * for the context's current request parameter
135      *
136      * @param objectModel The Map objectModel
137      * @see org.apache.cocoon.acting.ValidatorActionResult
138      */

139     public ValidatorActionResult getParamResult(Map JavaDoc objectModel) {
140         ValidatorActionResult result = ValidatorActionResult.NOTPRESENT;
141         Map JavaDoc param_result = getResults(objectModel);
142         if (param_result != null) {
143             result = (ValidatorActionResult) param_result.get(current_parameter);
144         }
145         return (result != null? result : ValidatorActionResult.NOTPRESENT);
146     }
147
148
149     /**
150      * Test whether the validation returned no error for this
151      * parameter.
152      *
153      * @param objectModel The Map objectModel
154      * @param name Request parameter's name
155      * @return true only if the parameter was validated and the validation
156      * did not return an error.
157      */

158     public static boolean isOK(Map JavaDoc objectModel, String JavaDoc name) {
159         return getParamResult(objectModel, name).equals(ValidatorActionResult.OK);
160     }
161
162     /**
163      * Test whether the validation returned no error for the
164      * context's current parameter.
165      *
166      * @param objectModel The Map objectModel
167      * @return true only if the parameter was validated and the validation
168      * did not return an error.
169      */

170     public boolean isOK(Map JavaDoc objectModel) {
171         return isOK(objectModel, current_parameter);
172     }
173
174
175     /**
176      * Test whether the validation returned an error for this
177      * parameter.
178      *
179      * @param objectModel The Map objectModel
180      * @param name Request parameter's name
181      * @return true if the parameter was either not validated or the validation
182      * returned an error.
183      */

184     public static boolean isError(Map JavaDoc objectModel, String JavaDoc name) {
185         return getParamResult(objectModel, name).ge(ValidatorActionResult.ERROR);
186     }
187
188     /**
189      * Test whether the validation returned an error for the
190      * context's current parameter.
191      *
192      * @param objectModel The Map objectModel
193      * @return true if the parameter was either not validated or the validation
194      * returned an error.
195      */

196     public boolean isError(Map JavaDoc objectModel) {
197         return isError(objectModel, current_parameter);
198     }
199
200
201     /**
202      * Test whether the validated parameter was null but wasn't allowed to.
203      *
204      * @param objectModel The Map objectModel
205      * @param name Request parameter's name
206      * @return true if the parameter was validated and the validation
207      * returned an error because the parameter was null but wasn't allowd to.
208      */

209     public static boolean isNull(Map JavaDoc objectModel, String JavaDoc name) {
210         return getParamResult(objectModel, name).equals(ValidatorActionResult.ISNULL);
211     }
212
213     /**
214      * Test whether the context's current parameter as validated was null but
215      * wasn't allowed to.
216      *
217      * @param objectModel The Map objectModel
218      * @return true if the parameter was validated and the validation
219      * returned an error because the parameter was null but wasn't allowd to.
220      */

221     public boolean isNull(Map JavaDoc objectModel) {
222         return isNull(objectModel, current_parameter);
223     }
224
225
226     /**
227      * Test whether the validated parameter was too small.
228      *
229      * @param objectModel The Map objectModel
230      * @param name Request parameter's name
231      * @return true if the parameter was validated and the validation
232      * returned an error because either its value or its length was
233      * too small.
234      */

235     public static boolean isTooSmall(Map JavaDoc objectModel, String JavaDoc name) {
236         boolean ok = getParamResult(objectModel, name).equals(ValidatorActionResult.TOOSMALL);
237
238         if (!ok) {
239             ok = isNull(objectModel, name);
240         }
241
242         return ok;
243     }
244
245     /**
246      * Test whether the context's current parameter was too small.
247      *
248      * @param objectModel The Map objectModel
249      * @return true if the parameter was validated and the validation
250      * returned an error because either its value or its length was
251      * too small.
252      */

253     public boolean isTooSmall(Map JavaDoc objectModel) {
254         return isTooSmall(objectModel, current_parameter);
255     }
256
257
258     /**
259      * Test whether the validated parameter was too large.
260      *
261      * @param objectModel The Map objectModel
262      * @param name Request parameter's name
263      * @return true if the parameter was validated and the validation
264      * returned an error because either its value or its length was
265      * too large.
266      */

267     public static boolean isTooLarge(Map JavaDoc objectModel, String JavaDoc name) {
268         return (getParamResult(objectModel, name) == ValidatorActionResult.TOOLARGE);
269     }
270
271     /**
272      * Test whether the context's current parameter was too large.
273      *
274      * @param objectModel The Map objectModel
275      * @return true if the parameter was validated and the validation
276      * returned an error because either its value or its length was
277      * too large.
278      */

279     public boolean isTooLarge(Map JavaDoc objectModel) {
280         return isTooLarge(objectModel, current_parameter);
281     }
282
283
284     /**
285      * Test whether the validated parameter wasn't matched by the requested
286      * regular expression.
287      *
288      * @param objectModel The Map objectModel
289      * @param name Request parameter's name
290      * @return true if the parameter was validated and the validation
291      * returned an error because its value wasn't matched by the requested
292      * regular expression.
293      */

294     public static boolean isNoMatch(Map JavaDoc objectModel, String JavaDoc name) {
295         return getParamResult(objectModel, name).equals(ValidatorActionResult.NOMATCH);
296     }
297
298     /**
299      * Test whether the context's current parameter wasn't matched by the requested
300      * regular expression.
301      *
302      * @param objectModel The Map objectModel
303      * @return true if the parameter was validated and the validation
304      * returned an error because its value wasn't matched by the requested
305      * regular expression.
306      */

307     public boolean isNoMatch(Map JavaDoc objectModel) {
308         return isNoMatch(objectModel, current_parameter);
309     }
310
311
312     /**
313      * Test whether the validated parameter wasn't validated
314      *
315      * @param objectModel The Map objectModel
316      * @param name Request parameter's name
317      * @return true if the parameter was not validated.
318      */

319     public static boolean isNotPresent(Map JavaDoc objectModel, String JavaDoc name) {
320         return getParamResult(objectModel, name).equals(ValidatorActionResult.NOTPRESENT);
321     }
322
323     /**
324      * Test whether the context's current parameter wasn't validated
325      *
326      * @param objectModel The Map objectModel
327      * @return true if the parameter was not validated.
328      */

329     public boolean isNotPresent(Map JavaDoc objectModel) {
330         return isNotPresent(objectModel, current_parameter);
331     }
332
333
334     /**
335      * Set up the complementary configuration file. Please note that
336      * multiple Actions can share the same configurations. By using
337      * this approach, we can limit the number of config files.
338      * Also note that the configuration file does not have to be a file.
339      *
340      * This is based on the similar named functions in
341      * org.apache.cocoon.acting.AbstractComplimentaryConfigurableAction
342      * with the addition of reloadable configuration files, reloadable
343      * flagg, manager, and logger parameter.
344      *
345      * @param descriptor URL of descriptor.xml file @see org.apache.cocoon.acting.AbstractComplimentaryConfigurableAction
346      * @param resolver
347      * @param reloadable set to <code>true</code> if changes of
348      * <code>descriptor</code> should trigger a reload. Note that this
349      * only works if <code>Source</code> is able to determine the
350      * modification time @see org.apache.cocoon.environment.Source
351      * @param logger used to send debug and error messages to
352      * @return up-to-date configuration, either (re)loaded or cached.
353      */

354
355     protected static Configuration getConfiguration(String JavaDoc descriptor, SourceResolver resolver,
356                                                     boolean reloadable, Logger logger)
357             throws ConfigurationException {
358
359         if (descriptor == null) {
360             throw new ConfigurationException("The form descriptor is not set!");
361         }
362
363         ConfigurationHelper conf = null;
364         synchronized (FormValidatorHelper.configurations) {
365             Source source = null;
366             try {
367                 source = resolver.resolveURI(descriptor);
368                 conf = (ConfigurationHelper) FormValidatorHelper.configurations.get(source.getURI());
369                 if (conf == null || (reloadable && conf.lastModified != source.getLastModified())) {
370                     logger.debug("(Re)Loading " + descriptor);
371
372                     if (conf == null) {
373                         conf = new ConfigurationHelper();
374                     }
375
376                     SAXConfigurationHandler builder = new SAXConfigurationHandler();
377                     SourceUtil.toSAX(source, builder);
378
379                     conf.lastModified = source.getLastModified();
380                     conf.configuration = builder.getConfiguration();
381
382                     FormValidatorHelper.cacheConfiguration(source.getURI(), conf);
383                 } else {
384                     logger.debug("Using cached configuration for " + descriptor);
385                 }
386             } catch (Exception JavaDoc e) {
387                 logger.error("Could not configure Database mapping environment", e);
388                 throw new ConfigurationException("Error trying to load configurations for resource: " + source.getURI());
389             } finally {
390                 resolver.release(source);
391             }
392         }
393
394         return conf.configuration;
395     }
396
397     /**
398      * Cache the configuration so that we can use it later.
399      */

400     private static void cacheConfiguration(String JavaDoc descriptor, ConfigurationHelper conf) {
401         synchronized (FormValidatorHelper.configurations) {
402             FormValidatorHelper.configurations.put(descriptor, conf);
403         }
404     }
405
406     /**
407      * Iterate over a set of configurations and return the one whose
408      * name matches the given one.
409      *
410      * @param conf set of configurations
411      * @param name name of configuration
412      * @param logger
413      * @return specified configuration or <code>null</code> if not found.
414      */

415     protected static Configuration getConfigurationByName(Configuration[] conf,
416                                                           String JavaDoc name,
417                                                           Logger logger
418                                                           ) {
419         int j = 0;
420         boolean found = false;
421         String JavaDoc setname = null;
422         for (j = 0; j < conf.length; j++) {
423             setname = conf[j].getAttribute("name", "");
424             if (name.trim().equals(setname.trim())) {
425                 found = true;
426                 break;
427             }
428         }
429         if (!found) {
430             logger.debug("FormValidatorHelper.getConfigurationByName: configuration " + name + " not found.");
431             return null;
432         }
433         return conf[j];
434     }
435
436     /**
437      * Get an attribute for a parameter as specified in
438      * descriptor.xml.
439      *
440      * @param descriptor URL of descriptor.xml file @see org.apache.cocoon.acting.AbstractComplimentaryConfigurableAction
441      * @param resolver
442      * @param reloadable set to <code>true</code> if changes of
443      * <code>descriptor</code> should trigger a reload. Note that this
444      * only works if <code>Source</code> is able to determine the
445      * modification time @see org.apache.cocoon.environment.Source
446      * @param logger used to send debug and error messages to
447      * @param attribute attribute name
448      * @return attribute value or <code>null</code>
449      */

450     public static String JavaDoc getParameterAttributes(String JavaDoc descriptor,
451                                                 SourceResolver resolver,
452                                                 boolean reloadable,
453                                                 String JavaDoc constraintset,
454                                                 String JavaDoc parameter,
455                                                 String JavaDoc attribute,
456                                                 Logger logger
457                                                 ) {
458         try {
459             Configuration conf = getConfiguration(descriptor, resolver, reloadable, logger);
460             Configuration[] desc = conf.getChildren("parameter");
461             Configuration[] csets = conf.getChildren("constraint-set");
462
463             Configuration cset = getConfigurationByName(csets, constraintset, logger);
464
465             Configuration[] set = cset.getChildren("validate");
466             Configuration constraints = getConfigurationByName(set, parameter, logger);
467             Configuration descr = getConfigurationByName(desc, parameter, logger);
468             return constraints.getAttribute(attribute, descr.getAttribute(attribute, ""));
469         } catch (Exception JavaDoc e) {
470             logger.debug("FormValidatorHelper.getParameterAttributes Exception " + e);
471         }
472         
473         return "";
474     }
475
476
477     /**
478      * Get an attribute for the context's current parameter as specified in
479      * descriptor.xml.
480      *
481      * @param attribute attribute name
482      * @return attribute value or <code>null</code>
483      */

484     public String JavaDoc getParameterAttribute(String JavaDoc attribute) {
485         return FormValidatorHelper.getParameterAttributes(current_descriptor,
486                 current_resolver,
487                 current_reloadable,
488                 current_constraint_set,
489                 current_parameter,
490                 attribute,
491                 current_logger);
492     }
493
494     /**
495      * Get an attribute for a parameter as specified in
496      * descriptor.xml.
497      *
498      * @param attribute attribute name
499      * @return attribute value or <code>null</code>
500      */

501     public String JavaDoc getParameterAttribute(String JavaDoc parameter, String JavaDoc attribute) {
502         return FormValidatorHelper.getParameterAttributes(current_descriptor,
503                 current_resolver,
504                 current_reloadable,
505                 current_constraint_set,
506                 parameter,
507                 attribute,
508                 current_logger);
509     }
510 }
511
Popular Tags