cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From br...@apache.org
Subject svn commit: r388155 - in /cocoon: branches/BRANCH_2_1_X/ branches/BRANCH_2_1_X/src/blocks/forms/conf/ trunk/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/samples/ trunk/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/coco...
Date Thu, 23 Mar 2006 12:38:48 GMT
Author: bruno
Date: Thu Mar 23 04:38:28 2006
New Revision: 388155

URL: http://svn.apache.org/viewcvs?rev=388155&view=rev
Log:
Forms: an fd:java validator
Contributed by Simone Gianni (COCOON-1806)

Added:
    cocoon/trunk/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/samples/CustomBirthDateValidator.java
  (with props)
    cocoon/trunk/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/validation/impl/JavaClassValidatorBuilder.java
  (with props)
Modified:
    cocoon/branches/BRANCH_2_1_X/src/blocks/forms/conf/forms-validators.xconf
    cocoon/branches/BRANCH_2_1_X/status.xml
    cocoon/trunk/cocoon-forms/cocoon-forms-sample/src/main/resources/samples/forms/form2_model.xml

Modified: cocoon/branches/BRANCH_2_1_X/src/blocks/forms/conf/forms-validators.xconf
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/blocks/forms/conf/forms-validators.xconf?rev=388155&r1=388154&r2=388155&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/forms/conf/forms-validators.xconf (original)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/forms/conf/forms-validators.xconf Thu Mar 23 04:38:28
2006
@@ -32,6 +32,7 @@
     <validator name="mod10" class="org.apache.cocoon.forms.validation.impl.Mod10ValidatorBuilder"/>
     <validator name="regexp" class="org.apache.cocoon.forms.validation.impl.RegExpValidatorBuilder"/>
     <validator name="captcha" class="org.apache.cocoon.forms.validation.impl.CaptchaValidatorBuilder"/>
+    <validator name="java" class="org.apache.cocoon.forms.validation.impl.JavaClassValidatorBuilder"/>
   </forms-validators>
 
 </xconf>

Modified: cocoon/branches/BRANCH_2_1_X/status.xml
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/status.xml?rev=388155&r1=388154&r2=388155&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/status.xml (original)
+++ cocoon/branches/BRANCH_2_1_X/status.xml Thu Mar 23 04:38:28 2006
@@ -180,6 +180,10 @@
   <release version="@version@" date="@date@">
 -->
   <release version="2.1.9" date="TBD">
+    <action dev="BRD" type="add" fixes-bug="COCOON-1806" due-to="Simone Gianni" due-to-email="s.gianni@thebug.it">
+      CForms: added an fd:java validator to make it easy to implement custom
+      WidgetValidator's in Java (for use in the form definition).
+    </action>
     <action dev="AG" type="update">
       Updated commons-io to 1.2.
     </action>

