You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by bl...@apache.org on 2001/12/07 14:44:54 UTC

cvs commit: jakarta-avalon/src/test/org/apache/avalon/framework/configuration/test SAXConfigurationHandlerTestCase.java

bloritsch    01/12/07 05:44:54

  Modified:    src/java/org/apache/avalon/framework/configuration
                        DefaultConfigurationBuilder.java
                        SAXConfigurationHandler.java
               src/test/org/apache/avalon/framework/configuration/test
                        SAXConfigurationHandlerTestCase.java
  Added:       src/java/org/apache/avalon/framework/configuration
                        NamespacedSAXConfigurationHandler.java
  Removed:     src/java/org/apache/avalon/framework/configuration
                        ClassicSAXConfigurationHandler.java
  Log:
  Restructure configuration so that it is backwards compatible
  
  Revision  Changes    Path
  1.11      +32 -2     jakarta-avalon/src/java/org/apache/avalon/framework/configuration/DefaultConfigurationBuilder.java
  
  Index: DefaultConfigurationBuilder.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon/src/java/org/apache/avalon/framework/configuration/DefaultConfigurationBuilder.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- DefaultConfigurationBuilder.java	2001/11/19 11:47:37	1.10
  +++ DefaultConfigurationBuilder.java	2001/12/07 13:44:53	1.11
  @@ -21,6 +21,7 @@
    *
    * @author <a href="mailto:fede@apache.org">Federico Barbieri</a>
    * @author <a href="mailto:peter@apache.org">Peter Donald</a>
  + * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
    */
   public class DefaultConfigurationBuilder
   {
  @@ -28,15 +29,32 @@
       private XMLReader                             m_parser;
   
       /**
  -     * Create a Configuration Builder with a default XMLReader.
  +     * Create a Configuration Builder with a default XMLReader that ignores
  +     * namespaces.  In order to enable namespaces, use either the constructor
  +     * that has a boolean or that allows you to pass in your own XMLReader.
        */
       public DefaultConfigurationBuilder()
       {
  +        this( false );
  +    }
  +
  +    /**
  +     * Create a Configuration Builder with a default XMLReader that implements
  +     * namespaces if passed <code>true</code> or defaults to the original
  +     * functionality if passed <code>false</code>
  +     */
  +    public DefaultConfigurationBuilder( final boolean enableNamespaces )
  +    {
           //yaya the bugs with some compilers and final variables ..
           try
           {
               final SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
  -            saxParserFactory.setNamespaceAware(true);
  +
  +            if ( enableNamespaces )
  +            {
  +                saxParserFactory.setNamespaceAware(true);
  +            }
  +
               final SAXParser saxParser = saxParserFactory.newSAXParser();
               this.setParser(saxParser.getXMLReader());
           }
  @@ -72,6 +90,18 @@
        */
       protected SAXConfigurationHandler getHandler()
       {
  +        try
  +        {
  +            if ( m_parser.getFeature( "http://xml.org/sax/features/namespaces" ) )
  +            {
  +                return new NamespacedSAXConfigurationHandler();
  +            }
  +        }
  +        catch ( Exception e )
  +        {
  +            // ignore error and fall through to the non-namespaced version
  +        }
  +
           return new SAXConfigurationHandler();
       }
   
  
  
  
  1.12      +7 -74     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.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- SAXConfigurationHandler.java	2001/11/26 09:28:43	1.11
  +++ SAXConfigurationHandler.java	2001/12/07 13:44:54	1.12
  @@ -8,31 +8,26 @@
   package org.apache.avalon.framework.configuration;
   
   import java.util.ArrayList;
  -import java.util.Iterator;
   import org.xml.sax.Attributes;
   import org.xml.sax.ErrorHandler;
   import org.xml.sax.Locator;
   import org.xml.sax.SAXException;
   import org.xml.sax.SAXParseException;
   import org.xml.sax.helpers.DefaultHandler;
  -import org.xml.sax.helpers.NamespaceSupport;
  -import org.xml.sax.helpers.AttributesImpl;
   
   /**
    * A SAXConfigurationHandler helps build Configurations out of sax events.
    *
    * @author <a href="mailto:fede@apache.org">Federico Barbieri</a>
  - * @author <a href="mailto:peter@apache.org">Peter Donald</a>
  + * @author <a href="mailto:donaldp@apache.org">Peter Donald</a>
    */
   public class SAXConfigurationHandler
       extends DefaultHandler
       implements ErrorHandler
   {
  -    private final ArrayList              m_elements         = new ArrayList();
  -    private final ArrayList              m_prefixes         = new ArrayList();
  +    private final ArrayList              m_elements        = new ArrayList();
       private Configuration                m_configuration;
       private Locator                      m_locator;
  -    private NamespaceSupport             m_namespaceSupport = new NamespaceSupport();
   
       public Configuration getConfiguration()
       {
  @@ -42,12 +37,6 @@
       public void clear()
       {
           m_elements.clear();
  -        Iterator i = m_prefixes.iterator();
  -        while ( i.hasNext() )
  -        {
  -            ( (ArrayList) i.next() ).clear();
  -        }
  -        m_prefixes.clear();
           m_locator = null;
       }
   
  @@ -56,20 +45,6 @@
           m_locator = locator;
       }
   
  -    public void startDocument()
  -        throws SAXException
  -    {
  -        m_namespaceSupport.reset();
  -        super.startDocument();
  -    }
  -
  -    public void endDocument()
  -        throws SAXException
  -    {
  -        super.endDocument();
  -        m_namespaceSupport.reset();
  -    }
  -
       public void characters( final char[] ch, int start, int end )
           throws SAXException
       {
  @@ -101,29 +76,17 @@
       {
           final int location = m_elements.size() - 1;
           final Object object = m_elements.remove( location );
  -        final ArrayList prefixes = (ArrayList) m_prefixes.remove( location );
  -
  -        final Iterator i = prefixes.iterator();
  -        while ( i.hasNext() )
  -        {
  -            endPrefixMapping( (String) i.next() );
  -        }
  -        prefixes.clear();
   
           if( 0 == location )
           {
               m_configuration = (Configuration)object;
           }
  -
  -        m_namespaceSupport.popContext();
       }
   
       protected DefaultConfiguration createConfiguration( final String localName,
  -                                                        final String namespaceURI,
                                                           final String location )
       {
  -        final String prefix = m_namespaceSupport.getPrefix( namespaceURI );
  -        return new DefaultConfiguration( localName, location, namespaceURI, prefix );
  +        return new DefaultConfiguration( localName, location );
       }
   
       public void startElement( final String namespaceURI,
  @@ -132,30 +95,8 @@
                                 final Attributes attributes )
           throws SAXException
       {
  -        m_namespaceSupport.pushContext();
  -        final ArrayList prefixes = new ArrayList();
  -        AttributesImpl componentAttr = new AttributesImpl();
  -
  -        for (int i = 0; i < attributes.getLength(); i++)
  -        {
  -            if ( attributes.getQName(i).startsWith("xmlns") )
  -            {
  -                prefixes.add( attributes.getLocalName(i) );
  -                this.startPrefixMapping( attributes.getLocalName(i),
  -                                         attributes.getValue(i) );
  -            }
  -            else
  -            {
  -                componentAttr.addAttribute( attributes.getURI( i ),
  -                                            attributes.getLocalName( i ),
  -                                            attributes.getQName( i ),
  -                                            attributes.getType( i ),
  -                                            attributes.getValue( i ) );
  -            }
  -        }
  -
           final DefaultConfiguration configuration =
  -            createConfiguration( localName, namespaceURI, getLocationString() );
  +            createConfiguration( rawName, getLocationString() );
           final int size = m_elements.size() - 1;
   
           if( size > -1 )
  @@ -174,14 +115,13 @@
           }
   
           m_elements.add( configuration );
  -        m_prefixes.add( prefixes );
   
  -        final int attributesSize = componentAttr.getLength();
  +        final int attributesSize = attributes.getLength();
   
           for( int i = 0; i < attributesSize; i++ )
           {
  -            final String name = componentAttr.getQName( i );
  -            final String value = componentAttr.getValue( i );
  +            final String name = attributes.getQName( i );
  +            final String value = attributes.getValue( i );
               configuration.setAttribute( name, value );
           }
       }
  @@ -226,12 +166,5 @@
                   m_locator.getLineNumber() + ":" +
                   m_locator.getColumnNumber();
           }
  -    }
  -
  -    public void startPrefixMapping(String prefix, String uri)
  -        throws SAXException
  -    {
  -        m_namespaceSupport.declarePrefix( prefix, uri );
  -        super.startPrefixMapping( prefix, uri );
       }
   }
  
  
  
  1.1                  jakarta-avalon/src/java/org/apache/avalon/framework/configuration/NamespacedSAXConfigurationHandler.java
  
  Index: NamespacedSAXConfigurationHandler.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE file.
   */
  package org.apache.avalon.framework.configuration;
  
  import java.util.ArrayList;
  import java.util.Iterator;
  import org.xml.sax.Attributes;
  import org.xml.sax.ErrorHandler;
  import org.xml.sax.Locator;
  import org.xml.sax.SAXException;
  import org.xml.sax.SAXParseException;
  import org.xml.sax.helpers.DefaultHandler;
  import org.xml.sax.helpers.NamespaceSupport;
  import org.xml.sax.helpers.AttributesImpl;
  
  /**
   * A SAXConfigurationHandler helps build Configurations out of sax events,
   * including namespace information.
   *
   * @author <a href="mailto:fede@apache.org">Federico Barbieri</a>
   * @author <a href="mailto:peter@apache.org">Peter Donald</a>
   */
  public class NamespacedSAXConfigurationHandler
      extends SAXConfigurationHandler
  {
      private final ArrayList              m_elements         = new ArrayList();
      private final ArrayList              m_prefixes         = new ArrayList();
      private Configuration                m_configuration;
      private Locator                      m_locator;
      private NamespaceSupport             m_namespaceSupport = new NamespaceSupport();
  
      public Configuration getConfiguration()
      {
          return m_configuration;
      }
  
      public void clear()
      {
          m_elements.clear();
          Iterator i = m_prefixes.iterator();
          while ( i.hasNext() )
          {
              ( (ArrayList) i.next() ).clear();
          }
          m_prefixes.clear();
          m_locator = null;
      }
  
      public void setDocumentLocator( final Locator locator )
      {
          m_locator = locator;
      }
  
      public void startDocument()
          throws SAXException
      {
          m_namespaceSupport.reset();
          super.startDocument();
      }
  
      public void endDocument()
          throws SAXException
      {
          super.endDocument();
          m_namespaceSupport.reset();
      }
  
      public void characters( final char[] ch, int start, int end )
          throws SAXException
      {
          String value = (new String( ch, start, end )).trim();
  
          if( value.equals( "" ) )
          {
              return;
          }
  
          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 );
      }
  
      public void endElement( final String namespaceURI,
                              final String localName,
                              final String rawName )
          throws SAXException
      {
          final int location = m_elements.size() - 1;
          final Object object = m_elements.remove( location );
          final ArrayList prefixes = (ArrayList) m_prefixes.remove( location );
  
          final Iterator i = prefixes.iterator();
          while ( i.hasNext() )
          {
              endPrefixMapping( (String) i.next() );
          }
          prefixes.clear();
  
          if( 0 == location )
          {
              m_configuration = (Configuration)object;
          }
  
          m_namespaceSupport.popContext();
      }
  
      protected DefaultConfiguration createConfiguration( final String localName,
                                                          final String namespaceURI,
                                                          final String location )
      {
          final String prefix = m_namespaceSupport.getPrefix( namespaceURI );
          return new DefaultConfiguration( localName, location, namespaceURI, prefix );
      }
  
      public void startElement( final String namespaceURI,
                                final String localName,
                                final String rawName,
                                final Attributes attributes )
          throws SAXException
      {
          m_namespaceSupport.pushContext();
          final ArrayList prefixes = new ArrayList();
          AttributesImpl componentAttr = new AttributesImpl();
  
          for (int i = 0; i < attributes.getLength(); i++)
          {
              if ( attributes.getQName(i).startsWith("xmlns") )
              {
                  prefixes.add( attributes.getLocalName(i) );
                  this.startPrefixMapping( attributes.getLocalName(i),
                                           attributes.getValue(i) );
              }
              else
              {
                  componentAttr.addAttribute( attributes.getURI( i ),
                                              attributes.getLocalName( i ),
                                              attributes.getQName( i ),
                                              attributes.getType( i ),
                                              attributes.getValue( i ) );
              }
          }
  
          final DefaultConfiguration configuration =
              createConfiguration( localName, namespaceURI, getLocationString() );
          final int size = m_elements.size() - 1;
  
          if( size > -1 )
          {
              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 );
          }
  
          m_elements.add( configuration );
          m_prefixes.add( prefixes );
  
          final int attributesSize = componentAttr.getLength();
  
          for( int i = 0; i < attributesSize; i++ )
          {
              final String name = componentAttr.getQName( i );
              final String value = componentAttr.getValue( i );
              configuration.setAttribute( name, value );
          }
      }
  
      /**
       * This just throws an exception on a parse error.
       */
      public void error( final SAXParseException exception )
          throws SAXException
      {
          throw exception;
      }
  
      /**
       * This just throws an exception on a parse error.
       */
      public void warning( final SAXParseException exception )
          throws SAXException
      {
          throw exception;
      }
  
      /**
       * This just throws an exception on a parse error.
       */
      public void fatalError( final SAXParseException exception )
          throws SAXException
      {
          throw exception;
      }
  
      protected String getLocationString()
      {
          if( null == m_locator )
          {
              return "Unknown";
          }
          else
          {
              return
                  m_locator.getSystemId() + ":" +
                  m_locator.getLineNumber() + ":" +
                  m_locator.getColumnNumber();
          }
      }
  
      public void startPrefixMapping(String prefix, String uri)
          throws SAXException
      {
          m_namespaceSupport.declarePrefix( prefix, uri );
          super.startPrefixMapping( prefix, uri );
      }
  }
  
  
  
  1.8       +56 -23    jakarta-avalon/src/test/org/apache/avalon/framework/configuration/test/SAXConfigurationHandlerTestCase.java
  
  Index: SAXConfigurationHandlerTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon/src/test/org/apache/avalon/framework/configuration/test/SAXConfigurationHandlerTestCase.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- SAXConfigurationHandlerTestCase.java	2001/11/19 18:02:34	1.7
  +++ SAXConfigurationHandlerTestCase.java	2001/12/07 13:44:54	1.8
  @@ -11,6 +11,7 @@
   import junit.framework.TestCase;
   import org.apache.avalon.framework.configuration.Configuration;
   import org.apache.avalon.framework.configuration.SAXConfigurationHandler;
  +import org.apache.avalon.framework.configuration.NamespacedSAXConfigurationHandler;
   import org.xml.sax.Attributes;
   import org.xml.sax.helpers.AttributesImpl;
   
  @@ -21,8 +22,6 @@
    */
   public final class SAXConfigurationHandlerTestCase extends TestCase
   {
  -    private SAXConfigurationHandler m_handler;
  -
       public SAXConfigurationHandlerTestCase()
       {
           this("SAXConfigurationHandler Test Case ");
  @@ -33,16 +32,6 @@
           super( name );
       }
   
  -    public void setUp()
  -    {
  -        m_handler = new SAXConfigurationHandler( );
  -    }
  -
  -    public void tearDowm()
  -    {
  -        m_handler = null;
  -    }
  -
       /**
        * Test the ContentHandler.  The XML created should look like this:
        *
  @@ -52,8 +41,52 @@
        *   &lt;/rawName&gt;
        * </pre>
        */
  -    public void testHandling() throws Exception
  +    public void testDefaultHandling() throws Exception
       {
  +        SAXConfigurationHandler handler = new SAXConfigurationHandler( );
  +
  +        final String rootURI = "";
  +        final String rootlocal = "rawName";
  +        final String rootraw = "rawName";
  +        final String childURI = "namespaceURI";
  +        final String childlocal = "localName";
  +        final String childraw = "child:" + childlocal;
  +        final String childvalue = "value";
  +        final String attqName = "attqName";
  +        final String attValue = "attValue";
  +
  +        final AttributesImpl attributes  = new AttributesImpl();
  +        attributes.addAttribute("",attqName,attqName,
  +                                "CDATA",attValue);
  +
  +        final AttributesImpl childAttributes  = new AttributesImpl();
  +        childAttributes.addAttribute("", "child", "xmlns:child", "CDATA", childURI);
  +
  +        handler.startDocument();
  +        handler.startPrefixMapping( "child", childURI );
  +        handler.startElement( rootURI, rootlocal, rootraw, attributes );
  +        handler.startElement( childURI,
  +                                childlocal,
  +                                childraw,
  +                                childAttributes );
  +
  +        handler.characters( childvalue.toCharArray(), 0, childvalue.length() );
  +        handler.endElement( childURI, childlocal, childraw );
  +        handler.endElement( null, null, rootraw);
  +        handler.endPrefixMapping( "child" );
  +        handler.endDocument();
  +
  +        final Configuration configuration = handler.getConfiguration();
  +        assertEquals( attValue, configuration.getAttribute(attqName));
  +        assertEquals( childvalue, configuration.getChild(childraw).getValue());
  +        assertEquals( "", configuration.getChild(childraw).getNamespace() );
  +        assertEquals( rootraw, configuration.getName());
  +    }
  +
  +    public void testNamespaceHandling() throws Exception
  +    {
  +        SAXConfigurationHandler handler = new NamespacedSAXConfigurationHandler( );
  +
           final String rootURI = "";
           final String rootlocal = "rawName";
           final String rootraw = "rawName";
  @@ -71,21 +104,21 @@
           final AttributesImpl childAttributes  = new AttributesImpl();
           childAttributes.addAttribute("", "child", "xmlns:child", "CDATA", childURI);
   
  -        m_handler.startDocument();
  -        m_handler.startPrefixMapping( "child", childURI );
  -        m_handler.startElement( rootURI, rootlocal, rootraw, attributes );
  -        m_handler.startElement( childURI,
  +        handler.startDocument();
  +        handler.startPrefixMapping( "child", childURI );
  +        handler.startElement( rootURI, rootlocal, rootraw, attributes );
  +        handler.startElement( childURI,
                                   childlocal,
                                   childraw,
                                   childAttributes );
   
  -        m_handler.characters( childvalue.toCharArray(), 0, childvalue.length() );
  -        m_handler.endElement( childURI, childlocal, childraw );
  -        m_handler.endElement( null, null, rootraw);
  -        m_handler.endPrefixMapping( "child" );
  -        m_handler.endDocument();
  +        handler.characters( childvalue.toCharArray(), 0, childvalue.length() );
  +        handler.endElement( childURI, childlocal, childraw );
  +        handler.endElement( null, null, rootraw);
  +        handler.endPrefixMapping( "child" );
  +        handler.endDocument();
   
  -        final Configuration configuration = m_handler.getConfiguration();
  +        final Configuration configuration = handler.getConfiguration();
           assertEquals( attValue, configuration.getAttribute(attqName));
           assertEquals( childvalue, configuration.getChild(childlocal).getValue());
           assertEquals( childURI, configuration.getChild(childlocal).getNamespace() );
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>