KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > datatype > validationruleimpl > RegExpValidationRuleBuilder


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.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 JavaDoc;
24
25 /**
26  * Builds {@link RegExpValidationRule}s.
27  *
28  * @version $Id: RegExpValidationRuleBuilder.java 56582 2004-11-04 10:16:22Z sylvain $
29  */

30 public class RegExpValidationRuleBuilder extends AbstractValidationRuleBuilder {
31
32     public ValidationRule build(Element JavaDoc validationRuleElement) throws Exception JavaDoc {
33         RegExpValidationRule rule = new RegExpValidationRule();
34
35         String JavaDoc 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 JavaDoc("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