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