/* * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package com.sun.xml.internal.ws.streaming; import javax.xml.namespace.QName; import static javax.xml.stream.XMLStreamConstants.*; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; import javax.xml.stream.XMLStreamConstants; /** *
XMLStreamReaderUtil provides some utility methods intended to be used * in conjunction with a StAX XMLStreamReader.
* * @author WS Development Team */ public class XMLStreamReaderUtil { private XMLStreamReaderUtil() { } public static void close(XMLStreamReader reader) { try { reader.close(); } catch (XMLStreamException e) { throw wrapException(e); } } public static void readRest(XMLStreamReader reader) { try { while(reader.getEventType() != XMLStreamConstants.END_DOCUMENT) { reader.next(); } } catch (XMLStreamException e) { throw wrapException(e); } } public static int next(XMLStreamReader reader) { try { int readerEvent = reader.next(); while (readerEvent != END_DOCUMENT) { switch (readerEvent) { case START_ELEMENT: case END_ELEMENT: case CDATA: case CHARACTERS: case PROCESSING_INSTRUCTION: return readerEvent; default: // falls through ignoring event } readerEvent = reader.next(); } return readerEvent; } catch (XMLStreamException e) { throw wrapException(e); } } public static int nextElementContent(XMLStreamReader reader) { int state = nextContent(reader); if (state == CHARACTERS) { throw new XMLStreamReaderException( "xmlreader.unexpectedCharacterContent", reader.getText()); } return state; } public static void toNextTag(XMLStreamReader reader, QName name) { // skip any whitespace if (reader.getEventType() != XMLStreamConstants.START_ELEMENT && reader.getEventType() != XMLStreamConstants.END_ELEMENT) { XMLStreamReaderUtil.nextElementContent(reader); } if(reader.getEventType() == XMLStreamConstants.END_ELEMENT && name.equals(reader.getName())) { XMLStreamReaderUtil.nextElementContent(reader); } } /** * Moves next and read spaces from the reader as long as to the next element. * Comments are ignored * @param reader * @return */ public static String nextWhiteSpaceContent(XMLStreamReader reader) { next(reader); return currentWhiteSpaceContent(reader); } /** * Read spaces from the reader as long as to the next element, starting from * current position. Comments are ignored. * @param reader * @return */ public static String currentWhiteSpaceContent(XMLStreamReader reader) { // since the there might be several valid chunks (spaces/comment/spaces) // StringBuilder must be used; it's initialized lazily, only when needed StringBuilder whiteSpaces = null; for (;;) { switch (reader.getEventType()) { case START_ELEMENT: case END_ELEMENT: case END_DOCUMENT: return whiteSpaces == null ? null : whiteSpaces.toString(); case CHARACTERS: if (reader.isWhiteSpace()) { if (whiteSpaces == null) { whiteSpaces = new StringBuilder(); } whiteSpaces.append(reader.getText()); } else { throw new XMLStreamReaderException( "xmlreader.unexpectedCharacterContent", reader.getText()); } } next(reader); } } public static int nextContent(XMLStreamReader reader) { for (;;) { int state = next(reader); switch (state) { case START_ELEMENT: case END_ELEMENT: case END_DOCUMENT: return state; case CHARACTERS: if (!reader.isWhiteSpace()) { return CHARACTERS; } } } } /** * Skip current element, leaving the cursor at END_ELEMENT of * current element. */ public static void skipElement(XMLStreamReader reader) { assert reader.getEventType() == START_ELEMENT; skipTags(reader, true); assert reader.getEventType() == END_ELEMENT; } /** * Skip following siblings, leaving cursor at END_ELEMENT of * parent element. */ public static void skipSiblings(XMLStreamReader reader, QName parent) { skipTags(reader, reader.getName().equals(parent)); assert reader.getEventType() == END_ELEMENT; } private static void skipTags(XMLStreamReader reader, boolean exitCondition) { try { int state, tags = 0; while ((state = reader.next()) != END_DOCUMENT) { if (state == START_ELEMENT) { tags++; } else if (state == END_ELEMENT) { if (tags == 0 && exitCondition) return; tags--; } } } catch (XMLStreamException e) { throw wrapException(e); } } /* * Get the text of an element */ public static String getElementText(XMLStreamReader reader) { try { return reader.getElementText(); } catch (XMLStreamException e) { throw wrapException(e); } } /* * Get a QName with 'someUri' and 'localname' from an * element of qname type: *