KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > excalibur > xml > sax > ContentHandlerAdapter


1 /*
2  * Copyright 2002-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.excalibur.xml.sax;
18
19 import java.util.Enumeration JavaDoc;
20
21 import org.xml.sax.AttributeList JavaDoc;
22 import org.xml.sax.ContentHandler JavaDoc;
23 import org.xml.sax.DocumentHandler JavaDoc;
24 import org.xml.sax.Locator JavaDoc;
25 import org.xml.sax.SAXException JavaDoc;
26 import org.xml.sax.helpers.AttributesImpl JavaDoc;
27 import org.xml.sax.helpers.NamespaceSupport JavaDoc;
28
29 /**
30  * This class is an utility class adapting a SAX version 2.0
31  * {@link ContentHandler} to receive SAX version 1.0 events.
32  *
33  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
34  * @version CVS $Revision: 1.4 $ $Date: 2004/02/28 11:47:20 $
35  */

36
37 public class ContentHandlerAdapter
38     implements DocumentHandler JavaDoc
39 {
40     private final static String JavaDoc XMLNS = "xmlns";
41     private final static String JavaDoc XMLNS_PREFIX = "xmlns:";
42
43     private final ContentHandler JavaDoc m_handler;
44     private final NamespaceSupport JavaDoc m_support = new NamespaceSupport JavaDoc();
45
46     public ContentHandlerAdapter( final ContentHandler JavaDoc handler )
47     {
48         m_handler = handler;
49     }
50
51     public void setDocumentLocator( final Locator JavaDoc locator )
52     {
53         m_handler.setDocumentLocator( locator );
54     }
55
56     public void startDocument() throws SAXException JavaDoc
57     {
58         m_handler.startDocument();
59     }
60
61     public void endDocument() throws SAXException JavaDoc
62     {
63         m_handler.endDocument();
64     }
65
66     public void characters( final char ch[],
67                             final int start,
68                             final int length ) throws SAXException JavaDoc
69     {
70         m_handler.characters( ch, start, length );
71     }
72
73     public void ignorableWhitespace( final char ch[],
74                                      final int start,
75                                      final int length ) throws SAXException JavaDoc
76     {
77         m_handler.ignorableWhitespace( ch, start, length );
78     }
79
80     public void processingInstruction( final String JavaDoc target,
81                                        final String JavaDoc data ) throws SAXException JavaDoc
82     {
83         m_handler.processingInstruction( target, data );
84     }
85
86     public void startElement( final String JavaDoc name,
87                               final AttributeList JavaDoc atts ) throws SAXException JavaDoc
88     {
89         m_support.pushContext();
90
91         for( int i = 0; i < atts.getLength(); i++ )
92         {
93             final String JavaDoc attributeName = atts.getName( i );
94             if( attributeName.startsWith( XMLNS_PREFIX ) )
95             {
96                 m_support.declarePrefix( attributeName.substring( 6 ), atts.getValue( i ) );
97             }
98             else if( attributeName.equals( XMLNS ) )
99             {
100                 m_support.declarePrefix( "", atts.getValue( i ) );
101             }
102         }
103
104         final AttributesImpl JavaDoc attributes = new AttributesImpl JavaDoc();
105         for( int i = 0; i < atts.getLength(); i++ )
106         {
107             final String JavaDoc attributeName = atts.getName( i );
108             if( !attributeName.startsWith( XMLNS_PREFIX ) && !attributeName.equals( XMLNS ) )
109             {
110                 final String JavaDoc[] parts = m_support.processName( attributeName, new String JavaDoc[ 3 ], true );
111                 attributes.addAttribute( parts[ 0 ], parts[ 1 ], parts[ 2 ], atts.getType( i ), atts.getValue( i ) );
112             }
113         }
114
115         final Enumeration JavaDoc e = m_support.getDeclaredPrefixes();
116         while( e.hasMoreElements() )
117         {
118             final String JavaDoc prefix = (String JavaDoc)e.nextElement();
119             m_handler.startPrefixMapping( prefix, m_support.getURI( prefix ) );
120         }
121
122         final String JavaDoc[] parts = m_support.processName( name, new String JavaDoc[ 3 ], false );
123         m_handler.startElement( parts[ 0 ], parts[ 1 ], parts[ 2 ], attributes );
124     }
125
126     public void endElement( final String JavaDoc name ) throws SAXException JavaDoc
127     {
128         final String JavaDoc[] parts = m_support.processName( name, new String JavaDoc[ 3 ], false );
129         m_handler.endElement( parts[ 0 ], parts[ 1 ], parts[ 2 ] );
130
131         final Enumeration JavaDoc e = m_support.getDeclaredPrefixes();
132         while( e.hasMoreElements() )
133         {
134             final String JavaDoc prefix = (String JavaDoc)e.nextElement();
135             m_handler.endPrefixMapping( prefix );
136         }
137
138         m_support.popContext();
139     }
140 }
141
Popular Tags