Author: giacomo Date: Sun Mar 20 07:41:01 2005 New Revision: 158332 URL: http://svn.apache.org/viewcvs?view=rev&rev=158332 Log: new Datatypes and its Convertor
Added: cocoon/blocks/core/forms/trunk/java/org/apache/cocoon/forms/datatype/convertor/BeanConvertor.java cocoon/blocks/core/forms/trunk/java/org/apache/cocoon/forms/datatype/convertor/BeanConvertorBuilder.java cocoon/blocks/core/forms/trunk/java/org/apache/cocoon/forms/datatype/typeimpl/BeanType.java cocoon/blocks/core/forms/trunk/java/org/apache/cocoon/forms/datatype/typeimpl/BeanTypeBuilder.java Modified: cocoon/blocks/core/forms/trunk/WEB-INF/xconf/cocoon-forms.xconf cocoon/blocks/core/forms/trunk/samples/flow/binding_example.js Modified: cocoon/blocks/core/forms/trunk/WEB-INF/xconf/cocoon-forms.xconf URL: http://svn.apache.org/viewcvs/cocoon/blocks/core/forms/trunk/WEB-INF/xconf/cocoon-forms.xconf?view=diff&r1=158331&r2=158332 ============================================================================== --- cocoon/blocks/core/forms/trunk/WEB-INF/xconf/cocoon-forms.xconf (original) +++ cocoon/blocks/core/forms/trunk/WEB-INF/xconf/cocoon-forms.xconf Sun Mar 20 07:41:01 2005 @@ -108,6 +108,11 @@ <convertor name="enum" src="org.apache.cocoon.forms.datatype.convertor.EnumConvertorBuilder"/> </convertors> </datatype> + <datatype name="bean" src="org.apache.cocoon.forms.datatype.typeimpl.BeanTypeBuilder"> + <convertors default="bean" plain="bean"> + <convertor name="bean" src="org.apache.cocoon.forms.datatype.convertor.BeanConvertorBuilder"/> + </convertors> + </datatype> </datatypes> <validation-rules> <!-- old-style datatype validators (deprecated) --> Added: cocoon/blocks/core/forms/trunk/java/org/apache/cocoon/forms/datatype/convertor/BeanConvertor.java URL: http://svn.apache.org/viewcvs/cocoon/blocks/core/forms/trunk/java/org/apache/cocoon/forms/datatype/convertor/BeanConvertor.java?view=auto&rev=158332 ============================================================================== --- cocoon/blocks/core/forms/trunk/java/org/apache/cocoon/forms/datatype/convertor/BeanConvertor.java (added) +++ cocoon/blocks/core/forms/trunk/java/org/apache/cocoon/forms/datatype/convertor/BeanConvertor.java Sun Mar 20 07:41:01 2005 @@ -0,0 +1,145 @@ +/* + * 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.convertor; + +import org.apache.avalon.framework.CascadingRuntimeException; +import org.apache.commons.jxpath.JXPathContext; + +import org.xml.sax.ContentHandler; +import org.xml.sax.SAXException; + +import java.util.Locale; +import java.util.Map; +import java.util.WeakHashMap; + + +/** + * Converts String representation of beans to bean instances and vice versa. + * + * <p> + * Sometimes the toString() method doesn't give a good representation of a + * Java Bean suited for selection list IDs. For this an optional + * <fd:id-path>jx-path</fd:id-path> attribute can be specified to + * have this convertor to use a different string representation. + * </p> + * + * @author <a href="mailto:[EMAIL PROTECTED]">Giacomo Pati</a> + * @version $Id: BeanConvertor.java,v 1.3 2004/12/30 13:37:45 giacomo Exp $ + */ +public class BeanConvertor + implements Convertor +{ + //~ Instance fields -------------------------------------------------------- + + private Class m_class; + + private Map m_objects = new WeakHashMap( ); + + private String m_idPath; + + //~ Constructors ----------------------------------------------------------- + + /** + * Construct a new BeanConvertor for a class + * + * @param className The package-qualified name of the class implementing + * the typesafe enum pattern. + * @param idPath Path to the identity field of the bean + * + * @throws CascadingRuntimeException If the class cannot be found + */ + public BeanConvertor( final String className, + final String idPath ) + { + try + { + m_class = Class.forName( className ); + } + catch( ClassNotFoundException e ) + { + throw new CascadingRuntimeException( "Class " + className + + " not found", e ); + } + + m_idPath = idPath; + } + + //~ Methods ---------------------------------------------------------------- + + /** + * @see org.apache.cocoon.forms.datatype.convertor.Convertor#getTypeClass() + */ + public Class getTypeClass( ) + { + return m_class; + } + + /** + * @see org.apache.cocoon.forms.datatype.convertor.Convertor#convertFromString(java.lang.String, + * java.util.Locale, + * org.apache.cocoon.forms.datatype.convertor.Convertor.FormatCache) + */ + public ConversionResult convertFromString( final String value, + final Locale locale, + final FormatCache formatCache ) + { + return new ConversionResult( m_objects.get( value ) ); + } + + /** + * @see org.apache.cocoon.forms.datatype.convertor.Convertor#convertToString(java.lang.Object, + * java.util.Locale, + * org.apache.cocoon.forms.datatype.convertor.Convertor.FormatCache) + */ + public String convertToString( final Object value, + final Locale locale, + final FormatCache formatCache ) + { + String idValue = ""; + + if( null != value ) + { + if( m_idPath != null ) + { + final JXPathContext ctx = JXPathContext.newContext( value ); + idValue = ctx.getValue( m_idPath ).toString( ); + } + else + { + idValue = value.toString( ); + } + } + + m_objects.put( idValue, value ); + + return idValue; + } + + /** + * We do not enerate any SAX events + * + * @param contentHandler The contentHandler + * @param locale The locale + * + * @throws SAXException Just in case of failure that could never happen + */ + public void generateSaxFragment( final ContentHandler contentHandler, + final Locale locale ) + throws SAXException + { + // intentionally empty + } +} Added: cocoon/blocks/core/forms/trunk/java/org/apache/cocoon/forms/datatype/convertor/BeanConvertorBuilder.java URL: http://svn.apache.org/viewcvs/cocoon/blocks/core/forms/trunk/java/org/apache/cocoon/forms/datatype/convertor/BeanConvertorBuilder.java?view=auto&rev=158332 ============================================================================== --- cocoon/blocks/core/forms/trunk/java/org/apache/cocoon/forms/datatype/convertor/BeanConvertorBuilder.java (added) +++ cocoon/blocks/core/forms/trunk/java/org/apache/cocoon/forms/datatype/convertor/BeanConvertorBuilder.java Sun Mar 20 07:41:01 2005 @@ -0,0 +1,79 @@ +/* + * 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.convertor; + +import org.apache.cocoon.forms.Constants; +import org.apache.cocoon.forms.util.DomHelper; + +import org.w3c.dom.Element; + + +/** + * Creates [EMAIL PROTECTED] BeanConvertor}s + * + * <p> + * The optional <fd:bean>FQCN</fd:bean> attribute is used to give + * this convertor a hint of which concrete bean class we are going to work with. + * If this attribute is not specified java.lang.Object is used. + * <p> + * Sometimes the toString() method doesn't give a good representation of a + * Java Bean suited for selection list IDs. For this an optional + * <fd:id-path>jx-path</fd:id-path> attribute can be specified to + * have this convertor to use a different string representation. + * </p> + * + * @author <a href="mailto:[EMAIL PROTECTED]">Giacomo Pati</a> + * @version $Id: BeanConvertorBuilder.java,v 1.2 2004/12/27 13:30:48 giacomo Exp $ + */ +public class BeanConvertorBuilder + implements ConvertorBuilder +{ + //~ Methods ---------------------------------------------------------------- + + /** + * Build a [EMAIL PROTECTED] BeanConvertor} + * + * @param configElement The configuration element + * + * @return An initialized [EMAIL PROTECTED] Convertor} + * + * @throws Exception In case of failure + */ + public Convertor build( final Element configElement ) + throws Exception + { + if( configElement == null ) + { + return null; + } + + final Element beanEl = + DomHelper.getChildElement( configElement, Constants.DEFINITION_NS, + "bean", false ); + final String clazz = + ( ( beanEl == null ) ? Object.class.getName( ) + : beanEl.getFirstChild( ).getNodeValue( ) ); + final Element idPathEl = + DomHelper.getChildElement( configElement, Constants.DEFINITION_NS, + "id-path", false ); + final String idPath = + ( ( idPathEl != null ) + ? idPathEl.getFirstChild( ).getNodeValue( ) : null ); + final BeanConvertor convertor = new BeanConvertor( clazz, idPath ); + + return convertor; + } +} Added: cocoon/blocks/core/forms/trunk/java/org/apache/cocoon/forms/datatype/typeimpl/BeanType.java URL: http://svn.apache.org/viewcvs/cocoon/blocks/core/forms/trunk/java/org/apache/cocoon/forms/datatype/typeimpl/BeanType.java?view=auto&rev=158332 ============================================================================== --- cocoon/blocks/core/forms/trunk/java/org/apache/cocoon/forms/datatype/typeimpl/BeanType.java (added) +++ cocoon/blocks/core/forms/trunk/java/org/apache/cocoon/forms/datatype/typeimpl/BeanType.java Sun Mar 20 07:41:01 2005 @@ -0,0 +1,75 @@ +/* + * 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.typeimpl; + +import org.apache.cocoon.forms.datatype.convertor.Convertor; + + +/** + * The CForm type of a bean + * + * @author <a href="mailto:[EMAIL PROTECTED]">Giacomo Pati</a> + * @version $Id: BeanType.java,v 1.1 2004/12/21 14:37:32 giacomo Exp $ + */ +public class BeanType + extends AbstractDatatype +{ + //~ Constructors ----------------------------------------------------------- + + /** + * Creates a new BeanType object. + * + * @param arrayType whether it's an array or not + * @param builder The [EMAIL PROTECTED] BeanTypeBuilder} + */ + public BeanType( final boolean arrayType, + final BeanTypeBuilder builder ) + { + super( ); + setArrayType( arrayType ); + setBuilder( builder ); + } + + //~ Methods ---------------------------------------------------------------- + + /** + * @see org.apache.cocoon.forms.datatype.Datatype#getDescriptiveName() + */ + public String getDescriptiveName( ) + { + final Class c1 = this.getConvertor( ).getTypeClass( ); + + return this.getConvertor( ).getTypeClass( ).getName( ); + } + + /** + * We make sure the plain Convertor is the same + * + * @return The convertor + */ + public Convertor getPlainConvertor( ) + { + return getConvertor( ); + } + + /** + * @see org.apache.cocoon.forms.datatype.Datatype#getTypeClass() + */ + public Class getTypeClass( ) + { + return this.getConvertor( ).getTypeClass( ); + } +} Added: cocoon/blocks/core/forms/trunk/java/org/apache/cocoon/forms/datatype/typeimpl/BeanTypeBuilder.java URL: http://svn.apache.org/viewcvs/cocoon/blocks/core/forms/trunk/java/org/apache/cocoon/forms/datatype/typeimpl/BeanTypeBuilder.java?view=auto&rev=158332 ============================================================================== --- cocoon/blocks/core/forms/trunk/java/org/apache/cocoon/forms/datatype/typeimpl/BeanTypeBuilder.java (added) +++ cocoon/blocks/core/forms/trunk/java/org/apache/cocoon/forms/datatype/typeimpl/BeanTypeBuilder.java Sun Mar 20 07:41:01 2005 @@ -0,0 +1,49 @@ +/* + * 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.typeimpl; + +import org.apache.cocoon.forms.datatype.Datatype; +import org.apache.cocoon.forms.datatype.DatatypeManager; + +import org.w3c.dom.Element; + + +/** + * Builder for [EMAIL PROTECTED] BeanType} + * + * @author <a href="mailto:[EMAIL PROTECTED]">Giacomo Pati</a> + * @version $Id: BeanTypeBuilder.java,v 1.1 2004/12/21 14:37:32 giacomo Exp $ + */ +public class BeanTypeBuilder + extends AbstractDatatypeBuilder +{ + //~ Methods ---------------------------------------------------------------- + + /** + * @see org.apache.cocoon.forms.datatype.DatatypeBuilder#build(org.w3c.dom.Element, + * boolean, org.apache.cocoon.forms.datatype.DatatypeManager) + */ + public Datatype build( final Element datatypeElement, + final boolean arrayType, + final DatatypeManager datatypeManager ) + throws Exception + { + final BeanType type = new BeanType( arrayType, this ); + buildValidationRules( datatypeElement, type, datatypeManager ); + buildConvertor( datatypeElement, type ); + return type; + } +} Modified: cocoon/blocks/core/forms/trunk/samples/flow/binding_example.js URL: http://svn.apache.org/viewcvs/cocoon/blocks/core/forms/trunk/samples/flow/binding_example.js?view=diff&r1=158331&r2=158332 ============================================================================== --- cocoon/blocks/core/forms/trunk/samples/flow/binding_example.js (original) +++ cocoon/blocks/core/forms/trunk/samples/flow/binding_example.js Sun Mar 20 07:41:01 2005 @@ -80,6 +80,8 @@ contact.setId("1"); contact.setFirstName("Hermann"); bean.addContact(contact); + bean.addDrink("Maer"); + bean.addDrink("Leffe"); form.load(bean); form.showForm("form2-display-pipeline");