001: /*
002: * Fast Infoset ver. 0.1 software ("Software")
003: *
004: * Copyright, 2004-2005 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * Software is licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License. You may
008: * obtain a copy of the License at:
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
014: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
015: * License for the specific language governing permissions and limitations.
016: *
017: * Sun supports and benefits from the global community of open source
018: * developers, and thanks the community for its important contributions and
019: * open standards-based technology, which Sun has adopted into many of its
020: * products.
021: *
022: * Please note that portions of Software may be provided with notices and
023: * open source licenses from such communities and third parties that govern the
024: * use of those portions, and any licenses granted hereunder do not alter any
025: * rights and obligations you may have under such open source licenses,
026: * however, the disclaimer of warranty and limitation of liability provisions
027: * in this License will apply to all Software in this distribution.
028: *
029: * You acknowledge that the Software is not designed, licensed or intended
030: * for use in the design, construction, operation or maintenance of any nuclear
031: * facility.
032: *
033: * Apache License
034: * Version 2.0, January 2004
035: * http://www.apache.org/licenses/
036: *
037: */
038:
039: package com.sun.xml.fastinfoset.tools;
040:
041: import javax.xml.namespace.QName;
042: import javax.xml.stream.XMLStreamConstants;
043: import javax.xml.stream.XMLStreamException;
044: import javax.xml.stream.XMLStreamReader;
045: import org.xml.sax.ContentHandler;
046: import org.xml.sax.SAXException;
047: import org.xml.sax.ext.LexicalHandler;
048: import org.xml.sax.helpers.AttributesImpl;
049: import com.sun.xml.fastinfoset.CommonResourceBundle;
050:
051: public class StAX2SAXReader {
052:
053: /**
054: * Content handler where events are pushed.
055: */
056: ContentHandler _handler;
057:
058: /**
059: * Lexical handler to report lexical events.
060: */
061: LexicalHandler _lexicalHandler;
062:
063: /**
064: * XML stream reader where events are pulled.
065: */
066: XMLStreamReader _reader;
067:
068: public StAX2SAXReader(XMLStreamReader reader, ContentHandler handler) {
069: _handler = handler;
070: _reader = reader;
071: }
072:
073: public StAX2SAXReader(XMLStreamReader reader) {
074: _reader = reader;
075: }
076:
077: public void setContentHandler(ContentHandler handler) {
078: _handler = handler;
079: }
080:
081: public void setLexicalHandler(LexicalHandler lexicalHandler) {
082: _lexicalHandler = lexicalHandler;
083: }
084:
085: public void adapt() throws XMLStreamException, SAXException {
086: QName qname;
087: String prefix, localPart;
088: AttributesImpl attrs = new AttributesImpl();
089: char[] buffer;
090: int nsc;
091: int nat;
092:
093: _handler.startDocument();
094:
095: try {
096:
097: while (_reader.hasNext()) {
098: int event = _reader.next();
099:
100: switch (event) {
101: case XMLStreamConstants.START_ELEMENT: {
102: // Report namespace events first
103: nsc = _reader.getNamespaceCount();
104: for (int i = 0; i < nsc; i++) {
105: _handler.startPrefixMapping(_reader
106: .getNamespacePrefix(i), _reader
107: .getNamespaceURI(i));
108: }
109:
110: // Collect list of attributes
111: attrs.clear();
112: nat = _reader.getAttributeCount();
113: for (int i = 0; i < nat; i++) {
114: QName q = _reader.getAttributeName(i);
115: String qName = _reader.getAttributePrefix(i);
116: if (qName == null || qName == "") {
117: qName = q.getLocalPart();
118: } else {
119: qName = qName + ":" + q.getLocalPart();
120: }
121: attrs.addAttribute(_reader
122: .getAttributeNamespace(i), q
123: .getLocalPart(), qName, _reader
124: .getAttributeType(i), _reader
125: .getAttributeValue(i));
126: }
127:
128: // Report start element
129: qname = _reader.getName();
130: prefix = qname.getPrefix();
131: localPart = qname.getLocalPart();
132:
133: _handler.startElement(_reader.getNamespaceURI(),
134: localPart, (prefix.length() > 0) ? (prefix
135: + ":" + localPart) : localPart,
136: attrs);
137: break;
138: }
139: case XMLStreamConstants.END_ELEMENT: {
140: // Report end element
141: qname = _reader.getName();
142: prefix = qname.getPrefix();
143: localPart = qname.getLocalPart();
144:
145: _handler.endElement(_reader.getNamespaceURI(),
146: localPart, (prefix.length() > 0) ? (prefix
147: + ":" + localPart) : localPart);
148:
149: // Report end namespace events
150: nsc = _reader.getNamespaceCount();
151: for (int i = 0; i < nsc; i++) {
152: _handler.endPrefixMapping(_reader
153: .getNamespacePrefix(i));
154: }
155: break;
156: }
157: case XMLStreamConstants.CHARACTERS:
158: _handler.characters(_reader.getTextCharacters(),
159: _reader.getTextStart(), _reader
160: .getTextLength());
161: break;
162: case XMLStreamConstants.COMMENT:
163: _lexicalHandler.comment(
164: _reader.getTextCharacters(), _reader
165: .getTextStart(), _reader
166: .getTextLength());
167: break;
168: case XMLStreamConstants.PROCESSING_INSTRUCTION:
169: _handler.processingInstruction(_reader
170: .getPITarget(), _reader.getPIData());
171: break;
172: case XMLStreamConstants.END_DOCUMENT:
173: break;
174: default:
175: throw new RuntimeException(CommonResourceBundle
176: .getInstance().getString(
177: "message.StAX2SAXReader",
178: new Object[] { Integer
179: .valueOf(event) }));
180: } // switch
181: }
182: } catch (XMLStreamException e) {
183: _handler.endDocument(); // flush whatever we have
184: throw e;
185: }
186:
187: _handler.endDocument();
188: }
189:
190: }
|