1 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 ; 33 34 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 { 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 datatypeElement, boolean arrayType) throws Exception { 73 String 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 validationRuleElement) throws Exception { 84 String 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 dataTypeName, Element convertorElement) throws Exception { 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 |