leosutic    02/05/06 03:46:57

  Modified:    src/java/org/apache/avalon/framework/configuration
                        SAXConfigurationHandler.java
                        AbstractConfiguration.java
               src/test/org/apache/avalon/framework/configuration/test
                        DefaultConfigurationBuilderTestCase.java
  Log:
  Changed the handling of whitespace in elements. Previously, all text content
  (the text received via the characters() method in the handler) was trimmed. 
This
  was changed in the previous version, but the code that threw an exception
  when mixed content (element with both children and text) was detected was
  not changed. The result was that if an element had children, it could not have
  any text content at all, even whitespace - which was not the intent. I moved
  the check for mixed content to the endElement() event, where it is possible to
  try to resolve the issue by checking if the content is all whitespace and in 
that case,
  remove it.
  
  Also, I updated the configuration builder test case to check for the bug
  reported at:
  
  http://nagoya.apache.org/bugzilla/show_bug.cgi?id=4791
  
  Revision  Changes    Path
  1.16      +26 -21    
jakarta-avalon/src/java/org/apache/avalon/framework/configuration/SAXConfigurationHandler.java
  
  Index: SAXConfigurationHandler.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-avalon/src/java/org/apache/avalon/framework/configuration/SAXConfigurationHandler.java,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- SAXConfigurationHandler.java      30 Apr 2002 14:00:34 -0000      1.15
  +++ SAXConfigurationHandler.java      6 May 2002 10:46:57 -0000       1.16
  @@ -20,6 +20,7 @@
    *
    * @author <a href="mailto:[EMAIL PROTECTED]">Federico Barbieri</a>
    * @author <a href="mailto:[EMAIL PROTECTED]">Peter Donald</a>
  + * @author <a href="mailto:[EMAIL PROTECTED]">Leo Sutic</a>
    */
   public class SAXConfigurationHandler
       extends DefaultHandler
  @@ -79,13 +80,6 @@
           final DefaultConfiguration configuration =
               (DefaultConfiguration)m_elements.get( m_elements.size() - 1 );
   
  -        if( 0 != configuration.getChildCount() )
  -        {
  -            throw new SAXException( "Not allowed to define mixed content in 
the " +
  -                                    "element " + configuration.getName() + " 
at " +
  -                                    configuration.getLocation() );
  -        }
  -
           value = configuration.getValue( "" ) + value;
           configuration.setValue( value );
       }
  @@ -104,17 +98,35 @@
           throws SAXException
       {
           final int location = m_elements.size() - 1;
  -        final Object object = m_elements.remove( location );
  +        final DefaultConfiguration object = (DefaultConfiguration) 
m_elements.remove( location );
   
  +        // Check for validity
  +        if ( object.getValue( null ) != null && object.getChildCount() > 0 ) 
  +        {
  +            // Could be invalid - the configuration has children and a value.
  +            // It is valid, however, to have a value consisting of just 
whitespace.
  +            // So let's trim the value, and see if we can resolve the 
conflict that way.
  +            if ( object.getValue("").trim().equals("") )
  +            {
  +                // Resolved!
  +                object.setValue( null );
  +            }
  +            else 
  +            {
  +                throw new SAXException( "Not allowed to define mixed content 
in the " +
  +                                        "element " + object.getName() + " at 
" +
  +                                        object.getLocation() );
  +            }
  +        }
  +        
           if( 0 == location )
           {
  -            m_configuration = (Configuration)object;
  +            m_configuration = object;
               final String value = m_configuration.getValue( null );
  -         if( null != value )
  -         {
  -             ((DefaultConfiguration)m_configuration).setValue( value.trim() 
);
  -         }
  -
  +            if( null != value )
  +            {
  +                ((DefaultConfiguration)m_configuration).setValue( 
value.trim() );
  +            }
           }
       }
   
  @@ -155,13 +167,6 @@
           {
               final DefaultConfiguration parent =
                   (DefaultConfiguration)m_elements.get( size );
  -
  -            if( null != parent.getValue( null ) )
  -            {
  -                throw new SAXException( "Not allowed to define mixed content 
in the " +
  -                                        "element " + parent.getName() + " at 
" +
  -                                        parent.getLocation() );
  -            }
   
               parent.addChild( configuration );
           }
  
  
  
  1.13      +9 -8      