Added: cocoon/trunk/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/samples/CustomBirthDateValidator.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/samples/CustomBirthDateValidator.java?rev=388155&view=auto
==============================================================================
--- cocoon/trunk/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/samples/CustomBirthDateValidator.java
(added)
+++ cocoon/trunk/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/samples/CustomBirthDateValidator.java
Thu Mar 23 04:38:28 2006
@@ -0,0 +1,66 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.forms.samples;
+
+import java.util.Date;
+import java.util.GregorianCalendar;
+
+import org.apache.avalon.framework.context.Context;
+import org.apache.avalon.framework.context.ContextException;
+import org.apache.avalon.framework.context.Contextualizable;
+import org.apache.avalon.framework.logger.LogEnabled;
+import org.apache.avalon.framework.logger.Logger;
+import org.apache.cocoon.forms.formmodel.Widget;
+import org.apache.cocoon.forms.validation.ValidationError;
+import org.apache.cocoon.forms.validation.ValidationErrorAware;
+import org.apache.cocoon.forms.validation.WidgetValidator;
+
+/**
+ * Example of a custom validator.  Check that the given date is a valid birth date, i.e.

+ * is at least 5 years before current date and no more than 100 years old.
+ */
+public class CustomBirthDateValidator implements WidgetValidator, LogEnabled, Contextualizable
{
+
+    private Logger logger = null;
+    private Context context = null;
+    
+    
+    public boolean validate(Widget widget) {
+        Date birthDate = (Date) widget.getValue();
+        if (logger.isDebugEnabled()) logger.debug("Validating date " + birthDate);
+        GregorianCalendar cal = new GregorianCalendar();
+        cal.add(GregorianCalendar.YEAR, -5);
+        Date maxDate = cal.getTime();
+        cal.add(GregorianCalendar.YEAR, -95);
+        Date minDate = cal.getTime();
+        if (birthDate.after(maxDate) || birthDate.before(minDate)) {
+            if (widget instanceof ValidationErrorAware) {
+                ((ValidationErrorAware)widget).setValidationError(new ValidationError("Invalid
birth date", false));
+            }
+            return false;
+        }
+        return true;
+    }
+
+    public void enableLogging(Logger logger) {
+        this.logger = logger;
+    }
+
+    public void contextualize(Context context) throws ContextException {
+        this.context = context;
+    }
+
+}

Propchange: cocoon/trunk/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/samples/CustomBirthDateValidator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: cocoon/trunk/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/validation/impl/JavaClassValidatorBuilder.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/validation/impl/JavaClassValidatorBuilder.java?rev=388155&view=auto
==============================================================================
--- cocoon/trunk/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/validation/impl/JavaClassValidatorBuilder.java
(added)
+++ cocoon/trunk/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/validation/impl/JavaClassValidatorBuilder.java
Thu Mar 23 04:38:28 2006
@@ -0,0 +1,76 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.forms.validation.impl;
+
+import org.apache.avalon.framework.context.Context;
+import org.apache.avalon.framework.context.ContextException;
+import org.apache.avalon.framework.context.Contextualizable;
+import org.apache.avalon.framework.logger.LogEnabled;
+import org.apache.avalon.framework.logger.Logger;
+import org.apache.avalon.framework.service.ServiceException;
+import org.apache.avalon.framework.service.ServiceManager;
+import org.apache.avalon.framework.service.Serviceable;
+import org.apache.avalon.framework.thread.ThreadSafe;
+import org.apache.cocoon.components.LifecycleHelper;
+import org.apache.cocoon.forms.formmodel.WidgetDefinition;
+import org.apache.cocoon.forms.util.DomHelper;
+import org.apache.cocoon.forms.validation.WidgetValidator;
+import org.apache.cocoon.forms.validation.WidgetValidatorBuilder;
+import org.apache.cocoon.util.ClassUtils;
+import org.w3c.dom.Element;
+
+/**
+ * A {@link org.apache.cocoon.forms.validation.WidgetValidatorBuilder} that creates java
classes.
+ * <p>
+ * The syntax for this validator is as follows :<br/>
+ * <pre>
+ *   &lt;java class="com.my.SuperValidator"/&gt;
+ * </pre>
+ *
+ * @version $Id$
+ */
+public class JavaClassValidatorBuilder implements WidgetValidatorBuilder, ThreadSafe, Serviceable,
LogEnabled, Contextualizable  {
+
+    private ServiceManager manager;
+    private Logger logger = null;
+    private Context context = null;
+    
+
+    public void service(ServiceManager manager) throws ServiceException {
+        this.manager = manager;
+    }
+
+    public WidgetValidator build(Element validationRuleElement, WidgetDefinition definition)
throws Exception {
+        String name = DomHelper.getAttribute(validationRuleElement, "class");
+
+        Object validator = ClassUtils.newInstance(name);
+        if (validator instanceof WidgetValidator) {
+            LifecycleHelper.setupComponent(validator, logger, context, manager, null);
+            return (WidgetValidator)validator;
+        } else {
+            throw new Exception("Class " + validator.getClass() + " is not a " + WidgetValidator.class.getName());
+        }
+    }
+    
+    public void enableLogging(Logger logger) {
+        this.logger = logger;
+    }
+
+    public void contextualize(Context context) throws ContextException {
+        this.context = context;
+    }
+
+}

Propchange: cocoon/trunk/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/validation/impl/JavaClassValidatorBuilder.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: cocoon/trunk/cocoon-forms/cocoon-forms-sample/src/main/resources/samples/forms/form2_model.xml
URL: http://svn.apache.org/viewcvs/cocoon/trunk/cocoon-forms/cocoon-forms-sample/src/main/resources/samples/forms/form2_model.xml?rev=388155&r1=388154&r2=388155&view=diff
==============================================================================
--- cocoon/trunk/cocoon-forms/cocoon-forms-sample/src/main/resources/samples/forms/form2_model.xml
(original)
+++ cocoon/trunk/cocoon-forms/cocoon-forms-sample/src/main/resources/samples/forms/form2_model.xml
Thu Mar 23 04:38:28 2006
@@ -76,6 +76,10 @@
           </fd:patterns>
         </fd:convertor>
       </fd:datatype>
+      <fd:validation>
+          <fd:java class="org.apache.cocoon.forms.samples.CustomBirthDateValidator"/>
+      </fd:validation>
+          
     </fd:field>
   
     <fd:field id="number" required="true">



Mime
View raw message