KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > woody > 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.woody.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.cocoon.woody.util.DomHelper;
30 import org.apache.cocoon.woody.util.SimpleServiceSelector;
31 import org.apache.cocoon.woody.datatype.convertor.Convertor;
32 import org.w3c.dom.Element JavaDoc;
33
34 /**
35  * Implementation of the {@link DatatypeManager} component.
36  *
37  * <p>It supports an extensible number of datatype and validation rule implementations
38  * by delegating the creation of them to {@link DatatypeBuilder}s and {@link ValidationRuleBuilder}s.
39  * Currently the list of datatype and validationrule builders is hardcoded, but this will
40  * become externally configurable in the future.
41  *
42  * @version $Id: DefaultDatatypeManager.java 30932 2004-07-29 17:35:38Z vgritsenko $
43  *
44  */

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