Thu, 31 Aug 2017 15:18:52 +0800
merge
1 /*
2 * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 *
25 * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC.
26 */
28 package com.sun.xml.internal.fastinfoset.tools;
30 import javax.xml.namespace.QName;
31 import javax.xml.stream.XMLStreamConstants;
32 import javax.xml.stream.XMLStreamException;
33 import javax.xml.stream.XMLStreamReader;
34 import org.xml.sax.ContentHandler;
35 import org.xml.sax.SAXException;
36 import org.xml.sax.ext.LexicalHandler;
37 import org.xml.sax.helpers.AttributesImpl;
38 import com.sun.xml.internal.fastinfoset.CommonResourceBundle;
40 public class StAX2SAXReader {
42 /**
43 * Content handler where events are pushed.
44 */
45 ContentHandler _handler;
47 /**
48 * Lexical handler to report lexical events.
49 */
50 LexicalHandler _lexicalHandler;
52 /**
53 * XML stream reader where events are pulled.
54 */
55 XMLStreamReader _reader;
57 public StAX2SAXReader(XMLStreamReader reader, ContentHandler handler) {
58 _handler = handler;
59 _reader = reader;
60 }
62 public StAX2SAXReader(XMLStreamReader reader) {
63 _reader = reader;
64 }
66 public void setContentHandler(ContentHandler handler) {
67 _handler = handler;
68 }
70 public void setLexicalHandler(LexicalHandler lexicalHandler) {
71 _lexicalHandler = lexicalHandler;
72 }
74 public void adapt() throws XMLStreamException, SAXException {
75 QName qname;
76 String prefix, localPart;
77 AttributesImpl attrs = new AttributesImpl();
78 char[] buffer;
79 int nsc;
80 int nat;
82 _handler.startDocument();
84 try {
86 while (_reader.hasNext()) {
87 int event = _reader.next();
90 switch(event) {
91 case XMLStreamConstants.START_ELEMENT: {
92 // Report namespace events first
93 nsc = _reader.getNamespaceCount();
94 for (int i = 0; i < nsc; i++) {
95 _handler.startPrefixMapping(_reader.getNamespacePrefix(i),
96 _reader.getNamespaceURI(i));
97 }
99 // Collect list of attributes
100 attrs.clear();
101 nat = _reader.getAttributeCount();
102 for (int i = 0; i < nat; i++) {
103 QName q = _reader.getAttributeName(i);
104 String qName = _reader.getAttributePrefix(i);
105 if (qName == null || qName == "") {
106 qName = q.getLocalPart();
107 } else {
108 qName = qName + ":" + q.getLocalPart();
109 }
110 attrs.addAttribute(_reader.getAttributeNamespace(i),
111 q.getLocalPart(),
112 qName,
113 _reader.getAttributeType(i),
114 _reader.getAttributeValue(i));
115 }
117 // Report start element
118 qname = _reader.getName();
119 prefix = qname.getPrefix();
120 localPart = qname.getLocalPart();
122 _handler.startElement(_reader.getNamespaceURI(),
123 localPart,
124 (prefix.length() > 0) ?
125 (prefix + ":" + localPart) : localPart,
126 attrs);
127 break;
128 }
129 case XMLStreamConstants.END_ELEMENT: {
130 // Report end element
131 qname = _reader.getName();
132 prefix = qname.getPrefix();
133 localPart = qname.getLocalPart();
135 _handler.endElement(_reader.getNamespaceURI(),
136 localPart,
137 (prefix.length() > 0) ?
138 (prefix + ":" + localPart) : localPart);
140 // Report end namespace events
141 nsc = _reader.getNamespaceCount();
142 for (int i = 0; i < nsc; i++) {
143 _handler.endPrefixMapping(_reader.getNamespacePrefix(i));
144 }
145 break;
146 }
147 case XMLStreamConstants.CHARACTERS:
148 _handler.characters(_reader.getTextCharacters(), _reader.getTextStart(), _reader.getTextLength());
149 break;
150 case XMLStreamConstants.COMMENT:
151 _lexicalHandler.comment(_reader.getTextCharacters(), _reader.getTextStart(), _reader.getTextLength());
152 break;
153 case XMLStreamConstants.PROCESSING_INSTRUCTION:
154 _handler.processingInstruction(_reader.getPITarget(), _reader.getPIData());
155 break;
156 case XMLStreamConstants.END_DOCUMENT:
157 break;
158 default:
159 throw new RuntimeException(CommonResourceBundle.getInstance().getString("message.StAX2SAXReader", new Object[]{Integer.valueOf(event)}));
160 } // switch
161 }
162 }
163 catch (XMLStreamException e) {
164 _handler.endDocument(); // flush whatever we have
165 throw e;
166 }
168 _handler.endDocument();
169 }
171 }