commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject cvs commit: jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/strategy HyphenatedNameMapper.java
Date Wed, 05 Jun 2002 10:10:34 GMT
jstrachan    2002/06/05 03:10:34

  Modified:    betwixt/src/test/org/apache/commons/betwixt/strategy
                        TestHyphenatedNameMapper.java
               betwixt/src/java/org/apache/commons/betwixt/strategy
                        HyphenatedNameMapper.java
  Log:
  Patched the HyphenatedNameMapper so that it can be configured to convert to upper case element/attribute
names if need be (the default is lower case to preserve backwards compatiblity).
  Also the separator can be configured, so the HyphenatedNameMapper can be used to convert
FooBar into FOO_BAR if required, by the following code...
  
  NameMapper mapper = new HyphenatedNameMapper(true, "_");
  
  Have added some extra test cases for this behaviour.
  
  Revision  Changes    Path
  1.2       +17 -3     jakarta-commons-sandbox/betwixt/src/test/org/apache/commons/betwixt/strategy/TestHyphenatedNameMapper.java
  
  Index: TestHyphenatedNameMapper.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/betwixt/src/test/org/apache/commons/betwixt/strategy/TestHyphenatedNameMapper.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TestHyphenatedNameMapper.java	31 May 2002 23:17:55 -0000	1.1
  +++ TestHyphenatedNameMapper.java	5 Jun 2002 10:10:33 -0000	1.2
  @@ -5,7 +5,7 @@
    * version 1.1, a copy of which has been included with this distribution in
    * the LICENSE file.
    * 
  - * $Id: TestHyphenatedNameMapper.java,v 1.1 2002/05/31 23:17:55 jvanzyl Exp $
  + * $Id: TestHyphenatedNameMapper.java,v 1.2 2002/06/05 10:10:33 jstrachan Exp $
    */
   package org.apache.commons.betwixt.strategy;
   
  @@ -16,7 +16,7 @@
   /** Test harness for the HyphenatedNameMapper
     *
     * @author <a href="mailto:jason@zenplex.com">Jason van Zyl</a>
  -  * @version $Revision: 1.1 $
  +  * @version $Revision: 1.2 $
     */
   public class TestHyphenatedNameMapper extends TestCase {
       
  @@ -28,10 +28,24 @@
           super(testName);
       }
       
  -    public void testBeanWriter()  {
  +    public void testLowerCase()  {
           HyphenatedNameMapper mapper = new HyphenatedNameMapper();
           String result = mapper.mapTypeToElementName("FooBar");
           assertEquals("foo-bar", result);
  +    }
  +    
  +    public void testUpperCase()  {
  +        HyphenatedNameMapper mapper = new HyphenatedNameMapper(true, "_");
  +        String result = mapper.mapTypeToElementName("FooBar");
  +        assertEquals("FOO_BAR", result);
  +    }
  +    
  +    public void testUpperCaseViaProperties()  {
  +        HyphenatedNameMapper mapper = new HyphenatedNameMapper();
  +        mapper.setUpperCase(true);
  +        mapper.setSeparator("_");
  +        String result = mapper.mapTypeToElementName("FooBar");
  +        assertEquals("FOO_BAR", result);
       }
   }
   
  
  
  
  1.2       +88 -11    jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/strategy/HyphenatedNameMapper.java
  
  Index: HyphenatedNameMapper.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/strategy/HyphenatedNameMapper.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- HyphenatedNameMapper.java	31 May 2002 23:19:10 -0000	1.1
  +++ HyphenatedNameMapper.java	5 Jun 2002 10:10:33 -0000	1.2
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/strategy/HyphenatedNameMapper.java,v
1.1 2002/05/31 23:19:10 jvanzyl Exp $
  - * $Revision: 1.1 $
  - * $Date: 2002/05/31 23:19:10 $
  + * $Header: /home/cvs/jakarta-commons-sandbox/betwixt/src/java/org/apache/commons/betwixt/strategy/HyphenatedNameMapper.java,v
