You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by sy...@apache.org on 2003/10/22 22:22:07 UTC
cvs commit: cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/formmodel AbstractDatatypeWidgetDefinitionBuilder.java
sylvain 2003/10/22 13:22:07
Modified: src/blocks/woody/conf woody-datatype.xconf
woody-datatype.xroles
src/blocks/woody/java/org/apache/cocoon/woody/datatype
SelectionListBuilder.java
src/blocks/woody/java/org/apache/cocoon/woody/formmodel
AbstractDatatypeWidgetDefinitionBuilder.java
Added: src/blocks/woody/java/org/apache/cocoon/woody/datatype
DefaultSelectionListBuilder.java
FlowJXPathSelectionList.java
FlowJXPathSelectionListBuilder.java
Log:
Componentize selection lists, and add the new flow-jxpath implementation that uses flow page data to build the selection list
Revision Changes Path
1.5 +6 -0 cocoon-2.1/src/blocks/woody/conf/woody-datatype.xconf
Index: woody-datatype.xconf
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/conf/woody-datatype.xconf,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- woody-datatype.xconf 1 Oct 2003 18:20:23 -0000 1.4
+++ woody-datatype.xconf 22 Oct 2003 20:22:07 -0000 1.5
@@ -51,4 +51,10 @@
<validation-rule name="regexp" src="org.apache.cocoon.woody.datatype.validationruleimpl.RegExpValidationRuleBuilder"/>
</validation-rules>
</woody-datatype>
+
+ <woody-selection-lists default="default">
+ <selection-list name="default" class="org.apache.cocoon.woody.datatype.DefaultSelectionListBuilder"/>
+ <selection-list name="flow-jxpath" class="org.apache.cocoon.woody.datatype.FlowJXPathSelectionListBuilder"/>
+ </woody-selection-lists>
+
</xconf>
1.2 +4 -0 cocoon-2.1/src/blocks/woody/conf/woody-datatype.xroles
Index: woody-datatype.xroles
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/conf/woody-datatype.xroles,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- woody-datatype.xroles 22 Apr 2003 12:04:18 -0000 1.1
+++ woody-datatype.xroles 22 Oct 2003 20:22:07 -0000 1.2
@@ -4,4 +4,8 @@
<role name="org.apache.cocoon.woody.datatype.DatatypeManager"
shorthand="woody-datatype"
default-class="org.apache.cocoon.woody.datatype.DefaultDatatypeManager"/>
+
+ <role name="org.apache.cocoon.woody.datatype.SelectionListBuilderSelector"
+ shorthand="woody-selection-lists"
+ default-class="org.apache.cocoon.components.ExtendedComponentSelector"/>
</xroles>
1.4 +6 -49 cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/datatype/SelectionListBuilder.java
Index: SelectionListBuilder.java
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/datatype/SelectionListBuilder.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- SelectionListBuilder.java 26 Aug 2003 15:14:04 -0000 1.3
+++ SelectionListBuilder.java 22 Oct 2003 20:22:07 -0000 1.4
@@ -51,58 +51,15 @@
package org.apache.cocoon.woody.datatype;
import org.w3c.dom.Element;
-import org.w3c.dom.NodeList;
-import org.w3c.dom.Node;
-import org.apache.cocoon.woody.Constants;
-import org.apache.cocoon.woody.datatype.convertor.Convertor;
-import org.apache.cocoon.woody.datatype.convertor.DefaultFormatCache;
-import org.apache.cocoon.woody.util.DomHelper;
-import org.xml.sax.SAXException;
-
-import java.util.Locale;
/**
* Builds {@link SelectionList}s from an XML description.
- *
- * <p>Note: the class {@link DynamicSelectionList} also interprets the same wd:selection-list XML, so if
- * anything changes here to how that XML is interpreted, it also needs to change over there and vice versa.
+ *
+ * @version CVS $Id$
*/
-public class SelectionListBuilder {
-
- public static StaticSelectionList build(Element selectionListElement, Datatype datatype) throws Exception {
- StaticSelectionList selectionList = new StaticSelectionList(datatype);
- Convertor convertor = null;
- Convertor.FormatCache formatCache = new DefaultFormatCache();
-
- NodeList children = selectionListElement.getChildNodes();
- for (int i = 0; children.item(i) != null; i++) {
- Node node = children.item(i);
- if (convertor == null && node instanceof Element && Constants.WD_NS.equals(node.getNamespaceURI()) && "convertor".equals(node.getLocalName())) {
- Element convertorConfigElement = (Element)node;
- try {
- convertor = datatype.getBuilder().buildConvertor(convertorConfigElement);
- } catch (Exception e) {
- throw new SAXException("Error building convertor from convertor configuration embedded in selection list XML.", e);
- }
- } else if (node instanceof Element && Constants.WD_NS.equals(node.getNamespaceURI()) && "item".equals(node.getLocalName())) {
- if (convertor == null) {
- convertor = datatype.getConvertor();
- }
- Element element = (Element)node;
- String stringValue = element.getAttribute("value");
- Object value = convertor.convertFromString(stringValue, Locale.US, formatCache);
- if (value == null)
- throw new Exception("Could not convert the value \"" + stringValue + "\" to the type " + datatype.getDescriptiveName() + ", defined at " + DomHelper.getLocation(element));
-
- Object label = null;
- Element labelEl = DomHelper.getChildElement(element, Constants.WD_NS, "label");
- if (labelEl != null) {
- label = DomHelper.compileElementContent(labelEl);
- }
- selectionList.addItem(value, label);
- }
- }
+public interface SelectionListBuilder {
+
+ static final String ROLE = SelectionListBuilder.class.getName();
- return selectionList;
- }
+ SelectionList build(Element selectionListElement, Datatype datatype) throws Exception;
}
1.1 cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/datatype/DefaultSelectionListBuilder.java
Index: DefaultSelectionListBuilder.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Apache Cocoon" and "Apache Software Foundation" must not be
used to endorse or promote products derived from this software without
prior written permission. For written permission, please contact
apache@apache.org.
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation and was originally created by
Stefano Mazzocchi <st...@apache.org>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.cocoon.woody.datatype;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.apache.avalon.framework.service.ServiceException;
import org.apache.avalon.framework.service.ServiceManager;
import org.apache.avalon.framework.service.Serviceable;
import org.apache.cocoon.woody.Constants;
import org.apache.cocoon.woody.datatype.convertor.Convertor;
import org.apache.cocoon.woody.datatype.convertor.DefaultFormatCache;
import org.apache.cocoon.woody.util.DomHelper;
import org.apache.excalibur.source.Source;
import org.apache.excalibur.source.SourceResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import java.util.Locale;
/**
* Builds {@link SelectionList}s from an XML description or an URL.
*
* <p>Note: the class {@link DynamicSelectionList} also interprets the same wd:selection-list XML, so if
* anything changes here to how that XML is interpreted, it also needs to change over there and vice versa.
*
* @version CVS $Id: DefaultSelectionListBuilder.java,v 1.1 2003/10/22 20:22:07 sylvain Exp $
*/
public class DefaultSelectionListBuilder implements SelectionListBuilder, Serviceable {
private ServiceManager serviceManager;
public void service(ServiceManager manager) throws ServiceException {
this.serviceManager = manager;
}
public SelectionList build(Element selectionListElement, Datatype datatype) throws Exception {
SelectionList selectionList;
String src = selectionListElement.getAttribute("src");
if (src.length() > 0) {
boolean dynamic = DomHelper.getAttributeAsBoolean(selectionListElement, "dynamic", false);
if (!dynamic) {
selectionListElement = readSelectionList(src);
selectionList = buildStaticList(selectionListElement, datatype);
} else {
selectionList = new DynamicSelectionList(datatype, src, serviceManager);
}
} else {
// selection list is defined inline
selectionList = buildStaticList(selectionListElement, datatype);
}
return selectionList;
}
private SelectionList buildStaticList(Element selectionListElement, Datatype datatype) throws Exception {
StaticSelectionList selectionList = new StaticSelectionList(datatype);
Convertor convertor = null;
Convertor.FormatCache formatCache = new DefaultFormatCache();
NodeList children = selectionListElement.getChildNodes();
for (int i = 0; children.item(i) != null; i++) {
Node node = children.item(i);
if (convertor == null && node instanceof Element && Constants.WD_NS.equals(node.getNamespaceURI()) && "convertor".equals(node.getLocalName())) {
Element convertorConfigElement = (Element)node;
try {
convertor = datatype.getBuilder().buildConvertor(convertorConfigElement);
} catch (Exception e) {
throw new SAXException("Error building convertor from convertor configuration embedded in selection list XML.", e);
}
} else if (node instanceof Element && Constants.WD_NS.equals(node.getNamespaceURI()) && "item".equals(node.getLocalName())) {
if (convertor == null) {
convertor = datatype.getConvertor();
}
Element element = (Element)node;
String stringValue = element.getAttribute("value");
Object value = convertor.convertFromString(stringValue, Locale.US, formatCache);
if (value == null)
throw new Exception("Could not convert the value \"" + stringValue + "\" to the type " + datatype.getDescriptiveName() + ", defined at " + DomHelper.getLocation(element));
Object label = null;
Element labelEl = DomHelper.getChildElement(element, Constants.WD_NS, "label");
if (labelEl != null) {
label = DomHelper.compileElementContent(labelEl);
}
selectionList.addItem(value, label);
}
}
return selectionList;
}
private Element readSelectionList(String src) throws Exception {
SourceResolver resolver = null;
Source source = null;
try {
resolver = (SourceResolver)serviceManager.lookup(SourceResolver.ROLE);
source = resolver.resolveURI(src);
InputSource inputSource = new InputSource(source.getInputStream());
inputSource.setSystemId(source.getURI());
Document document = DomHelper.parse(inputSource);
Element selectionListElement = document.getDocumentElement();
if (!Constants.WD_NS.equals(selectionListElement.getNamespaceURI()) || !"selection-list".equals(selectionListElement.getLocalName()))
throw new Exception("Excepted a wd:selection-list element at " + DomHelper.getLocation(selectionListElement));
return selectionListElement;
} finally {
if (source != null)
resolver.release(source);
if (resolver != null)
serviceManager.release(resolver);
}
}
}
1.1 cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/datatype/FlowJXPathSelectionList.java
Index: FlowJXPathSelectionList.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Apache Cocoon" and "Apache Software Foundation" must not be
used to endorse or promote products derived from this software without
prior written permission. For written permission, please contact
apache@apache.org.
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation and was originally created by
Stefano Mazzocchi <st...@apache.org>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.cocoon.woody.datatype;
import java.util.Iterator;
import java.util.Locale;
import org.apache.avalon.framework.context.Context;
import org.apache.cocoon.components.ContextHelper;
import org.apache.cocoon.components.flow.FlowHelper;
import org.apache.cocoon.woody.Constants;
import org.apache.cocoon.xml.AttributesImpl;
import org.apache.commons.jxpath.JXPathContext;
import org.apache.commons.jxpath.Pointer;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
/**
* A selection list that takes its values from the flow page data.
*
* @see org.apache.cocoon.woody.datatype.FlowJXPathSelectionListBuilder
* @author <a href="http://www.apache.org/~sylvain/">Sylvain Wallez</a>
* @version CVS $Id: FlowJXPathSelectionList.java,v 1.1 2003/10/22 20:22:07 sylvain Exp $
*/
public class FlowJXPathSelectionList implements SelectionList {
private Context context;
private String listPath;
private String valuePath;
private String labelPath;
private Datatype datatype;
public FlowJXPathSelectionList(Context context, String listPath, String valuePath, String labelPath, Datatype datatype) {
this.context = context;
this.listPath = listPath;
this.valuePath = valuePath;
this.labelPath = labelPath;
this.datatype = datatype;
}
public Datatype getDatatype() {
return this.datatype;
}
public void generateSaxFragment(ContentHandler contentHandler, Locale locale) throws SAXException {
Object flowData = FlowHelper.getContextObject(ContextHelper.getObjectModel(this.context));
if (flowData == null) {
throw new SAXException("No flow data to produce selection list");
}
// Start the selection-list
contentHandler.startElement(Constants.WI_NS, SELECTION_LIST_EL, Constants.WI_PREFIX_COLON + SELECTION_LIST_EL, Constants.EMPTY_ATTRS);
// Move to the list location
JXPathContext ctx = JXPathContext.newContext(flowData);
// Iterate on all elements of the list
Iterator iter = ctx.iteratePointers(this.listPath);
while(iter.hasNext()) {
// Get a context on the current item
Pointer ptr = (Pointer)iter.next();
JXPathContext itemCtx = ctx.getRelativeContext(ptr);
// Get the value as a string
Object value = itemCtx.getValue(this.valuePath);
String stringValue = this.datatype.convertToString(value, locale);
// Get the label (can be ommitted)
itemCtx.setLenient(true);
Object label = itemCtx.getValue(this.labelPath);
String stringLabel = (label == null) ? stringValue : label.toString();
// Output this item
AttributesImpl itemAttrs = new AttributesImpl();
itemAttrs.addCDATAAttribute("value", stringValue);
contentHandler.startElement(Constants.WI_NS, ITEM_EL, Constants.WI_PREFIX_COLON + ITEM_EL, itemAttrs);
contentHandler.startElement(Constants.WI_NS, LABEL_EL, Constants.WI_PREFIX_COLON + LABEL_EL, Constants.EMPTY_ATTRS);
contentHandler.characters(stringLabel.toCharArray(), 0, stringLabel.length());
contentHandler.endElement(Constants.WI_NS, LABEL_EL, Constants.WI_PREFIX_COLON + LABEL_EL);
contentHandler.endElement(Constants.WI_NS, ITEM_EL, Constants.WI_PREFIX_COLON + ITEM_EL);
}
// End the selection-list
contentHandler.endElement(Constants.WI_NS, SELECTION_LIST_EL, Constants.WI_PREFIX_COLON + SELECTION_LIST_EL);
}
}
1.1 cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/datatype/FlowJXPathSelectionListBuilder.java
Index: FlowJXPathSelectionListBuilder.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Apache Cocoon" and "Apache Software Foundation" must not be
used to endorse or promote products derived from this software without
prior written permission. For written permission, please contact
apache@apache.org.
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation and was originally created by
Stefano Mazzocchi <st...@apache.org>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.cocoon.woody.datatype;
import org.apache.avalon.framework.context.Context;
import org.apache.avalon.framework.context.ContextException;
import org.apache.avalon.framework.context.Contextualizable;
import org.apache.cocoon.woody.util.DomHelper;
import org.w3c.dom.Element;
/**
* Builds a selection list that will take its values from the flow page data. The items list and,
* for each item, its value and label, are fetched using JXPath expressions.
* <p>
* If an item has no label, its value is used as the label.
* <p>
* Example:
* <pre>
* <wd:selection-list type="flow-jxpath"
* list-path="selectList" value-path="value" label-path="label"/gt;
* </pre>
* Flow script:
* <pre>
* var data = {
* selectList: [{value:3, label:"three"}, {value:4}]
* };
* form.showForm("form.html", data);
* </pre>
*
* @see org.apache.cocoon.woody.datatype.FlowJXPathSelectionList
* @author <a href="http://www.apache.org/~sylvain/">Sylvain Wallez</a>
* @version CVS $Id: FlowJXPathSelectionListBuilder.java,v 1.1 2003/10/22 20:22:07 sylvain Exp $
*/
public class FlowJXPathSelectionListBuilder implements SelectionListBuilder, Contextualizable {
private Context context;
public void contextualize(Context context) throws ContextException {
this.context = context;
}
public SelectionList build(Element selectionListElement, Datatype datatype) throws Exception {
String listPath = DomHelper.getAttribute(selectionListElement, "list-path");
String keyPath = DomHelper.getAttribute(selectionListElement, "value-path");
String valuePath = DomHelper.getAttribute(selectionListElement, "label-path");
return new FlowJXPathSelectionList(context, listPath, keyPath, valuePath, datatype);
}
}
1.4 +18 -40 cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/formmodel/AbstractDatatypeWidgetDefinitionBuilder.java
Index: AbstractDatatypeWidgetDefinitionBuilder.java
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/formmodel/AbstractDatatypeWidgetDefinitionBuilder.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- AbstractDatatypeWidgetDefinitionBuilder.java 25 Sep 2003 17:37:30 -0000 1.3
+++ AbstractDatatypeWidgetDefinitionBuilder.java 22 Oct 2003 20:22:07 -0000 1.4
@@ -50,16 +50,12 @@
*/
package org.apache.cocoon.woody.formmodel;
+import org.apache.avalon.framework.service.ServiceSelector;
import org.apache.cocoon.woody.Constants;
-import org.apache.cocoon.woody.datatype.DynamicSelectionList;
import org.apache.cocoon.woody.datatype.SelectionList;
import org.apache.cocoon.woody.datatype.SelectionListBuilder;
import org.apache.cocoon.woody.util.DomHelper;
-import org.apache.excalibur.source.Source;
-import org.apache.excalibur.source.SourceResolver;
-import org.w3c.dom.Document;
import org.w3c.dom.Element;
-import org.xml.sax.InputSource;
/**
* Abstract base class for WidgetDefinitionBuilders that build widgets that have datatypes/selection lists.
@@ -76,44 +72,26 @@
Element selectionListElement = DomHelper.getChildElement(widgetElement, Constants.WD_NS, "selection-list");
if (selectionListElement != null) {
- SelectionList selectionList;
- String src = selectionListElement.getAttribute("src");
- if (src.length() > 0) {
- boolean dynamic = DomHelper.getAttributeAsBoolean(selectionListElement, "dynamic", false);
- if (!dynamic) {
- selectionListElement = readSelectionList(src);
- selectionList = SelectionListBuilder.build(selectionListElement, widget.getDatatype());
- } else {
- selectionList = new DynamicSelectionList(widget.getDatatype(), src, serviceManager);
- }
- } else {
- // selection list is defined inline
- selectionList = SelectionListBuilder.build(selectionListElement, widget.getDatatype());
+
+ // Get an appropriate list builder
+
+ ServiceSelector builderSelector = (ServiceSelector)this.serviceManager.lookup(SelectionListBuilder.ROLE + "Selector");
+
+ // listType can be null, meaning we will use the default selection list
+ String listType = selectionListElement.getAttribute("type");
+ if (listType.length() == 0) listType = null;
+
+ SelectionListBuilder builder = (SelectionListBuilder)builderSelector.select(listType);
+
+ try {
+ SelectionList list = builder.build(selectionListElement, widget.getDatatype());
+ widget.setSelectionList(list);
+ } finally {
+ builderSelector.release(builder);
+ this.serviceManager.release(builderSelector);
}
- widget.setSelectionList(selectionList);
return true;
} else
return false;
- }
-
- private Element readSelectionList(String src) throws Exception {
- SourceResolver resolver = null;
- Source source = null;
- try {
- resolver = (SourceResolver)serviceManager.lookup(SourceResolver.ROLE);
- source = resolver.resolveURI(src);
- InputSource inputSource = new InputSource(source.getInputStream());
- inputSource.setSystemId(source.getURI());
- Document document = DomHelper.parse(inputSource);
- Element selectionListElement = document.getDocumentElement();
- if (!Constants.WD_NS.equals(selectionListElement.getNamespaceURI()) || !"selection-list".equals(selectionListElement.getLocalName()))
- throw new Exception("Excepted a wd:selection-list element at " + DomHelper.getLocation(selectionListElement));
- return selectionListElement;
- } finally {
- if (source != null)
- resolver.release(source);
- if (resolver != null)
- serviceManager.release(resolver);
- }
}
}