KickJava   Java API By Example, From Geeks To Geeks.

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


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.FormsConstants;
19 import org.apache.cocoon.forms.datatype.ValidationRuleBuilder;
20 import org.apache.cocoon.forms.expression.ExpressionManager;
21 import org.apache.cocoon.forms.util.DomHelper;
22 import org.apache.avalon.framework.CascadingException;
23 import org.apache.avalon.framework.activity.Disposable;
24 import org.apache.avalon.framework.service.Serviceable;
25 import org.apache.avalon.framework.service.ServiceManager;
26 import org.apache.avalon.framework.service.ServiceException;
27 import org.apache.excalibur.xml.sax.XMLizable;
28 import org.w3c.dom.Element JavaDoc;
29 import org.outerj.expression.Expression;
30 import org.outerj.expression.TokenMgrError;
31
32 /**
33  * Abstract base class for ValidationRuleBuilder implementations.
34  *
35  * @version $Id: AbstractValidationRuleBuilder.java 326838 2005-10-20 06:26:53Z sylvain $
36  */

37 public abstract class AbstractValidationRuleBuilder
38     implements ValidationRuleBuilder, Serviceable, Disposable {
39
40     protected ExpressionManager expressionManager;
41     protected ServiceManager serviceManager;
42
43     /**
44      * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
45      */

46     public void service(ServiceManager serviceManager) throws ServiceException {
47         this.serviceManager = serviceManager;
48         this.expressionManager = (ExpressionManager)serviceManager.lookup(ExpressionManager.ROLE);
49     }
50
51     /**
52      * Checks if the validation rule configuration contains a custom failmessage, and if so,
53      * sets it one the ValidationRule.
54      */

55     protected void buildFailMessage(Element JavaDoc validationRuleElement, AbstractValidationRule rule) {
56         Element JavaDoc failMessageElement = DomHelper.getChildElement(validationRuleElement, FormsConstants.DEFINITION_NS, "failmessage");
57         if (failMessageElement != null) {
58             XMLizable failMessage = DomHelper.compileElementContent(failMessageElement);
59             rule.setFailMessage(failMessage);
60         }
61     }
62
63     /**
64      * Parses an expression and throws a nice error message if this fails.
65      */

66     protected Expression parseExpression(String JavaDoc exprString, Element JavaDoc element, String JavaDoc attrName) throws Exception JavaDoc {
67         try {
68             return expressionManager.parse(exprString);
69         } catch (TokenMgrError e) {
70             throw new CascadingException("Error in expression \"" + exprString + "\" in attribute \"" + attrName + "\" at " + DomHelper.getLocation(element), e);
71         } catch (Exception JavaDoc e) {
72             throw new CascadingException("Error in expression \"" + exprString + "\" in attribute \"" + attrName + "\" at " + DomHelper.getLocation(element), e);
73         }
74     }
75
76     public void dispose() {
77         this.serviceManager.release(this.expressionManager);
78         this.expressionManager = null;
79         this.serviceManager = null;
80     }
81 }
82
Popular Tags