1 16 package org.apache.cocoon.woody; 17 18 import org.apache.avalon.framework.CascadingException; 19 import org.apache.avalon.framework.activity.Disposable; 20 import org.apache.avalon.framework.activity.Initializable; 21 import org.apache.avalon.framework.component.Component; 22 import org.apache.avalon.framework.configuration.Configurable; 23 import org.apache.avalon.framework.configuration.Configuration; 24 import org.apache.avalon.framework.configuration.ConfigurationException; 25 import org.apache.avalon.framework.logger.AbstractLogEnabled; 26 import org.apache.avalon.framework.service.ServiceException; 27 import org.apache.avalon.framework.service.ServiceManager; 28 import org.apache.avalon.framework.service.Serviceable; 29 import org.apache.avalon.framework.thread.ThreadSafe; 30 import org.apache.cocoon.woody.formmodel.Form; 31 import org.apache.cocoon.woody.formmodel.FormDefinition; 32 import org.apache.cocoon.woody.formmodel.FormDefinitionBuilder; 33 import org.apache.cocoon.woody.formmodel.WidgetDefinitionBuilder; 34 import org.apache.cocoon.woody.util.DomHelper; 35 import org.apache.cocoon.woody.util.SimpleServiceSelector; 36 import org.apache.excalibur.source.Source; 37 import org.w3c.dom.Document ; 38 import org.w3c.dom.Element ; 39 import org.xml.sax.InputSource ; 40 41 46 public class DefaultFormManager 47 extends AbstractLogEnabled 48 implements FormManager, ThreadSafe, Serviceable, Disposable, Configurable, Component, Initializable { 49 50 protected static final String PREFIX = "WoodyForm:"; 51 protected ServiceManager manager; 52 protected Configuration configuration; 53 protected SimpleServiceSelector widgetDefinitionBuilderSelector; 54 protected CacheManager cacheManager; 55 56 public void service(ServiceManager serviceManager) throws ServiceException { 57 this.manager = serviceManager; 58 this.cacheManager = (CacheManager)serviceManager.lookup(CacheManager.ROLE); 59 } 60 61 64 public void configure(Configuration configuration) throws ConfigurationException { 65 this.configuration = configuration; 66 } 67 68 public void initialize() throws Exception { 69 widgetDefinitionBuilderSelector = new SimpleServiceSelector("widget", WidgetDefinitionBuilder.class); 70 widgetDefinitionBuilderSelector.service(new ServiceManager() { 71 final String WIDGET_DEFINITION_BUILDER_SELECTOR_ROLE = WidgetDefinitionBuilder.class.getName() + "Selector"; 72 73 public Object lookup(String name) throws ServiceException { 74 if (WIDGET_DEFINITION_BUILDER_SELECTOR_ROLE.equals(name)) 75 return widgetDefinitionBuilderSelector; 76 else 77 return manager.lookup(name); 78 } 79 80 public boolean hasService(String name) { 81 if (WIDGET_DEFINITION_BUILDER_SELECTOR_ROLE.equals(name)) 82 return true; 83 else 84 return manager.hasService(name); 85 } 86 87 public void release(Object service) { 88 if (service != widgetDefinitionBuilderSelector) 89 manager.release(service); 90 } 91 }); 92 widgetDefinitionBuilderSelector.configure(configuration.getChild("widgets")); 93 } 94 95 public Form createForm(Source source) throws Exception { 96 FormDefinition formDefinition = getFormDefinition(source); 97 return (Form)formDefinition.createInstance(); 98 } 99 100 public FormDefinition getFormDefinition(Source source) throws Exception { 101 FormDefinition formDefinition = (FormDefinition)this.cacheManager.get(source, PREFIX); 102 if (formDefinition == null) { 103 Document formDocument; 104 try { 105 InputSource inputSource = new InputSource (source.getInputStream()); 106 inputSource.setSystemId(source.getURI()); 107 formDocument = DomHelper.parse(inputSource); 108 } catch (Exception e) { 109 throw new CascadingException("Could not parse form definition from " + 110 source.getURI(), e); 111 } 112 113 Element formElement = formDocument.getDocumentElement(); 114 115 if (!(formElement.getLocalName().equals("form") || Constants.WD_NS.equals(formElement.getNamespaceURI()))) 117 throw new Exception ("Expected a Woody form element at " + DomHelper.getLocation(formElement)); 118 119 FormDefinitionBuilder formDefinitionBuilder = (FormDefinitionBuilder)widgetDefinitionBuilderSelector.select("form"); 120 formDefinition = (FormDefinition)formDefinitionBuilder.buildWidgetDefinition(formElement); 121 this.cacheManager.set(formDefinition, source, PREFIX); 122 } 123 return formDefinition; 124 } 125 126 129 public void dispose() { 130 if (this.widgetDefinitionBuilderSelector != null) { 131 this.widgetDefinitionBuilderSelector.dispose(); 132 this.widgetDefinitionBuilderSelector = null; 133 } 134 this.manager.release(this.cacheManager); 135 this.cacheManager = null; 136 this.manager = null; 137 } 138 } 139 | Popular Tags |