Coverage Report - org.simpleframework.xml.stream.StreamProvider
 
Classes in this File Line Coverage Branch Coverage Complexity
StreamProvider
83%
5/6
N/A
1
 
 1  
 /*
 2  
  * StreamProvider.java January 2010
 3  
  *
 4  
  * Copyright (C) 2010, Niall Gallagher <niallg@users.sf.net>
 5  
  *
 6  
  * Licensed under the Apache License, Version 2.0 (the "License");
 7  
  * you may not use this file except in compliance with the License.
 8  
  * You may obtain a copy of the License at
 9  
  *
 10  
  *     http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 15  
  * implied. See the License for the specific language governing 
 16  
  * permissions and limitations under the License.
 17  
  */
 18  
 
 19  
 package org.simpleframework.xml.stream;
 20  
 
 21  
 import java.io.InputStream;
 22  
 import java.io.Reader;
 23  
 
 24  
 import javax.xml.stream.XMLEventReader;
 25  
 import javax.xml.stream.XMLInputFactory;
 26  
 
 27  
 /**
 28  
  * The <code>StreamProvider</code> object is used to provide event
 29  
  * reader implementations for StAX. Wrapping the mechanics of the
 30  
  * StAX framework within a <code>Provider</code> ensures that it can
 31  
  * be plugged in without any dependencies. This allows other parsers
 32  
  * to be swapped in should there be such a requirement.
 33  
  * 
 34  
  * @author Niall Gallagher
 35  
  * 
 36  
  * @see org.simpleframework.xml.stream.StreamProvider
 37  
  */
 38  
 class StreamProvider implements Provider {
 39  
    
 40  
    /**
 41  
     * This is the factory that is used to create StAX parsers.
 42  
     */
 43  
    private final XMLInputFactory factory;
 44  
    
 45  
    /**
 46  
     * Constructor for the <code>StreamProvider</code> object. This
 47  
     * is used to instantiate a parser factory that will be used to
 48  
     * create parsers when requested. Instantiating the factory up
 49  
     * front also checks that the framework is fully supported.
 50  
     */
 51  1
    public StreamProvider() {
 52  1
       this.factory = XMLInputFactory.newInstance();
 53  1
    }
 54  
 
 55  
    /**
 56  
     * This provides an <code>EventReader</code> that will read from
 57  
     * the specified input stream. When reading from an input stream
 58  
     * the character encoding should be taken from the XML prolog or
 59  
     * it should default to the UTF-8 character encoding.
 60  
     * 
 61  
     * @param source this is the stream to read the document with
 62  
     * 
 63  
     * @return this is used to return the event reader implementation
 64  
     */
 65  
    public EventReader provide(InputStream source) throws Exception {
 66  0
       return provide(factory.createXMLEventReader(source));
 67  
    }
 68  
    
 69  
    /**
 70  
     * This provides an <code>EventReader</code> that will read from
 71  
     * the specified reader. When reading from a reader the character
 72  
     * encoding should be the same as the source XML document.
 73  
     * 
 74  
     * @param source this is the reader to read the document with
 75  
     * 
 76  
     * @return this is used to return the event reader implementation
 77  
     */
 78  
    public EventReader provide(Reader source) throws Exception {
 79  4
       return provide(factory.createXMLEventReader(source));
 80  
    }
 81  
    
 82  
    /**
 83  
     * This provides an <code>EventReader</code> that will read from
 84  
     * the specified reader. The returned event reader is basically
 85  
     * a wrapper for the provided StAX implementation.
 86  
     * 
 87  
     * @param source this is the reader to read the document with
 88  
     * 
 89  
     * @return this is used to return the event reader implementation
 90  
     */
 91  
    private EventReader provide(XMLEventReader source) throws Exception {
 92  4
       return new StreamReader(source);
 93  
    }
 94  
 }