Coverage Report - org.simpleframework.xml.stream.PullProvider
 
Classes in this File Line Coverage Branch Coverage Complexity
PullProvider
33%
4/12
0%
0/4
1.667
 
 1  
 /*
 2  
  * PullProvider.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 org.xmlpull.v1.XmlPullParser;
 25  
 import org.xmlpull.v1.XmlPullParserFactory;
 26  
  
 27  
 /**
 28  
  * The <code>PullProvider</code> class is used to provide an event
 29  
  * reader that uses the XML pull API available on Google Android. It
 30  
  * provides the best performance on Android as it avoids having to
 31  
  * build a full DOM model. The <code>EventReader</code> produced by
 32  
  * this provider will have full namespace capabilities and also has
 33  
  * line numbers available for each of the events that are extracted.
 34  
  * 
 35  
  * @author Niall Gallagher
 36  
  */
 37  
 class PullProvider implements Provider {
 38  
    
 39  
    /**
 40  
     * This is used to create a new XML pull parser for the reader.
 41  
     */
 42  
    private final XmlPullParserFactory factory;
 43  
    
 44  
    /**
 45  
     * Constructor for the <code>PullProvider</code> object. This
 46  
     * will instantiate a namespace aware pull parser factory that
 47  
     * will be used to parse the XML documents that are read by
 48  
     * the framework. If XML pull is not available this will fail.
 49  
     */
 50  1
    public PullProvider() throws Exception {
 51  1
       this.factory = XmlPullParserFactory.newInstance();
 52  1
       this.factory.setNamespaceAware(true);
 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
       XmlPullParser parser = factory.newPullParser();
 67  
       
 68  0
       if(source != null) {
 69  0
          parser.setInput(source, null);
 70  
       }
 71  0
       return new PullReader(parser);  
 72  
    }
 73  
    
 74  
    /**
 75  
     * This provides an <code>EventReader</code> that will read from
 76  
     * the specified reader. When reading from a reader the character
 77  
     * encoding should be the same as the source XML document.
 78  
     * 
 79  
     * @param source this is the reader to read the document with
 80  
     * 
 81  
     * @return this is used to return the event reader implementation
 82  
     */
 83  
    public EventReader provide(Reader source) throws Exception {
 84  0
       XmlPullParser parser = factory.newPullParser();
 85  
       
 86  0
       if(source != null) {
 87  0
          parser.setInput(source);
 88  
       }
 89  0
       return new PullReader(parser);
 90  
    }
 91  
 }