src/share/jaxws_classes/com/sun/xml/internal/ws/streaming/XMLStreamReaderUtil.java

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 368
0989ad8c0860
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2012, 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.ws.streaming;
aoqi@0 27
aoqi@0 28 import javax.xml.namespace.QName;
aoqi@0 29 import static javax.xml.stream.XMLStreamConstants.*;
aoqi@0 30 import javax.xml.stream.XMLStreamException;
aoqi@0 31 import javax.xml.stream.XMLStreamReader;
aoqi@0 32 import javax.xml.stream.XMLStreamConstants;
aoqi@0 33
aoqi@0 34 /**
aoqi@0 35 * <p> XMLStreamReaderUtil provides some utility methods intended to be used
aoqi@0 36 * in conjunction with a StAX XMLStreamReader. </p>
aoqi@0 37 *
aoqi@0 38 * @author WS Development Team
aoqi@0 39 */
aoqi@0 40 public class XMLStreamReaderUtil {
aoqi@0 41
aoqi@0 42 private XMLStreamReaderUtil() {
aoqi@0 43 }
aoqi@0 44
aoqi@0 45 public static void close(XMLStreamReader reader) {
aoqi@0 46 try {
aoqi@0 47 reader.close();
aoqi@0 48 } catch (XMLStreamException e) {
aoqi@0 49 throw wrapException(e);
aoqi@0 50 }
aoqi@0 51 }
aoqi@0 52
aoqi@0 53 public static void readRest(XMLStreamReader reader) {
aoqi@0 54 try {
aoqi@0 55 while(reader.getEventType() != XMLStreamConstants.END_DOCUMENT) {
aoqi@0 56 reader.next();
aoqi@0 57 }
aoqi@0 58 } catch (XMLStreamException e) {
aoqi@0 59 throw wrapException(e);
aoqi@0 60 }
aoqi@0 61 }
aoqi@0 62
aoqi@0 63 public static int next(XMLStreamReader reader) {
aoqi@0 64 try {
aoqi@0 65 int readerEvent = reader.next();
aoqi@0 66
aoqi@0 67 while (readerEvent != END_DOCUMENT) {
aoqi@0 68 switch (readerEvent) {
aoqi@0 69 case START_ELEMENT:
aoqi@0 70 case END_ELEMENT:
aoqi@0 71 case CDATA:
aoqi@0 72 case CHARACTERS:
aoqi@0 73 case PROCESSING_INSTRUCTION:
aoqi@0 74 return readerEvent;
aoqi@0 75 default:
aoqi@0 76 // falls through ignoring event
aoqi@0 77 }
aoqi@0 78 readerEvent = reader.next();
aoqi@0 79 }
aoqi@0 80
aoqi@0 81 return readerEvent;
aoqi@0 82 }
aoqi@0 83 catch (XMLStreamException e) {
aoqi@0 84 throw wrapException(e);
aoqi@0 85 }
aoqi@0 86 }
aoqi@0 87
aoqi@0 88 public static int nextElementContent(XMLStreamReader reader) {
aoqi@0 89 int state = nextContent(reader);
aoqi@0 90 if (state == CHARACTERS) {
aoqi@0 91 throw new XMLStreamReaderException(
aoqi@0 92 "xmlreader.unexpectedCharacterContent", reader.getText());
aoqi@0 93 }
aoqi@0 94 return state;
aoqi@0 95 }
aoqi@0 96
aoqi@0 97 public static void toNextTag(XMLStreamReader reader, QName name) {
aoqi@0 98 // skip any whitespace
aoqi@0 99 if (reader.getEventType() != XMLStreamConstants.START_ELEMENT &&
aoqi@0 100 reader.getEventType() != XMLStreamConstants.END_ELEMENT) {
aoqi@0 101 XMLStreamReaderUtil.nextElementContent(reader);
aoqi@0 102 }
aoqi@0 103 if(reader.getEventType() == XMLStreamConstants.END_ELEMENT && name.equals(reader.getName())) {
aoqi@0 104 XMLStreamReaderUtil.nextElementContent(reader);
aoqi@0 105 }
aoqi@0 106 }
aoqi@0 107
aoqi@0 108 /**
aoqi@0 109 * Moves next and read spaces from the reader as long as to the next element.
aoqi@0 110 * Comments are ignored
aoqi@0 111 * @param reader
aoqi@0 112 * @return
aoqi@0 113 */
aoqi@0 114 public static String nextWhiteSpaceContent(XMLStreamReader reader) {
aoqi@0 115 next(reader);
aoqi@0 116 return currentWhiteSpaceContent(reader);
aoqi@0 117 }
aoqi@0 118
aoqi@0 119 /**
aoqi@0 120 * Read spaces from the reader as long as to the next element, starting from
aoqi@0 121 * current position. Comments are ignored.
aoqi@0 122 * @param reader
aoqi@0 123 * @return
aoqi@0 124 */
aoqi@0 125 public static String currentWhiteSpaceContent(XMLStreamReader reader) {
aoqi@0 126
aoqi@0 127 // since the there might be several valid chunks (spaces/comment/spaces)
aoqi@0 128 // StringBuilder must be used; it's initialized lazily, only when needed
aoqi@0 129 StringBuilder whiteSpaces = null;
aoqi@0 130
aoqi@0 131 for (;;) {
aoqi@0 132 switch (reader.getEventType()) {
aoqi@0 133 case START_ELEMENT:
aoqi@0 134 case END_ELEMENT:
aoqi@0 135 case END_DOCUMENT:
aoqi@0 136 return whiteSpaces == null ? null : whiteSpaces.toString();
aoqi@0 137 case CHARACTERS:
aoqi@0 138 if (reader.isWhiteSpace()) {
aoqi@0 139 if (whiteSpaces == null) {
aoqi@0 140 whiteSpaces = new StringBuilder();
aoqi@0 141 }
aoqi@0 142 whiteSpaces.append(reader.getText());
aoqi@0 143 } else {
aoqi@0 144 throw new XMLStreamReaderException(
aoqi@0 145 "xmlreader.unexpectedCharacterContent", reader.getText());
aoqi@0 146 }
aoqi@0 147 }
aoqi@0 148 next(reader);
aoqi@0 149 }
aoqi@0 150 }
aoqi@0 151
aoqi@0 152 public static int nextContent(XMLStreamReader reader) {
aoqi@0 153 for (;;) {
aoqi@0 154 int state = next(reader);
aoqi@0 155 switch (state) {
aoqi@0 156 case START_ELEMENT:
aoqi@0 157 case END_ELEMENT:
aoqi@0 158 case END_DOCUMENT:
aoqi@0 159 return state;
aoqi@0 160 case CHARACTERS:
aoqi@0 161 if (!reader.isWhiteSpace()) {
aoqi@0 162 return CHARACTERS;
aoqi@0 163 }
aoqi@0 164 }
aoqi@0 165 }
aoqi@0 166 }
aoqi@0 167
aoqi@0 168 /**
aoqi@0 169 * Skip current element, leaving the cursor at END_ELEMENT of
aoqi@0 170 * current element.
aoqi@0 171 */
aoqi@0 172 public static void skipElement(XMLStreamReader reader) {
aoqi@0 173 assert reader.getEventType() == START_ELEMENT;
aoqi@0 174 skipTags(reader, true);
aoqi@0 175 assert reader.getEventType() == END_ELEMENT;
aoqi@0 176 }
aoqi@0 177
aoqi@0 178 /**
aoqi@0 179 * Skip following siblings, leaving cursor at END_ELEMENT of
aoqi@0 180 * parent element.
aoqi@0 181 */
aoqi@0 182 public static void skipSiblings(XMLStreamReader reader, QName parent) {
aoqi@0 183 skipTags(reader, reader.getName().equals(parent));
aoqi@0 184 assert reader.getEventType() == END_ELEMENT;
aoqi@0 185 }
aoqi@0 186
aoqi@0 187 private static void skipTags(XMLStreamReader reader, boolean exitCondition) {
aoqi@0 188 try {
aoqi@0 189 int state, tags = 0;
aoqi@0 190 while ((state = reader.next()) != END_DOCUMENT) {
aoqi@0 191 if (state == START_ELEMENT) {
aoqi@0 192 tags++;
aoqi@0 193 }
aoqi@0 194 else if (state == END_ELEMENT) {
aoqi@0 195 if (tags == 0 && exitCondition) return;
aoqi@0 196 tags--;
aoqi@0 197 }
aoqi@0 198 }
aoqi@0 199 }
aoqi@0 200 catch (XMLStreamException e) {
aoqi@0 201 throw wrapException(e);
aoqi@0 202 }
aoqi@0 203 }
aoqi@0 204
aoqi@0 205 /*
aoqi@0 206 * Get the text of an element
aoqi@0 207 */
aoqi@0 208 public static String getElementText(XMLStreamReader reader) {
aoqi@0 209 try {
aoqi@0 210 return reader.getElementText();
aoqi@0 211 } catch (XMLStreamException e) {
aoqi@0 212 throw wrapException(e);
aoqi@0 213 }
aoqi@0 214 }
aoqi@0 215
aoqi@0 216 /*
aoqi@0 217 * Get a QName with 'someUri' and 'localname' from an
aoqi@0 218 * element of qname type:
aoqi@0 219 * <xyz xmlns:ns1="someUri">ns1:localname</xyz>
aoqi@0 220 */
aoqi@0 221 public static QName getElementQName(XMLStreamReader reader) {
aoqi@0 222 try {
aoqi@0 223 String text = reader.getElementText().trim();
aoqi@0 224 String prefix = text.substring(0,text.indexOf(':'));
aoqi@0 225 String namespaceURI = reader.getNamespaceContext().getNamespaceURI(prefix);
aoqi@0 226 if (namespaceURI == null) {
aoqi@0 227 namespaceURI = "";
aoqi@0 228 }
aoqi@0 229 String localPart = text.substring(
aoqi@0 230 text.indexOf(':') + 1, text.length());
aoqi@0 231 return new QName(namespaceURI, localPart);
aoqi@0 232 } catch (XMLStreamException e) {
aoqi@0 233 throw wrapException(e);
aoqi@0 234 }
aoqi@0 235 }
aoqi@0 236
aoqi@0 237 /**
aoqi@0 238 * Read all attributes into an data structure. Note that this method cannot
aoqi@0 239 * be called multiple times to get the same list of attributes.
aoqi@0 240 */
aoqi@0 241 public static Attributes getAttributes(XMLStreamReader reader) {
aoqi@0 242 return (reader.getEventType() == START_ELEMENT ||
aoqi@0 243 reader.getEventType() == ATTRIBUTE) ?
aoqi@0 244 new AttributesImpl(reader) : null;
aoqi@0 245 }
aoqi@0 246
aoqi@0 247 public static void verifyReaderState(XMLStreamReader reader, int expectedState) {
aoqi@0 248 int state = reader.getEventType();
aoqi@0 249 if (state != expectedState) {
aoqi@0 250 throw new XMLStreamReaderException(
aoqi@0 251 "xmlreader.unexpectedState",
aoqi@0 252 getStateName(expectedState), getStateName(state));
aoqi@0 253 }
aoqi@0 254 }
aoqi@0 255
aoqi@0 256 public static void verifyTag(XMLStreamReader reader, String namespaceURI, String localName) {
aoqi@0 257 if (!localName.equals(reader.getLocalName()) || !namespaceURI.equals(reader.getNamespaceURI())) {
aoqi@0 258 throw new XMLStreamReaderException(
aoqi@0 259 "xmlreader.unexpectedState.tag",
aoqi@0 260 "{" + namespaceURI + "}" + localName,
aoqi@0 261 "{" + reader.getNamespaceURI() + "}" + reader.getLocalName());
aoqi@0 262 }
aoqi@0 263 }
aoqi@0 264
aoqi@0 265 public static void verifyTag(XMLStreamReader reader, QName name) {
aoqi@0 266 verifyTag(reader, name.getNamespaceURI(), name.getLocalPart());
aoqi@0 267 }
aoqi@0 268
aoqi@0 269 public static String getStateName(XMLStreamReader reader) {
aoqi@0 270 return getStateName(reader.getEventType());
aoqi@0 271 }
aoqi@0 272
aoqi@0 273 public static String getStateName(int state) {
aoqi@0 274 switch (state) {
aoqi@0 275 case ATTRIBUTE:
aoqi@0 276 return "ATTRIBUTE";
aoqi@0 277 case CDATA:
aoqi@0 278 return "CDATA";
aoqi@0 279 case CHARACTERS:
aoqi@0 280 return "CHARACTERS";
aoqi@0 281 case COMMENT:
aoqi@0 282 return "COMMENT";
aoqi@0 283 case DTD:
aoqi@0 284 return "DTD";
aoqi@0 285 case END_DOCUMENT:
aoqi@0 286 return "END_DOCUMENT";
aoqi@0 287 case END_ELEMENT:
aoqi@0 288 return "END_ELEMENT";
aoqi@0 289 case ENTITY_DECLARATION:
aoqi@0 290 return "ENTITY_DECLARATION";
aoqi@0 291 case ENTITY_REFERENCE:
aoqi@0 292 return "ENTITY_REFERENCE";
aoqi@0 293 case NAMESPACE:
aoqi@0 294 return "NAMESPACE";
aoqi@0 295 case NOTATION_DECLARATION:
aoqi@0 296 return "NOTATION_DECLARATION";
aoqi@0 297 case PROCESSING_INSTRUCTION:
aoqi@0 298 return "PROCESSING_INSTRUCTION";
aoqi@0 299 case SPACE:
aoqi@0 300 return "SPACE";
aoqi@0 301 case START_DOCUMENT:
aoqi@0 302 return "START_DOCUMENT";
aoqi@0 303 case START_ELEMENT:
aoqi@0 304 return "START_ELEMENT";
aoqi@0 305 default :
aoqi@0 306 return "UNKNOWN";
aoqi@0 307 }
aoqi@0 308 }
aoqi@0 309
aoqi@0 310 private static XMLStreamReaderException wrapException(XMLStreamException e) {
aoqi@0 311 return new XMLStreamReaderException("xmlreader.ioException",e);
aoqi@0 312 }
aoqi@0 313
aoqi@0 314 // -- Auxiliary classes ----------------------------------------------
aoqi@0 315
aoqi@0 316 /**
aoqi@0 317 * AttributesImpl class copied from old StAXReader. This class is used to implement
aoqi@0 318 * getAttributes() on a StAX Reader.
aoqi@0 319 */
aoqi@0 320 public static class AttributesImpl implements Attributes {
aoqi@0 321
aoqi@0 322 static final String XMLNS_NAMESPACE_URI = "http://www.w3.org/2000/xmlns/";
aoqi@0 323
aoqi@0 324 static class AttributeInfo {
aoqi@0 325
aoqi@0 326 private QName name;
aoqi@0 327 private String value;
aoqi@0 328
aoqi@0 329 public AttributeInfo(QName name, String value) {
aoqi@0 330 this.name = name;
aoqi@0 331 if (value == null) {
aoqi@0 332 // e.g., <return xmlns=""> -- stax returns null
aoqi@0 333 this.value = "";
aoqi@0 334 } else {
aoqi@0 335 this.value = value;
aoqi@0 336 }
aoqi@0 337 }
aoqi@0 338
aoqi@0 339 QName getName() {
aoqi@0 340 return name;
aoqi@0 341 }
aoqi@0 342
aoqi@0 343 String getValue() {
aoqi@0 344 return value;
aoqi@0 345 }
aoqi@0 346
aoqi@0 347 /*
aoqi@0 348 * Return "xmlns:" as part of name if namespace.
aoqi@0 349 */
aoqi@0 350 String getLocalName() {
aoqi@0 351 if (isNamespaceDeclaration()) {
aoqi@0 352 if (name.getLocalPart().equals("")) {
aoqi@0 353 return "xmlns";
aoqi@0 354 }
aoqi@0 355 return "xmlns:" + name.getLocalPart();
aoqi@0 356 }
aoqi@0 357 return name.getLocalPart();
aoqi@0 358 }
aoqi@0 359
aoqi@0 360 boolean isNamespaceDeclaration() {
aoqi@0 361 return (name.getNamespaceURI() == XMLNS_NAMESPACE_URI);
aoqi@0 362 }
aoqi@0 363 }
aoqi@0 364
aoqi@0 365 // stores qname and value for each attribute
aoqi@0 366 AttributeInfo [] atInfos;
aoqi@0 367
aoqi@0 368 /*
aoqi@0 369 * Will create a list that contains the namespace declarations
aoqi@0 370 * as well as the other attributes.
aoqi@0 371 */
aoqi@0 372 public AttributesImpl(XMLStreamReader reader) {
aoqi@0 373 if (reader == null) {
aoqi@0 374
aoqi@0 375 // this is the case when we call getAttributes() on the
aoqi@0 376 // reader when it is not on a start tag
aoqi@0 377 atInfos = new AttributeInfo[0];
aoqi@0 378 } else {
aoqi@0 379
aoqi@0 380 // this is the normal case
aoqi@0 381 int index = 0;
aoqi@0 382 int namespaceCount = reader.getNamespaceCount();
aoqi@0 383 int attributeCount = reader.getAttributeCount();
aoqi@0 384 atInfos = new AttributeInfo[namespaceCount + attributeCount];
aoqi@0 385 for (int i=0; i<namespaceCount; i++) {
aoqi@0 386 String namespacePrefix = reader.getNamespacePrefix(i);
aoqi@0 387
aoqi@0 388 // will be null if default prefix. QName can't take null
aoqi@0 389 if (namespacePrefix == null) {
aoqi@0 390 namespacePrefix = "";
aoqi@0 391 }
aoqi@0 392 atInfos[index++] = new AttributeInfo(
aoqi@0 393 new QName(XMLNS_NAMESPACE_URI,
aoqi@0 394 namespacePrefix,
aoqi@0 395 "xmlns"),
aoqi@0 396 reader.getNamespaceURI(i));
aoqi@0 397 }
aoqi@0 398 for (int i=0; i<attributeCount; i++) {
aoqi@0 399 atInfos[index++] = new AttributeInfo(
aoqi@0 400 reader.getAttributeName(i),
aoqi@0 401 reader.getAttributeValue(i));
aoqi@0 402 }
aoqi@0 403 }
aoqi@0 404 }
aoqi@0 405
aoqi@0 406 public int getLength() {
aoqi@0 407 return atInfos.length;
aoqi@0 408 }
aoqi@0 409
aoqi@0 410 public String getLocalName(int index) {
aoqi@0 411 if (index >= 0 && index < atInfos.length) {
aoqi@0 412 return atInfos[index].getLocalName();
aoqi@0 413 }
aoqi@0 414 return null;
aoqi@0 415 }
aoqi@0 416
aoqi@0 417 public QName getName(int index) {
aoqi@0 418 if (index >= 0 && index < atInfos.length) {
aoqi@0 419 return atInfos[index].getName();
aoqi@0 420 }
aoqi@0 421 return null;
aoqi@0 422 }
aoqi@0 423
aoqi@0 424 public String getPrefix(int index) {
aoqi@0 425 if (index >= 0 && index < atInfos.length) {
aoqi@0 426 return atInfos[index].getName().getPrefix();
aoqi@0 427 }
aoqi@0 428 return null;
aoqi@0 429 }
aoqi@0 430
aoqi@0 431 public String getURI(int index) {
aoqi@0 432 if (index >= 0 && index < atInfos.length) {
aoqi@0 433 return atInfos[index].getName().getNamespaceURI();
aoqi@0 434 }
aoqi@0 435 return null;
aoqi@0 436 }
aoqi@0 437
aoqi@0 438 public String getValue(int index) {
aoqi@0 439 if (index >= 0 && index < atInfos.length) {
aoqi@0 440 return atInfos[index].getValue();
aoqi@0 441 }
aoqi@0 442 return null;
aoqi@0 443 }
aoqi@0 444
aoqi@0 445 public String getValue(QName name) {
aoqi@0 446 int index = getIndex(name);
aoqi@0 447 if (index != -1) {
aoqi@0 448 return atInfos[index].getValue();
aoqi@0 449 }
aoqi@0 450 return null;
aoqi@0 451 }
aoqi@0 452
aoqi@0 453 public String getValue(String localName) {
aoqi@0 454 int index = getIndex(localName);
aoqi@0 455 if (index != -1) {
aoqi@0 456 return atInfos[index].getValue();
aoqi@0 457 }
aoqi@0 458 return null;
aoqi@0 459 }
aoqi@0 460
aoqi@0 461 public String getValue(String uri, String localName) {
aoqi@0 462 int index = getIndex(uri, localName);
aoqi@0 463 if (index != -1) {
aoqi@0 464 return atInfos[index].getValue();
aoqi@0 465 }
aoqi@0 466 return null;
aoqi@0 467 }
aoqi@0 468
aoqi@0 469 public boolean isNamespaceDeclaration(int index) {
aoqi@0 470 if (index >= 0 && index < atInfos.length) {
aoqi@0 471 return atInfos[index].isNamespaceDeclaration();
aoqi@0 472 }
aoqi@0 473 return false;
aoqi@0 474 }
aoqi@0 475
aoqi@0 476 public int getIndex(QName name) {
aoqi@0 477 for (int i=0; i<atInfos.length; i++) {
aoqi@0 478 if (atInfos[i].getName().equals(name)) {
aoqi@0 479 return i;
aoqi@0 480 }
aoqi@0 481 }
aoqi@0 482 return -1;
aoqi@0 483 }
aoqi@0 484
aoqi@0 485 public int getIndex(String localName) {
aoqi@0 486 for (int i=0; i<atInfos.length; i++) {
aoqi@0 487 if (atInfos[i].getName().getLocalPart().equals(localName)) {
aoqi@0 488 return i;
aoqi@0 489 }
aoqi@0 490 }
aoqi@0 491 return -1;
aoqi@0 492 }
aoqi@0 493
aoqi@0 494 public int getIndex(String uri, String localName) {
aoqi@0 495 QName qName;
aoqi@0 496 for (int i=0; i<atInfos.length; i++) {
aoqi@0 497 qName = atInfos[i].getName();
aoqi@0 498 if (qName.getNamespaceURI().equals(uri) &&
aoqi@0 499 qName.getLocalPart().equals(localName)) {
aoqi@0 500
aoqi@0 501 return i;
aoqi@0 502 }
aoqi@0 503 }
aoqi@0 504 return -1;
aoqi@0 505 }
aoqi@0 506 }
aoqi@0 507 }

mercurial