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.typeimpl;
018:
019: import org.apache.avalon.framework.configuration.Configurable;
020: import org.apache.avalon.framework.configuration.Configuration;
021: import org.apache.avalon.framework.configuration.ConfigurationException;
022: import org.apache.avalon.framework.service.ServiceException;
023: import org.apache.avalon.framework.service.ServiceManager;
024: import org.apache.avalon.framework.service.Serviceable;
025: import org.apache.cocoon.woody.Constants;
026: import org.apache.cocoon.woody.datatype.DatatypeBuilder;
027: import org.apache.cocoon.woody.datatype.DatatypeManager;
028: import org.apache.cocoon.woody.datatype.ValidationRule;
029: import org.apache.cocoon.woody.datatype.convertor.Convertor;
030: import org.apache.cocoon.woody.datatype.convertor.ConvertorBuilder;
031: import org.apache.cocoon.woody.util.DomHelper;
032: import org.apache.cocoon.woody.util.SimpleServiceSelector;
033: import org.w3c.dom.Element;
034:
035: /**
036: * Abstract base class for datatype builders, most concrete datatype builders
037: * will derive from this class.
038: * @version $Id: AbstractDatatypeBuilder.java 433543 2006-08-22 06:22:54Z crossley $
039: */
040: public abstract class AbstractDatatypeBuilder implements
041: DatatypeBuilder, Serviceable, Configurable {
042: protected ServiceManager serviceManager;
043: private SimpleServiceSelector convertorBuilders;
044: private String defaultConvertorHint;
045: private Convertor plainConvertor;
046:
047: public void service(ServiceManager serviceManager)
048: throws ServiceException {
049: this .serviceManager = serviceManager;
050: }
051:
052: public void configure(Configuration configuration)
053: throws ConfigurationException {
054: convertorBuilders = new SimpleServiceSelector("convertor",
055: ConvertorBuilder.class);
056: Configuration convertorsConf = configuration
057: .getChild("convertors");
058: convertorBuilders.configure(convertorsConf);
059: defaultConvertorHint = convertorsConf.getAttribute("default");
060:
061: String plainConvertorHint = convertorsConf
062: .getAttribute("plain");
063: ConvertorBuilder plainConvertorBuilder;
064: try {
065: plainConvertorBuilder = (ConvertorBuilder) convertorBuilders
066: .select(plainConvertorHint);
067: } catch (ServiceException e) {
068: throw new ConfigurationException(
069: "Convertor defined in plain attribute unavailable.",
070: e);
071: }
072:
073: try {
074: plainConvertor = plainConvertorBuilder.build(null);
075: } catch (Exception e) {
076: throw new ConfigurationException(
077: "Error create plain convertor.", e);
078: }
079: }
080:
081: public void buildConvertor(Element datatypeEl,
082: AbstractDatatype datatype) throws Exception {
083: Element convertorEl = DomHelper.getChildElement(datatypeEl,
084: Constants.WD_NS, "convertor", false);
085: Convertor convertor = buildConvertor(convertorEl);
086: datatype.setConvertor(convertor);
087: }
088:
089: public Convertor buildConvertor(Element convertorEl)
090: throws Exception {
091: String type = null;
092: // convertor configuration is allowed to be null, so check that it is not null
093: if (convertorEl != null)
094: type = convertorEl.getAttribute("type");
095: if (type == null || type.length() == 0)
096: type = defaultConvertorHint;
097: ConvertorBuilder convertorBuilder = (ConvertorBuilder) convertorBuilders
098: .select(type);
099: return convertorBuilder.build(convertorEl);
100: }
101:
102: public Convertor getPlainConvertor() {
103: return plainConvertor;
104: }
105:
106: protected void buildValidationRules(Element datatypeElement,
107: AbstractDatatype datatype, DatatypeManager datatypeManager)
108: throws Exception {
109: Element validationElement = DomHelper.getChildElement(
110: datatypeElement, Constants.WD_NS, "validation");
111: if (validationElement != null) {
112: Element[] validationElements = DomHelper.getChildElements(
113: validationElement, Constants.WD_NS);
114: for (int i = 0; i < validationElements.length; i++) {
115: ValidationRule rule = datatypeManager
116: .createValidationRule(validationElements[i]);
117: if (rule.supportsType(datatype.getTypeClass(), datatype
118: .isArrayType())) {
119: datatype.addValidationRule(rule);
120: } else {
121: throw new Exception(
122: "Validation rule \""
123: + validationElements[i]
124: .getLocalName()
125: + "\" cannot be used with strings, error at "
126: + DomHelper
127: .getLocation(validationElements[i]));
128: }
129: }
130: }
131: }
132: }
|