src/share/jaxws_classes/com/sun/istack/internal/XMLStreamReaderToContentHandler.java

Tue, 09 Apr 2013 14:51:13 +0100

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 637
9c07ef4934dd
permissions
-rw-r--r--

8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com

ohair@286 1 /*
alanb@368 2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
ohair@286 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286 4 *
ohair@286 5 * This code is free software; you can redistribute it and/or modify it
ohair@286 6 * under the terms of the GNU General Public License version 2 only, as
ohair@286 7 * published by the Free Software Foundation. Oracle designates this
ohair@286 8 * particular file as subject to the "Classpath" exception as provided
ohair@286 9 * by Oracle in the LICENSE file that accompanied this code.
ohair@286 10 *
ohair@286 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286 14 * version 2 for more details (a copy is included in the LICENSE file that
ohair@286 15 * accompanied this code).
ohair@286 16 *
ohair@286 17 * You should have received a copy of the GNU General Public License version
ohair@286 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286 20 *
ohair@286 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286 22 * or visit www.oracle.com if you need additional information or have any
ohair@286 23 * questions.
ohair@286 24 */
ohair@286 25
ohair@286 26 package com.sun.istack.internal;
ohair@286 27
ohair@286 28 import org.xml.sax.ContentHandler;
ohair@286 29 import org.xml.sax.SAXException;
ohair@286 30 import org.xml.sax.Locator;
ohair@286 31 import org.xml.sax.Attributes;
ohair@286 32 import org.xml.sax.helpers.AttributesImpl;
ohair@286 33
ohair@286 34 import javax.xml.stream.XMLStreamReader;
ohair@286 35 import javax.xml.stream.XMLStreamException;
ohair@286 36 import javax.xml.stream.XMLStreamConstants;
ohair@286 37 import javax.xml.namespace.QName;
ohair@286 38
ohair@286 39 /**
ohair@286 40 * This is a simple utility class that adapts StAX events from an
ohair@286 41 * {@link XMLStreamReader} to SAX events on a
ohair@286 42 * {@link ContentHandler}, bridging between the two
ohair@286 43 * parser technologies.
ohair@286 44 *
ohair@286 45 * @author Ryan.Shoemaker@Sun.COM
ohair@286 46 * @version 1.0
ohair@286 47 */
ohair@286 48 public class XMLStreamReaderToContentHandler {
ohair@286 49
ohair@286 50 // StAX event source
ohair@286 51 private final XMLStreamReader staxStreamReader;
ohair@286 52
ohair@286 53 // SAX event sink
ohair@286 54 private final ContentHandler saxHandler;
ohair@286 55
ohair@286 56 // if true, when the conversion is completed, leave the cursor to the last
ohair@286 57 // event that was fired (such as end element)
ohair@286 58 private final boolean eagerQuit;
ohair@286 59
ohair@286 60 /**
ohair@286 61 * If true, not start/endDocument event.
ohair@286 62 */
ohair@286 63 private final boolean fragment;
ohair@286 64
ohair@286 65 // array of the even length of the form { prefix0, uri0, prefix1, uri1, ... }
ohair@286 66 private final String[] inscopeNamespaces;
ohair@286 67
ohair@286 68 /**
ohair@286 69 * @see #XMLStreamReaderToContentHandler(XMLStreamReader, ContentHandler, boolean, boolean, String[])
ohair@286 70 */
ohair@286 71 public XMLStreamReaderToContentHandler(XMLStreamReader staxCore, ContentHandler saxCore, boolean eagerQuit, boolean fragment) {
ohair@286 72 this(staxCore, saxCore, eagerQuit, fragment, new String[0]);
ohair@286 73 }
ohair@286 74
ohair@286 75 /**
ohair@286 76 * Construct a new StAX to SAX adapter that will convert a StAX event
ohair@286 77 * stream into a SAX event stream.
ohair@286 78 *
ohair@286 79 * @param staxCore
ohair@286 80 * StAX event source
ohair@286 81 * @param saxCore
ohair@286 82 * SAXevent sink
ohair@286 83 * @param eagerQuit
ohair@286 84 * @param fragment
ohair@286 85 * @param inscopeNamespaces
ohair@286 86 * array of the even length of the form { prefix0, uri0, prefix1, uri1, ... }
ohair@286 87 */
ohair@286 88 public XMLStreamReaderToContentHandler(XMLStreamReader staxCore, ContentHandler saxCore,
ohair@286 89 boolean eagerQuit, boolean fragment, String[] inscopeNamespaces) {
ohair@286 90 this.staxStreamReader = staxCore;
ohair@286 91 this.saxHandler = saxCore;
ohair@286 92 this.eagerQuit = eagerQuit;
ohair@286 93 this.fragment = fragment;
ohair@286 94 this.inscopeNamespaces = inscopeNamespaces;
ohair@286 95 assert inscopeNamespaces.length%2 == 0;
ohair@286 96 }
ohair@286 97
ohair@286 98
ohair@286 99 /*
ohair@286 100 * @see StAXReaderToContentHandler#bridge()
ohair@286 101 */
ohair@286 102 public void bridge() throws XMLStreamException {
ohair@286 103
ohair@286 104 try {
ohair@286 105 // remembers the nest level of elements to know when we are done.
ohair@286 106 int depth=0;
ohair@286 107
ohair@286 108 // if the parser is at the start tag, proceed to the first element
ohair@286 109 int event = staxStreamReader.getEventType();
ohair@286 110 if(event == XMLStreamConstants.START_DOCUMENT) {
ohair@286 111 // nextTag doesn't correctly handle DTDs
ohair@286 112 while( !staxStreamReader.isStartElement() )
ohair@286 113 event = staxStreamReader.next();
ohair@286 114 }
ohair@286 115
ohair@286 116
ohair@286 117 if( event!=XMLStreamConstants.START_ELEMENT)
ohair@286 118 throw new IllegalStateException("The current event is not START_ELEMENT\n but " + event);
ohair@286 119
ohair@286 120 handleStartDocument();
ohair@286 121
ohair@286 122 for(int i=0; i < inscopeNamespaces.length; i+=2) {
ohair@286 123 saxHandler.startPrefixMapping(inscopeNamespaces[i], inscopeNamespaces[i+1]);
ohair@286 124 }
ohair@286 125
ohair@286 126 OUTER:
ohair@286 127 do {
ohair@286 128 // These are all of the events listed in the javadoc for
ohair@286 129 // XMLEvent.
ohair@286 130 // The spec only really describes 11 of them.
ohair@286 131 switch (event) {
ohair@286 132 case XMLStreamConstants.START_ELEMENT :
ohair@286 133 depth++;
ohair@286 134 handleStartElement();
ohair@286 135 break;
ohair@286 136 case XMLStreamConstants.END_ELEMENT :
ohair@286 137 handleEndElement();
ohair@286 138 depth--;
ohair@286 139 if(depth==0 && eagerQuit)
ohair@286 140 break OUTER;
ohair@286 141 break;
ohair@286 142 case XMLStreamConstants.CHARACTERS :
ohair@286 143 handleCharacters();
ohair@286 144 break;
ohair@286 145 case XMLStreamConstants.ENTITY_REFERENCE :
ohair@286 146 handleEntityReference();
ohair@286 147 break;
ohair@286 148 case XMLStreamConstants.PROCESSING_INSTRUCTION :
ohair@286 149 handlePI();
ohair@286 150 break;
ohair@286 151 case XMLStreamConstants.COMMENT :
ohair@286 152 handleComment();
ohair@286 153 break;
ohair@286 154 case XMLStreamConstants.DTD :
ohair@286 155 handleDTD();
ohair@286 156 break;
ohair@286 157 case XMLStreamConstants.ATTRIBUTE :
ohair@286 158 handleAttribute();
ohair@286 159 break;
ohair@286 160 case XMLStreamConstants.NAMESPACE :
ohair@286 161 handleNamespace();
ohair@286 162 break;
ohair@286 163 case XMLStreamConstants.CDATA :
ohair@286 164 handleCDATA();
ohair@286 165 break;
ohair@286 166 case XMLStreamConstants.ENTITY_DECLARATION :
ohair@286 167 handleEntityDecl();
ohair@286 168 break;
ohair@286 169 case XMLStreamConstants.NOTATION_DECLARATION :
ohair@286 170 handleNotationDecl();
ohair@286 171 break;
ohair@286 172 case XMLStreamConstants.SPACE :
ohair@286 173 handleSpace();
ohair@286 174 break;
ohair@286 175 default :
ohair@286 176 throw new InternalError("processing event: " + event);
ohair@286 177 }
ohair@286 178
ohair@286 179 event=staxStreamReader.next();
ohair@286 180 } while (depth!=0);
ohair@286 181
ohair@286 182 for(int i=0; i < inscopeNamespaces.length; i+=2) {
ohair@286 183 saxHandler.endPrefixMapping(inscopeNamespaces[i]);
ohair@286 184 }
ohair@286 185
ohair@286 186 handleEndDocument();
ohair@286 187 } catch (SAXException e) {
ohair@286 188 throw new XMLStreamException2(e);
ohair@286 189 }
ohair@286 190 }
ohair@286 191
ohair@286 192 private void handleEndDocument() throws SAXException {
ohair@286 193 if(fragment)
ohair@286 194 return;
ohair@286 195
ohair@286 196 saxHandler.endDocument();
ohair@286 197 }
ohair@286 198
ohair@286 199 private void handleStartDocument() throws SAXException {
ohair@286 200 if(fragment)
ohair@286 201 return;
ohair@286 202
ohair@286 203 saxHandler.setDocumentLocator(new Locator() {
ohair@286 204 public int getColumnNumber() {
ohair@286 205 return staxStreamReader.getLocation().getColumnNumber();
ohair@286 206 }
ohair@286 207 public int getLineNumber() {
ohair@286 208 return staxStreamReader.getLocation().getLineNumber();
ohair@286 209 }
ohair@286 210 public String getPublicId() {
ohair@286 211 return staxStreamReader.getLocation().getPublicId();
ohair@286 212 }
ohair@286 213 public String getSystemId() {
ohair@286 214 return staxStreamReader.getLocation().getSystemId();
ohair@286 215 }
ohair@286 216 });
ohair@286 217 saxHandler.startDocument();
ohair@286 218 }
ohair@286 219
ohair@286 220 private void handlePI() throws XMLStreamException {
ohair@286 221 try {
ohair@286 222 saxHandler.processingInstruction(
ohair@286 223 staxStreamReader.getPITarget(),
ohair@286 224 staxStreamReader.getPIData());
ohair@286 225 } catch (SAXException e) {
ohair@286 226 throw new XMLStreamException2(e);
ohair@286 227 }
ohair@286 228 }
ohair@286 229
ohair@286 230 private void handleCharacters() throws XMLStreamException {
ohair@286 231 try {
ohair@286 232 saxHandler.characters(
ohair@286 233 staxStreamReader.getTextCharacters(),
ohair@286 234 staxStreamReader.getTextStart(),
ohair@286 235 staxStreamReader.getTextLength() );
ohair@286 236 } catch (SAXException e) {
ohair@286 237 throw new XMLStreamException2(e);
ohair@286 238 }
ohair@286 239 }
ohair@286 240
ohair@286 241 private void handleEndElement() throws XMLStreamException {
ohair@286 242 QName qName = staxStreamReader.getName();
ohair@286 243
ohair@286 244 try {
ohair@286 245 String pfix = qName.getPrefix();
ohair@286 246 String rawname = (pfix == null || pfix.length() == 0)
ohair@286 247 ? qName.getLocalPart()
ohair@286 248 : pfix + ':' + qName.getLocalPart();
ohair@286 249 // fire endElement
ohair@286 250 saxHandler.endElement(
ohair@286 251 qName.getNamespaceURI(),
ohair@286 252 qName.getLocalPart(),
ohair@286 253 rawname);
ohair@286 254
ohair@286 255 // end namespace bindings
ohair@286 256 int nsCount = staxStreamReader.getNamespaceCount();
ohair@286 257 for (int i = nsCount - 1; i >= 0; i--) {
ohair@286 258 String prefix = staxStreamReader.getNamespacePrefix(i);
ohair@286 259 if (prefix == null) { // true for default namespace
ohair@286 260 prefix = "";
ohair@286 261 }
ohair@286 262 saxHandler.endPrefixMapping(prefix);
ohair@286 263 }
ohair@286 264 } catch (SAXException e) {
ohair@286 265 throw new XMLStreamException2(e);
ohair@286 266 }
ohair@286 267 }
ohair@286 268
ohair@286 269 private void handleStartElement() throws XMLStreamException {
ohair@286 270
ohair@286 271 try {
ohair@286 272 // start namespace bindings
ohair@286 273 int nsCount = staxStreamReader.getNamespaceCount();
ohair@286 274 for (int i = 0; i < nsCount; i++) {
ohair@286 275 saxHandler.startPrefixMapping(
ohair@286 276 fixNull(staxStreamReader.getNamespacePrefix(i)),
ohair@286 277 fixNull(staxStreamReader.getNamespaceURI(i)));
ohair@286 278 }
ohair@286 279
ohair@286 280 // fire startElement
ohair@286 281 QName qName = staxStreamReader.getName();
ohair@286 282 String prefix = qName.getPrefix();
ohair@286 283 String rawname;
ohair@286 284 if(prefix==null || prefix.length()==0)
ohair@286 285 rawname = qName.getLocalPart();
ohair@286 286 else
ohair@286 287 rawname = prefix + ':' + qName.getLocalPart();
ohair@286 288 Attributes attrs = getAttributes();
ohair@286 289 saxHandler.startElement(
ohair@286 290 qName.getNamespaceURI(),
ohair@286 291 qName.getLocalPart(),
ohair@286 292 rawname,
ohair@286 293 attrs);
ohair@286 294 } catch (SAXException e) {
ohair@286 295 throw new XMLStreamException2(e);
ohair@286 296 }
ohair@286 297 }
ohair@286 298
ohair@286 299 private static String fixNull(String s) {
ohair@286 300 if(s==null) return "";
ohair@286 301 else return s;
ohair@286 302 }
ohair@286 303
ohair@286 304 /**
ohair@286 305 * Get the attributes associated with the given START_ELEMENT or ATTRIBUTE
ohair@286 306 * StAXevent.
ohair@286 307 *
ohair@286 308 * @return the StAX attributes converted to an org.xml.sax.Attributes
ohair@286 309 */
ohair@286 310 private Attributes getAttributes() {
ohair@286 311 AttributesImpl attrs = new AttributesImpl();
ohair@286 312
ohair@286 313 int eventType = staxStreamReader.getEventType();
ohair@286 314 if (eventType != XMLStreamConstants.ATTRIBUTE
ohair@286 315 && eventType != XMLStreamConstants.START_ELEMENT) {
ohair@286 316 throw new InternalError(
ohair@286 317 "getAttributes() attempting to process: " + eventType);
ohair@286 318 }
ohair@286 319
ohair@286 320 // in SAX, namespace declarations are not part of attributes by default.
ohair@286 321 // (there's a property to control that, but as far as we are concerned
ohair@286 322 // we don't use it.) So don't add xmlns:* to attributes.
ohair@286 323
ohair@286 324 // gather non-namespace attrs
ohair@286 325 for (int i = 0; i < staxStreamReader.getAttributeCount(); i++) {
ohair@286 326 String uri = staxStreamReader.getAttributeNamespace(i);
ohair@286 327 if(uri==null) uri="";
ohair@286 328 String localName = staxStreamReader.getAttributeLocalName(i);
ohair@286 329 String prefix = staxStreamReader.getAttributePrefix(i);
ohair@286 330 String qName;
ohair@286 331 if(prefix==null || prefix.length()==0)
ohair@286 332 qName = localName;
ohair@286 333 else
ohair@286 334 qName = prefix + ':' + localName;
ohair@286 335 String type = staxStreamReader.getAttributeType(i);
ohair@286 336 String value = staxStreamReader.getAttributeValue(i);
ohair@286 337
ohair@286 338 attrs.addAttribute(uri, localName, qName, type, value);
ohair@286 339 }
ohair@286 340
ohair@286 341 return attrs;
ohair@286 342 }
ohair@286 343
ohair@286 344 private void handleNamespace() {
ohair@286 345 // no-op ???
ohair@286 346 // namespace events don't normally occur outside of a startElement
ohair@286 347 // or endElement
ohair@286 348 }
ohair@286 349
ohair@286 350 private void handleAttribute() {
ohair@286 351 // no-op ???
ohair@286 352 // attribute events don't normally occur outside of a startElement
ohair@286 353 // or endElement
ohair@286 354 }
ohair@286 355
ohair@286 356 private void handleDTD() {
ohair@286 357 // no-op ???
ohair@286 358 // it seems like we need to pass this info along, but how?
ohair@286 359 }
ohair@286 360
ohair@286 361 private void handleComment() {
ohair@286 362 // no-op ???
ohair@286 363 }
ohair@286 364
ohair@286 365 private void handleEntityReference() {
ohair@286 366 // no-op ???
ohair@286 367 }
ohair@286 368
ohair@286 369 private void handleSpace() {
ohair@286 370 // no-op ???
ohair@286 371 // this event is listed in the javadoc, but not in the spec.
ohair@286 372 }
ohair@286 373
ohair@286 374 private void handleNotationDecl() {
ohair@286 375 // no-op ???
ohair@286 376 // this event is listed in the javadoc, but not in the spec.
ohair@286 377 }
ohair@286 378
ohair@286 379 private void handleEntityDecl() {
ohair@286 380 // no-op ???
ohair@286 381 // this event is listed in the javadoc, but not in the spec.
ohair@286 382 }
ohair@286 383
ohair@286 384 private void handleCDATA() {
ohair@286 385 // no-op ???
ohair@286 386 // this event is listed in the javadoc, but not in the spec.
ohair@286 387 }
ohair@286 388 }

mercurial