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

mercurial