KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > woody > 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.woody.datatype.validationruleimpl;
17
18 import org.apache.cocoon.woody.datatype.ValidationRuleBuilder;
19 import org.apache.cocoon.woody.util.DomHelper;
20 import org.apache.cocoon.woody.Constants;
21 import org.apache.cocoon.woody.expression.ExpressionManager;
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 36537 2004-08-17 20:36:47Z vgritsenko $
36  */

37 public abstract class AbstractValidationRuleBuilder implements ValidationRuleBuilder, Serviceable, Disposable {
38     protected ExpressionManager expressionManager;
39     protected ServiceManager serviceManager;
40
41     public void service(ServiceManager serviceManager) throws ServiceException {
42         this.serviceManager = serviceManager;
43         this.expressionManager = (ExpressionManager)serviceManager.lookup(ExpressionManager.ROLE);
44     }
45
46     /**
47      * Checks if the validation rule configuration contains a custom failmessage, and if so,
48      * sets it one the ValidationRule.
49      */

50     protected void buildFailMessage(Element JavaDoc validationRuleElement, AbstractValidationRule rule) {
51         Element JavaDoc failMessageElement = DomHelper.getChildElement(validationRuleElement, Constants.WD_NS, "failmessage");
52         if (failMessageElement != null) {
53             XMLizable failMessage = DomHelper.compileElementContent(failMessageElement);
54             rule.setFailMessage(failMessage);
55         }
56     }
57
58     /**
59      * Parses an expression and throws a nice error message if this fails.
60      */

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