001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.woody.datatype;
018:
019: import org.apache.avalon.framework.thread.ThreadSafe;
020: import org.apache.avalon.framework.configuration.Configurable;
021: import org.apache.avalon.framework.configuration.Configuration;
022: import org.apache.avalon.framework.configuration.ConfigurationException;
023: import org.apache.avalon.framework.logger.AbstractLogEnabled;
024: import org.apache.avalon.framework.service.ServiceException;
025: import org.apache.avalon.framework.service.Serviceable;
026: import org.apache.avalon.framework.service.ServiceManager;
027: import org.apache.avalon.framework.activity.Initializable;
028: import org.apache.avalon.framework.activity.Disposable;
029: import org.apache.avalon.framework.CascadingException;
030: import org.apache.cocoon.woody.util.DomHelper;
031: import org.apache.cocoon.woody.util.SimpleServiceSelector;
032: import org.apache.cocoon.woody.datatype.convertor.Convertor;
033: import org.w3c.dom.Element;
034:
035: /**
036: * Implementation of the {@link DatatypeManager} component.
037: *
038: * <p>It supports an extensible number of datatype and validation rule implementations
039: * by delegating the creation of them to {@link DatatypeBuilder}s and {@link ValidationRuleBuilder}s.
040: * Currently the list of datatype and validationrule builders is hardcoded, but this will
041: * become externally configurable in the future.
042: *
043: * @version $Id: DefaultDatatypeManager.java 433543 2006-08-22 06:22:54Z crossley $
044: *
045: */
046: public class DefaultDatatypeManager extends AbstractLogEnabled
047: implements DatatypeManager, ThreadSafe, Serviceable,
048: Configurable, Initializable, Disposable {
049: private SimpleServiceSelector typeBuilderSelector;
050: private SimpleServiceSelector validationRuleBuilderSelector;
051: private ServiceManager serviceManager;
052: private Configuration configuration;
053:
054: public void configure(Configuration configuration)
055: throws ConfigurationException {
056: this .configuration = configuration;
057: }
058:
059: public void service(ServiceManager serviceManager)
060: throws ServiceException {
061: this .serviceManager = serviceManager;
062: }
063:
064: public void initialize() throws Exception {
065: typeBuilderSelector = new SimpleServiceSelector("datatype",
066: DatatypeBuilder.class);
067: typeBuilderSelector.service(serviceManager);
068: typeBuilderSelector.configure(configuration
069: .getChild("datatypes"));
070:
071: validationRuleBuilderSelector = new SimpleServiceSelector(
072: "validation-rule", ValidationRuleBuilder.class);
073: validationRuleBuilderSelector.service(serviceManager);
074: validationRuleBuilderSelector.configure(configuration
075: .getChild("validation-rules"));
076:
077: configuration = null;
078: }
079:
080: public Datatype createDatatype(Element datatypeElement,
081: boolean arrayType) throws Exception {
082: String typeName = DomHelper.getAttribute(datatypeElement,
083: "base");
084: DatatypeBuilder builder = null;
085: try {
086: builder = (DatatypeBuilder) typeBuilderSelector
087: .select(typeName);
088: } catch (ServiceException e) {
089: throw new CascadingException("Unknown datatype '"
090: + typeName + "' specified at "
091: + DomHelper.getLocation(datatypeElement), e);
092: }
093: return builder.build(datatypeElement, arrayType, this );
094: }
095:
096: public ValidationRule createValidationRule(
097: Element validationRuleElement) throws Exception {
098: String name = validationRuleElement.getLocalName();
099: ValidationRuleBuilder builder = null;
100: try {
101: builder = (ValidationRuleBuilder) validationRuleBuilderSelector
102: .select(name);
103: } catch (ServiceException e) {
104: throw new CascadingException("Unknown validation rule \""
105: + name + "\" specified at "
106: + DomHelper.getLocation(validationRuleElement), e);
107: }
108: return builder.build(validationRuleElement);
109: }
110:
111: public Convertor createConvertor(String dataTypeName,
112: Element convertorElement) throws Exception {
113: DatatypeBuilder datatypeBulder = (DatatypeBuilder) typeBuilderSelector
114: .select(dataTypeName);
115: return datatypeBulder.buildConvertor(convertorElement);
116: }
117:
118: public void dispose() {
119: typeBuilderSelector.dispose();
120: validationRuleBuilderSelector.dispose();
121: }
122: }
|