bruno       2003/07/09 05:17:21

  Modified:    src/blocks/woody/conf woody.xconf
               src/blocks/woody/samples/forms form1.xml form1_template.xml
                        form1_template.xsp
               src/blocks/woody/samples/messages WoodyMessages.xml
  Added:       
src/blocks/woody/java/org/apache/cocoon/woody/datatype/validationruleimpl
                        RegExpValidationRule.java
                        RegExpValidationRuleBuilder.java
  Log:
  Added a regexp validation rule, based on a contribution by Frank Taffelt.
  
  Revision  Changes    Path
  1.4       +1 -0      cocoon-2.1/src/blocks/woody/conf/woody.xconf
  
  Index: woody.xconf
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/conf/woody.xconf,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- woody.xconf       26 Jun 2003 09:20:59 -0000      1.3
  +++ woody.xconf       9 Jul 2003 12:17:21 -0000       1.4
  @@ -30,6 +30,7 @@
         <validation-rule name="range" 
factory="org.apache.cocoon.woody.datatype.validationruleimpl.RangeValidationRuleBuilder"/>
         <validation-rule name="assert" 
factory="org.apache.cocoon.woody.datatype.validationruleimpl.AssertValidationRuleBuilder"/>
         <validation-rule name="mod10" 
factory="org.apache.cocoon.woody.datatype.validationruleimpl.Mod10ValidationRuleBuilder"/>
  +      <validation-rule name="regexp" 
factory="org.apache.cocoon.woody.datatype.validationruleimpl.RegExpValidationRuleBuilder"/>
       </validation-rules>
     </woody-datatype>
   </xconf>
  
  
  
  1.1                  
cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/datatype/validationruleimpl/RegExpValidationRule.java
  
  Index: RegExpValidationRule.java
  ===================================================================
  /*
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  
   Redistribution and use in source and binary forms, with or without modifica-
   tion, are permitted provided that the following conditions are met:
  
   1. Redistributions of  source code must  retain the above copyright  notice,
      this list of conditions and the following disclaimer.
  
   2. Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
  
   3. The end-user documentation included with the redistribution, if any, must
      include  the following  acknowledgment:  "This product includes  software
      developed  by the  Apache Software Foundation  (http://www.apache.org/)."
      Alternately, this  acknowledgment may  appear in the software itself,  if
      and wherever such third-party acknowledgments normally appear.
  
   4. The names "Apache Cocoon" and  "Apache Software Foundation" must  not  be
      used to  endorse or promote  products derived from  this software without
      prior written permission. For written permission, please contact
      [EMAIL PROTECTED]
  
   5. Products  derived from this software may not  be called "Apache", nor may
      "Apache" appear  in their name,  without prior written permission  of the
      Apache Software Foundation.
  
   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  
   This software  consists of voluntary contributions made  by many individuals
   on  behalf of the Apache Software  Foundation and was  originally created by
   Stefano Mazzocchi  <[EMAIL PROTECTED]>. For more  information on the Apache
   Software Foundation, please see <http://www.apache.org/>.
  
  */
  package org.apache.cocoon.woody.datatype.validationruleimpl;
  
  import org.apache.cocoon.woody.datatype.ValidationError;
  import org.apache.cocoon.woody.datatype.validationruleimpl.AbstractValidationRule;
  import org.apache.oro.text.regex.Pattern;
  import org.apache.oro.text.regex.PatternMatcher;
  import org.apache.oro.text.regex.Perl5Matcher;
  import org.outerj.expression.ExpressionContext;
  
  
  /**
   * Checks that a String matches a regular expression.
   *
   * <p>The <a href="http://jakarta.apache.org/oro/";>Jakarta ORO</a> library
   * is used as regexp engine.
   */
  public class RegExpValidationRule extends AbstractValidationRule {
        /** Compiled regular expression. */
        private Pattern pattern;
      /** Original string representation of the regexp, used for informational 
purposes only. */
      private String regexp;
  
      public ValidationError validate(Object value, ExpressionContext 
expressionContext) {
        String string = (String)value;
        
        if(matchesRegExp(string))
                return null;   
            else
                return hasFailMessage() ? getFailMessage() : new 
ValidationError("validation.string.regexp", new String[] {regexp});
      }
      
      private boolean matchesRegExp(String string) {
          PatternMatcher matcher = new Perl5Matcher();
          return matcher.matches(string, pattern);
      }
      
      void setPattern(String regexp, Pattern pattern) {
          this.regexp = regexp;
        this.pattern = pattern;
      }
  
      
      public boolean supportsType(Class clazz, boolean arrayType) {
          return clazz.isAssignableFrom(String.class) && !arrayType;
      }
  }
  
  
  
  1.1                  
cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/datatype/validationruleimpl/RegExpValidationRuleBuilder.java
  
  Index: RegExpValidationRuleBuilder.java
  ===================================================================
  /*
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  
   Redistribution and use in source and binary forms, with or without modifica-
   tion, are permitted provided that the following conditions are met:
  
   1. Redistributions of  source code must  retain the above copyright  notice,
      this list of conditions and the following disclaimer.
  
   2. Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
  
   3. The end-user documentation included with the redistribution, if any, must
      include  the following  acknowledgment:  "This product includes  software
      developed  by the  Apache Software Foundation  (http://www.apache.org/)."
      Alternately, this  acknowledgment may  appear in the software itself,  if
      and wherever such third-party acknowledgments normally appear.
  
   4. The names "Apache Cocoon" and  "Apache Software Foundation" must  not  be
      used to  endorse or promote  products derived from  this software without
      prior written permission. For written permission, please contact
      [EMAIL PROTECTED]
  
   5. Products  derived from this software may not  be called "Apache", nor may
      "Apache" appear  in their name,  without prior written permission  of the
      Apache Software Foundation.
  
   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  
   This software  consists of voluntary contributions made  by many individuals
   on  behalf of the Apache Software  Foundation and was  originally created by
   Stefano Mazzocchi  <[EMAIL PROTECTED]>. For more  information on the Apache
   Software Foundation, please see <http://www.apache.org/>.
  
  */
  package org.apache.cocoon.woody.datatype.validationruleimpl;
  
  import org.apache.cocoon.woody.datatype.ValidationRule;
  import 
org.apache.cocoon.woody.datatype.validationruleimpl.AbstractValidationRuleBuilder;
  import org.apache.cocoon.woody.util.DomHelper;
  import org.apache.oro.text.regex.Perl5Compiler;
  import org.apache.oro.text.regex.Pattern;
  import org.apache.oro.text.regex.MalformedPatternException;
  import org.w3c.dom.Element;
  
  /**
   * Builds [EMAIL PROTECTED] RegExpValidationRule}s.
   */
  public class RegExpValidationRuleBuilder extends AbstractValidationRuleBuilder {
  
      public ValidationRule build(Element validationRuleElement) throws Exception {
          RegExpValidationRule rule = new RegExpValidationRule();
  
          String regexp = DomHelper.getAttribute(validationRuleElement, "pattern");
          buildFailMessage(validationRuleElement, rule);
  
          Perl5Compiler compiler = new Perl5Compiler();
          Pattern pattern = null;
          try {
              pattern = compiler.compile(regexp, Perl5Compiler.READ_ONLY_MASK);
          } catch (MalformedPatternException e) {
              throw new Exception("Invalid regular expression at " + 
DomHelper.getLocation(validationRuleElement) + ": " + e.getMessage());
          }
          rule.setPattern(regexp, pattern);
  
          buildFailMessage(validationRuleElement, rule);
  
          return rule;
      }
  
  }
  
  
  
  1.5       +11 -0     cocoon-2.1/src/blocks/woody/samples/forms/form1.xml
  
  Index: form1.xml
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/samples/forms/form1.xml,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- form1.xml 26 Jun 2003 09:18:41 -0000      1.4
  +++ form1.xml 9 Jul 2003 12:17:21 -0000       1.5
  @@ -121,6 +121,17 @@
       </wd:validation>
     </wd:aggregatefield>
   
  +  <wd:field id="ipaddress" required="true">
  +    <wd:label>Please enter your IP address</wd:label>
  +    <wd:datatype base="string">
  +      <wd:validation>
  +        <wd:regexp 
