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.forms.datatype.typeimpl;
018:
019: import org.apache.avalon.framework.activity.Disposable;
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.service.ServiceException;
024: import org.apache.avalon.framework.service.ServiceManager;
025: import org.apache.avalon.framework.service.Serviceable;
026: import org.apache.avalon.framework.context.Contextualizable;
027: import org.apache.avalon.framework.context.Context;
028: import org.apache.avalon.framework.context.ContextException;
029: import org.apache.avalon.framework.logger.AbstractLogEnabled;
030: import org.apache.cocoon.forms.FormsConstants;
031: import org.apache.cocoon.forms.datatype.DatatypeBuilder;
032: import org.apache.cocoon.forms.datatype.DatatypeManager;
033: import org.apache.cocoon.forms.datatype.ValidationRule;
034: import org.apache.cocoon.forms.datatype.convertor.Convertor;
035: import org.apache.cocoon.forms.datatype.convertor.ConvertorBuilder;
036: import org.apache.cocoon.forms.util.DomHelper;
037: import org.apache.cocoon.forms.util.SimpleServiceSelector;
038: import org.w3c.dom.Element;
039:
040: /**
041: * Abstract base class for datatype builders, most concrete datatype builders
042: * will derive from this class.
043: * @version $Id: AbstractDatatypeBuilder.java 449149 2006-09-23 03:58:05Z crossley $
044: */
045: public abstract class AbstractDatatypeBuilder extends
046: AbstractLogEnabled implements DatatypeBuilder, Serviceable,
047: Configurable, Contextualizable, Disposable {
048: protected ServiceManager serviceManager;
049: private SimpleServiceSelector convertorBuilders;
050: private String defaultConvertorHint;
051: private Convertor plainConvertor;
052: private Context context;
053:
054: public void service(ServiceManager serviceManager)
055: throws ServiceException {
056: this .serviceManager = serviceManager;
057: }
058:
059: public void contextualize(Context context) throws ContextException {
060: this .context = context;
061: }
062:
063: public void configure(Configuration configuration)
064: throws ConfigurationException {
065: convertorBuilders = new SimpleServiceSelector("convertor",
066: ConvertorBuilder.class);
067: try {
068: convertorBuilders.enableLogging(getLogger());
069: convertorBuilders.contextualize(context);
070: convertorBuilders.service(serviceManager);
071: } catch (Exception e) {
072: throw new ConfigurationException(
073: "Error setting up convertor builder selector.", e);
074: }
075: Configuration convertorsConf = configuration
076: .getChild("convertors");
077: convertorBuilders.configure(convertorsConf);
078: defaultConvertorHint = convertorsConf.getAttribute("default");
079:
080: String plainConvertorHint = convertorsConf
081: .getAttribute("plain");
082: ConvertorBuilder plainConvertorBuilder;
083: try {
084: plainConvertorBuilder = (ConvertorBuilder) convertorBuilders
085: .select(plainConvertorHint);
086: } catch (ServiceException e) {
087: throw new ConfigurationException(
088: "Convertor defined in plain attribute unavailable.",
089: e);
090: }
091:
092: try {
093: plainConvertor = plainConvertorBuilder.build(null);
094: } catch (Exception e) {
095: throw new ConfigurationException(
096: "Error create plain convertor.", e);
097: }
098: }
099:
100: public void dispose() {
101: convertorBuilders.dispose();
102: }
103:
104: public void buildConvertor(Element datatypeEl,
105: AbstractDatatype datatype) throws Exception {
106: Element convertorEl = DomHelper.getChildElement(datatypeEl,
107: FormsConstants.DEFINITION_NS, "convertor", false);
108: Convertor convertor = buildConvertor(convertorEl);
109: datatype.setConvertor(convertor);
110: }
111:
112: public Convertor buildConvertor(Element convertorEl)
113: throws Exception {
114: String type = null;
115: // convertor configuration is allowed to be null, so check that it is not null
116: if (convertorEl != null)
117: type = convertorEl.getAttribute("type");
118: if (type == null || type.length() == 0)
119: type = defaultConvertorHint;
120: ConvertorBuilder convertorBuilder = (ConvertorBuilder) convertorBuilders
121: .select(type);
122: return convertorBuilder.build(convertorEl);
123: }
124:
125: public Convertor getPlainConvertor() {
126: return plainConvertor;
127: }
128:
129: protected void buildValidationRules(Element datatypeElement,
130: AbstractDatatype datatype, DatatypeManager datatypeManager)
131: throws Exception {
132: Element validationElement = DomHelper.getChildElement(
133: datatypeElement, FormsConstants.DEFINITION_NS,
134: "validation");
135: if (validationElement != null) {
136: Element[] validationElements = DomHelper.getChildElements(
137: validationElement, FormsConstants.DEFINITION_NS);
138: for (int i = 0; i < validationElements.length; i++) {
139: ValidationRule rule = datatypeManager
140: .createValidationRule(validationElements[i]);
141: if (rule.supportsType(datatype.getTypeClass(), datatype
142: .isArrayType())) {
143: datatype.addValidationRule(rule);
144: } else {
145: throw new Exception("Validation rule \""
146: + validationElements[i].getLocalName()
147: + "\" cannot be used with type <"
148: + (datatype.isArrayType() ? "array of "
149: : "")
150: + datatype.getTypeClass().getName()
151: + ">, error at "
152: + DomHelper
153: .getLocation(validationElements[i]));
154: }
155: }
156: }
157: }
158: }
|