You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Calvin Yu <cy...@yahoo.com> on 2002/08/10 05:40:02 UTC
[BETWIXT][PATCH] Better hypenated name mapper support
There was a problem where the element name of the element that wraps a
collection isn't getting hyphenated when using <addDefaults/>. In the
patch, I merged that code with other code that seemed to be doing the
same thing (in cases where .betwixt files aren't used).
I also wrote a test to check the problem, added an extra property to
CustomerBean to test.
Calvin
Re: [BETWIXT][PATCH] Better hypenated name mapper support
Posted by robert burrell donkin <ro...@blueyonder.co.uk>.
patch committed.
many thanks.
- robert
On Saturday, August 10, 2002, at 04:40 AM, Calvin Yu wrote:
> There was a problem where the element name of the element that wraps a
> collection isn't getting hyphenated when using <addDefaults/>. In the
> patch, I merged that code with other code that seemed to be doing the
> same thing (in cases where .betwixt files aren't used).
>
> I also wrote a test to check the problem, added an extra property to
> CustomerBean to test.
>
> Calvin
>
>
>
> Index: XMLIntrospector.java
> ===================================================================
> RCS file: /home/cvspublic/jakarta-
> commons/betwixt/src/java/org/apache/commons/betwixt/XMLIntrospector.java,
> v
> retrieving revision 1.8
> diff -u -r1.8 XMLIntrospector.java
> --- XMLIntrospector.java 2 Jul 2002 20:31:45 -0000 1.8
> +++ XMLIntrospector.java 9 Aug 2002 03:45:54 -0000
> @@ -495,106 +495,18 @@
> List attributes)
> throws
> IntrospectionException {
> - Class type = propertyDescriptor.getPropertyType();
> - NodeDescriptor nodeDescriptor = null;
> - Method readMethod = propertyDescriptor.getReadMethod();
> - Method writeMethod = propertyDescriptor.getWriteMethod();
> -
> - if ( readMethod == null ) {
> - if (log.isTraceEnabled()) {
> - log.trace( "No read method" );
> - }
> - return;
> + NodeDescriptor nodeDescriptor = XMLIntrospectorHelper
> + .createDescriptor(propertyDescriptor,
> + isAttributesForPrimitives(),
> + this);
> + if (nodeDescriptor == null) {
> + return;
> + }
> + if (nodeDescriptor instanceof ElementDescriptor) {
> + elements.add(nodeDescriptor);
> + } else {
> + attributes.add(nodeDescriptor);
> }
> -
> - if ( log.isTraceEnabled() ) {
> - log.trace( "Read method=" + readMethod.getName() );
> - }
> -
> - // choose response from property type
> -
> - // XXX: ignore class property ??
> - if ( Class.class.equals( type ) && "class".equals
> ( propertyDescriptor.getName() ) ) {
> - if (log.isTraceEnabled()) {
> - log.trace( "Ignoring class property" );
> - }
> - return;
> - }
> - if ( isPrimitiveType( type ) ) {
> - if (log.isTraceEnabled()) {
> - log.trace( "Primitive type" );
> - }
> - if ( isAttributesForPrimitives() ) {
> - if (log.isTraceEnabled()) {
> - log.trace( "Added attribute" );
> - }
> - nodeDescriptor = new AttributeDescriptor();
> - attributes.add( nodeDescriptor );
> - }
> - else {
> - if (log.isTraceEnabled()) {
> - log.trace( "Added element" );
> - }
> - nodeDescriptor = new ElementDescriptor(true);
> - elements.add( nodeDescriptor );
> - }
> - nodeDescriptor.setTextExpression( new MethodExpression
> ( readMethod ) );
> -
> - if ( writeMethod != null ) {
> - nodeDescriptor.setUpdater( new MethodUpdater
> ( writeMethod ) );
> - }
> - }
> - else if ( isLoopType( type ) ) {
> - if (log.isTraceEnabled()) {
> - log.trace("Loop type");
> - }
> - ElementDescriptor loopDescriptor = new ElementDescriptor();
> - loopDescriptor.setContextExpression(
> - new IteratorExpression( new MethodExpression( readMethod
> ) )
> - );
> - // XXX: need to support some kind of 'add' or handle arrays,
> s or indexed properties
> - //loopDescriptor.setUpdater( new MethodUpdater( writeMethod
> ) );
> - if ( Map.class.isAssignableFrom( type ) ) {
> - loopDescriptor.setQualifiedName( "entry" );
> - }
> -
> - ElementDescriptor elementDescriptor = new ElementDescriptor(
> );
> - elementDescriptor.setElementDescriptors( new
> ElementDescriptor[] { loopDescriptor } );
> -
> elementDescriptor.setWrapCollectionsInElement(isWrapCollectionsInElement(
> ));
> -
> - nodeDescriptor = elementDescriptor;
> - elements.add( nodeDescriptor );
> - }
> - else {
> - if (log.isTraceEnabled()) {
> - log.trace( "Standard property" );
> - }
> - ElementDescriptor elementDescriptor = new ElementDescriptor(
> );
> - elementDescriptor.setContextExpression( new MethodExpression(
> readMethod ) );
> -
> - if ( writeMethod != null ) {
> - elementDescriptor.setUpdater( new MethodUpdater
> ( writeMethod ) );
> - }
> -
> - nodeDescriptor = elementDescriptor;
> - elements.add( nodeDescriptor );
> - }
> -
> - nodeDescriptor.setLocalName
> ( getNameMapper().mapTypeToElementName( propertyDescriptor.getName() ) );
> - if (nodeDescriptor instanceof AttributeDescriptor) {
> - // we want to use the attributemapper only when it is an
> attribute..
> - nodeDescriptor.setLocalName
> ( getAttributeNameMapper().mapTypeToElementName
> ( propertyDescriptor.getName() ) );
> - }
> - else{
> - nodeDescriptor.setLocalName
> ( getElementNameMapper().mapTypeToElementName( propertyDescriptor.getName(
> ) ) );
> - }
> -
> - nodeDescriptor.setPropertyName( propertyDescriptor.getName() );
> - nodeDescriptor.setPropertyType( type );
> -
> - // XXX: associate more bean information with the descriptor?
> - //nodeDescriptor.setDisplayName
> ( propertyDescriptor.getDisplayName() );
> - //nodeDescriptor.setShortDescription
> ( propertyDescriptor.getShortDescription() );
> }
>
> /** Factory method to create XMLBeanInfo instances */
>
> Index: XMLIntrospectorHelper.java
> ===================================================================
> RCS file: /home/cvspublic/jakarta-
> commons/betwixt/src/java/org/apache/commons/betwixt/digester/XMLIntrospectorHelper.
> java,v
> retrieving revision 1.5
> diff -u -r1.5 XMLIntrospectorHelper.java
> --- XMLIntrospectorHelper.java 8 Jul 2002 22:34:34 -0000 1.5
> +++ XMLIntrospectorHelper.java 10 Aug 2002 03:29:51 -0000
> @@ -123,10 +123,12 @@
> boolean useAttributesForPrimitives,
> XMLIntrospector introspector
> ) throws IntrospectionException {
> + String name = propertyDescriptor.getName();
> Class type = propertyDescriptor.getPropertyType();
> -
> +
> if (log.isTraceEnabled()) {
> - log.trace(propertyDescriptor.getPropertyType());
> + log.trace("Creating descriptor for property: name="
> + + name + " type=" + type);
> }
>
> NodeDescriptor nodeDescriptor = null;
> @@ -134,7 +136,10 @@
> Method writeMethod = propertyDescriptor.getWriteMethod();
>
> if ( readMethod == null ) {
> - log.trace( "No read method" );
> + if (log.isTraceEnabled()) {
> + log.trace( "No read method for property: name="
> + + name + " type=" + type);
> + }
> return null;
> }
>
> @@ -145,25 +150,36 @@
> // choose response from property type
>
> // XXX: ignore class property ??
> - if ( Class.class.equals( type ) && "class".equals
> ( propertyDescriptor.getName() ) ) {
> + if ( Class.class.equals( type ) && "class".equals( name ) ) {
> log.trace( "Ignoring class property" );
> return null;
> }
> if ( isPrimitiveType( type ) ) {
> - log.trace( "Primitive type" );
> + if (log.isTraceEnabled()) {
> + log.trace( "Primitive type: " + name);
> + }
> if ( useAttributesForPrimitives ) {
> - log.trace( "Added attribute" );
> + if (log.isTraceEnabled()) {
> + log.trace( "Adding property as attribute: " + name )
> ;
> + }
> nodeDescriptor = new AttributeDescriptor();
> }
> else {
> - log.trace( "Added element" );
> + if (log.isTraceEnabled()) {
> + log.trace( "Adding property as element: " + name );
> + }
> nodeDescriptor = new ElementDescriptor(true);
> }
> nodeDescriptor.setTextExpression( new MethodExpression
> ( readMethod ) );
> - nodeDescriptor.setUpdater( new MethodUpdater( writeMethod )
> );
> +
> + if ( writeMethod != null ) {
> + nodeDescriptor.setUpdater( new MethodUpdater
> ( writeMethod ) );
> + }
> }
> else if ( isLoopType( type ) ) {
> - log.trace("Loop type");
> + if (log.isTraceEnabled()) {
> + log.trace("Loop type: " + name);
> + }
> ElementDescriptor loopDescriptor = new ElementDescriptor();
> loopDescriptor.setContextExpression(
> new IteratorExpression( new MethodExpression( readMethod
> ) )
> @@ -181,23 +197,36 @@
> nodeDescriptor = elementDescriptor;
> }
> else {
> - log.trace( "Standard property" );
> + if (log.isTraceEnabled()) {
> + log.trace( "Standard property: " + name);
> + }
> ElementDescriptor elementDescriptor = new ElementDescriptor(
> );
> elementDescriptor.setContextExpression( new MethodExpression(
> readMethod ) );
> - elementDescriptor.setUpdater( new MethodUpdater( writeMethod
> ) );
>
> - nodeDescriptor = elementDescriptor;
> + if ( writeMethod != null ) {
> + elementDescriptor.setUpdater( new MethodUpdater
> ( writeMethod ) );
> + }
> +
> + nodeDescriptor = elementDescriptor;
> }
> - nodeDescriptor.setLocalName( propertyDescriptor.getName() );
> +
> + nodeDescriptor.setLocalName
> ( introspector.getNameMapper().mapTypeToElementName( name ) );
> + if (nodeDescriptor instanceof AttributeDescriptor) {
> + // we want to use the attributemapper only when it is an
> attribute..
> + nodeDescriptor.setLocalName
> ( introspector.getAttributeNameMapper().mapTypeToElementName( name ) );
> + }
> + else{
> + nodeDescriptor.setLocalName
> ( introspector.getElementNameMapper().mapTypeToElementName( name ) );
> + }
> +
> nodeDescriptor.setPropertyName( propertyDescriptor.getName() );
> - nodeDescriptor.setPropertyType( type );
> + nodeDescriptor.setPropertyType( type );
>
> // XXX: associate more bean information with the descriptor?
> //nodeDescriptor.setDisplayName
> ( propertyDescriptor.getDisplayName() );
> //nodeDescriptor.setShortDescription
> ( propertyDescriptor.getShortDescription() );
> return nodeDescriptor;
> }
> -
>
> public static void configureProperty( ElementDescriptor
> elementDescriptor, PropertyDescriptor propertyDescriptor ) {
> Class type = propertyDescriptor.getPropertyType();
>
> /*
> * $Header: $
> * $Revision: $
> * $Date: $
> *
> * ====================================================================
> *
> * The Apache Software License, Version 1.1
> *
> * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
> * reserved.
> *
> * Redistribution and use in source and binary forms, with or without
> * modification, 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 acknowlegement:
> * "This product includes software developed by the
> * Apache Software Foundation (http://www.apache.org/)."
> * Alternately, this acknowlegement may appear in the software itself,
> * if and wherever such third-party acknowlegements normally appear.
> *
> * 4. The names "The Jakarta Project", "Commons", 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 names without prior written
> * permission of the Apache Group.
> *
> * 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 (INCLUDING, 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. For more
> * information on the Apache Software Foundation, please see
> * <http://www.apache.org/>.
> *
> * $Id: $
> */
> package org.apache.commons.betwixt.digester;
>
> import java.beans.BeanInfo;
> import java.beans.Introspector;
> import java.beans.IntrospectionException;
> import java.beans.PropertyDescriptor;
>
> import junit.framework.Test;
> import junit.framework.TestCase;
> import junit.framework.TestSuite;
> import junit.textui.TestRunner;
>
> import org.apache.commons.betwixt.CustomerBean;
> import org.apache.commons.betwixt.NodeDescriptor;
> import org.apache.commons.betwixt.XMLIntrospector;
> import org.apache.commons.betwixt.strategy.HyphenatedNameMapper;
>
> /** Test harness for the XMLIntrospectorHelper
> *
> * @author <a href="mailto:cyu77@yahoo.com">Calvin Yu</a>
> * @version $Revision: $
> */
> public class TestXMLIntrospectorHelper extends TestCase {
>
> public static void main( String[] args ) {
> TestRunner.run( suite() );
> }
>
> public static Test suite() {
> return new TestSuite(TestXMLIntrospectorHelper.class);
> }
>
> public TestXMLIntrospectorHelper(String testName) {
> super(testName);
> }
>
> /**
> * Test the helper's <code>createDescriptor</code> method when a
> hyphenated name
> * mapper is set.
> */
> public void testCreateDescriptorWithHyphenatedElementNameMapper()
> throws Exception {
> XMLIntrospector introspector = new XMLIntrospector();
> introspector.setAttributesForPrimitives(false);
> introspector.setElementNameMapper(new HyphenatedNameMapper());
> BeanInfo beanInfo = Introspector.getBeanInfo(CustomerBean.class);
>
> NodeDescriptor nickNameProperty = createDescriptor("nickName",
> beanInfo, introspector);
> assertNotNull("nickName property not found", nickNameProperty);
> assertEquals("nick name property", "nick-name", nickNameProperty.
> getLocalName());
>
> NodeDescriptor projectNamesProperty =
> createDescriptor("projectNames", beanInfo, introspector);
> assertNotNull("projectNames property not found",
> projectNamesProperty);
> assertEquals("project names property", "project-names",
> projectNamesProperty.getLocalName());
> }
>
> /**
> * Find the specified property and convert it into a descriptor.
> */
> private NodeDescriptor createDescriptor(String propertyName, BeanInfo
> beanInfo, XMLIntrospector introspector)
> throws IntrospectionException {
> PropertyDescriptor[] properties = beanInfo.getPropertyDescriptors(
> );
> for (int i=0; i<properties.length; i++) {
> if (propertyName.equals(properties[i].getName())) {
> NodeDescriptor desc = XMLIntrospectorHelper
> .createDescriptor(properties[i],
>
> introspector.isAttributesForPrimitives(),
> introspector);
> return desc;
> }
> }
> return null;
> }
>
> }
>
>
> Index: CustomerBean.java
> ===================================================================
> RCS file: /home/cvspublic/jakarta-
> commons/betwixt/src/test/org/apache/commons/betwixt/CustomerBean.java,v
> retrieving revision 1.1
> diff -u -r1.1 CustomerBean.java
> --- CustomerBean.java 10 Jun 2002 17:53:32 -0000 1.1
> +++ CustomerBean.java 10 Aug 2002 03:33:48 -0000
> @@ -85,6 +85,7 @@
>
> private String id;
> private String name;
> + private String nickName;
> private String[] emails;
> private int[] numbers;
> private AddressBean address;
> @@ -102,6 +103,10 @@
> return name;
> }
>
> + public String getNickName() {
> + return nickName;
> + }
> +
> public String[] getEmails() {
> return emails;
> }
> @@ -144,7 +149,11 @@
> public void setName(String name) {
> this.name = name;
> }
> -
> +
> + public void setNickName(String nickName) {
> + this.nickName = nickName;
> + }
> +
> public void setEmails(String[] emails) {
> this.emails = emails;
> }
>
>
> --
> To unsubscribe, e-mail: <mailto:commons-dev-unsubscribe@jakarta.apache.
> org>
> For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.
> org>
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>