KickJava   Java API By Example, From Geeks To Geeks.

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


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.Attributes 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.AttributeListImpl JavaDoc;
27 import org.xml.sax.helpers.NamespaceSupport JavaDoc;
28
29 /**
30  * This class is an utility class adapting a SAX version 1.0
31  * {@link DocumentHandler} to receive SAX version 2.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 public class DocumentHandlerAdapter
37     implements ContentHandler JavaDoc
38 {
39     private final static String JavaDoc XMLNS = "xmlns";
40     private final static String JavaDoc XMLNS_PREFIX = "xmlns:";
41     private final static String JavaDoc CDATA = "CDATA";
42     private final DocumentHandler JavaDoc m_documentHandler;
43     private final NamespaceSupport JavaDoc m_support = new NamespaceSupport JavaDoc();
44     private boolean m_contextPushed = false;
45
46     /**
47      * Create a new <code>ContentHandlerWrapper</code> instance.
48      */

49     public DocumentHandlerAdapter( final DocumentHandler JavaDoc documentHandler )
50     {
51         m_documentHandler = documentHandler;
52     }
53
54     /**
55      * Receive an object for locating the origin of SAX document events.
56      */

57     public void setDocumentLocator( final Locator JavaDoc locator )
58     {
59         m_documentHandler.setDocumentLocator( locator );
60     }
61
62     /**
63      * Receive notification of the beginning of a document.
64      */

65     public void startDocument() throws SAXException JavaDoc
66     {
67         m_documentHandler.startDocument();
68     }
69
70     /**
71      * Receive notification of the end of a document.
72      */

73     public void endDocument()
74         throws SAXException JavaDoc
75     {
76         m_documentHandler.endDocument();
77     }
78
79     /**
80      * Begin the scope of a prefix-URI Namespace mapping.
81      */

82     public void startPrefixMapping( final String JavaDoc prefix, final String JavaDoc uri ) throws SAXException JavaDoc
83     {
84         if( !m_contextPushed )
85         {
86             m_support.pushContext();
87             m_contextPushed = true;
88         }
89
90         m_support.declarePrefix( prefix, uri );
91     }
92
93     /**
94      * End the scope of a prefix-URI mapping.
95      */

96     public void endPrefixMapping( final String JavaDoc prefix ) throws SAXException JavaDoc
97     {
98         //do nothing
99
}
100
101     /**
102      * Receive notification of the beginning of an element.
103      */

104     public void startElement( final String JavaDoc uri,
105                               final String JavaDoc loc,
106                               final String JavaDoc raw,
107                               final Attributes JavaDoc a ) throws SAXException JavaDoc
108     {
109         if( !m_contextPushed )
110         {
111             m_support.pushContext();
112         }
113         m_contextPushed = false;
114
115         final String JavaDoc name = getTagName( loc, raw, uri );
116
117         final AttributeListImpl JavaDoc attributeList = new AttributeListImpl JavaDoc();
118         for( int i = 0; i < a.getLength(); i++ )
119         {
120             String JavaDoc attributeName = a.getQName( i );
121             if( ( attributeName == null ) || ( attributeName.length() == 0 ) )
122             {
123                 final String JavaDoc attributeNamespaceURI = a.getURI( i );
124                 final String JavaDoc attributeLocalName = a.getLocalName( i );
125                 if( attributeNamespaceURI.length() == 0 )
126                 {
127                     attributeName = attributeLocalName;
128                 }
129                 else
130                 {
131                     final String JavaDoc prefix = m_support.getPrefix( attributeNamespaceURI );
132                     if( prefix == null )
133                     {
134                         throw new SAXException JavaDoc( "No attribute prefix for namespace URI: " + attributeNamespaceURI );
135                     }
136                     attributeName = prefix + ':' + attributeLocalName;
137                 }
138             }
139             attributeList.addAttribute( attributeName, a.getType( i ), a.getValue( i ) );
140         }
141
142         final Enumeration JavaDoc e = m_support.getDeclaredPrefixes();
143         while( e.hasMoreElements() )
144         {
145             final String JavaDoc prefix = (String JavaDoc)e.nextElement();
146             if( prefix.length() == 0 )
147             {
148                 attributeList.addAttribute( XMLNS, CDATA, uri );
149             }
150             else
151             {
152                 attributeList.addAttribute( XMLNS_PREFIX + prefix, CDATA, uri );
153             }
154         }
155
156         m_documentHandler.startElement( name, attributeList );
157     }
158
159     /**
160      * Receive notification of the end of an element.
161      */

162     public void endElement( final String JavaDoc uri,
163                             final String JavaDoc loc,
164                             final String JavaDoc raw ) throws SAXException JavaDoc
165     {
166         final String JavaDoc name = getTagName( loc, raw, uri );
167         m_documentHandler.endElement( name );
168         m_support.popContext();
169     }
170
171     /**
172      * Receive notification of character data.
173      */

174     public void characters( final char[] ch,
175                             final int start,
176                             final int len ) throws SAXException JavaDoc
177     {
178         m_documentHandler.characters( ch, start, len );
179     }
180
181     /**
182      * Receive notification of ignorable whitespace in element content.
183      */

184     public void ignorableWhitespace( final char[] ch,
185                                      final int start,
186                                      final int len ) throws SAXException JavaDoc
187     {
188         m_documentHandler.ignorableWhitespace( ch, start, len );
189     }
190
191     /**
192      * Receive notification of a processing instruction.
193      */

194     public void processingInstruction( final String JavaDoc target,
195                                        final String JavaDoc data ) throws SAXException JavaDoc
196     {
197         m_documentHandler.processingInstruction( target, data );
198     }
199
200     /**
201      * Receive notification of a skipped entity.
202      *
203      * @param name The name of the skipped entity. If it is a parameter
204      * entity, the name will begin with '%'.
205      */

206     public void skippedEntity( final String JavaDoc name ) throws SAXException JavaDoc
207     {
208         //do nothing
209
}
210
211     private String JavaDoc getTagName( final String JavaDoc loc, final String JavaDoc raw, final String JavaDoc uri ) throws SAXException JavaDoc
212     {
213         if( raw != null && raw.length() > 0 )
214         {
215             return raw;
216         }
217         else
218         {
219             final String JavaDoc prefix = getTagPrefix( uri );
220             return ( ( prefix.length() == 0 ) ? "" : ( prefix + ':' ) ) + loc;
221         }
222     }
223
224     private String JavaDoc getTagPrefix( final String JavaDoc uri ) throws SAXException JavaDoc
225     {
226         if( m_support.getPrefix( uri ) == null )
227         {
228             if( ( uri == null ) || ( uri.length() < 1 ) )
229             {
230                 return "";
231             }
232             else
233             {
234                 final String JavaDoc defaultURI = m_support.getURI( "" );
235                 if( ( defaultURI != null ) && defaultURI.equals( uri ) )
236                 {
237                     return ""; // default namespace
238
}
239                 else
240                 {
241                     throw new SAXException JavaDoc( "No element prefix for namespace URI: " + uri );
242                 }
243             }
244         }
245         else
246         {
247             return m_support.getPrefix( uri );
248         }
249     }
250 }
251
Popular Tags