KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > woody > 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.woody.formmodel;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Collections JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
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 JavaDoc;
39 import org.w3c.dom.Node JavaDoc;
40 import org.w3c.dom.NodeList JavaDoc;
41
42 /**
43  * Abstract base class for WidgetDefinitionBuilders. Provides functionality
44  * common to many implementations.
45  *
46  * @version $Id: AbstractWidgetDefinitionBuilder.java 36537 2004-08-17 20:36:47Z vgritsenko $
47  */

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 JavaDoc widgetElement, AbstractWidgetDefinition widgetDefinition) {
64         widgetDefinition.setLocation(DomHelper.getLocation(widgetElement));
65     }
66
67     protected void setId(Element JavaDoc widgetElement, AbstractWidgetDefinition widgetDefinition) throws Exception JavaDoc {
68         String JavaDoc id = DomHelper.getAttribute(widgetElement, "id");
69         if (id.length() < 1) {
70             throw new Exception JavaDoc("Missing id attribute on element '" + widgetElement.getTagName() + "' at " +
71                                 DomHelper.getLocation(widgetElement));
72         }
73         widgetDefinition.setId(id);
74     }
75
76     protected WidgetDefinition buildAnotherWidgetDefinition(Element JavaDoc widgetDefinition) throws Exception JavaDoc {
77         String JavaDoc 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 JavaDoc buildEventListeners(Element JavaDoc widgetElement, String JavaDoc elementName, Class JavaDoc listenerClass) throws Exception JavaDoc {
89         List JavaDoc result = null;
90         Element JavaDoc listenerElement = DomHelper.getChildElement(widgetElement, Constants.WD_NS, elementName);
91         if (listenerElement != null) {
92             NodeList JavaDoc 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 JavaDoc)list.item(i), listenerClass);
96                     if (result == null) result = new ArrayList JavaDoc();
97                     result.add(listener);
98                 }
99             }
100         }
101
102         return result == null ? Collections.EMPTY_LIST : result;
103     }
104
105     protected void setDisplayData(Element JavaDoc widgetElement, AbstractWidgetDefinition widgetDefinition) throws Exception JavaDoc {
106         final String JavaDoc[] names = {"label", "help", "hint"};
107         Map JavaDoc displayData = new HashMap JavaDoc(names.length);
108         for (int i = 0; i < names.length; i++) {
109             XMLizable data = null;
110             Element JavaDoc dataElement = DomHelper.getChildElement(widgetElement, Constants.WD_NS, names[i]);
111             if (dataElement != null) {
112                 data = DomHelper.compileElementContent(dataElement);
113             }
114
115             // NOTE: We put also null values in the may in order to test their existence
116
// (see AbstractWidgetDefinition.generateDisplayData)
117
displayData.put(names[i], data);
118         }
119
120         widgetDefinition.setDisplayData(displayData);
121     }
122
123     protected void setValidators(Element JavaDoc widgetElement, AbstractWidgetDefinition widgetDefinition) throws Exception JavaDoc {
124         Element JavaDoc validatorElement = DomHelper.getChildElement(widgetElement, Constants.WD_NS, "validation");
125         if (validatorElement != null) {
126             NodeList JavaDoc list = validatorElement.getChildNodes();
127             for (int i = 0; i < list.getLength(); i++) {
128                 if (list.item(i).getNodeType() == Node.ELEMENT_NODE) {
129                     Element JavaDoc element = (Element JavaDoc)list.item(i);
130                     String JavaDoc 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