1 16 package org.apache.cocoon.forms.datatype.validationruleimpl; 17 18 import org.apache.cocoon.forms.datatype.ValidationRule; 19 import org.apache.cocoon.forms.util.DomHelper; 20 import org.apache.oro.text.regex.Perl5Compiler; 21 import org.apache.oro.text.regex.Pattern; 22 import org.apache.oro.text.regex.MalformedPatternException; 23 import org.w3c.dom.Element ; 24 25 30 public class RegExpValidationRuleBuilder extends AbstractValidationRuleBuilder { 31 32 public ValidationRule build(Element validationRuleElement) throws Exception { 33 RegExpValidationRule rule = new RegExpValidationRule(); 34 35 String regexp = DomHelper.getAttribute(validationRuleElement, "pattern"); 36 buildFailMessage(validationRuleElement, rule); 37 38 Perl5Compiler compiler = new Perl5Compiler(); 39 Pattern pattern = null; 40 try { 41 pattern = compiler.compile(regexp, Perl5Compiler.READ_ONLY_MASK); 42 } catch (MalformedPatternException e) { 43 throw new Exception ("Invalid regular expression at " + DomHelper.getLocation(validationRuleElement) + ": " + e.getMessage()); 44 } 45 rule.setPattern(regexp, pattern); 46 47 buildFailMessage(validationRuleElement, rule); 48 49 return rule; 50 } 51 52 } 53 | Popular Tags |