alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (StAXSource.java)

This example Java source code file (StAXSource.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

feature, illegalargumentexception, illegalstateexception, staxsource, string, unsupportedoperationexception, xmleventreader, xmlstreamexception, xmlstreamreader

The StAXSource.java Java example source code

/*
 * Copyright (c) 2005, 2006, 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 javax.xml.transform.stax;

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.events.XMLEvent;
import javax.xml.transform.Source;

/**
 * <p>Acts as a holder for an XML {@link Source} in the
 * form of a StAX reader,i.e.
 * {@link XMLStreamReader} or {@link XMLEventReader}.
 * <code>StAXSource can be used in all cases that accept
 * a <code>Source, e.g. {@link javax.xml.transform.Transformer},
 * {@link javax.xml.validation.Validator} which accept
 * <code>Source as input.
 *
 * <p>StAXSources are consumed during processing
 * and are not reusable.</p>
 *
 * @author <a href="mailto:Neeraj.Bajaj@Sun.com">Neeraj Bajaj
 * @author <a href="mailto:Jeff.Suttor@Sun.com">Jeff Suttor
 *
 * @see <a href="http://jcp.org/en/jsr/detail?id=173">
 *  JSR 173: Streaming API for XML</a>
 * @see XMLStreamReader
 * @see XMLEventReader
 *
 * @since 1.6
 */
public class StAXSource implements Source {

    /** If {@link javax.xml.transform.TransformerFactory#getFeature(String name)}
     * returns true when passed this value as an argument,
     * the Transformer supports Source input of this type.
     */
    public static final String FEATURE =
        "http://javax.xml.transform.stax.StAXSource/feature";

    /** <p>XMLEventReader to be used for source input.

*/ private XMLEventReader xmlEventReader = null; /** <p>XMLStreamReader to be used for source input.

*/ private XMLStreamReader xmlStreamReader = null; /** <p>System identifier of source input.

*/ private String systemId = null; /** * <p>Creates a new instance of a StAXSource * by supplying an {@link XMLEventReader}.</p> * * <p>XMLEventReader must be a * non-<code>null reference.

* * <p>XMLEventReader must be in * {@link XMLStreamConstants#START_DOCUMENT} or * {@link XMLStreamConstants#START_ELEMENT} state.</p> * * @param xmlEventReader <code>XMLEventReader used to create * this <code>StAXSource. * * @throws XMLStreamException If <code>xmlEventReader access * throws an <code>Exception. * @throws IllegalArgumentException If <code>xmlEventReader == * <code>null. * @throws IllegalStateException If <code>xmlEventReader * is not in <code>XMLStreamConstants.START_DOCUMENT or * <code>XMLStreamConstants.START_ELEMENT state. */ public StAXSource(final XMLEventReader xmlEventReader) throws XMLStreamException { if (xmlEventReader == null) { throw new IllegalArgumentException( "StAXSource(XMLEventReader) with XMLEventReader == null"); } // TODO: This is ugly ... // there is no way to know the current position(event) of // XMLEventReader. peek() is the only way to know the next event. // The next event on the input stream should be // XMLStreamConstants.START_DOCUMENT or // XMLStreamConstants.START_ELEMENT. XMLEvent event = xmlEventReader.peek(); int eventType = event.getEventType(); if (eventType != XMLStreamConstants.START_DOCUMENT && eventType != XMLStreamConstants.START_ELEMENT) { throw new IllegalStateException( "StAXSource(XMLEventReader) with XMLEventReader " + "not in XMLStreamConstants.START_DOCUMENT or " + "XMLStreamConstants.START_ELEMENT state"); } this.xmlEventReader = xmlEventReader; systemId = event.getLocation().getSystemId(); } /** * <p>Creates a new instance of a StAXSource * by supplying an {@link XMLStreamReader}.</p> * * <p>XMLStreamReader must be a * non-<code>null reference.

* * <p>XMLStreamReader must be in * {@link XMLStreamConstants#START_DOCUMENT} or * {@link XMLStreamConstants#START_ELEMENT} state.</p> * * @param xmlStreamReader <code>XMLStreamReader used to create * this <code>StAXSource. * * @throws IllegalArgumentException If <code>xmlStreamReader == * <code>null. * @throws IllegalStateException If <code>xmlStreamReader * is not in <code>XMLStreamConstants.START_DOCUMENT or * <code>XMLStreamConstants.START_ELEMENT state. */ public StAXSource(final XMLStreamReader xmlStreamReader) { if (xmlStreamReader == null) { throw new IllegalArgumentException( "StAXSource(XMLStreamReader) with XMLStreamReader == null"); } int eventType = xmlStreamReader.getEventType(); if (eventType != XMLStreamConstants.START_DOCUMENT && eventType != XMLStreamConstants.START_ELEMENT) { throw new IllegalStateException( "StAXSource(XMLStreamReader) with XMLStreamReader" + "not in XMLStreamConstants.START_DOCUMENT or " + "XMLStreamConstants.START_ELEMENT state"); } this.xmlStreamReader = xmlStreamReader; systemId = xmlStreamReader.getLocation().getSystemId(); } /** * <p>Get the XMLEventReader used by this * <code>StAXSource.

* * <p>XMLEventReader will be null. * if this <code>StAXSource was created with a * <code>XMLStreamReader.

* * @return <code>XMLEventReader used by this * <code>StAXSource. */ public XMLEventReader getXMLEventReader() { return xmlEventReader; } /** * <p>Get the XMLStreamReader used by this * <code>StAXSource.

* * <p>XMLStreamReader will be null * if this <code>StAXSource was created with a * <code>XMLEventReader.

* * @return <code>XMLStreamReader used by this * <code>StAXSource. */ public XMLStreamReader getXMLStreamReader() { return xmlStreamReader; } /** * <p>In the context of a StAXSource, it is not appropriate * to explicitly set the system identifier. * The <code>XMLStreamReader or XMLEventReader * used to construct this <code>StAXSource determines the * system identifier of the XML source.</p> * * <p>An {@link UnsupportedOperationException} is always * thrown by this method.</p> * * @param systemId Ignored. * * @throws UnsupportedOperationException Is <strong>always * thrown by this method. */ public void setSystemId(final String systemId) { throw new UnsupportedOperationException( "StAXSource#setSystemId(systemId) cannot set the " + "system identifier for a StAXSource"); } /** * <p>Get the system identifier used by this * <code>StAXSource.

* * <p>The XMLStreamReader or XMLEventReader * used to construct this <code>StAXSource is queried to determine * the system identifier of the XML source.</p> * * <p>The system identifier may be null or * an empty <code>"" String.

* * @return System identifier used by this <code>StAXSource. */ public String getSystemId() { return systemId; } }

Other Java examples (source code examples)

Here is a short list of links related to this Java StAXSource.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

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.