001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.woody.formmodel;
018:
019: import java.util.ArrayList;
020: import java.util.Collections;
021: import java.util.HashMap;
022: import java.util.List;
023: import java.util.Map;
024:
025: import org.apache.avalon.framework.CascadingException;
026: import org.apache.avalon.framework.activity.Disposable;
027: import org.apache.avalon.framework.service.ServiceException;
028: import org.apache.avalon.framework.service.ServiceManager;
029: import org.apache.avalon.framework.service.ServiceSelector;
030: import org.apache.avalon.framework.service.Serviceable;
031: import org.apache.cocoon.woody.Constants;
032: import org.apache.cocoon.woody.datatype.DatatypeManager;
033: import org.apache.cocoon.woody.event.WidgetListener;
034: import org.apache.cocoon.woody.event.WidgetListenerBuilderUtil;
035: import org.apache.cocoon.woody.expression.ExpressionManager;
036: import org.apache.cocoon.woody.util.DomHelper;
037: import org.apache.cocoon.woody.validation.WidgetValidatorBuilder;
038: import org.apache.excalibur.xml.sax.XMLizable;
039: import org.w3c.dom.Element;
040: import org.w3c.dom.Node;
041: import org.w3c.dom.NodeList;
042:
043: /**
044: * Abstract base class for WidgetDefinitionBuilders. Provides functionality
045: * common to many implementations.
046: *
047: * @version $Id: AbstractWidgetDefinitionBuilder.java 433543 2006-08-22 06:22:54Z crossley $
048: */
049: public abstract class AbstractWidgetDefinitionBuilder implements
050: WidgetDefinitionBuilder, Serviceable, Disposable {
051: protected ServiceSelector widgetDefinitionBuilderSelector;
052: protected ServiceSelector widgetValidatorBuilderSelector;
053: protected DatatypeManager datatypeManager;
054: protected ExpressionManager expressionManager;
055: protected ServiceManager serviceManager;
056:
057: public void service(ServiceManager serviceManager)
058: throws ServiceException {
059: this .serviceManager = serviceManager;
060: this .widgetDefinitionBuilderSelector = (ServiceSelector) serviceManager
061: .lookup(WidgetDefinitionBuilder.class.getName()
062: + "Selector");
063: this .datatypeManager = (DatatypeManager) serviceManager
064: .lookup(DatatypeManager.ROLE);
065: this .expressionManager = (ExpressionManager) serviceManager
066: .lookup(ExpressionManager.ROLE);
067: this .widgetValidatorBuilderSelector = (ServiceSelector) serviceManager
068: .lookup(WidgetValidatorBuilder.ROLE + "Selector");
069: }
070:
071: protected void setLocation(Element widgetElement,
072: AbstractWidgetDefinition widgetDefinition) {
073: widgetDefinition.setLocation(DomHelper
074: .getLocation(widgetElement));
075: }
076:
077: protected void setId(Element widgetElement,
078: AbstractWidgetDefinition widgetDefinition) throws Exception {
079: String id = DomHelper.getAttribute(widgetElement, "id");
080: if (id.length() < 1) {
081: throw new Exception("Missing id attribute on element '"
082: + widgetElement.getTagName() + "' at "
083: + DomHelper.getLocation(widgetElement));
084: }
085: widgetDefinition.setId(id);
086: }
087:
088: protected WidgetDefinition buildAnotherWidgetDefinition(
089: Element widgetDefinition) throws Exception {
090: String widgetName = widgetDefinition.getLocalName();
091: WidgetDefinitionBuilder builder = null;
092: try {
093: builder = (WidgetDefinitionBuilder) widgetDefinitionBuilderSelector
094: .select(widgetName);
095: } catch (ServiceException e) {
096: throw new CascadingException("Unknown kind of widget '"
097: + widgetName + "' at "
098: + DomHelper.getLocation(widgetDefinition), e);
099: }
100: return builder.buildWidgetDefinition(widgetDefinition);
101: }
102:
103: protected List buildEventListeners(Element widgetElement,
104: String elementName, Class listenerClass) throws Exception {
105: List result = null;
106: Element listenerElement = DomHelper.getChildElement(
107: widgetElement, Constants.WD_NS, elementName);
108: if (listenerElement != null) {
109: NodeList list = listenerElement.getChildNodes();
110: for (int i = 0; i < list.getLength(); i++) {
111: if (list.item(i).getNodeType() == Node.ELEMENT_NODE) {
112: WidgetListener listener = WidgetListenerBuilderUtil
113: .getWidgetListener((Element) list.item(i),
114: listenerClass);
115: if (result == null)
116: result = new ArrayList();
117: result.add(listener);
118: }
119: }
120: }
121:
122: return result == null ? Collections.EMPTY_LIST : result;
123: }
124:
125: protected void setDisplayData(Element widgetElement,
126: AbstractWidgetDefinition widgetDefinition) throws Exception {
127: final String[] names = { "label", "help", "hint" };
128: Map displayData = new HashMap(names.length);
129: for (int i = 0; i < names.length; i++) {
130: XMLizable data = null;
131: Element dataElement = DomHelper.getChildElement(
132: widgetElement, Constants.WD_NS, names[i]);
133: if (dataElement != null) {
134: data = DomHelper.compileElementContent(dataElement);
135: }
136:
137: // NOTE: We put also null values in the may in order to test their existence
138: // (see AbstractWidgetDefinition.generateDisplayData)
139: displayData.put(names[i], data);
140: }
141:
142: widgetDefinition.setDisplayData(displayData);
143: }
144:
145: protected void setValidators(Element widgetElement,
146: AbstractWidgetDefinition widgetDefinition) throws Exception {
147: Element validatorElement = DomHelper.getChildElement(
148: widgetElement, Constants.WD_NS, "validation");
149: if (validatorElement != null) {
150: NodeList list = validatorElement.getChildNodes();
151: for (int i = 0; i < list.getLength(); i++) {
152: if (list.item(i).getNodeType() == Node.ELEMENT_NODE) {
153: Element element = (Element) list.item(i);
154: String name = element.getLocalName();
155: WidgetValidatorBuilder builder;
156: try {
157: builder = (WidgetValidatorBuilder) this .widgetValidatorBuilderSelector
158: .select(name);
159: } catch (ServiceException e) {
160: throw new CascadingException(
161: "Unknow kind of validator '"
162: + name
163: + "' at "
164: + DomHelper
165: .getLocation(element),
166: e);
167: }
168:
169: widgetDefinition.addValidator(builder.build(
170: element, widgetDefinition));
171: }
172: }
173: }
174: }
175:
176: public void dispose() {
177: this.serviceManager
178: .release(this.widgetDefinitionBuilderSelector);
179: this.widgetDefinitionBuilderSelector = null;
180: this.serviceManager.release(this.datatypeManager);
181: this.datatypeManager = null;
182: this.serviceManager.release(this.expressionManager);
183: this.expressionManager = null;
184: this.serviceManager
185: .release(this.widgetValidatorBuilderSelector);
186: this.widgetValidatorBuilderSelector = null;
187: this.serviceManager = null;
188: }
189: }
|