1 /*
2 * Copyright (c) 2005, 2016, 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
102 //Main Work Starts Here.
103 public void parse() throws IOException, SAXException, XMLStreamException {
104 bridge();
105 }
106
107
108 /**
109 * This class is only used internally so this method should never
110 * be called.
111 */
112 public void parse(String sysId) throws IOException, SAXException {
113 throw new IOException("This method is not yet implemented.");
114 }
115
116
117 public void bridge() throws XMLStreamException {
118
119 try {
120 // remembers the nest level of elements to know when we are done.
121 int depth=0;
122
123 // skip over START_DOCUMENT
124 int event = staxStreamReader.getEventType();
125 if (event == XMLStreamConstants.START_DOCUMENT) {
126 event = staxStreamReader.next();
127 }
128
129 // If not a START_ELEMENT (e.g., a DTD), skip to next tag
130 if (event != XMLStreamConstants.START_ELEMENT) {
131 event = staxStreamReader.nextTag();
132 // An error if a START_ELEMENT isn't found now
133 if (event != XMLStreamConstants.START_ELEMENT) {
134 throw new IllegalStateException("The current event is " +
135 "not START_ELEMENT\n but" + event);
136 }
137 }
138
139 handleStartDocument();
140
141 do {
142 // These are all of the events listed in the javadoc for
143 // XMLEvent.
144 // The spec only really describes 11 of them.
145 switch (event) {
146 case XMLStreamConstants.START_ELEMENT :
147 depth++;
148 handleStartElement();
149 break;
150 case XMLStreamConstants.END_ELEMENT :
151 handleEndElement();
152 depth--;
153 break;
154 case XMLStreamConstants.CHARACTERS :
155 handleCharacters();
156 break;
157 case XMLStreamConstants.ENTITY_REFERENCE :
158 handleEntityReference();
159 break;
160 case XMLStreamConstants.PROCESSING_INSTRUCTION :
173 handleNamespace();
174 break;
175 case XMLStreamConstants.CDATA :
176 handleCDATA();
177 break;
178 case XMLStreamConstants.ENTITY_DECLARATION :
179 handleEntityDecl();
180 break;
181 case XMLStreamConstants.NOTATION_DECLARATION :
182 handleNotationDecl();
183 break;
184 case XMLStreamConstants.SPACE :
185 handleSpace();
186 break;
187 default :
188 throw new InternalError("processing event: " + event);
189 }
190
191 event=staxStreamReader.next();
192 } while (depth!=0);
193
194 handleEndDocument();
195 } catch (SAXException e) {
196 throw new XMLStreamException(e);
197 }
198 }
199
200 private void handleEndDocument() throws SAXException {
201 _sax.endDocument();
202 }
203
204 private void handleStartDocument() throws SAXException {
205 _sax.setDocumentLocator(new Locator2() {
206 public int getColumnNumber() {
207 return staxStreamReader.getLocation().getColumnNumber();
208 }
209 public int getLineNumber() {
210 return staxStreamReader.getLocation().getLineNumber();
211 }
212 public String getPublicId() {
|
1 /*
2 * Copyright (c) 2005, 2019, 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
102 //Main Work Starts Here.
103 public void parse() throws IOException, SAXException, XMLStreamException {
104 bridge();
105 }
106
107
108 /**
109 * This class is only used internally so this method should never
110 * be called.
111 */
112 public void parse(String sysId) throws IOException, SAXException {
113 throw new IOException("This method is not yet implemented.");
114 }
115
116
117 public void bridge() throws XMLStreamException {
118
119 try {
120 // remembers the nest level of elements to know when we are done.
121 int depth=0;
122 boolean startedAtDocument = false;
123
124 // skip over START_DOCUMENT
125 int event = staxStreamReader.getEventType();
126 if (event == XMLStreamConstants.START_DOCUMENT) {
127 startedAtDocument = true;
128 event = staxStreamReader.next();
129 }
130
131 handleStartDocument();
132
133 // Handle the prolog: http://www.w3.org/TR/REC-xml/#NT-prolog
134 while (event != XMLStreamConstants.START_ELEMENT) {
135 switch (event) {
136 case XMLStreamConstants.CHARACTERS :
137 handleCharacters();
138 break;
139 case XMLStreamConstants.PROCESSING_INSTRUCTION :
140 handlePI();
141 break;
142 case XMLStreamConstants.COMMENT :
143 handleComment();
144 break;
145 case XMLStreamConstants.DTD :
146 handleDTD();
147 break;
148 case XMLStreamConstants.SPACE :
149 handleSpace();
150 break;
151 default :
152 throw new InternalError("processing prolog event: " + event);
153 }
154 event=staxStreamReader.next();
155 }
156
157 do {
158 // These are all of the events listed in the javadoc for
159 // XMLEvent.
160 // The spec only really describes 11 of them.
161 switch (event) {
162 case XMLStreamConstants.START_ELEMENT :
163 depth++;
164 handleStartElement();
165 break;
166 case XMLStreamConstants.END_ELEMENT :
167 handleEndElement();
168 depth--;
169 break;
170 case XMLStreamConstants.CHARACTERS :
171 handleCharacters();
172 break;
173 case XMLStreamConstants.ENTITY_REFERENCE :
174 handleEntityReference();
175 break;
176 case XMLStreamConstants.PROCESSING_INSTRUCTION :
189 handleNamespace();
190 break;
191 case XMLStreamConstants.CDATA :
192 handleCDATA();
193 break;
194 case XMLStreamConstants.ENTITY_DECLARATION :
195 handleEntityDecl();
196 break;
197 case XMLStreamConstants.NOTATION_DECLARATION :
198 handleNotationDecl();
199 break;
200 case XMLStreamConstants.SPACE :
201 handleSpace();
202 break;
203 default :
204 throw new InternalError("processing event: " + event);
205 }
206
207 event=staxStreamReader.next();
208 } while (depth!=0);
209
210 if (startedAtDocument) {
211 // Handle the Misc (http://www.w3.org/TR/REC-xml/#NT-Misc) that can follow the document element
212 while (event != XMLStreamConstants.END_DOCUMENT) {
213 switch (event) {
214 case XMLStreamConstants.CHARACTERS :
215 handleCharacters();
216 break;
217 case XMLStreamConstants.PROCESSING_INSTRUCTION :
218 handlePI();
219 break;
220 case XMLStreamConstants.COMMENT :
221 handleComment();
222 break;
223 case XMLStreamConstants.SPACE :
224 handleSpace();
225 break;
226 default :
227 throw new InternalError("processing misc event after document element: " + event);
228 }
229 event=staxStreamReader.next();
230 }
231 }
232
233 handleEndDocument();
234 } catch (SAXException e) {
235 throw new XMLStreamException(e);
236 }
237 }
238
239 private void handleEndDocument() throws SAXException {
240 _sax.endDocument();
241 }
242
243 private void handleStartDocument() throws SAXException {
244 _sax.setDocumentLocator(new Locator2() {
245 public int getColumnNumber() {
246 return staxStreamReader.getLocation().getColumnNumber();
247 }
248 public int getLineNumber() {
249 return staxStreamReader.getLocation().getLineNumber();
250 }
251 public String getPublicId() {
|