KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > formmodel > AbstractWidgetDefinitionBuilder


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.forms.formmodel;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Collections JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.apache.avalon.framework.CascadingException;
26 import org.apache.avalon.framework.activity.Disposable;
27 import org.apache.avalon.framework.service.ServiceException;
28 import org.apache.avalon.framework.service.ServiceManager;
29 import org.apache.avalon.framework.service.ServiceSelector;
30 import org.apache.avalon.framework.service.Serviceable;
31 import org.apache.cocoon.forms.FormsConstants;
32 import org.apache.cocoon.forms.datatype.DatatypeManager;
33 import org.apache.cocoon.forms.event.CreateListener;
34 import org.apache.cocoon.forms.event.WidgetListener;
35 import org.apache.cocoon.forms.event.WidgetListenerBuilder;
36 import org.apache.cocoon.forms.expression.ExpressionManager;
37 import org.apache.cocoon.forms.util.DomHelper;
38 import org.apache.cocoon.forms.validation.WidgetValidatorBuilder;
39 import org.apache.excalibur.xml.sax.XMLizable;
40 import org.w3c.dom.Element JavaDoc;
41 import org.w3c.dom.Node JavaDoc;
42 import org.w3c.dom.NodeList JavaDoc;
43
44 /**
45  * Abstract base class for WidgetDefinitionBuilders. Provides functionality
46  * common to many implementations.
47  *
48  * @version $Id: AbstractWidgetDefinitionBuilder.java 330531 2005-11-03 12:24:03Z sylvain $
49  */