1.2 2002/06/05 10:10:33 jstrachan Exp $
  + * $Revision: 1.2 $
  + * $Date: 2002/06/05 10:10:33 $
    *
    * ====================================================================
    *
  @@ -57,7 +57,7 @@
    * information on the Apache Software Foundation, please see
    * <http://www.apache.org/>.
    * 
  - * $Id: HyphenatedNameMapper.java,v 1.1 2002/05/31 23:19:10 jvanzyl Exp $
  + * $Id: HyphenatedNameMapper.java,v 1.2 2002/06/05 10:10:33 jstrachan Exp $
    */
   package org.apache.commons.betwixt.strategy;
   
  @@ -65,32 +65,109 @@
   
   /** 
    * A name mapper which converts types to a hypenated String. So
  - * a bean type of FooBar will be converted to the element name "foo-bar"
  + * a bean type of FooBar will be converted to the element name "foo-bar".
  + * The name mapper can be configured to convert to upper case and to
  + * use a different separator via the <code>separator</code> and 
  + * <code>upperCase</code> properties, so that FooBar can be converted
  + * to FOO_BAR if needed, by calling the constructor
  + * <code>new HyphenatedNameMapper(true, "_")</code>.
    * 
    * @author <a href="mailto:jason@zenplex.com">Jason van Zyl</a>
  - * @version $Revision: 1.1 $
  + * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
  + * @version $Revision: 1.2 $
    */
   public class HyphenatedNameMapper implements NameMapper {
   
  +    /** the separator used to seperate words, which defaults to '-' */
  +    private String separator = "-";
  +
  +    /** whether upper or lower case conversions should be performed */
  +    private boolean upperCase = false;
  +    
  +    public HyphenatedNameMapper() {
  +    }
  +    
  +    public HyphenatedNameMapper(boolean upperCase) {
  +        this.upperCase = upperCase;
  +    }
  +    
  +    public HyphenatedNameMapper(boolean upperCase, String separator) {
  +        this.upperCase = upperCase;
  +        this.separator = separator;
  +    }
  +    
       /**
        * @return the element name for the given bean type name
        */
       public String mapTypeToElementName(String typeName) {
           
           StringBuffer sb = new StringBuffer();
  -        
  -        sb.append(Character.toLowerCase(typeName.charAt(0)));        
  +
  +        sb.append(convertChar(typeName.charAt(0)));        
           
           for (int i = 1; i < typeName.length(); i++) {
               if (Character.isUpperCase(typeName.charAt(i))) {
  -                sb.append('-');
  -                sb.append(Character.toLowerCase(typeName.charAt(i)));
  +                sb.append(separator);
  +                sb.append(convertChar(typeName.charAt(i)));
               }
               else {
  -                sb.append(typeName.charAt(i));
  +                if ( upperCase ) {
  +                    sb.append(convertChar(typeName.charAt(i)));
  +                }
  +                else {
  +                    sb.append(typeName.charAt(i));
  +                }
               }
           } 
           
           return sb.toString();
  +    }
  +    
  +    // Properties
  +    //-------------------------------------------------------------------------       

  +    /** 
  +     * @return the separator used to seperate words, which defaults to '-' 
  +     */
  +    public String getSeparator() {
  +        return separator;
  +    }
  +    
  +    /** 
  +     * Sets the separator used to seperate words, which defaults to '-' 
  +     */
  +    public void setSeparator(String separator) {
  +        this.separator = separator;
  +    }
  +    
  +    /** 
  +     * @return whether upper or lower case conversions should be performed, 
  +     * which defaults to false for lower case
  +     */    
  +    public boolean isUpperCase() {
  +        return upperCase;
  +    }
  +    
  +    /** 
  +     * Sets whether upper or lower case conversions should be performed,
  +     * which defaults to false for lower case
  +     */    
  +    public void setUpperCase(boolean upperCase) {
  +        this.upperCase = upperCase;
  +    }
  +    
  +    // Implementation methods
  +    //-------------------------------------------------------------------------       

  +    
  +    /** 
  +     * Performs type conversion on the given character based on whether
  +     * upper or lower case conversions are being used
  +     */
  +    protected char convertChar(char ch) {
  +        if ( upperCase ) {
  +            return Character.toUpperCase(ch);
  +        }
  +        else {
  +            return Character.toLowerCase(ch);
  +        }
       }
   }
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message