KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > datatype > DefaultDatatypeManager


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;
17
18 import org.apache.avalon.framework.thread.ThreadSafe;
19 import org.apache.avalon.framework.configuration.Configurable;
20 import org.apache.avalon.framework.configuration.Configuration;
21 import org.apache.avalon.framework.configuration.ConfigurationException;
22 import org.apache.avalon.framework.logger.AbstractLogEnabled;
23 import org.apache.avalon.framework.service.ServiceException;
24 import org.apache.avalon.framework.service.Serviceable;
25 import org.apache.avalon.framework.service.ServiceManager;
26 import org.apache.avalon.framework.activity.Initializable;
27 import org.apache.avalon.framework.activity.Disposable;
28 import org.apache.avalon.framework.CascadingException;
29 import org.apache.avalon.framework.context.Contextualizable;
30 import org.apache.avalon.framework.context.Context;
31 import org.apache.avalon.framework.context.ContextException;
32 import org.apache.cocoon.forms.datatype.convertor.Convertor;
33 import org.apache.cocoon.forms.util.DomHelper;
34 import org.apache.cocoon.forms.util.SimpleServiceSelector;
35 import org.w3c.dom.Element JavaDoc;
36
37 /**
38  * Implementation of the {@link DatatypeManager} component.
39  *
40  * <p>It supports an extensible number of datatype and validation rule implementations
41  * by delegating the creation of them to {@link DatatypeBuilder}s and {@link ValidationRuleBuilder}s.
42  * Currently the list of datatype and validationrule builders is hardcoded, but this will
43  * become externally configurable in the future.
44  *
45  * @version $Id: DefaultDatatypeManager.java 56582 2004-11-04 10:16:22Z sylvain $
46  *
47  */

48 public class DefaultDatatypeManager extends AbstractLogEnabled implements DatatypeManager, ThreadSafe, Serviceable,
49         Configurable, Initializable, Disposable, Contextualizable {
50     private SimpleServiceSelector typeBuilderSelector;
51     private SimpleServiceSelector validationRuleBuilderSelector;
52     private ServiceManager serviceManager;
53     private Configuration configuration;
54     private Context context;
55
56     public void contextualize(Context context) throws ContextException {
57         this.context = context;
58     }
59
60     public void configure(Configuration configuration) throws ConfigurationException {
61         this.configuration = configuration;
62     }
63
64     public void service(ServiceManager serviceManager) throws ServiceException {
65         this.serviceManager = serviceManager;
66     }
67
68     public void initialize() throws Exception JavaDoc {
69         typeBuilderSelector = new SimpleServiceSelector("datatype", DatatypeBuilder.class);
70         typeBuilderSelector.enableLogging(getLogger());
71         typeBuilderSelector.contextualize(context);
72         typeBuilderSelector.service(serviceManager);
73         typeBuilderSelector.configure(configuration.getChild("datatypes"));
74
75         validationRuleBuilderSelector = new SimpleServiceSelector("validation-rule", ValidationRuleBuilder.class);
76         validationRuleBuilderSelector.enableLogging(getLogger());
77         validationRuleBuilderSelector.contextualize(context);
78         validationRuleBuilderSelector.service(serviceManager);
79         validationRuleBuilderSelector.configure(configuration.getChild("validation-rules"));
80
81         configuration = null;
82     }
83
84     public Datatype createDatatype(Element JavaDoc datatypeElement, boolean arrayType) throws Exception JavaDoc {
85         String JavaDoc typeName = DomHelper.getAttribute(datatypeElement, "base");
86         DatatypeBuilder builder = null;
87         try {
88             builder = (DatatypeBuilder)typeBuilderSelector.select(typeName);
89         } catch (ServiceException e) {
90             throw new CascadingException("Unknown datatype '" + typeName + "' specified at " + DomHelper.getLocation(datatypeElement), e);
91         }
92         return builder.build(datatypeElement, arrayType, this);
93     }
94
95     public ValidationRule createValidationRule(Element JavaDoc validationRuleElement) throws Exception JavaDoc {
96         String JavaDoc name = validationRuleElement.getLocalName();
97         ValidationRuleBuilder builder = null;
98         try {
99             builder = (ValidationRuleBuilder)validationRuleBuilderSelector.select(name);
100         } catch (ServiceException e) {
101             throw new CascadingException("Unknown validation rule \"" + name + "\" specified at " + DomHelper.getLocation(validationRuleElement), e);
102         }
103         return builder.build(validationRuleElement);
104     }
105
106     public Convertor createConvertor(String JavaDoc dataTypeName, Element JavaDoc convertorElement) throws Exception JavaDoc {
107         DatatypeBuilder datatypeBulder = (DatatypeBuilder)typeBuilderSelector.select(dataTypeName);
108         return datatypeBulder.buildConvertor(convertorElement);
109     }
110
111     public void dispose() {
112         typeBuilderSelector.dispose();
113         validationRuleBuilderSelector.dispose();
114     }
115 }
116
Popular Tags