src/share/jaxws_classes/com/sun/xml/internal/bind/v2/runtime/unmarshaller/StAXStreamConnector.java

Thu, 12 Oct 2017 19:44:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 19:44:07 +0800
changeset 760
e530533619ec
parent 658
45676aaa9d47
parent 637
9c07ef4934dd
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.xml.internal.bind.v2.runtime.unmarshaller;
aoqi@0 27
aoqi@0 28 import java.lang.reflect.Constructor;
aoqi@0 29
aoqi@0 30 import javax.xml.stream.Location;
aoqi@0 31 import javax.xml.stream.XMLStreamConstants;
aoqi@0 32 import javax.xml.stream.XMLStreamException;
aoqi@0 33 import javax.xml.stream.XMLStreamReader;
aoqi@0 34
aoqi@0 35 import com.sun.xml.internal.bind.WhiteSpaceProcessor;
aoqi@0 36
aoqi@0 37 import org.xml.sax.Attributes;
aoqi@0 38 import org.xml.sax.SAXException;
aoqi@0 39
aoqi@0 40 /**
aoqi@0 41 * Reads XML from StAX {@link XMLStreamReader} and
aoqi@0 42 * feeds events to {@link XmlVisitor}.
aoqi@0 43 * <p>
aoqi@0 44 * TODO:
aoqi@0 45 * Finding the optimized FI implementations is a bit hacky and not very
aoqi@0 46 * extensible. Can we use the service provider mechanism in general for
aoqi@0 47 * concrete implementations of StAXConnector.
aoqi@0 48 *
aoqi@0 49 * @author Ryan.Shoemaker@Sun.COM
aoqi@0 50 * @author Kohsuke Kawaguchi
aoqi@0 51 * @version JAXB 2.0
aoqi@0 52 */
aoqi@0 53 class StAXStreamConnector extends StAXConnector {
aoqi@0 54
aoqi@0 55 /**
aoqi@0 56 * Creates a {@link StAXConnector} from {@link XMLStreamReader}.
aoqi@0 57 *
aoqi@0 58 * This method checks if the parser is FI parser and acts accordingly.
aoqi@0 59 */
aoqi@0 60 public static StAXConnector create(XMLStreamReader reader, XmlVisitor visitor) {
aoqi@0 61 // try optimized codepath
aoqi@0 62 final Class readerClass = reader.getClass();
aoqi@0 63 if (FI_STAX_READER_CLASS != null && FI_STAX_READER_CLASS.isAssignableFrom(readerClass) && FI_CONNECTOR_CTOR!=null) {
aoqi@0 64 try {
aoqi@0 65 return FI_CONNECTOR_CTOR.newInstance(reader,visitor);
aoqi@0 66 } catch (Exception t) {
aoqi@0 67 }
aoqi@0 68 }
aoqi@0 69
aoqi@0 70 // Quick hack until SJSXP fixes 6270116
aoqi@0 71 boolean isZephyr = readerClass.getName().equals("com.sun.xml.internal.stream.XMLReaderImpl");
aoqi@0 72 if (getBoolProp(reader,"org.codehaus.stax2.internNames") &&
aefimov@658 73 getBoolProp(reader,"org.codehaus.stax2.internNsUris"))
aoqi@0 74 ; // no need for interning
aoqi@0 75 else
aoqi@0 76 if (isZephyr)
aoqi@0 77 ; // no need for interning
aoqi@0 78 else
aoqi@0 79 if (checkImplementaionNameOfSjsxp(reader))
aoqi@0 80 ; // no need for interning.
aoqi@0 81 else
aoqi@0 82 visitor = new InterningXmlVisitor(visitor);
aoqi@0 83
aoqi@0 84 if (STAX_EX_READER_CLASS!=null && STAX_EX_READER_CLASS.isAssignableFrom(readerClass)) {
aoqi@0 85 try {
aoqi@0 86 return STAX_EX_CONNECTOR_CTOR.newInstance(reader,visitor);
aoqi@0 87 } catch (Exception t) {
aoqi@0 88 }
aoqi@0 89 }
aoqi@0 90
aoqi@0 91 return new StAXStreamConnector(reader,visitor);
aoqi@0 92 }
aoqi@0 93
aoqi@0 94 private static boolean checkImplementaionNameOfSjsxp(XMLStreamReader reader) {
aoqi@0 95 try {
aoqi@0 96 Object name = reader.getProperty("http://java.sun.com/xml/stream/properties/implementation-name");
aoqi@0 97 return name!=null && name.equals("sjsxp");
aoqi@0 98 } catch (Exception e) {
aoqi@0 99 // be defensive against broken StAX parsers since javadoc is not clear
aoqi@0 100 // about when an error happens
aoqi@0 101 return false;
aoqi@0 102 }
aoqi@0 103 }
aoqi@0 104
aoqi@0 105 private static boolean getBoolProp(XMLStreamReader r, String n) {
aoqi@0 106 try {
aoqi@0 107 Object o = r.getProperty(n);
aoqi@0 108 if(o instanceof Boolean) return (Boolean)o;
aoqi@0 109 return false;
aoqi@0 110 } catch (Exception e) {
aoqi@0 111 // be defensive against broken StAX parsers since javadoc is not clear
aoqi@0 112 // about when an error happens
aoqi@0 113 return false;
aoqi@0 114 }
aoqi@0 115 }
aoqi@0 116
aoqi@0 117
aoqi@0 118 // StAX event source
aoqi@0 119 private final XMLStreamReader staxStreamReader;
aoqi@0 120
aoqi@0 121 /**
aoqi@0 122 * SAX may fire consecutive characters event, but we don't allow it.
aoqi@0 123 * so use this buffer to perform buffering.
aoqi@0 124 */
aoqi@0 125 protected final StringBuilder buffer = new StringBuilder();
aoqi@0 126
aoqi@0 127 /**
aoqi@0 128 * Set to true if the text() event is reported, and therefore
aoqi@0 129 * the following text() event should be suppressed.
aoqi@0 130 */
aoqi@0 131 protected boolean textReported = false;
aoqi@0 132
aoqi@0 133 protected StAXStreamConnector(XMLStreamReader staxStreamReader, XmlVisitor visitor) {
aoqi@0 134 super(visitor);
aoqi@0 135 this.staxStreamReader = staxStreamReader;
aoqi@0 136 }
aoqi@0 137
aoqi@0 138 public void bridge() throws XMLStreamException {
aoqi@0 139
aoqi@0 140 try {
aoqi@0 141 // remembers the nest level of elements to know when we are done.
aoqi@0 142 int depth=0;
aoqi@0 143
aoqi@0 144 // if the parser is at the start tag, proceed to the first element
aoqi@0 145 int event = staxStreamReader.getEventType();
aoqi@0 146 if(event == XMLStreamConstants.START_DOCUMENT) {
aoqi@0 147 // nextTag doesn't correctly handle DTDs
aoqi@0 148 while( !staxStreamReader.isStartElement() )
aoqi@0 149 event = staxStreamReader.next();
aoqi@0 150 }
aoqi@0 151
aoqi@0 152
aoqi@0 153 if( event!=XMLStreamConstants.START_ELEMENT)
aoqi@0 154 throw new IllegalStateException("The current event is not START_ELEMENT\n but " + event);
aoqi@0 155
aoqi@0 156 handleStartDocument(staxStreamReader.getNamespaceContext());
aoqi@0 157
aoqi@0 158 OUTER:
aoqi@0 159 while(true) {
aoqi@0 160 // These are all of the events listed in the javadoc for
aoqi@0 161 // XMLEvent.
aoqi@0 162 // The spec only really describes 11 of them.
aoqi@0 163 switch (event) {
aoqi@0 164 case XMLStreamConstants.START_ELEMENT :
aoqi@0 165 handleStartElement();
aoqi@0 166 depth++;
aoqi@0 167 break;
aoqi@0 168 case XMLStreamConstants.END_ELEMENT :
aoqi@0 169 depth--;
aoqi@0 170 handleEndElement();
aoqi@0 171 if(depth==0) break OUTER;
aoqi@0 172 break;
aoqi@0 173 case XMLStreamConstants.CHARACTERS :
aoqi@0 174 case XMLStreamConstants.CDATA :
aoqi@0 175 case XMLStreamConstants.SPACE :
aoqi@0 176 handleCharacters();
aoqi@0 177 break;
aoqi@0 178 // otherwise simply ignore
aoqi@0 179 }
aoqi@0 180
aoqi@0 181 event=staxStreamReader.next();
aoqi@0 182 }
aoqi@0 183
aoqi@0 184 staxStreamReader.next(); // move beyond the end tag.
aoqi@0 185
aoqi@0 186 handleEndDocument();
aoqi@0 187 } catch (SAXException e) {
aoqi@0 188 throw new XMLStreamException(e);
aoqi@0 189 }
aoqi@0 190 }
aoqi@0 191
aoqi@0 192 protected Location getCurrentLocation() {
aoqi@0 193 return staxStreamReader.getLocation();
aoqi@0 194 }
aoqi@0 195
aoqi@0 196 protected String getCurrentQName() {
aoqi@0 197 return getQName(staxStreamReader.getPrefix(),staxStreamReader.getLocalName());
aoqi@0 198 }
aoqi@0 199
aoqi@0 200 private void handleEndElement() throws SAXException {
aoqi@0 201 processText(false);
aoqi@0 202
aoqi@0 203 // fire endElement
aoqi@0 204 tagName.uri = fixNull(staxStreamReader.getNamespaceURI());
aoqi@0 205 tagName.local = staxStreamReader.getLocalName();
aoqi@0 206 visitor.endElement(tagName);
aoqi@0 207
aoqi@0 208 // end namespace bindings
aoqi@0 209 int nsCount = staxStreamReader.getNamespaceCount();
aoqi@0 210 for (int i = nsCount - 1; i >= 0; i--) {
aoqi@0 211 visitor.endPrefixMapping(fixNull(staxStreamReader.getNamespacePrefix(i)));
aoqi@0 212 }
aoqi@0 213 }
aoqi@0 214
aoqi@0 215 private void handleStartElement() throws SAXException {
aoqi@0 216 processText(true);
aoqi@0 217
aoqi@0 218 // start namespace bindings
aoqi@0 219 int nsCount = staxStreamReader.getNamespaceCount();
aoqi@0 220 for (int i = 0; i < nsCount; i++) {
aoqi@0 221 visitor.startPrefixMapping(
aefimov@658 222 fixNull(staxStreamReader.getNamespacePrefix(i)),
aefimov@658 223 fixNull(staxStreamReader.getNamespaceURI(i)));
aoqi@0 224 }
aoqi@0 225
aoqi@0 226 // fire startElement
aoqi@0 227 tagName.uri = fixNull(staxStreamReader.getNamespaceURI());
aoqi@0 228 tagName.local = staxStreamReader.getLocalName();
aoqi@0 229 tagName.atts = attributes;
aoqi@0 230
aoqi@0 231 visitor.startElement(tagName);
aoqi@0 232 }
aoqi@0 233
aoqi@0 234 /**
aoqi@0 235 * Proxy of {@link Attributes} that read from {@link XMLStreamReader}.
aoqi@0 236 */
aoqi@0 237 private final Attributes attributes = new Attributes() {
aoqi@0 238 public int getLength() {
aoqi@0 239 return staxStreamReader.getAttributeCount();
aoqi@0 240 }
aoqi@0 241
aoqi@0 242 public String getURI(int index) {
aoqi@0 243 String uri = staxStreamReader.getAttributeNamespace(index);
aoqi@0 244 if(uri==null) return "";
aoqi@0 245 return uri;
aoqi@0 246 }
aoqi@0 247
aoqi@0 248 public String getLocalName(int index) {
aoqi@0 249 return staxStreamReader.getAttributeLocalName(index);
aoqi@0 250 }
aoqi@0 251
aoqi@0 252 public String getQName(int index) {
aoqi@0 253 String prefix = staxStreamReader.getAttributePrefix(index);
aoqi@0 254 if(prefix==null || prefix.length()==0)
aoqi@0 255 return getLocalName(index);
aoqi@0 256 else
aoqi@0 257 return prefix + ':' + getLocalName(index);
aoqi@0 258 }
aoqi@0 259
aoqi@0 260 public String getType(int index) {
aoqi@0 261 return staxStreamReader.getAttributeType(index);
aoqi@0 262 }
aoqi@0 263
aoqi@0 264 public String getValue(int index) {
aoqi@0 265 return staxStreamReader.getAttributeValue(index);
aoqi@0 266 }
aoqi@0 267
aoqi@0 268 public int getIndex(String uri, String localName) {
aoqi@0 269 for( int i=getLength()-1; i>=0; i-- )
aoqi@0 270 if( localName.equals(getLocalName(i)) && uri.equals(getURI(i)))
aoqi@0 271 return i;
aoqi@0 272 return -1;
aoqi@0 273 }
aoqi@0 274
aoqi@0 275 // this method sholdn't be used that often (if at all)
aoqi@0 276 // so it's OK to be slow.
aoqi@0 277 public int getIndex(String qName) {
aoqi@0 278 for( int i=getLength()-1; i>=0; i-- ) {
aoqi@0 279 if(qName.equals(getQName(i)))
aoqi@0 280 return i;
aoqi@0 281 }
aoqi@0 282 return -1;
aoqi@0 283 }
aoqi@0 284
aoqi@0 285 public String getType(String uri, String localName) {
aoqi@0 286 int index = getIndex(uri,localName);
aoqi@0 287 if(index<0) return null;
aoqi@0 288 return getType(index);
aoqi@0 289 }
aoqi@0 290
aoqi@0 291 public String getType(String qName) {
aoqi@0 292 int index = getIndex(qName);
aoqi@0 293 if(index<0) return null;
aoqi@0 294 return getType(index);
aoqi@0 295 }
aoqi@0 296
aoqi@0 297 public String getValue(String uri, String localName) {
aoqi@0 298 int index = getIndex(uri,localName);
aoqi@0 299 if(index<0) return null;
aoqi@0 300 return getValue(index);
aoqi@0 301 }
aoqi@0 302
aoqi@0 303 public String getValue(String qName) {
aoqi@0 304 int index = getIndex(qName);
aoqi@0 305 if(index<0) return null;
aoqi@0 306 return getValue(index);
aoqi@0 307 }
aoqi@0 308 };
aoqi@0 309
aoqi@0 310 protected void handleCharacters() throws XMLStreamException, SAXException {
aoqi@0 311 if( predictor.expectText() )
aoqi@0 312 buffer.append(
aefimov@658 313 staxStreamReader.getTextCharacters(),
aefimov@658 314 staxStreamReader.getTextStart(),
aefimov@658 315 staxStreamReader.getTextLength() );
aoqi@0 316 }
aoqi@0 317
aoqi@0 318 private void processText( boolean ignorable ) throws SAXException {
aefimov@658 319 if( predictor.expectText() && (!ignorable || !WhiteSpaceProcessor.isWhiteSpace(buffer) || context.getCurrentState().isMixed())) {
aoqi@0 320 if(textReported) {
aoqi@0 321 textReported = false;
aoqi@0 322 } else {
aoqi@0 323 visitor.text(buffer);
aoqi@0 324 }
aoqi@0 325 }
aoqi@0 326 buffer.setLength(0);
aoqi@0 327 }
aoqi@0 328
aoqi@0 329
aoqi@0 330
aoqi@0 331 /**
aoqi@0 332 * Reference to FI's StAXReader class, if FI can be loaded.
aoqi@0 333 */
aoqi@0 334 private static final Class FI_STAX_READER_CLASS = initFIStAXReaderClass();
aoqi@0 335 private static final Constructor<? extends StAXConnector> FI_CONNECTOR_CTOR = initFastInfosetConnectorClass();
aoqi@0 336
aoqi@0 337 private static Class initFIStAXReaderClass() {
aoqi@0 338 try {
aoqi@0 339 Class<?> fisr = Class.forName("com.sun.xml.internal.org.jvnet.fastinfoset.stax.FastInfosetStreamReader");
aoqi@0 340 Class<?> sdp = Class.forName("com.sun.xml.internal.fastinfoset.stax.StAXDocumentParser");
aoqi@0 341 // Check if StAXDocumentParser implements FastInfosetStreamReader
aoqi@0 342 if (fisr.isAssignableFrom(sdp))
aoqi@0 343 return sdp;
aoqi@0 344 else
aoqi@0 345 return null;
aoqi@0 346 } catch (Throwable e) {
aoqi@0 347 return null;
aoqi@0 348 }
aoqi@0 349 }
aoqi@0 350
aoqi@0 351 private static Constructor<? extends StAXConnector> initFastInfosetConnectorClass() {
aoqi@0 352 try {
aoqi@0 353 if (FI_STAX_READER_CLASS == null)
aoqi@0 354 return null;
aoqi@0 355
aoqi@0 356 Class c = Class.forName(
aoqi@0 357 "com.sun.xml.internal.bind.v2.runtime.unmarshaller.FastInfosetConnector");
aoqi@0 358 return c.getConstructor(FI_STAX_READER_CLASS,XmlVisitor.class);
aoqi@0 359 } catch (Throwable e) {
aoqi@0 360 return null;
aoqi@0 361 }
aoqi@0 362 }
aoqi@0 363
aoqi@0 364 //
aoqi@0 365 // reference to StAXEx classes
aoqi@0 366 //
aoqi@0 367 private static final Class STAX_EX_READER_CLASS = initStAXExReader();
aoqi@0 368 private static final Constructor<? extends StAXConnector> STAX_EX_CONNECTOR_CTOR = initStAXExConnector();
aoqi@0 369
aoqi@0 370 private static Class initStAXExReader() {
aoqi@0 371 try {
aoqi@0 372 return Class.forName("com.sun.xml.internal.org.jvnet.staxex.XMLStreamReaderEx");
aoqi@0 373 } catch (Throwable e) {
aoqi@0 374 return null;
aoqi@0 375 }
aoqi@0 376 }
aoqi@0 377
aoqi@0 378 private static Constructor<? extends StAXConnector> initStAXExConnector() {
aoqi@0 379 try {
aoqi@0 380 Class c = Class.forName("com.sun.xml.internal.bind.v2.runtime.unmarshaller.StAXExConnector");
aoqi@0 381 return c.getConstructor(STAX_EX_READER_CLASS,XmlVisitor.class);
aoqi@0 382 } catch (Throwable e) {
aoqi@0 383 return null;
aoqi@0 384 }
aoqi@0 385 }
aoqi@0 386
aoqi@0 387 }

mercurial