KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > woody > datatype > typeimpl > AbstractDatatypeBuilder


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.typeimpl;
17
18 import org.apache.avalon.framework.configuration.Configurable;
19 import org.apache.avalon.framework.configuration.Configuration;
20 import org.apache.avalon.framework.configuration.ConfigurationException;
21 import org.apache.avalon.framework.service.ServiceException;
22 import org.apache.avalon.framework.service.ServiceManager;
23 import org.apache.avalon.framework.service.Serviceable;
24 import org.apache.cocoon.woody.Constants;
25 import org.apache.cocoon.woody.datatype.DatatypeBuilder;
26 import org.apache.cocoon.woody.datatype.DatatypeManager;
27 import org.apache.cocoon.woody.datatype.ValidationRule;
28 import org.apache.cocoon.woody.datatype.convertor.Convertor;
29 import org.apache.cocoon.woody.datatype.convertor.ConvertorBuilder;
30 import org.apache.cocoon.woody.util.DomHelper;
31 import org.apache.cocoon.woody.util.SimpleServiceSelector;
32 import org.w3c.dom.Element JavaDoc;
33
34 /**
35  * Abstract base class for datatype builders, most concrete datatype builders
36  * will derive from this class.
37  * @version $Id: AbstractDatatypeBuilder.java 30932 2004-07-29 17:35:38Z vgritsenko $
38  */

39 public abstract class AbstractDatatypeBuilder implements DatatypeBuilder, Serviceable, Configurable {
40     protected ServiceManager serviceManager;
41     private SimpleServiceSelector convertorBuilders;
42     private String JavaDoc defaultConvertorHint;
43     private Convertor plainConvertor;
44
45     public void service(ServiceManager serviceManager) throws ServiceException {
46         this.serviceManager = serviceManager;
47     }
48
49     public void configure(Configuration configuration) throws ConfigurationException {
50         convertorBuilders = new SimpleServiceSelector("convertor", ConvertorBuilder.class);
51         Configuration convertorsConf = configuration.getChild("convertors");
52         convertorBuilders.configure(convertorsConf);
53         defaultConvertorHint = convertorsConf.getAttribute("default");
54
55         String JavaDoc plainConvertorHint = convertorsConf.getAttribute("plain");
56         ConvertorBuilder plainConvertorBuilder;
57         try {
58             plainConvertorBuilder = (ConvertorBuilder)convertorBuilders.select(plainConvertorHint);
59         } catch (ServiceException e) {
60             throw new ConfigurationException("Convertor defined in plain attribute unavailable.", e);
61         }
62
63         try {
64             plainConvertor = plainConvertorBuilder.build(null);
65         } catch (Exception JavaDoc e) {
66             throw new ConfigurationException("Error create plain convertor.", e);
67         }
68     }
69
70     public void buildConvertor(Element JavaDoc datatypeEl, AbstractDatatype datatype) throws Exception JavaDoc {
71         Element JavaDoc convertorEl = DomHelper.getChildElement(datatypeEl, Constants.WD_NS, "convertor", false);
72         Convertor convertor = buildConvertor(convertorEl);
73         datatype.setConvertor(convertor);
74     }
75
76     public Convertor buildConvertor(Element JavaDoc convertorEl) throws Exception JavaDoc {
77         String JavaDoc type = null;
78         // convertor configuration is allowed to be null, so check that it is not null
79
if (convertorEl != null)
80             type = convertorEl.getAttribute("type");
81         if (type == null || type.equals(""))
82             type = defaultConvertorHint;
83         ConvertorBuilder convertorBuilder = (ConvertorBuilder)convertorBuilders.select(type);
84         return convertorBuilder.build(convertorEl);
85     }
86
87     public Convertor getPlainConvertor() {
88         return plainConvertor;
89     }
90
91     protected void buildValidationRules(Element JavaDoc datatypeElement, AbstractDatatype datatype, DatatypeManager datatypeManager) throws Exception JavaDoc {
92         Element JavaDoc validationElement = DomHelper.getChildElement(datatypeElement, Constants.WD_NS, "validation");
93         if (validationElement != null) {
94             Element JavaDoc[] validationElements = DomHelper.getChildElements(validationElement, Constants.WD_NS);
95             for (int i = 0; i < validationElements.length; i++) {
96                 ValidationRule rule = datatypeManager.createValidationRule(validationElements[i]);
97                 if (rule.supportsType(datatype.getTypeClass(), datatype.isArrayType())) {
98                     datatype.addValidationRule(rule);
99                 } else {
100                     throw new Exception JavaDoc("Validation rule \"" + validationElements[i].getLocalName() + "\" cannot be used with strings, error at " + DomHelper.getLocation(validationElements[i]));
101                 }
102             }
103         }
104     }
105 }
106
Popular Tags