jstrachan    2003/03/20 11:29:07

  Modified:    betwixt/src/java/org/apache/commons/betwixt/io
                        BeanRuleSet.java BeanCreateRule.java
  Added:       betwixt/src/test/org/apache/commons/betwixt/derived
                        EmployeeBean.java PersonListBean.java
                        TestDerived.java PersonBean.java person-list.xml
                        ManagerBean.java
               betwixt/src/java/org/apache/commons/betwixt/expression
                        ClassNameExpression.java
  Log:
  Added provisional support to allow bean types to be customized by the presence of a 
'className' attribute.
  
  Its fairly simple and only works for parsing of beans right now. We need to add this 
customization feature to the ElementDescriptors so that this can be enabled as 
required and 
  then used to auto-default an AttributeDescriptor with a ClassNameExpression so that 
the output of the bean will also convey the className attribute again.
  
  Also the attribute used to describe the class name should be configurable.
  
  Maybe we should default it to 'class' since its rare to ever get a bean with a 
'class' setter property?
  
  Revision  Changes    Path
  1.1                  
jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/derived/EmployeeBean.java
  
  Index: EmployeeBean.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/derived/EmployeeBean.java,v
 1.1 2003/03/20 19:29:00 jstrachan Exp $
   * $Revision: 1.1 $
   * $Date: 2003/03/20 19:29:00 $
   *
   * ====================================================================
   *
   * 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 [EMAIL PROTECTED]
   *
   * 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: EmployeeBean.java,v 1.1 2003/03/20 19:29:00 jstrachan Exp $
   */
  package org.apache.commons.betwixt.derived;
  
  /** <p><code>EmployeeBean</code> a sample derived bean.</p>
    *
    * @author <a href="mailto:[EMAIL PROTECTED]">James Strachan</a>
    * @version $Revision: 1.1 $
    */
  public class EmployeeBean extends PersonBean {
      
      public EmployeeBean() {
      }
  }
  
  
  
  1.1                  
jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/derived/PersonListBean.java
  
  Index: PersonListBean.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/derived/PersonListBean.java,v
 1.1 2003/03/20 19:29:00 jstrachan Exp $
   * $Revision: 1.1 $
   * $Date: 2003/03/20 19:29:00 $
   *
   * ====================================================================
   *
   * 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 [EMAIL PROTECTED]
   *
   * 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: PersonListBean.java,v 1.1 2003/03/20 19:29:00 jstrachan Exp $
   */
  package org.apache.commons.betwixt.derived;
  
  import java.util.ArrayList;
  import java.util.List;
  
  /** <p>Bean to test lists of people</p>
    *
    * @author Robert Burrell Donkin
    * @version $Revision: 1.1 $
    */
  public class PersonListBean {
  
      private PersonBean owner;    
      private ArrayList people = new ArrayList();
      
      public PersonListBean() {}
  
      public List getPersonList() {
          return people;
      }
  
      public void addPerson(PersonBean person) {
          people.add(person);
      }
      
      /**
       * @return PersonBean
       */
      public PersonBean getOwner() {
          return owner;
      }
  
      /**
       * Sets the owner.
       * @param owner The owner to set
       */
      public void setOwner(PersonBean owner) {
          this.owner = owner;
      }
  
  }
  
  
  
  1.1                  
jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/derived/TestDerived.java
  
  Index: TestDerived.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/derived/TestDerived.java,v
 1.1 2003/03/20 19:29:00 jstrachan Exp $
   * $Revision: 1.1 $
   * $Date: 2003/03/20 19:29:00 $
   *
   * ====================================================================
   *
   * 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 [EMAIL PROTECTED]
   *
   * 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: TestDerived.java,v 1.1 2003/03/20 19:29:00 jstrachan Exp $
   */
  package org.apache.commons.betwixt.derived;
  
  import java.io.FileInputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.StringReader;
  import java.io.StringWriter;
  import java.math.BigDecimal;
  import java.math.BigInteger;
  import java.sql.Date;
  import java.sql.Time;
  import java.sql.Timestamp;
  import java.util.List;
  
  import junit.framework.Test;
  import junit.framework.TestSuite;
  import junit.textui.TestRunner;
  
  import org.apache.commons.beanutils.ConvertUtils;
  import org.apache.commons.betwixt.AbstractTestCase;
  import org.apache.commons.betwixt.io.BeanReader;
  import org.apache.commons.betwixt.io.BeanWriter;
  
  import org.apache.commons.digester.Rule;
  import org.apache.commons.digester.ExtendedBaseRules;
  
  import org.xml.sax.Attributes;
  
  
  /** Test harness for the BeanReader
    *
    * @author <a href="mailto:[EMAIL PROTECTED]">James Strachan</a>
    * @version $Revision: 1.1 $
    */
  public class TestDerived extends AbstractTestCase {
      
      public static void main( String[] args ) {
          TestRunner.run( suite() );
      }
      
      public static Test suite() {
          return new TestSuite(TestDerived.class);
      }
      
      public TestDerived(String testName) {
          super(testName);
      }
      
      public void testPersonList() throws Exception {
  
          BeanReader reader = new BeanReader();
  //        reader.getXMLIntrospector().setLog(log);
                
  //        log = new SimpleLog("[TestPersonList:BeanReader]");
  //        log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
          
  //        reader.setLog(log);
          reader.registerBeanClass( PersonListBean.class );
          
          InputStream in =  
              new FileInputStream( 
getTestFile("src/test/org/apache/commons/betwixt/derived/person-list.xml") );
          try {
          
              checkBean((PersonListBean) reader.parse( in ));
              
          }
          finally {
              in.close();
          }   
      }
      
      protected void checkBean(PersonListBean bean) throws Exception {
          PersonBean owner = bean.getOwner();
          assertTrue("should have found an owner", owner != null );
          
          assertEquals("should be derived class", 
"org.apache.commons.betwixt.derived.EmployeeBean", owner.getClass().getName());
          
          
          assertEquals("PersonList size", 4, bean.getPersonList().size());
          assertEquals("PersonList value (1)", "Athos", ((PersonBean) 
bean.getPersonList().get(0)).getName());
          assertEquals("PersonList value (2)", "Porthos", ((PersonBean) 
bean.getPersonList().get(1)).getName());
          assertEquals("PersonList value (3)", "Aramis", ((PersonBean) 
bean.getPersonList().get(2)).getName());
          assertEquals("PersonList value (4)", "D'Artagnan", ((PersonBean) 
bean.getPersonList().get(3)).getName());
          
          PersonBean employee = (PersonBean) bean.getPersonList().get(1);
          assertEquals("should be derived class", 
"org.apache.commons.betwixt.derived.EmployeeBean", employee.getClass().getName());
          
          PersonBean manager = (PersonBean) bean.getPersonList().get(2);
          assertEquals("should be derived class", 
"org.apache.commons.betwixt.derived.ManagerBean", manager.getClass().getName());
  
          // derived properties are not implemented yet...        
  /*
          ManagerBean manager2 = (ManagerBean) manager;
          assertEquals("should have a derived property", 12, manager2.getCheeseSize());
  */        
      }
      
  }
  
  
  
  
  1.1                  
jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/derived/PersonBean.java
  
  Index: PersonBean.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/derived/PersonBean.java,v
 1.1 2003/03/20 19:29:00 jstrachan Exp $
   * $Revision: 1.1 $
   * $Date: 2003/03/20 19:29:00 $
   *
   * ====================================================================
   *
   * 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 [EMAIL PROTECTED]
   *
   * 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: PersonBean.java,v 1.1 2003/03/20 19:29:00 jstrachan Exp $
   */
  package org.apache.commons.betwixt.derived;
  
  /** <p><code>PersonBean</code> is a sample bean for use with the test cases.</p>
    *
    * @author <a href="mailto:[EMAIL PROTECTED]">Robert Burrell Donkin</a>
    * @author <a href="mailto:[EMAIL PROTECTED]">Michael Davey</a>
    * @version $Revision: 1.1 $
    */
  public class PersonBean {
      
      private int age;
      
      private String name;
      
      public PersonBean() {}
      
      public PersonBean(int age, String name) 
      {
          setAge(age);
          setName(name);
      }
      
      public int getAge() {
          return age;
      }
      
      public void setAge(int age) {
          this.age = age;
      }
      
      public String getName() {
          return name;
      }
      
      public void setName(String name) {
          this.name = name;
      }
      
      public String toString() {  
          return "[" + this.getClass().getName() + ": age=" + age + " name=" + name + 
"]";
      }
      
      public boolean equals( Object obj ) {
          if ( obj == null ) return false;
          return this.hashCode() == obj.hashCode();
      }
      
      public int hashCode() {
          return toString().hashCode();
      }
  }
  
  
  
  1.1                  
jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/derived/person-list.xml
  
  Index: person-list.xml
  ===================================================================
  <?xml version='1.0' ?>
  <PersonListBean>
  
                <owner className="org.apache.commons.betwixt.derived.EmployeeBean">
                        <age>27</age>
                        <name>Rob</name>
                </owner>
  
        <personList>
                <person>
                        <age>22</age>
                        <name>Athos</name>
                </person>
                <person className="org.apache.commons.betwixt.derived.EmployeeBean">
                        <age>25</age>
                        <name>Porthos</name>
                </person>
                <person className="org.apache.commons.betwixt.derived.ManagerBean">
                        <cheeseSize>12</cheeseSize>
                        <age>23</age>
                        <name>Aramis</name>
                </person>
                <person>
                        <age>18</age>
                        <name>D'Artagnan</name>
                </person>
      </personList>
  </PersonListBean>
  
  
  1.1                  
jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/derived/ManagerBean.java
  
  Index: ManagerBean.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/derived/ManagerBean.java,v
 1.1 2003/03/20 19:29:00 jstrachan Exp $
   * $Revision: 1.1 $
   * $Date: 2003/03/20 19:29:00 $
   *
   * ====================================================================
   *
   * 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 [EMAIL PROTECTED]
   *
   * 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: ManagerBean.java,v 1.1 2003/03/20 19:29:00 jstrachan Exp $
   */
  package org.apache.commons.betwixt.derived;
  
  /** <p><code>ManagerBean</code> a sample derived bean.</p>
    *
    * @author <a href="mailto:[EMAIL PROTECTED]">James Strachan</a>
    * @version $Revision: 1.1 $
    */
  public class ManagerBean extends EmployeeBean {
      
      private int cheeseSize;
       
      public ManagerBean() {
      }
      /**
       * @return int
       */
      public int getCheeseSize() {
          return cheeseSize;
      }
  
      /**
       * Sets the cheeseSize.
       * @param cheeseSize The cheeseSize to set
       */
      public void setCheeseSize(int cheeseSize) {
          this.cheeseSize = cheeseSize;
      }
  
  }
  
  
  
  1.4       +48 -8     
jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/io/BeanRuleSet.java
  
  Index: BeanRuleSet.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/io/BeanRuleSet.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- BeanRuleSet.java  20 Feb 2003 18:20:41 -0000      1.3
  +++ BeanRuleSet.java  20 Mar 2003 19:29:00 -0000      1.4
  @@ -109,6 +109,8 @@
       private Class baseBeanClass;
       /** Should ID/IDREFs be used to match beans created previously  */
       private boolean matchIDs;
  +    /** allows an attribute to be specified to overload the types of beans used */
  +    private String classNameAttribute = "className";
       
       /**
        * Base constructor.
  @@ -132,6 +134,33 @@
           this.matchIDs = matchIDs;
       }
       
  +
  +    /**
  +     * The name of the attribute which can be specified in the XML to override the
  +     * type of a bean used at a certain point in the schema.
  +     *
  +     * <p>The default value is 'className'.</p>
  +     * 
  +     * @return The name of the attribute used to overload the class name of a bean
  +     */
  +    public String getClassNameAttribute() {
  +        return classNameAttribute;
  +    }
  +
  +    /**
  +     * Sets the name of the attribute which can be specified in 
  +     * the XML to override the type of a bean used at a certain 
  +     * point in the schema.
  +     *
  +     * <p>The default value is 'className'.</p>
  +     * 
  +     * @param classNameAttribute The name of the attribute used to overload the 
class name of a bean
  +     */
  +    public void setClassNameAttribute(String classNameAttribute) {
  +        this.classNameAttribute = classNameAttribute;
  +    }
  +
  +    
   //-------------------------------- Ruleset implementation
   
       /** 
  @@ -658,6 +687,11 @@
                   // XXX the existing system
                   // XXX maybe it's going to have to change so i'll use 'idref' for 
nows
                   //
  +                
  +                /** 
  +                 * @todo this is a duplicate of the code in BeanCreateRule
  +                 * we should try refactor to some common place
  +                 */
                   if ( matchIDs ) {
                       String idref = attributes.getValue( "idref" );
                       if ( idref != null ) {
  @@ -677,14 +711,20 @@
                       }
                   }
                   
  +                Class theClass = beanClass;
                   try {
  +                    String className = attributes.getValue(classNameAttribute);
  +                    if (className != null) {
  +                        // load the class we should instantiate
  +                        theClass = 
getDigester().getClassLoader().loadClass(className);
  +                    }
                       if (log.isTraceEnabled()) {
  -                        log.trace( "Creating instance of " + beanClass );
  +                        log.trace( "Creating instance of " + theClass );
                       }
  -                    return beanClass.newInstance();
  +                    return theClass.newInstance();
                       
                   } catch (Exception e) {
  -                    log.warn( "Could not create instance of type: " + 
beanClass.getName() );
  +                    log.warn( "Could not create instance of type: " + 
theClass.getName() );
                       return null;
                   }
               }    
  
  
  
  1.22      +46 -8     
jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/io/BeanCreateRule.java
  
  Index: BeanCreateRule.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/io/BeanCreateRule.java,v
  retrieving revision 1.21
  retrieving revision 1.22
  diff -u -r1.21 -r1.22
  --- BeanCreateRule.java       24 Feb 2003 18:21:29 -0000      1.21
  +++ BeanCreateRule.java       20 Mar 2003 19:29:00 -0000      1.22
  @@ -115,6 +115,8 @@
       private String pathPrefix;
       /** Use id's to match beans? */
       private boolean matchIDs = true;
  +    /** allows an attribute to be specified to overload the types of beans used */
  +    private String classNameAttribute = "className";
       
       /**
        * Convenience constructor which uses <code>ID's</code> for matching.
  @@ -372,6 +374,35 @@
       public void finish() {}
   
   
  +    // Properties
  +    //-------------------------------------------------------------------------    
  +    
  +
  +    /**
  +     * The name of the attribute which can be specified in the XML to override the
  +     * type of a bean used at a certain point in the schema.
  +     *
  +     * <p>The default value is 'className'.</p>
  +     * 
  +     * @return The name of the attribute used to overload the class name of a bean
  +     */
  +    public String getClassNameAttribute() {
  +        return classNameAttribute;
  +    }
  +
  +    /**
  +     * Sets the name of the attribute which can be specified in 
  +     * the XML to override the type of a bean used at a certain 
  +     * point in the schema.
  +     *
  +     * <p>The default value is 'className'.</p>
  +     * 
  +     * @param classNameAttribute The name of the attribute used to overload the 
class name of a bean
  +     */
  +    public void setClassNameAttribute(String classNameAttribute) {
  +        this.classNameAttribute = classNameAttribute;
  +    }
  +
       // Implementation methods
       //-------------------------------------------------------------------------    
       
  @@ -406,14 +437,21 @@
               }
           }
           
  +        Class theClass = beanClass;
           try {
  +            
  +            String className = attributes.getValue(classNameAttribute);
  +            if (className != null) {
  +                // load the class we should instantiate
  +                theClass = getDigester().getClassLoader().loadClass(className);
  +            }
               if (log.isTraceEnabled()) {
  -                log.trace( "Creating instance of " + beanClass );
  +                log.trace( "Creating instance of " + theClass );
               }
  -            return beanClass.newInstance();
  +            return theClass.newInstance();
               
           } catch (Exception e) {
  -            log.warn( "Could not create instance of type: " + beanClass.getName() );
  +            log.warn( "Could not create instance of type: " + theClass.getName() );
               return null;
           }
       }    
  
  
  
  1.1                  
jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/expression/ClassNameExpression.java
  
  Index: ClassNameExpression.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/expression/ClassNameExpression.java,v
 1.1 2003/03/20 19:29:04 jstrachan Exp $
   * $Revision: 1.1 $
   * $Date: 2003/03/20 19:29:04 $
   *
   * ====================================================================
   *
   * 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
   * 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 [EMAIL PROTECTED]
   *
   * 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: ClassNameExpression.java,v 1.1 2003/03/20 19:29:04 jstrachan Exp $
   */
  package org.apache.commons.betwixt.expression;
  
  
  /** <p><code>ClassNameExpression</code> returns the current class name 
    * of the context bean
    *
    * @author <a href="mailto:[EMAIL PROTECTED]">James Strachan</a>
    * @version $Revision: 1.1 $
    */
  public class ClassNameExpression implements Expression {
  
      /** Base constructor */
      public ClassNameExpression() {
      }
      
      /** 
       * Evaluate on the current context and return the class name  
       *
       * @param context the context against which this expression will be evaluated
       * @return the name of the class of the current contex bean
       */
      public Object evaluate(Context context) {
          Object bean = context.getBean();
          if ( bean != null ) {
              return bean.getClass().getName();
          }
          return null;
      }
  
      /** 
       * Do nothing.
       * @see org.apache.commons.betwixt.expression.Expression
       */
      public void update(Context context, String newValue) {
          // do nothing
      }
      
      /** 
       * Returns something useful for logging.
       * @return something useful for logging
       */
      public String toString() {
          return "ClassNameExpression";
      }
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to