KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > jaxp > StaxSource


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

17 package org.apache.servicemix.jbi.jaxp;
18
19 import javax.xml.XMLConstants JavaDoc;
20 import javax.xml.stream.XMLStreamConstants;
21 import javax.xml.stream.XMLStreamException;
22 import javax.xml.stream.XMLStreamReader;
23 import javax.xml.transform.sax.SAXSource JavaDoc;
24
25 import org.xml.sax.Attributes JavaDoc;
26 import org.xml.sax.ContentHandler JavaDoc;
27 import org.xml.sax.DTDHandler JavaDoc;
28 import org.xml.sax.EntityResolver JavaDoc;
29 import org.xml.sax.ErrorHandler JavaDoc;
30 import org.xml.sax.InputSource JavaDoc;
31 import org.xml.sax.SAXException JavaDoc;
32 import org.xml.sax.SAXNotRecognizedException JavaDoc;
33 import org.xml.sax.SAXNotSupportedException JavaDoc;
34 import org.xml.sax.SAXParseException JavaDoc;
35 import org.xml.sax.XMLReader JavaDoc;
36 import org.xml.sax.helpers.AttributesImpl JavaDoc;
37
38 public class StaxSource extends SAXSource JavaDoc implements XMLReader JavaDoc {
39
40     private XMLStreamReader streamReader;
41
42     private ContentHandler JavaDoc contentHandler;
43
44     public StaxSource(XMLStreamReader streamReader) {
45         this.streamReader = streamReader;
46         setInputSource(new InputSource JavaDoc());
47     }
48
49     public XMLReader JavaDoc getXMLReader() {
50         return this;
51     }
52
53     protected void parse() throws SAXException JavaDoc {
54         try {
55             contentHandler.startDocument();
56             while (true) {
57                 switch (streamReader.getEventType()) {
58                 case XMLStreamConstants.ATTRIBUTE:
59                 case XMLStreamConstants.CDATA:
60                     break;
61                 case XMLStreamConstants.CHARACTERS:
62                     if (!streamReader.isWhiteSpace()) {
63                         
64                         int length = streamReader.getTextLength();
65                         int start = streamReader.getTextStart();
66                         char[] chars = streamReader.getTextCharacters();
67                         contentHandler.characters(chars, start, length);
68                         /*
69                         for (int textLength = streamReader.getTextLength(); textLength > 0; textLength -= chars.length) {
70                             int l = Math.min(textLength, chars.length);
71                             streamReader.getTextCharacters(0, chars, 0, l);
72                             contentHandler.characters(chars, 0, l);
73                         }
74                         */

75                     }
76                     break;
77                 case XMLStreamConstants.COMMENT:
78                 case XMLStreamConstants.DTD:
79                     break;
80                 case XMLStreamConstants.END_DOCUMENT:
81                     contentHandler.endDocument();
82                     return;
83                 case XMLStreamConstants.END_ELEMENT: {
84                     String JavaDoc uri = streamReader.getNamespaceURI();
85                     String JavaDoc localName = streamReader.getLocalName();
86                     String JavaDoc prefix = streamReader.getPrefix();
87                     String JavaDoc qname = prefix != null && prefix.length() > 0 ? prefix + ":" + localName : localName;
88                     contentHandler.endElement(uri, localName, qname);
89                     for (int i = 0; i < streamReader.getNamespaceCount(); i++) {
90                         //contentHandler.endPrefixMapping(streamReader.getNamespaceURI(i));
91
}
92                     break;
93                 }
94                 case XMLStreamConstants.ENTITY_DECLARATION:
95                 case XMLStreamConstants.ENTITY_REFERENCE:
96                 case XMLStreamConstants.NAMESPACE:
97                 case XMLStreamConstants.NOTATION_DECLARATION:
98                 case XMLStreamConstants.PROCESSING_INSTRUCTION:
99                 case XMLStreamConstants.SPACE:
100                 case XMLStreamConstants.START_DOCUMENT:
101                     break;
102                 case XMLStreamConstants.START_ELEMENT: {
103                     for (int i = 0; i < streamReader.getNamespaceCount(); i++) {
104                         //contentHandler.startPrefixMapping(streamReader.getNamespacePrefix(i),
105
// streamReader.getNamespaceURI(i));
106
}
107                     String JavaDoc uri = streamReader.getNamespaceURI();
108                     String JavaDoc localName = streamReader.getLocalName();
109                     String JavaDoc prefix = streamReader.getPrefix();
110                     String JavaDoc qname = prefix != null && prefix.length() > 0 ? prefix + ":" + localName : localName;
111                     contentHandler.startElement(uri, localName, qname, getAttributes());
112                     break;
113                 }
114                 }
115                 streamReader.next();
116             }
117         } catch (XMLStreamException e) {
118             if (e.getLocation() != null) {
119                 throw new SAXParseException JavaDoc(e.getMessage(), null, null, e.getLocation().getLineNumber(), e.getLocation()
120                         .getColumnNumber(), e);
121             } else {
122                 throw new SAXParseException JavaDoc(e.getMessage(), null, null, -1, -1, e);
123             }
124         }
125     }
126
127     protected String JavaDoc getQualifiedName() {
128         String JavaDoc prefix = streamReader.getPrefix();
129         if (prefix != null && prefix.length() > 0) {
130             return prefix + ":" + streamReader.getLocalName();
131         } else {
132             return streamReader.getLocalName();
133         }
134     }
135
136     protected Attributes JavaDoc getAttributes() {
137         AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
138         // Adding namespace declaration as attributes is necessary because
139
// the xalan implementation that ships with SUN JDK 1.4 is bugged
140
// and does not handle the startPrefixMapping method
141
for (int i = 0; i < streamReader.getNamespaceCount(); i++) {
142             String JavaDoc prefix = streamReader.getNamespacePrefix(i);
143             String JavaDoc uri = streamReader.getNamespaceURI(i);
144             if (uri == null) {
145                 uri = "";
146             }
147             // Default namespace
148
if (prefix == null || prefix.length() == 0) {
149                 attrs.addAttribute(XMLConstants.DEFAULT_NS_PREFIX,
150                                    null,
151                                    XMLConstants.XMLNS_ATTRIBUTE,
152                                    "CDATA",
153                                    uri);
154             } else {
155                 attrs.addAttribute(XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
156                                    prefix,
157                                    XMLConstants.XMLNS_ATTRIBUTE + ":" + prefix,
158                                    "CDATA",
159                                    uri);
160             }
161         }
162         for (int i = 0; i < streamReader.getAttributeCount(); i++) {
163             String JavaDoc uri = streamReader.getAttributeNamespace(i);
164             String JavaDoc localName = streamReader.getAttributeLocalName(i);
165             String JavaDoc prefix = streamReader.getAttributePrefix(i);
166             String JavaDoc qName;
167             if (prefix != null && prefix.length() > 0) {
168                 qName = prefix + ':' + localName;
169             } else {
170                 qName = localName;
171             }
172             String JavaDoc type = streamReader.getAttributeType(i);
173             String JavaDoc value = streamReader.getAttributeValue(i);
174             if (value == null) {
175                 value = "";
176             }
177             attrs.addAttribute(uri, localName, qName, type, value);
178         }
179         return attrs;
180     }
181
182     public boolean getFeature(String JavaDoc name) throws SAXNotRecognizedException JavaDoc, SAXNotSupportedException JavaDoc {
183         return false;
184     }
185
186     public void setFeature(String JavaDoc name, boolean value) throws SAXNotRecognizedException JavaDoc, SAXNotSupportedException JavaDoc {
187     }
188
189     public Object JavaDoc getProperty(String JavaDoc name) throws SAXNotRecognizedException JavaDoc, SAXNotSupportedException JavaDoc {
190         return null;
191     }
192
193     public void setProperty(String JavaDoc name, Object JavaDoc value) throws SAXNotRecognizedException JavaDoc, SAXNotSupportedException JavaDoc {
194     }
195
196     public void setEntityResolver(EntityResolver JavaDoc resolver) {
197     }
198
199     public EntityResolver JavaDoc getEntityResolver() {
200         return null;
201     }
202
203     public void setDTDHandler(DTDHandler JavaDoc handler) {
204     }
205
206     public DTDHandler JavaDoc getDTDHandler() {
207         return null;
208     }
209
210     public void setContentHandler(ContentHandler JavaDoc handler) {
211         this.contentHandler = handler;
212     }
213
214     public ContentHandler JavaDoc getContentHandler() {
215         return this.contentHandler;
216     }
217
218     public void setErrorHandler(ErrorHandler JavaDoc handler) {
219     }
220
221     public ErrorHandler JavaDoc getErrorHandler() {
222         return null;
223     }
224
225     public void parse(InputSource JavaDoc input) throws SAXException JavaDoc {
226         StaxSource.this.parse();
227     }
228
229     public void parse(String JavaDoc systemId) throws SAXException JavaDoc {
230         StaxSource.this.parse();
231     }
232
233 }
234
Popular Tags