pattern="^([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])$">
  +          <wd:failmessage>Invalid IP address.</wd:failmessage>
  +        </wd:regexp>
  +      </wd:validation>
  +    </wd:datatype>
  +  </wd:field>
  +
     <wd:repeater id="contacts">
       <wd:field id="firstname">
         <wd:label>Firstname</wd:label>
  
  
  
  1.6       +4 -0      cocoon-2.1/src/blocks/woody/samples/forms/form1_template.xml
  
  Index: form1_template.xml
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/samples/forms/form1_template.xml,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- form1_template.xml        26 Jun 2003 09:18:41 -0000      1.5
  +++ form1_template.xml        9 Jul 2003 12:17:21 -0000       1.6
  @@ -51,6 +51,10 @@
             <td valign="top"><wt:widget-label id="visa"/></td>
             <td valign="top"><wt:widget id="visa"/></td>
           </tr>
  +        <tr>
  +          <td valign="top"><wt:widget-label id="ipaddress"/></td>
  +          <td valign="top"><wt:widget id="ipaddress"/></td>
  +        </tr>
         </table>
   
         <wt:widget-label id="contacts"/><br/>
  
  
  
  1.2       +5 -1      cocoon-2.1/src/blocks/woody/samples/forms/form1_template.xsp
  
  Index: form1_template.xsp
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/samples/forms/form1_template.xsp,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- form1_template.xsp        5 Jul 2003 22:53:33 -0000       1.1
  +++ form1_template.xsp        9 Jul 2003 12:17:21 -0000       1.2
  @@ -56,6 +56,10 @@
             <td valign="top"><wt:widget-label id="visa"/></td>
             <td valign="top"><wt:widget id="visa"/></td>
           </tr>
  +        <tr>
  +          <td valign="top"><wt:widget-label id="ipaddress"/></td>
  +          <td valign="top"><wt:widget id="ipaddress"/></td>
  +        </tr>
         </table>
   
         <wt:widget-label id="contacts"/><br/>
  @@ -93,4 +97,4 @@
       </form>
     </content>
   </page>
  -</xsp:page>
  \ No newline at end of file
  +</xsp:page>
  
  
  
  1.4       +2 -0      cocoon-2.1/src/blocks/woody/samples/messages/WoodyMessages.xml
  
  Index: WoodyMessages.xml
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/samples/messages/WoodyMessages.xml,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- WoodyMessages.xml 26 Jun 2003 09:19:01 -0000      1.3
  +++ WoodyMessages.xml 9 Jul 2003 12:17:21 -0000       1.4
  @@ -10,6 +10,7 @@
     <message key="validation.string.range-length">Should contain from {0} to {1} 
characters.</message>
     <message key="validation.string.min-length">Should be at least {0} 
characters.</message>
     <message key="validation.string.max-length">Should be at most {0} 
characters.</message>
  +  <message key="validation.string.regexp">Does not match the following regular 
expression: {0}</message>
   
     <message key="validation.string.invalidemail">This field does not contain a valid 
email address</message>
   
  @@ -25,6 +26,7 @@
     <message key="validation.numeric.max">Number should be less than or equal to 
{0}.</message>
   
     <message key="validation.mod10">Mod 10 check failed.</message>
  +
   
     <message key="aggregatedfield.split-failed">Content of this field does not match 
the following regular expression: {0}</message>
   
  
  
  

Reply via email to