001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.servicemix.soap.util.stax;
018:
019: import javax.xml.XMLConstants;
020: import javax.xml.stream.XMLStreamConstants;
021: import javax.xml.stream.XMLStreamException;
022: import javax.xml.stream.XMLStreamReader;
023: import javax.xml.transform.sax.SAXSource;
024:
025: import org.xml.sax.Attributes;
026: import org.xml.sax.ContentHandler;
027: import org.xml.sax.DTDHandler;
028: import org.xml.sax.EntityResolver;
029: import org.xml.sax.ErrorHandler;
030: import org.xml.sax.InputSource;
031: import org.xml.sax.SAXException;
032: import org.xml.sax.SAXNotRecognizedException;
033: import org.xml.sax.SAXNotSupportedException;
034: import org.xml.sax.SAXParseException;
035: import org.xml.sax.XMLReader;
036: import org.xml.sax.helpers.AttributesImpl;
037:
038: public class StaxSource extends SAXSource implements XMLReader {
039:
040: private XMLStreamReader streamReader;
041:
042: private ContentHandler contentHandler;
043:
044: public StaxSource(XMLStreamReader streamReader) {
045: this .streamReader = streamReader;
046: setInputSource(new InputSource());
047: }
048:
049: public XMLReader getXMLReader() {
050: return this ;
051: }
052:
053: protected void parse() throws SAXException {
054: try {
055: contentHandler.startDocument();
056: while (true) {
057: switch (streamReader.getEventType()) {
058: case XMLStreamConstants.ATTRIBUTE:
059: case XMLStreamConstants.CDATA:
060: break;
061: case XMLStreamConstants.CHARACTERS:
062: if (!streamReader.isWhiteSpace()) {
063:
064: int length = streamReader.getTextLength();
065: int start = streamReader.getTextStart();
066: char[] chars = streamReader.getTextCharacters();
067: contentHandler.characters(chars, start, length);
068: /*
069: for (int textLength = streamReader.getTextLength(); textLength > 0; textLength -= chars.length) {
070: int l = Math.min(textLength, chars.length);
071: streamReader.getTextCharacters(0, chars, 0, l);
072: contentHandler.characters(chars, 0, l);
073: }
074: */
075: }
076: break;
077: case XMLStreamConstants.COMMENT:
078: case XMLStreamConstants.DTD:
079: break;
080: case XMLStreamConstants.END_DOCUMENT:
081: contentHandler.endDocument();
082: return;
083: case XMLStreamConstants.END_ELEMENT: {
084: String uri = streamReader.getNamespaceURI();
085: String localName = streamReader.getLocalName();
086: String prefix = streamReader.getPrefix();
087: String qname = prefix != null
088: && prefix.length() > 0 ? prefix + ":"
089: + localName : localName;
090: contentHandler.endElement(uri, localName, qname);
091: for (int i = 0; i < streamReader
092: .getNamespaceCount(); i++) {
093: //contentHandler.endPrefixMapping(streamReader.getNamespaceURI(i));
094: }
095: break;
096: }
097: case XMLStreamConstants.ENTITY_DECLARATION:
098: case XMLStreamConstants.ENTITY_REFERENCE:
099: case XMLStreamConstants.NAMESPACE:
100: case XMLStreamConstants.NOTATION_DECLARATION:
101: case XMLStreamConstants.PROCESSING_INSTRUCTION:
102: case XMLStreamConstants.SPACE:
103: case XMLStreamConstants.START_DOCUMENT:
104: break;
105: case XMLStreamConstants.START_ELEMENT: {
106: for (int i = 0; i < streamReader
107: .getNamespaceCount(); i++) {
108: //contentHandler.startPrefixMapping(streamReader.getNamespacePrefix(i),
109: // streamReader.getNamespaceURI(i));
110: }
111: String uri = streamReader.getNamespaceURI();
112: String localName = streamReader.getLocalName();
113: String prefix = streamReader.getPrefix();
114: String qname = prefix != null
115: && prefix.length() > 0 ? prefix + ":"
116: + localName : localName;
117: contentHandler.startElement(uri, localName, qname,
118: getAttributes());
119: break;
120: }
121: }
122: streamReader.next();
123: }
124: } catch (XMLStreamException e) {
125: if (e.getLocation() != null) {
126: throw new SAXParseException(e.getMessage(), null, null,
127: e.getLocation().getLineNumber(), e
128: .getLocation().getColumnNumber(), e);
129: } else {
130: throw new SAXParseException(e.getMessage(), null, null,
131: -1, -1, e);
132: }
133: }
134: }
135:
136: protected String getQualifiedName() {
137: String prefix = streamReader.getPrefix();
138: if (prefix != null && prefix.length() > 0) {
139: return prefix + ":" + streamReader.getLocalName();
140: } else {
141: return streamReader.getLocalName();
142: }
143: }
144:
145: protected Attributes getAttributes() {
146: AttributesImpl attrs = new AttributesImpl();
147: // Adding namespace declaration as attributes is necessary because
148: // the xalan implementation that ships with SUN JDK 1.4 is bugged
149: // and does not handle the startPrefixMapping method
150: for (int i = 0; i < streamReader.getNamespaceCount(); i++) {
151: String prefix = streamReader.getNamespacePrefix(i);
152: String uri = streamReader.getNamespaceURI(i);
153: if (uri == null) {
154: uri = "";
155: }
156: // Default namespace
157: if (prefix == null || prefix.length() == 0) {
158: attrs.addAttribute(XMLConstants.DEFAULT_NS_PREFIX,
159: null, XMLConstants.XMLNS_ATTRIBUTE, "CDATA",
160: uri);
161: } else {
162: attrs.addAttribute(XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
163: prefix, XMLConstants.XMLNS_ATTRIBUTE + ":"
164: + prefix, "CDATA", uri);
165: }
166: }
167: for (int i = 0; i < streamReader.getAttributeCount(); i++) {
168: String uri = streamReader.getAttributeNamespace(i);
169: String localName = streamReader.getAttributeLocalName(i);
170: String prefix = streamReader.getAttributePrefix(i);
171: String qName;
172: if (prefix != null && prefix.length() > 0) {
173: qName = prefix + ':' + localName;
174: } else {
175: qName = localName;
176: }
177: String type = streamReader.getAttributeType(i);
178: String value = streamReader.getAttributeValue(i);
179: if (value == null) {
180: value = "";
181: }
182: attrs.addAttribute(uri, localName, qName, type, value);
183: }
184: return attrs;
185: }
186:
187: public boolean getFeature(String name)
188: throws SAXNotRecognizedException, SAXNotSupportedException {
189: return false;
190: }
191:
192: public void setFeature(String name, boolean value)
193: throws SAXNotRecognizedException, SAXNotSupportedException {
194: }
195:
196: public Object getProperty(String name)
197: throws SAXNotRecognizedException, SAXNotSupportedException {
198: return null;
199: }
200:
201: public void setProperty(String name, Object value)
202: throws SAXNotRecognizedException, SAXNotSupportedException {
203: }
204:
205: public void setEntityResolver(EntityResolver resolver) {
206: }
207:
208: public EntityResolver getEntityResolver() {
209: return null;
210: }
211:
212: public void setDTDHandler(DTDHandler handler) {
213: }
214:
215: public DTDHandler getDTDHandler() {
216: return null;
217: }
218:
219: public void setContentHandler(ContentHandler handler) {
220: this .contentHandler = handler;
221: }
222:
223: public ContentHandler getContentHandler() {
224: return this .contentHandler;
225: }
226:
227: public void setErrorHandler(ErrorHandler handler) {
228: }
229:
230: public ErrorHandler getErrorHandler() {
231: return null;
232: }
233:
234: public void parse(InputSource input) throws SAXException {
235: StaxSource.this .parse();
236: }
237:
238: public void parse(String systemId) throws SAXException {
239: StaxSource.this.parse();
240: }
241:
242: }
|