KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > woody > 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.woody.datatype.validationruleimpl;
17
18 import org.apache.cocoon.woody.datatype.ValidationRule;
19 import org.apache.cocoon.woody.datatype.validationruleimpl.AbstractValidationRuleBuilder;
20 import org.apache.cocoon.woody.util.DomHelper;
21 import org.apache.oro.text.regex.Perl5Compiler;
22 import org.apache.oro.text.regex.Pattern;
23 import org.apache.oro.text.regex.MalformedPatternException;
24 import org.w3c.dom.Element JavaDoc;
25
26 /**
27  * Builds {@link RegExpValidationRule}s.
28  *
29  * @version $Id: RegExpValidationRuleBuilder.java 30932 2004-07-29 17:35:38Z vgritsenko $
30  */

31 public class RegExpValidationRuleBuilder extends AbstractValidationRuleBuilder {
32
33     public ValidationRule build(Element JavaDoc validationRuleElement) throws Exception JavaDoc {
34         RegExpValidationRule rule = new RegExpValidationRule();
35
36         String JavaDoc regexp = DomHelper.getAttribute(validationRuleElement, "pattern");
37         buildFailMessage(validationRuleElement, rule);
38
39         Perl5Compiler compiler = new Perl5Compiler();
40         Pattern pattern = null;
41         try {
42             pattern = compiler.compile(regexp, Perl5Compiler.READ_ONLY_MASK);
43         } catch (MalformedPatternException e) {
44             throw new Exception JavaDoc("Invalid regular expression at " + DomHelper.getLocation(validationRuleElement) + ": " + e.getMessage());
45         }
46         rule.setPattern(regexp, pattern);
47
48         buildFailMessage(validationRuleElement, rule);
49
50         return rule;
51     }
52
53 }
54
Popular Tags