|
Java example source code file (StAXEventConnector.java)
The StAXEventConnector.java Java example source code/* * Copyright (c) 1997, 2011, 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.bind.v2.runtime.unmarshaller; import java.util.Iterator; import javax.xml.namespace.QName; import javax.xml.stream.Location; import javax.xml.stream.XMLEventReader; import javax.xml.stream.XMLStreamConstants; import javax.xml.stream.XMLStreamException; import javax.xml.stream.events.Attribute; import javax.xml.stream.events.Characters; import javax.xml.stream.events.EndElement; import javax.xml.stream.events.Namespace; import javax.xml.stream.events.StartElement; import javax.xml.stream.events.XMLEvent; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.AttributesImpl; /** * This is a simple utility class that adapts StAX events from an * {@link XMLEventReader} to unmarshaller events on a * {@link XmlVisitor}, bridging between the two * parser technologies. * * @author Ryan.Shoemaker@Sun.COM * @version 1.0 */ final class StAXEventConnector extends StAXConnector { // StAX event source private final XMLEventReader staxEventReader; /** Current event. */ private XMLEvent event; /** * Shared and reused {@link Attributes}. */ private final AttributesImpl attrs = new AttributesImpl(); /** * SAX may fire consective characters event, but we don't allow it. * so use this buffer to perform buffering. */ private final StringBuilder buffer = new StringBuilder(); private boolean seenText; /** * Construct a new StAX to SAX adapter that will convert a StAX event * stream into a SAX event stream. * * @param staxCore * StAX event source * @param visitor * sink */ public StAXEventConnector(XMLEventReader staxCore, XmlVisitor visitor) { super(visitor); staxEventReader = staxCore; } public void bridge() throws XMLStreamException { try { // remembers the nest level of elements to know when we are done. int depth=0; event = staxEventReader.peek(); if( !event.isStartDocument() && !event.isStartElement() ) throw new IllegalStateException(); // if the parser is on START_DOCUMENT, skip ahead to the first element do { event = staxEventReader.nextEvent(); } while( !event.isStartElement() ); handleStartDocument(event.asStartElement().getNamespaceContext()); OUTER: while(true) { // These are all of the events listed in the javadoc for // XMLEvent. // The spec only really describes 11 of them. switch (event.getEventType()) { case XMLStreamConstants.START_ELEMENT : handleStartElement(event.asStartElement()); depth++; break; case XMLStreamConstants.END_ELEMENT : depth--; handleEndElement(event.asEndElement()); if(depth==0) break OUTER; break; case XMLStreamConstants.CHARACTERS : case XMLStreamConstants.CDATA : case XMLStreamConstants.SPACE : handleCharacters(event.asCharacters()); break; } event=staxEventReader.nextEvent(); } handleEndDocument(); event = null; // avoid keeping a stale reference } catch (SAXException e) { throw new XMLStreamException(e); } } protected Location getCurrentLocation() { return event.getLocation(); } protected String getCurrentQName() { QName qName; if(event.isEndElement()) qName = event.asEndElement().getName(); else qName = event.asStartElement().getName(); return getQName(qName.getPrefix(), qName.getLocalPart()); } private void handleCharacters(Characters event) throws SAXException, XMLStreamException { if(!predictor.expectText()) return; // text isn't expected. simply skip seenText = true; // check the next event XMLEvent next; while(true) { next = staxEventReader.peek(); if(!isIgnorable(next)) break; staxEventReader.nextEvent(); } if(isTag(next)) { // this is by far the common case --- you have <foo>abc or Other Java examples (source code examples)Here is a short list of links related to this Java StAXEventConnector.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.