50 public abstract class AbstractWidgetDefinitionBuilder implements WidgetDefinitionBuilder, Serviceable, Disposable {
51     protected ServiceSelector widgetDefinitionBuilderSelector;
52     protected ServiceSelector widgetValidatorBuilderSelector;
53     protected ServiceSelector widgetListenerBuilderSelector;
54     protected DatatypeManager datatypeManager;
55     protected ExpressionManager expressionManager;
56     protected ServiceManager serviceManager;
57     
58     protected WidgetDefinitionBuilderContext context = null;
59     
60     public WidgetDefinition buildWidgetDefinition(Element JavaDoc widgetElement, WidgetDefinitionBuilderContext context) throws Exception JavaDoc {
61         // so changes don't pollute upper levels
62
this.context = new WidgetDefinitionBuilderContext(context);
63         
64         
65         WidgetDefinition def = buildWidgetDefinition(widgetElement);
66         
67         // register this class with the local library, if any.
68
if(DomHelper.getAttributeAsBoolean(widgetElement,"register",false) && this.context!=null && this.context.getLocalLibrary()!=null) {
69             this.context.getLocalLibrary().addDefinition(def);
70         }
71         
72         this.context = null;
73         return def;
74     }
75
76     public void service(ServiceManager serviceManager) throws ServiceException {
77         this.serviceManager = serviceManager;
78         this.widgetDefinitionBuilderSelector = (ServiceSelector) serviceManager.lookup(WidgetDefinitionBuilder.class.getName() + "Selector");
79         this.datatypeManager = (DatatypeManager) serviceManager.lookup(DatatypeManager.ROLE);
80         this.expressionManager = (ExpressionManager) serviceManager.lookup(ExpressionManager.ROLE);
81         this.widgetValidatorBuilderSelector = (ServiceSelector) serviceManager.lookup(WidgetValidatorBuilder.ROLE + "Selector");
82         this.widgetListenerBuilderSelector = (ServiceSelector) serviceManager.lookup(WidgetListenerBuilder.ROLE + "Selector");
83     }
84     
85     protected void setupDefinition(Element JavaDoc widgetElement, AbstractWidgetDefinition definition) throws Exception JavaDoc {
86         
87         // location
88
definition.setLocation(DomHelper.getLocationObject(widgetElement));
89         
90         
91         if(this.context.getSuperDefinition()!=null)
92             definition.initializeFrom(this.context.getSuperDefinition());
93         
94         setCommonProperties(widgetElement, definition);
95         setValidators(widgetElement, definition);
96         setCreateListeners(widgetElement, definition);
97     }
98
99     private void setCommonProperties(Element JavaDoc widgetElement, AbstractWidgetDefinition widgetDefinition) throws Exception JavaDoc {
100         
101         // id
102
if (widgetDefinition instanceof FormDefinition) {
103             // FormDefinition is the *only* kind of widget that has an optional id
104
widgetDefinition.setId(DomHelper.getAttribute(widgetElement, "id", ""));
105         } else {
106             String JavaDoc id = DomHelper.getAttribute(widgetElement, "id");
107             if (id.length() < 1) {
108                 throw new Exception JavaDoc("Missing id attribute on element '" + widgetElement.getTagName() + "' at " +
109                                     DomHelper.getLocation(widgetElement));
110             }
111             if (id.indexOf('/') != -1 || id.indexOf('.') != -1) {
112                 throw new Exception JavaDoc("A widget name cannot contain '.' or '/' as this conflicts with widget paths, at " +
113                         DomHelper.getLocation(widgetElement));
114             }
115             if (id.indexOf(':') != -1) {
116                 throw new Exception JavaDoc("A widget name cannot contain ':' as this conflicts with library prefixes, at " +
117                         DomHelper.getLocation(widgetElement));
118             }
119             widgetDefinition.setId(id);
120         }
121
122         // state
123
String JavaDoc stateValue = DomHelper.getAttribute(widgetElement, "state", null);
124         if (stateValue != null) {
125             WidgetState state = WidgetState.stateForName(stateValue);
126             if (state == null) {
127                 throw new Exception JavaDoc ("Unknow value '" + stateValue +"' for state attribute at " +
128                         DomHelper.getLocation(widgetElement));
129             }
130             widgetDefinition.setState(state);
131         }
132
133         // attributes
134
Element JavaDoc attrContainer = DomHelper.getChildElement(widgetElement, FormsConstants.DEFINITION_NS, "attributes", false);
135         if (attrContainer != null) {
136             // There's a <fd:attributes> element. Get its <fd:attribute> children
137
Element JavaDoc[] attrs = DomHelper.getChildElements(attrContainer, FormsConstants.DEFINITION_NS, "attribute");
138             if (attrs != null && attrs.length > 0) {
139                 // We actually do have some
140
Map JavaDoc attrMap = new HashMap JavaDoc();
141                 for (int i = 0; i < attrs.length; i++) {
142                     attrMap.put(DomHelper.getAttribute(attrs[i], "name"), DomHelper.getAttribute(attrs[i], "value"));
143                 }
144                 widgetDefinition.setAttributes(attrMap);
145             }
146         }
147     }
148
149     protected WidgetDefinition buildAnotherWidgetDefinition(Element JavaDoc widgetDefinition) throws Exception JavaDoc {
150         String JavaDoc widgetName = widgetDefinition.getLocalName();
151         WidgetDefinitionBuilder builder = null;
152         try {
153             builder = (WidgetDefinitionBuilder)widgetDefinitionBuilderSelector.select(widgetName);
154         } catch (ServiceException e) {
155             throw new CascadingException("Unknown kind of widget '" + widgetName + "' at " +
156                                          DomHelper.getLocation(widgetDefinition), e);
157         }
158         
159         
160         return builder.buildWidgetDefinition(widgetDefinition, this.context);
161     }
162
163     protected List JavaDoc buildEventListeners(Element JavaDoc widgetElement, String JavaDoc elementName, Class JavaDoc listenerClass) throws Exception JavaDoc {
164         List JavaDoc result = null;
165         Element JavaDoc listenersElement = DomHelper.getChildElement(widgetElement, FormsConstants.DEFINITION_NS, elementName);
166         if (listenersElement != null) {
167             NodeList JavaDoc list = listenersElement.getChildNodes();
168             for (int i = 0; i < list.getLength(); i++) {
169                 if (list.item(i).getNodeType() == Node.ELEMENT_NODE) {
170                     Element JavaDoc listenerElement = (Element JavaDoc)list.item(i);
171                     WidgetListenerBuilder listenerBuilder = null;
172                     try {
173                         listenerBuilder = (WidgetListenerBuilder)widgetListenerBuilderSelector.select(listenerElement.getLocalName());
174                     } catch (ServiceException e) {
175                         throw new CascadingException("Unknown kind of eventlistener '" + listenerElement.getLocalName()
176                                 + "' at " + DomHelper.getLocation(listenerElement), e);
177                     }
178                     WidgetListener listener = listenerBuilder.buildListener(listenerElement, listenerClass);
179                     widgetListenerBuilderSelector.release(listenerBuilder);
180                     if (result == null) result = new ArrayList JavaDoc();
181                     result.add(listener);
182                 }
183             }
184         }
185
186         return result == null ? Collections.EMPTY_LIST : result;
187     }
188
189     protected void setDisplayData(Element JavaDoc widgetElement, AbstractWidgetDefinition widgetDefinition) throws Exception JavaDoc {
190         final String JavaDoc[] names = {"label", "help", "hint"};
191         Map JavaDoc displayData = new HashMap JavaDoc(names.length);
192         for (int i = 0; i < names.length; i++) {
193             XMLizable data = null;
194             Element JavaDoc dataElement = DomHelper.getChildElement(widgetElement, FormsConstants.DEFINITION_NS, names[i]);
195             if (dataElement != null) {
196                 data = DomHelper.compileElementContent(dataElement);
197             }
198
199             // NOTE: We put also null values in the may in order to test their existence
200
// (see AbstractWidgetDefinition.generateDisplayData)
201
displayData.put(names[i], data);
202         }
203
204         widgetDefinition.setDisplayData(displayData);
205     }
206
207     private void setValidators(Element JavaDoc widgetElement, AbstractWidgetDefinition widgetDefinition) throws Exception JavaDoc {
208         Element JavaDoc validatorElement = DomHelper.getChildElement(widgetElement, FormsConstants.DEFINITION_NS, "validation");
209         if (validatorElement != null) {
210             NodeList JavaDoc list = validatorElement.getChildNodes();
211             for (int i = 0; i < list.getLength(); i++) {
212                 if (list.item(i).getNodeType() == Node.ELEMENT_NODE) {
213                     Element JavaDoc element = (Element JavaDoc)list.item(i);
214                     String JavaDoc name = element.getLocalName();
215                     WidgetValidatorBuilder builder;
216                     try {
217                         builder = (WidgetValidatorBuilder)this.widgetValidatorBuilderSelector.select(name);
218                     } catch(ServiceException e) {
219                         throw new CascadingException("Unknow kind of validator '" + name + "' at " +
220                                                      DomHelper.getLocation(element), e);
221                     }
222
223                     widgetDefinition.addValidator(builder.build(element, widgetDefinition));
224                     widgetValidatorBuilderSelector.release(builder);
225                 }
226             }
227         }
228     }
229
230     private void setCreateListeners(Element JavaDoc widgetElement, AbstractWidgetDefinition widgetDefinition) throws Exception JavaDoc {
231         Iterator JavaDoc iter = buildEventListeners(widgetElement, "on-create", CreateListener.class).iterator();
232         while (iter.hasNext()) {
233             widgetDefinition.addCreateListener((CreateListener)iter.next());
234         }
235     }
236
237     public void dispose() {
238         this.serviceManager.release(this.widgetDefinitionBuilderSelector);
239         this.widgetDefinitionBuilderSelector = null;
240         this.serviceManager.release(this.datatypeManager);
241         this.datatypeManager = null;
242         this.serviceManager.release(this.expressionManager);
243         this.expressionManager = null;
244         this.serviceManager.release(this.widgetValidatorBuilderSelector);
245         this.widgetValidatorBuilderSelector = null;
246         this.serviceManager = null;
247     }
248 }
249
Popular Tags