avalon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mirceat...@apache.org
Subject cvs commit: jakarta-avalon-excalibur/xmlutil/src/java/org/apache/excalibur/xml/sax DocumentHandlerAdapter.java
Date Wed, 16 Oct 2002 17:14:20 GMT
mirceatoma    2002/10/16 10:14:20

  Modified:    xmlutil/src/java/org/apache/excalibur/xml/sax
                        DocumentHandlerAdapter.java
  Log:
  Adapter for deprecated DocumentHandler.
  
  Revision  Changes    Path
  1.4       +174 -58   jakarta-avalon-excalibur/xmlutil/src/java/org/apache/excalibur/xml/sax/DocumentHandlerAdapter.java
  
  Index: DocumentHandlerAdapter.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/xmlutil/src/java/org/apache/excalibur/xml/sax/DocumentHandlerAdapter.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- DocumentHandlerAdapter.java	16 Oct 2002 00:01:36 -0000	1.3
  +++ DocumentHandlerAdapter.java	16 Oct 2002 17:14:19 -0000	1.4
  @@ -5,119 +5,235 @@
    * version 1.1, a copy of which has been included with this distribution in
    * the LICENSE.txt file.
    */
  -
   package org.apache.excalibur.xml.sax;
   
   import java.util.Enumeration;
  -import org.xml.sax.AttributeList;
  +import org.xml.sax.Attributes;
   import org.xml.sax.ContentHandler;
   import org.xml.sax.DocumentHandler;
   import org.xml.sax.Locator;
   import org.xml.sax.SAXException;
  -import org.xml.sax.helpers.AttributesImpl;
  +import org.xml.sax.helpers.AttributeListImpl;
   import org.xml.sax.helpers.NamespaceSupport;
   
   /**
  - * This class is an utility class "wrapping" around a SAX version 2.0
  - * {@link ContentHandler} and forwarding the events to it.
  + * This class is an utility class adapting a SAX version 1.0
  + * {@link DocumentHandler} to receive SAX version 2.0 events.
    *
    * @author <a href="mailto:mirceatoma@apache.org">Mircea Toma</a>
    * @version CVS $Revision$ $Date$
    */
  -
  -public class DocumentHandlerAdapter implements DocumentHandler
  -{    
  +public class DocumentHandlerAdapter implements ContentHandler
  +{
       private final static String XMLNS = "xmlns";
       private final static String XMLNS_PREFIX = "xmlns:";
  -    private final ContentHandler m_handler;
  +    private final static String CDATA = "CDATA";
  +    private final DocumentHandler m_documentHandler;
       private final NamespaceSupport m_support = new NamespaceSupport();
  +    private boolean m_contextPushed = false;
       
  -    public DocumentHandlerAdapter( ContentHandler handler )
  +    /**
  +     * Create a new <code>ContentHandlerWrapper</code> instance.
  +     */
  +    public DocumentHandlerAdapter(final DocumentHandler documentHandler)
       {
  -        m_handler = handler;
  +        m_documentHandler = documentHandler;
       }
       
  -    public void setDocumentLocator( Locator locator )
  +    /**
  +     * Receive an object for locating the origin of SAX document events.
  +     */
  +    public void setDocumentLocator( final Locator locator )
       {
  -        m_handler.setDocumentLocator( locator );
  +        m_documentHandler.setDocumentLocator( locator );
       }
       
  +    /**
  +     * Receive notification of the beginning of a document.
  +     */
       public void startDocument() throws SAXException
       {
  -        m_handler.startDocument();
  +        m_documentHandler.startDocument();
       }
       
  -    public void endDocument() throws SAXException
  +    /**
  +     * Receive notification of the end of a document.
  +     */
  +    public void endDocument()
  +        throws SAXException
       {
  -        m_handler.endDocument();
  +        m_documentHandler.endDocument();
       }
       
  -    public void characters( char ch[], int start, int length ) throws SAXException
  +    /**
  +     * Begin the scope of a prefix-URI Namespace mapping.
  +     */
  +    public void startPrefixMapping( final String prefix, final String uri ) throws SAXException
       {
  -        m_handler.characters( ch, start, length );
  -    }
  -    
  -    public void ignorableWhitespace( char ch[], int start, int length ) throws SAXException
  -    {
  -        m_handler.ignorableWhitespace( ch, start, length );
  +        if ( !m_contextPushed )
  +        {
  +            m_support.pushContext();
  +            m_contextPushed = true;
  +        }
  +        
  +        m_support.declarePrefix( prefix, uri );
       }
       
  -    public void processingInstruction( String target, String data ) throws SAXException
  +    /**
  +     * End the scope of a prefix-URI mapping.
  +     */
  +    public void endPrefixMapping( final String prefix ) throws SAXException
       {
  -        m_handler.processingInstruction( target, data );
  +        //do nothing
       }
       
  -    public void startElement( String name, AttributeList atts ) throws SAXException
  +    /**
  +     * Receive notification of the beginning of an element.
  +     */
  +    public void startElement( final String uri,
  +                              final String loc,
  +                              final String raw,
  +                              final Attributes a ) throws SAXException
       {
  -        m_support.pushContext();
  -        
  -        for (int i = 0; i < atts.getLength(); i++)
  +        if ( !m_contextPushed )
           {
  -            final String attributeName = atts.getName(i);
  -            if ( attributeName.startsWith( XMLNS_PREFIX ) )
  -            {
  -                m_support.declarePrefix( attributeName.substring( 6 ), atts.getValue( i
) );
  -            }
  -            else if ( attributeName.equals( XMLNS ) )
  -            {
  -                m_support.declarePrefix( "", atts.getValue( i ) );
  -            }
  +            m_support.pushContext();
           }
  +        m_contextPushed = false;
           
  -        final AttributesImpl attributes = new AttributesImpl();
  -        for ( int i = 0; i < atts.getLength(); i++ )
  +        final String name = getTagName(loc, raw, uri);
  +        
  +        final AttributeListImpl attributeList = new AttributeListImpl();
  +        for (int i = 0; i < a.getLength(); i++)
           {
  -            final String attributeName = atts.getName(i);
  -            if ( !attributeName.startsWith( XMLNS_PREFIX ) && !attributeName.equals(
XMLNS ) )
  +            String attributeName = a.getQName( i );
  +            if ( ( attributeName == null ) || ( attributeName.length() == 0 ) )
               {
  -                final String[] parts = m_support.processName( name, new String[3], true
);
  -                attributes.addAttribute(parts[0], parts[1], parts[2], atts.getType( i ),
atts.getValue( i ) );
  +                final String attributeNamespaceURI = a.getURI(i);
  +                final String attributeLocalName = a.getLocalName(i);
  +                if ( attributeNamespaceURI.length() == 0 )
  +                {
  +                    attributeName = attributeLocalName;
  +                }
  +                else
  +                {
  +                    final String prefix = m_support.getPrefix( attributeNamespaceURI );
  +                    if (prefix == null) 
  +                    {                        
  +                        throw new SAXException( "No attribute prefix for namespace URI:
" + attributeNamespaceURI );
  +                    }
  +                    attributeName = prefix + ':' + attributeLocalName;
  +                }
               }
  +            attributeList.addAttribute( attributeName, a.getType( i ), a.getValue( i )
);
           }
           
           final Enumeration e = m_support.getDeclaredPrefixes();
           while( e.hasMoreElements() )
           {
               final String prefix = (String)e.nextElement();
  -            m_handler.startPrefixMapping( prefix, m_support.getURI( prefix ) );
  +            final String namespaceURI = m_support.getURI( prefix );
  +            if ( prefix.length() == 0 )
  +            {
  +                attributeList.addAttribute( XMLNS, CDATA, uri );
  +            }
  +            else
  +            {
  +                attributeList.addAttribute( XMLNS_PREFIX + prefix, CDATA, uri );
  +            }
           }
           
  -        final String[] parts = m_support.processName( name, new String[3], false );
  -        m_handler.startElement( parts[0], parts[1], parts[2], attributes );
  +        m_documentHandler.startElement( name, attributeList );
       }
       
  -    public void endElement( String name ) throws SAXException
  +    /**
  +     * Receive notification of the end of an element.
  +     */
  +    public void endElement( final String uri,
  +                            final String loc,
  +                            final String raw ) throws SAXException
       {
  -        final String[] parts = m_support.processName( name, new String[3], false );
  -        m_handler.endElement( parts[0], parts[1], parts[2] );
  -        
  -        final Enumeration e = m_support.getDeclaredPrefixes();
  -        while( e.hasMoreElements() )
  +        final String name = getTagName( loc, raw, uri );        
  +        m_documentHandler.endElement( name );        
  +        m_support.popContext();
  +    }
  +    
  +    /**
  +     * Receive notification of character data.
  +     */
  +    public void characters( final char[] ch,
  +                            final int start,
  +                            final int len ) throws SAXException
  +    {
  +        m_documentHandler.characters( ch, start, len );
  +    }
  +    
  +    /**
  +     * Receive notification of ignorable whitespace in element content.
  +     */
  +    public void ignorableWhitespace( final char[] ch,
  +                                     final int start,
  +                                     final int len ) throws SAXException
  +    {
  +        m_documentHandler.ignorableWhitespace( ch, start, len );
  +    }
  +    
  +    /**
  +     * Receive notification of a processing instruction.
  +     */
  +    public void processingInstruction( final String target,
  +                                       final String data ) throws SAXException
  +    {
  +        m_documentHandler.processingInstruction( target, data );
  +    }
  +    
  +    /**
  +     * Receive notification of a skipped entity.
  +     *
  +     * @param name The name of the skipped entity.  If it is a  parameter
  +     *             entity, the name will begin with '%'.
  +     */
  +    public void skippedEntity( final String name ) throws SAXException
  +    {
  +        //do nothing
  +    }
  +    
  +    private String getTagName( String loc, String raw, String uri ) throws SAXException
  +    {
  +        if (raw != null && raw.length() > 0) 
           {
  -            final String prefix = (String)e.nextElement();
  -            m_handler.endPrefixMapping( prefix );
  +            return raw;
           }
  -        
  -        m_support.popContext();
  +        else
  +        {
  +            final String prefix = getTagPrefix( uri );            
  +            return ( ( prefix.length() == 0 ) ? "" : ( prefix + ':' ) ) + loc;
  +        }
  +    }
  +    
  +    private String getTagPrefix( String uri ) throws SAXException
  +    {
  +        if ( m_support.getPrefix( uri ) == null )
  +        {
  +            if ( ( uri == null ) || ( uri.length() < 1 ) )
  +            {
  +                return "";
  +            }
  +            else
  +            {
  +                final String defaultURI = m_support.getURI( "" );
  +                if ( ( defaultURI != null ) && defaultURI.equals( uri ) ) 
  +                {
  +                    return ""; // default namespace
  +                }
  +                else
  +                {
  +                    throw new SAXException( "No element prefix for namespace URI: " + uri
);
  +                }
  +            }
  +        }
  +        else {
  +            return m_support.getPrefix( uri );
  +        }        
       }
   }
  
  
  

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


Mime
View raw message