Author: joerg Date: Mon Oct 11 06:29:10 2004 New Revision: 54580 Added: cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms/datatype/AbstractJavaSelectionList.java cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms/datatype/JavaSelectionList.java cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms/datatype/JavaSelectionListBuilder.java cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms/samples/DateTestJavaSelectionList.java Modified: cocoon/trunk/src/blocks/forms/conf/forms-selection-lists.xconf cocoon/trunk/src/blocks/forms/samples/forms/form1.xml cocoon/trunk/src/blocks/forms/samples/forms/form1_template.xml cocoon/trunk/src/blocks/forms/samples/forms/form1_template_action.xml cocoon/trunk/status.xml Log: added JavaSelectionList (Bug 29715, by Nuno Santos)
Modified: cocoon/trunk/src/blocks/forms/conf/forms-selection-lists.xconf ============================================================================== --- cocoon/trunk/src/blocks/forms/conf/forms-selection-lists.xconf (original) +++ cocoon/trunk/src/blocks/forms/conf/forms-selection-lists.xconf Mon Oct 11 06:29:10 2004 @@ -21,6 +21,7 @@ <selection-list name="default" class="org.apache.cocoon.forms.datatype.DefaultSelectionListBuilder"/> <selection-list name="flow-jxpath" class="org.apache.cocoon.forms.datatype.FlowJXPathSelectionListBuilder"/> <selection-list name="enum" class="org.apache.cocoon.forms.datatype.EnumSelectionListBuilder"/> + <selection-list name="java" class="org.apache.cocoon.forms.datatype.JavaSelectionListBuilder"/> </forms-selection-lists> </xconf> Added: cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms/datatype/AbstractJavaSelectionList.java ============================================================================== --- (empty file) +++ cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms/datatype/AbstractJavaSelectionList.java Mon Oct 11 06:29:10 2004 @@ -0,0 +1,238 @@ +/* + * Copyright 1999-2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.cocoon.forms.datatype; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Locale; + +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.forms.Constants; +import org.apache.cocoon.forms.datatype.convertor.Convertor; +import org.apache.cocoon.forms.datatype.convertor.DefaultFormatCache; +import org.apache.cocoon.xml.AttributesImpl; +import org.apache.cocoon.xml.XMLUtils; +import org.xml.sax.ContentHandler; +import org.xml.sax.SAXException; + +/** + * Abstract implementation of a JavaSelectionList + */ +public abstract class AbstractJavaSelectionList implements JavaSelectionList, + Serviceable { + + protected Datatype datatype; + protected ServiceManager manager; + + private HashMap attributes; + private List items = new ArrayList(); + private boolean nullable; + private boolean rebuild = true; + + /* + * (non-Javadoc) + * + * @see org.apache.cocoon.forms.datatype.JavaSelectionList#getAttribute(java.lang.String) + */ + public String getAttribute(String name) { + if (this.attributes == null) { + return null; + } + return (String) this.attributes.get(name); + } + + /* + * (non-Javadoc) + * + * @see org.apache.cocoon.forms.datatype.JavaSelectionList#removeAttribute(java.lang.String) + */ + public void removeAttribute(String name) { + if (this.attributes != null) { + this.attributes.remove(name); + } + } + + /* + * (non-Javadoc) + * + * @see org.apache.cocoon.forms.datatype.JavaSelectionList#setAttribute(java.lang.String, + * java.lang.String) + */ + public void setAttribute(String name, String value) { + if (this.attributes == null) { + this.attributes = new HashMap(); + } + this.attributes.put(name, value); + + } + + /* + * (non-Javadoc) + * + * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager) + */ + public void service(ServiceManager manager) throws ServiceException { + this.manager = manager; + } + + /* + * (non-Javadoc) + * + * @see org.apache.cocoon.forms.datatype.JavaSelectionList#isNullable() + */ + public boolean isNullable() { + return this.nullable; + } + + /* + * (non-Javadoc) + * + * @see org.apache.cocoon.forms.datatype.JavaSelectionList#setDatatype(org.apache.cocoon.forms.datatype.Datatype) + */ + public void setDatatype(Datatype datatype) { + this.datatype = datatype; + } + + /* + * (non-Javadoc) + * + * @see org.apache.cocoon.forms.datatype.JavaSelectionList#setNullable(boolean) + */ + public void setNullable(boolean nullable) { + this.nullable = nullable; + } + + /* + * (non-Javadoc) + * + * @see org.apache.cocoon.forms.datatype.SelectionList#getDatatype() + */ + public Datatype getDatatype() { + return this.datatype; + } + + /** + * Enforce one rebuild on next usage of #generateSaxFragment(ContentHandler, Locale). + */ + public void markForRebuild() { + this.rebuild = true; + } + + public void generateSaxFragment(ContentHandler contentHandler, Locale locale) + throws SAXException { + if (this.rebuild) + try { + this.items.clear(); + this.rebuild = this.build(); + } catch (Exception e) { + e.printStackTrace(); + } + Convertor.FormatCache formatCache = new DefaultFormatCache(); + contentHandler.startElement(Constants.INSTANCE_NS, SELECTION_LIST_EL, + Constants.INSTANCE_PREFIX_COLON + SELECTION_LIST_EL, + XMLUtils.EMPTY_ATTRIBUTES); + if (nullable) { + AttributesImpl voidAttrs = new AttributesImpl(); + voidAttrs.addCDATAAttribute("value", ""); + contentHandler.startElement(Constants.INSTANCE_NS, ITEM_EL, + Constants.INSTANCE_PREFIX_COLON + ITEM_EL, voidAttrs); + contentHandler.endElement(Constants.INSTANCE_NS, ITEM_EL, + Constants.INSTANCE_PREFIX_COLON + ITEM_EL); + } + Iterator itemIt = items.iterator(); + while (itemIt.hasNext()) { + SelectionListItem item = (SelectionListItem) itemIt.next(); + item.generateSaxFragment(contentHandler, locale, formatCache); + } + contentHandler.endElement(Constants.INSTANCE_NS, SELECTION_LIST_EL, + Constants.INSTANCE_PREFIX_COLON + SELECTION_LIST_EL); + } + + /** + * Build the list of SelectionListItems using #addItem(Object, String). + * @return <code>true</code> if the list should be rebuild on each usage, + * <code>false</code> if it is static. + */ + protected abstract boolean build() throws Exception; + + /** + * Adds a new item to this selection list. + * + * @param value + * a value of the correct type (i.e. the type with which this + * selectionlist is associated) + * @param label + * a SAX-fragment such as a + * [EMAIL PROTECTED] org.apache.cocoon.xml.SaxBuffer}, can be null + */ + protected void addItem(Object value, String label) { + items.add(new SelectionListItem(value, label)); + } + + protected List getItems() { + return items; + } + + private final class SelectionListItem { + private final Object value; + + private final String label; + + public SelectionListItem(Object value, String label) { + this.value = value; + this.label = label; + } + + public Object getValue() { + return value; + } + + public void generateSaxFragment(ContentHandler contentHandler, + Locale locale, Convertor.FormatCache formatCache) + throws SAXException { + AttributesImpl itemAttrs = new AttributesImpl(); + String stringValue; + if (this.value == null) { + stringValue = ""; + } else { + stringValue = datatype.getConvertor().convertToString(value, + locale, formatCache); + } + itemAttrs.addCDATAAttribute("value", stringValue); + contentHandler.startElement(Constants.INSTANCE_NS, ITEM_EL, + Constants.INSTANCE_PREFIX_COLON + ITEM_EL, itemAttrs); + contentHandler.startElement(Constants.INSTANCE_NS, LABEL_EL, + Constants.INSTANCE_PREFIX_COLON + LABEL_EL, + XMLUtils.EMPTY_ATTRIBUTES); + if (label == null) { + contentHandler.characters(stringValue.toCharArray(), 0, + stringValue.length()); + } else { + contentHandler.characters(label.toCharArray(), 0, label + .length()); + } + contentHandler.endElement(Constants.INSTANCE_NS, LABEL_EL, + Constants.INSTANCE_PREFIX_COLON + LABEL_EL); + contentHandler.endElement(Constants.INSTANCE_NS, ITEM_EL, + Constants.INSTANCE_PREFIX_COLON + ITEM_EL); + } + } + +} \ No newline at end of file Added: cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms/datatype/JavaSelectionList.java ============================================================================== --- (empty file) +++ cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms/datatype/JavaSelectionList.java Mon Oct 11 06:29:10 2004 @@ -0,0 +1,36 @@ +/* + * Copyright 1999-2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.cocoon.forms.datatype; + +/** + * A selection list that takes its values from the custom java class itself. + * + * + */ +public interface JavaSelectionList extends SelectionList{ + + void setDatatype(Datatype datatype); + + boolean isNullable(); + + void setNullable(boolean nullable); + + String getAttribute(String name); + + void setAttribute(String name, String value); + + void removeAttribute(String name); +} Added: cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms/datatype/JavaSelectionListBuilder.java ============================================================================== --- (empty file) +++ cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms/datatype/JavaSelectionListBuilder.java Mon Oct 11 06:29:10 2004 @@ -0,0 +1,93 @@ +/* + * Copyright 1999-2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.cocoon.forms.datatype; + +import org.apache.avalon.framework.logger.AbstractLogEnabled; +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.components.LifecycleHelper; +import org.apache.cocoon.forms.util.DomHelper; +import org.w3c.dom.Element; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; + +/** + * Builds [EMAIL PROTECTED] SelectionList}s from a JavaSelectionList class + * + * + */ +public class JavaSelectionListBuilder extends AbstractLogEnabled implements + SelectionListBuilder, Serviceable { + + private ServiceManager manager; + + /* + * (non-Javadoc) + * + * @see org.apache.cocoon.forms.datatype.SelectionListBuilder#build(org.w3c.dom.Element, + * org.apache.cocoon.forms.datatype.Datatype) + */ + public SelectionList build(Element selectionListElement, Datatype datatype) + throws Exception { + String className = DomHelper + .getAttribute(selectionListElement, "class"); + boolean nullable = DomHelper.getAttributeAsBoolean( + selectionListElement, "nullable", true); + + try { + Class clasz = Class.forName(className); + if (JavaSelectionList.class.isAssignableFrom(clasz)) { + JavaSelectionList list = (JavaSelectionList) clasz + .newInstance(); + LifecycleHelper.setupComponent(list, getLogger(), null, + this.manager, null, null, true); + list.setDatatype(datatype); + list.setNullable(nullable); + // pass the attributes to the SelectionList + NamedNodeMap attrs = selectionListElement.getAttributes(); + int size = attrs.getLength(); + for (int i = 0; i < size; i++) { + Node attr = attrs.item(i); + String name = attr.getNodeName(); + list.setAttribute(name, attr.getNodeValue()); + } + return list; + } else { + return new StaticSelectionList(datatype); + } + } catch (ClassNotFoundException e) { + e.printStackTrace(); + throw e; + } catch (InstantiationException e) { + e.printStackTrace(); + throw e; + } catch (IllegalAccessException e) { + e.printStackTrace(); + throw e; + } + } + + /* + * (non-Javadoc) + * + * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager) + */ + public void service(ServiceManager manager) throws ServiceException { + this.manager = manager; + + } +} \ No newline at end of file Added: cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms/samples/DateTestJavaSelectionList.java ============================================================================== --- (empty file) +++ cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms/samples/DateTestJavaSelectionList.java Mon Oct 11 06:29:10 2004 @@ -0,0 +1,38 @@ +/* + * Copyright 1999-2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.cocoon.forms.samples; + +import java.util.Calendar; +import java.util.Date; + +import org.apache.cocoon.forms.datatype.AbstractJavaSelectionList; + +public class DateTestJavaSelectionList extends AbstractJavaSelectionList { + + /* (non-Javadoc) + * @see org.apache.cocoon.forms.datatype.AbstractJavaSelectionList#build() + */ + protected boolean build() throws Exception { + Calendar c = Calendar.getInstance(); + c.set(2003, 0, 1); + this.addItem(c.getTime(), null); + c.set(2004, 0, 1); + this.addItem(c.getTime(), null); + this.addItem(new Date(), null); + return false; + } + +} Modified: cocoon/trunk/src/blocks/forms/samples/forms/form1.xml ============================================================================== --- cocoon/trunk/src/blocks/forms/samples/forms/form1.xml (original) +++ cocoon/trunk/src/blocks/forms/samples/forms/form1.xml Mon Oct 11 06:29:10 2004 @@ -16,7 +16,7 @@ --> <!--+ - | CVS $Id: form1.xml,v 1.8 2004/06/29 13:06:04 sylvain Exp $ + | CVS $Id$ +--> <fd:form xmlns:fd="http://apache.org/cocoon/forms/1.0#definition" @@ -237,6 +237,14 @@ </fd:selection-list> </fd:field> + <fd:field id="altbirthdate2" required="true"> + <fd:label>Select another date on which you'd rather had been born:</fd:label> + <fd:datatype base="date"> + <fd:convertor variant="date" style="full"/> + </fd:datatype> + <fd:selection-list type="java" class="org.apache.cocoon.forms.samples.DateTestJavaSelectionList" nullable="false"/> + </fd:field> + <fd:field id="dieselprice" required="true"> <fd:label>Price for a liter diesel:</fd:label> <fd:datatype base="decimal"> Modified: cocoon/trunk/src/blocks/forms/samples/forms/form1_template.xml ============================================================================== --- cocoon/trunk/src/blocks/forms/samples/forms/form1_template.xml (original) +++ cocoon/trunk/src/blocks/forms/samples/forms/form1_template.xml Mon Oct 11 06:29:10 2004 @@ -87,6 +87,7 @@ <ft:widget id="visa"/> <ft:widget id="ipaddress"/> <ft:widget id="altbirthdate"/> + <ft:widget id="altbirthdate2"/> <ft:widget id="dieselprice"/> </fi:items> </fi:group> Modified: cocoon/trunk/src/blocks/forms/samples/forms/form1_template_action.xml ============================================================================== --- cocoon/trunk/src/blocks/forms/samples/forms/form1_template_action.xml (original) +++ cocoon/trunk/src/blocks/forms/samples/forms/form1_template_action.xml Mon Oct 11 06:29:10 2004 @@ -85,6 +85,7 @@ <ft:widget id="visa"/> <ft:widget id="ipaddress"/> <ft:widget id="altbirthdate"/> + <ft:widget id="altbirthdate2"/> <ft:widget id="birthdate"/> <ft:widget id="dieselprice"/> </fi:items> Modified: cocoon/trunk/status.xml ============================================================================== --- cocoon/trunk/status.xml (original) +++ cocoon/trunk/status.xml Mon Oct 11 06:29:10 2004 @@ -205,17 +205,20 @@ <changes> <release version="@version@" date="@date@"> + <action dev="JH" type="add" fixes-bug="29715" due-to="Nuno Santos" due-to-email="[EMAIL PROTECTED]"> + Forms block: added possibility to add a SelectionList based on Java. + </action> <action dev="TC" type="add" fixes-bug="30417"> - Added best-fit-while-keeping-aspect-ratio option to the ImageReader + Added best-fit-while-keeping-aspect-ratio option to the ImageReader </action> <action dev="RP" type="add" due-to="Adam Ratclif" fixes-bug="31359"> - Apply patch: Add support for calling webservices from within Flowscript. + Apply patch: Add support for calling webservices from within Flowscript. </action> <action dev="TC" type="fix" fixes-bug="31545"> - Throw a more meaningful exception if charset classes are missing + Throw a more meaningful exception if charset classes are missing </action> <action dev="TC" type="fix" fixes-bug="30874"> - Fixes the SQLTransformer not to close the statement twice + Fixes the SQLTransformer not to close the statement twice </action> <action dev="CZ" type="add"> New getSitemapPath() method on the Request object to get the path to the