KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > xml > fastinfoset > stax > StAX2SAXReader


1 /*
2  * Fast Infoset ver. 0.1 software ("Software")
3  *
4  * Copyright, 2004-2005 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Software is licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License. You may
8  * obtain a copy of the License at:
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations.
16  *
17  * Sun supports and benefits from the global community of open source
18  * developers, and thanks the community for its important contributions and
19  * open standards-based technology, which Sun has adopted into many of its
20  * products.
21  *
22  * Please note that portions of Software may be provided with notices and
23  * open source licenses from such communities and third parties that govern the
24  * use of those portions, and any licenses granted hereunder do not alter any
25  * rights and obligations you may have under such open source licenses,
26  * however, the disclaimer of warranty and limitation of liability provisions
27  * in this License will apply to all Software in this distribution.
28  *
29  * You acknowledge that the Software is not designed, licensed or intended
30  * for use in the design, construction, operation or maintenance of any nuclear
31  * facility.
32  *
33  * Apache License
34  * Version 2.0, January 2004
35  * http://www.apache.org/licenses/
36  *
37  */

38
39
40 package com.sun.xml.fastinfoset.stax;
41
42 import javax.xml.namespace.QName JavaDoc;
43 import javax.xml.stream.XMLStreamConstants;
44 import javax.xml.stream.XMLStreamException;
45 import javax.xml.stream.XMLStreamReader;
46 import org.xml.sax.ContentHandler JavaDoc;
47 import org.xml.sax.SAXException JavaDoc;
48 import org.xml.sax.ext.LexicalHandler JavaDoc;
49 import org.xml.sax.helpers.AttributesImpl JavaDoc;
50 import com.sun.xml.fastinfoset.CommonResourceBundle;
51
52 public class StAX2SAXReader {
53     
54     /**
55      * Content handler where events are pushed.
56      */

57     ContentHandler JavaDoc _handler;
58     
59     /**
60      * Lexical handler to report lexical events.
61      */

62     LexicalHandler JavaDoc _lexicalHandler;
63     
64     /**
65      * XML stream reader where events are pulled.
66      */

67     XMLStreamReader _reader;
68     
69     public StAX2SAXReader(XMLStreamReader reader, ContentHandler JavaDoc handler) {
70         _handler = handler;
71         _reader = reader;
72     }
73     
74     public StAX2SAXReader(XMLStreamReader reader) {
75         _reader = reader;
76     }
77
78     public void setContentHandler(ContentHandler JavaDoc handler) {
79         _handler = handler;
80     }
81
82     public void setLexicalHandler(LexicalHandler JavaDoc lexicalHandler) {
83         _lexicalHandler = lexicalHandler;
84     }
85         
86     public void adapt() throws XMLStreamException, SAXException JavaDoc {
87         QName JavaDoc qname;
88         String JavaDoc prefix, localPart;
89         AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
90         char[] buffer;
91         int nsc;
92         int nat;
93         
94         _handler.startDocument();
95         
96         try {
97             
98             while (_reader.hasNext()) {
99                 int event = _reader.next();
100
101
102                 switch(event) {
103                 case XMLStreamConstants.START_ELEMENT: {
104                     // Report namespace events first
105
nsc = _reader.getNamespaceCount();
106                     for (int i = 0; i < nsc; i++) {
107                         _handler.startPrefixMapping(_reader.getNamespacePrefix(i),
108                             _reader.getNamespaceURI(i));
109                     }
110
111                     // Collect list of attributes
112
attrs.clear();
113                     nat = _reader.getAttributeCount();
114                     for (int i = 0; i < nat; i++) {
115                         QName JavaDoc q = _reader.getAttributeName(i);
116                         String JavaDoc qName = _reader.getAttributePrefix(i);
117                         if (qName == null || qName == "") {
118                             qName = q.getLocalPart();
119                         } else {
120                             qName = qName + ":" + q.getLocalPart();
121                         }
122                         attrs.addAttribute(_reader.getAttributeNamespace(i),
123                                            q.getLocalPart(),
124                                            qName,
125                                            _reader.getAttributeType(i),
126                                            _reader.getAttributeValue(i));
127                     }
128
129                     // Report start element
130
qname = _reader.getName();
131                     prefix = qname.getPrefix();
132                     localPart = qname.getLocalPart();
133
134                     _handler.startElement(_reader.getNamespaceURI(),
135                                           localPart,
136                                           (prefix.length() > 0) ?
137                                               (prefix + ":" + localPart) : localPart,
138                                           attrs);
139                     break;
140                 }
141                 case XMLStreamConstants.END_ELEMENT: {
142                     // Report end element
143
qname = _reader.getName();
144                     prefix = qname.getPrefix();
145                     localPart = qname.getLocalPart();
146
147                     _handler.endElement(_reader.getNamespaceURI(),
148                                         localPart,
149                                         (prefix.length() > 0) ?
150                                             (prefix + ":" + localPart) : localPart);
151
152                     // Report end namespace events
153
nsc = _reader.getNamespaceCount();
154                     for (int i = 0; i < nsc; i++) {
155                         _handler.endPrefixMapping(_reader.getNamespacePrefix(i));
156                     }
157                     break;
158                 }
159                 case XMLStreamConstants.CHARACTERS:
160                     _handler.characters(_reader.getTextCharacters(), _reader.getTextStart(), _reader.getTextLength());
161                     break;
162                 case XMLStreamConstants.COMMENT:
163                     _lexicalHandler.comment(_reader.getTextCharacters(), _reader.getTextStart(), _reader.getTextLength());
164                     break;
165                 case XMLStreamConstants.PROCESSING_INSTRUCTION:
166                     _handler.processingInstruction(_reader.getPITarget(), _reader.getPIData());
167                     break;
168                 case XMLStreamConstants.END_DOCUMENT:
169                     break;
170                 default:
171                     throw new RuntimeException JavaDoc(CommonResourceBundle.getInstance().getString("message.StAX2SAXReader", new Object JavaDoc[]{new Integer JavaDoc(event)}));
172                 } // switch
173
}
174         }
175         catch (XMLStreamException e) {
176             _handler.endDocument(); // flush whatever we have
177
throw e;
178         }
179         
180         _handler.endDocument();
181     }
182
183 }
184
185
Popular Tags