01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.forms.validation.impl;
18:
19: import org.apache.avalon.framework.context.Context;
20: import org.apache.avalon.framework.context.ContextException;
21: import org.apache.avalon.framework.context.Contextualizable;
22: import org.apache.avalon.framework.logger.LogEnabled;
23: import org.apache.avalon.framework.logger.Logger;
24: import org.apache.avalon.framework.service.ServiceException;
25: import org.apache.avalon.framework.service.ServiceManager;
26: import org.apache.avalon.framework.service.Serviceable;
27: import org.apache.avalon.framework.thread.ThreadSafe;
28: import org.apache.cocoon.components.LifecycleHelper;
29: import org.apache.cocoon.forms.formmodel.WidgetDefinition;
30: import org.apache.cocoon.forms.util.DomHelper;
31: import org.apache.cocoon.forms.validation.WidgetValidator;
32: import org.apache.cocoon.forms.validation.WidgetValidatorBuilder;
33: import org.apache.cocoon.util.ClassUtils;
34: import org.apache.cocoon.util.ConfigurationUtil;
35: import org.w3c.dom.Element;
36:
37: /**
38: * A {@link org.apache.cocoon.forms.validation.WidgetValidatorBuilder} that creates java classes.
39: * <p>
40: * The syntax for this validator is as follows :<br/>
41: * <pre>
42: * <java class="com.my.SuperValidator"/>
43: * </pre>
44: *
45: * @version $Id: JavaClassValidatorBuilder.java 449149 2006-09-23 03:58:05Z crossley $
46: */
47: public class JavaClassValidatorBuilder implements
48: WidgetValidatorBuilder, ThreadSafe, Serviceable, LogEnabled,
49: Contextualizable {
50:
51: private ServiceManager manager;
52: private Logger logger;
53: private Context context;
54:
55: /**
56: * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
57: */
58: public void service(ServiceManager manager) throws ServiceException {
59: this .manager = manager;
60: }
61:
62: /**
63: * @see org.apache.cocoon.forms.validation.WidgetValidatorBuilder#build(org.w3c.dom.Element, org.apache.cocoon.forms.formmodel.WidgetDefinition)
64: */
65: public WidgetValidator build(Element validationRuleElement,
66: WidgetDefinition definition) throws Exception {
67: String name = DomHelper.getAttribute(validationRuleElement,
68: "class");
69:
70: Object validator = ClassUtils.newInstance(name);
71: if (validator instanceof WidgetValidator) {
72: LifecycleHelper.setupComponent(validator, logger, context,
73: manager, ConfigurationUtil
74: .toConfiguration(validationRuleElement));
75: return (WidgetValidator) validator;
76: } else {
77: throw new Exception("Class " + validator.getClass()
78: + " is not a " + WidgetValidator.class.getName());
79: }
80: }
81:
82: /**
83: * @see org.apache.avalon.framework.logger.LogEnabled#enableLogging(org.apache.avalon.framework.logger.Logger)
84: */
85: public void enableLogging(Logger logger) {
86: this .logger = logger;
87: }
88:
89: /**
90: * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
91: */
92: public void contextualize(Context context) throws ContextException {
93: this.context = context;
94: }
95: }
|