jakarta-avalon/src/java/org/apache/avalon/framework/configuration/AbstractConfiguration.java
  
  Index: AbstractConfiguration.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-avalon/src/java/org/apache/avalon/framework/configuration/AbstractConfiguration.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- AbstractConfiguration.java        11 Dec 2001 09:00:45 -0000      1.12
  +++ AbstractConfiguration.java        6 May 2002 10:46:57 -0000       1.13
  @@ -15,7 +15,8 @@
    * @author <a href="mailto:[EMAIL PROTECTED]">Stefano Mazzocchi</a>
    * @author <a href="mailto:[EMAIL PROTECTED]">Peter Donald</a>
    * @author <a href="mailto:[EMAIL PROTECTED]">Pierpaolo Fumagalli</a>
  - * @version CVS $Revision: 1.12 $ $Date: 2001/12/11 09:00:45 $
  + * @author <a href="mailto:[EMAIL PROTECTED]">Leo Sutic</a>
  + * @version CVS $Revision: 1.13 $ $Date: 2002/05/06 10:46:57 $
    */
   public abstract class AbstractConfiguration
       implements Configuration
  @@ -42,7 +43,7 @@
       public int getValueAsInteger()
           throws ConfigurationException
       {
  -        final String value = getValue();
  +        final String value = getValue().trim();
           try
           {
               if( value.startsWith( "0x" ) )
  @@ -103,7 +104,7 @@
       public long getValueAsLong()
           throws ConfigurationException
       {
  -        final String value = getValue();
  +        final String value = getValue().trim();
           try
           {
               if( value.startsWith( "0x" ) )
  @@ -161,7 +162,7 @@
       public float getValueAsFloat()
           throws ConfigurationException
       {
  -        final String value = getValue();
  +        final String value = getValue().trim();
           try
           {
               return Float.parseFloat( value );
  @@ -201,12 +202,12 @@
       public boolean getValueAsBoolean()
           throws ConfigurationException
       {
  -        final String value = getValue();
  -        if( value.equals( "true" ) )
  +        final String value = getValue().trim();
  +        if( value.trim().equals( "true" ) )
           {
               return true;
           }
  -        else if( value.equals( "false" ) )
  +        else if( value.trim().equals( "false" ) )
           {
               return false;
           }
  @@ -268,7 +269,7 @@
       public int getAttributeAsInteger( final String name )
           throws ConfigurationException
       {
  -        final String value = getAttribute( name );
  +        final String value = getAttribute( name ).trim();
           try
           {
               if( value.startsWith( "0x" ) )
  
  
  
  1.4       +12 -6     
jakarta-avalon/src/test/org/apache/avalon/framework/configuration/test/DefaultConfigurationBuilderTestCase.java
  
  Index: DefaultConfigurationBuilderTestCase.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-avalon/src/test/org/apache/avalon/framework/configuration/test/DefaultConfigurationBuilderTestCase.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- DefaultConfigurationBuilderTestCase.java  15 Dec 2001 01:17:28 -0000      
1.3
  +++ DefaultConfigurationBuilderTestCase.java  6 May 2002 10:46:57 -0000       
1.4
  @@ -22,6 +22,7 @@
    *
    * @author <a href="mailto:[EMAIL PROTECTED]">Jeff Turner</a>
    * @author <a href="mailto:[EMAIL PROTECTED]">Ran Tene</a>
  + * @author <a href="mailto:[EMAIL PROTECTED]">Leo Sutic</a>
    */
   public final class DefaultConfigurationBuilderTestCase
       extends TestCase
  @@ -110,7 +111,7 @@
       "<?xml version=\"1.0\" ?>"+
       "<conf:config"+
       "       boolAttr=\"true\" floatAttr=\"1.32\""+
  -    "       xmlns:conf=\"http://conf.com\"; xmlns:a=\"http://a.com\"; 
xmlns:b=\"http://b.com\"; xmlns:c=\"http://c.com\";>"+
  +    "       xmlns:conf=\"http://conf.com\"; xmlns:a=\"http://a.com\"; 
xmlns:b=\"http://b.com\"; xmlns:c=\"http://c.com\"; xmlns:d=\"http://d.com\"; 
xmlns:e=\"http://e.com\";>"+
       "   <a:elements-a>"+
       "       <c:element name=\"a\"/>"+
       "   </a:elements-a>"+
  @@ -121,6 +122,8 @@
       "   <elements-c>"+
       "   true"+
       "   </elements-c>"+
  +    "   <d:element>d:element</d:element>"+
  +    "   <e:element>e:element</e:element>"+        
       "</conf:config>";
       /*
       "<?xml version=\"1.0\"?>"+
  @@ -149,7 +152,7 @@
   
           Configuration[] children;
           children = conf.getChildren();
  -        assertEquals( 4, children.length );
  +        assertEquals( 6, children.length );
           assertEquals( "a:elements-a", children[0].getName() );
           assertEquals( "elements-b", children[1].getName() );
           assertEquals( "b", children[1].getChild("element", 
false).getAttribute("name") );
  @@ -157,7 +160,7 @@
           assertEquals( "elements-c", children[3].getName() );
   
           final String[] attrNames = conf.getAttributeNames();
  -        assertEquals( 6, attrNames.length );
  +        assertEquals( 8, attrNames.length );
           assertEquals( "true", conf.getAttribute("boolAttr", null) );
           assertEquals( true, conf.getAttributeAsBoolean("boolAttr") );
           assertEquals( (float)1.32, conf.getAttributeAsFloat("floatAttr"), 
0.0 );
  @@ -187,6 +190,9 @@
           assertEquals( "Standard getChild() lookup failed", "b:elements-b", 
conf.getChild( "b:elements-b", false ).getName() );
           assertEquals( "Boolean value surrounded by whitespace failed", true, 
conf.getChild("elements-c").getValueAsBoolean( false ) );
           assertEquals( "A value-containing element should have no child 
nodes", 0, conf.getChild("elements-c").getChildren().length );
  +        
  +        assertEquals( "d:element", conf.getChild("d:element").getValue() );
  +        assertEquals( "e:element", conf.getChild("e:element").getValue() );
       }
   
   
  @@ -209,7 +215,7 @@
   
           Configuration[] children;
           children = conf.getChildren();
  -        assertEquals( 4, children.length );
  +        assertEquals( 6, children.length );
           assertEquals( "elements-a", children[0].getName() );
           assertEquals( "http://a.com";, children[0].getNamespace() );
           assertEquals( "elements-b", children[1].getName() );
  @@ -282,12 +288,12 @@
       protected  void tearDown()
           throws Exception
       {
  -        FileWriter writer = new FileWriter( m_file );
  +        /*FileWriter writer = new FileWriter( m_file );
           writer.write( "" );
           writer.close();
           writer = new FileWriter( m_nsFile );
           writer.write( "" );
  -        writer.close();
  +        writer.close();*/
           m_builder = null;
           m_nsBuilder = null;
       }
  
  
  

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

Reply via email to