1 16 package org.apache.cocoon.woody.formmodel; 17 18 import java.util.ArrayList ; 19 import java.util.Collections ; 20 import java.util.HashMap ; 21 import java.util.List ; 22 import java.util.Map ; 23 24 import org.apache.avalon.framework.CascadingException; 25 import org.apache.avalon.framework.activity.Disposable; 26 import org.apache.avalon.framework.service.ServiceException; 27 import org.apache.avalon.framework.service.ServiceManager; 28 import org.apache.avalon.framework.service.ServiceSelector; 29 import org.apache.avalon.framework.service.Serviceable; 30 import org.apache.cocoon.woody.Constants; 31 import org.apache.cocoon.woody.datatype.DatatypeManager; 32 import org.apache.cocoon.woody.event.WidgetListener; 33 import org.apache.cocoon.woody.event.WidgetListenerBuilderUtil; 34 import org.apache.cocoon.woody.expression.ExpressionManager; 35 import org.apache.cocoon.woody.util.DomHelper; 36 import org.apache.cocoon.woody.validation.WidgetValidatorBuilder; 37 import org.apache.excalibur.xml.sax.XMLizable; 38 import org.w3c.dom.Element ; 39 import org.w3c.dom.Node ; 40 import org.w3c.dom.NodeList ; 41 42 48 public abstract class AbstractWidgetDefinitionBuilder implements WidgetDefinitionBuilder, Serviceable, Disposable { 49 protected ServiceSelector widgetDefinitionBuilderSelector; 50 protected ServiceSelector widgetValidatorBuilderSelector; 51 protected DatatypeManager datatypeManager; 52 protected ExpressionManager expressionManager; 53 protected ServiceManager serviceManager; 54 55 public void service(ServiceManager serviceManager) throws ServiceException { 56 this.serviceManager = serviceManager; 57 this.widgetDefinitionBuilderSelector = (ServiceSelector) serviceManager.lookup(WidgetDefinitionBuilder.class.getName() + "Selector"); 58 this.datatypeManager = (DatatypeManager) serviceManager.lookup(DatatypeManager.ROLE); 59 this.expressionManager = (ExpressionManager) serviceManager.lookup(ExpressionManager.ROLE); 60 this.widgetValidatorBuilderSelector = (ServiceSelector) serviceManager.lookup(WidgetValidatorBuilder.ROLE + "Selector"); 61 } 62 63 protected void setLocation(Element widgetElement, AbstractWidgetDefinition widgetDefinition) { 64 widgetDefinition.setLocation(DomHelper.getLocation(widgetElement)); 65 } 66 67 protected void setId(Element widgetElement, AbstractWidgetDefinition widgetDefinition) throws Exception { 68 String id = DomHelper.getAttribute(widgetElement, "id"); 69 if (id.length() < 1) { 70 throw new Exception ("Missing id attribute on element '" + widgetElement.getTagName() + "' at " + 71 DomHelper.getLocation(widgetElement)); 72 } 73 widgetDefinition.setId(id); 74 } 75 76 protected WidgetDefinition buildAnotherWidgetDefinition(Element widgetDefinition) throws Exception { 77 String widgetName = widgetDefinition.getLocalName(); 78 WidgetDefinitionBuilder builder = null; 79 try { 80 builder = (WidgetDefinitionBuilder)widgetDefinitionBuilderSelector.select(widgetName); 81 } catch (ServiceException e) { 82 throw new CascadingException("Unknown kind of widget '" + widgetName + "' at " + 83 DomHelper.getLocation(widgetDefinition), e); 84 } 85 return builder.buildWidgetDefinition(widgetDefinition); 86 } 87 88 protected List buildEventListeners(Element widgetElement, String elementName, Class listenerClass) throws Exception { 89 List result = null; 90 Element listenerElement = DomHelper.getChildElement(widgetElement, Constants.WD_NS, elementName); 91 if (listenerElement != null) { 92 NodeList list = listenerElement.getChildNodes(); 93 for (int i = 0; i < list.getLength(); i++) { 94 if (list.item(i).getNodeType() == Node.ELEMENT_NODE) { 95 WidgetListener listener = WidgetListenerBuilderUtil.getWidgetListener((Element )list.item(i), listenerClass); 96 if (result == null) result = new ArrayList (); 97 result.add(listener); 98 } 99 } 100 } 101 102 return result == null ? Collections.EMPTY_LIST : result; 103 } 104 105 protected void setDisplayData(Element widgetElement, AbstractWidgetDefinition widgetDefinition) throws Exception { 106 final String [] names = {"label", "help", "hint"}; 107 Map displayData = new HashMap (names.length); 108 for (int i = 0; i < names.length; i++) { 109 XMLizable data = null; 110 Element dataElement = DomHelper.getChildElement(widgetElement, Constants.WD_NS, names[i]); 111 if (dataElement != null) { 112 data = DomHelper.compileElementContent(dataElement); 113 } 114 115 displayData.put(names[i], data); 118 } 119 120 widgetDefinition.setDisplayData(displayData); 121 } 122 123 protected void setValidators(Element widgetElement, AbstractWidgetDefinition widgetDefinition) throws Exception { 124 Element validatorElement = DomHelper.getChildElement(widgetElement, Constants.WD_NS, "validation"); 125 if (validatorElement != null) { 126 NodeList list = validatorElement.getChildNodes(); 127 for (int i = 0; i < list.getLength(); i++) { 128 if (list.item(i).getNodeType() == Node.ELEMENT_NODE) { 129 Element element = (Element )list.item(i); 130 String name = element.getLocalName(); 131 WidgetValidatorBuilder builder; 132 try { 133 builder = (WidgetValidatorBuilder)this.widgetValidatorBuilderSelector.select(name); 134 } catch(ServiceException e) { 135 throw new CascadingException("Unknow kind of validator '" + name + "' at " + 136 DomHelper.getLocation(element), e); 137 } 138 139 widgetDefinition.addValidator(builder.build(element, widgetDefinition)); 140 } 141 } 142 } 143 } 144 145 public void dispose() { 146 this.serviceManager.release(this.widgetDefinitionBuilderSelector); 147 this.widgetDefinitionBuilderSelector = null; 148 this.serviceManager.release(this.datatypeManager); 149 this.datatypeManager = null; 150 this.serviceManager.release(this.expressionManager); 151 this.expressionManager = null; 152 this.serviceManager.release(this.widgetValidatorBuilderSelector); 153 this.widgetValidatorBuilderSelector = null; 154 this.serviceManager = null; 155 } 156 } 157 | Popular Tags |