Coverage Report - org.simpleframework.xml.stream.DocumentProvider
 
Classes in this File Line Coverage Branch Coverage Complexity
DocumentProvider
100%
9/9
N/A
1
 
 1  
 /*
 2  
  * DocumentProvider.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.parsers.DocumentBuilder;
 25  
 import javax.xml.parsers.DocumentBuilderFactory;
 26  
 
 27  
 import org.w3c.dom.Document;
 28  
 import org.xml.sax.InputSource;
 29  
 
 30  
 /**
 31  
  * The <code>DocumentProvider</code> object is used to provide event
 32  
  * reader implementations for DOM. Wrapping the mechanics of the
 33  
  * DOM framework within a <code>Provider</code> ensures that it can
 34  
  * be plugged in without any dependencies. This allows other parsers
 35  
  * to be swapped in should there be such a requirement.
 36  
  * 
 37  
  * @author Niall Gallagher
 38  
  * 
 39  
  * @see org.simpleframework.xml.stream.DocumentProvider
 40  
  */
 41  
 class DocumentProvider implements Provider {
 42  
    
 43  
    /**
 44  
     * This is the factory that is used to create DOM parsers.
 45  
     */
 46  
    private final DocumentBuilderFactory factory;
 47  
    
 48  
    /**
 49  
     * Constructor for the <code>DocumentProvider</code> object. This
 50  
     * is used to instantiate a parser factory that will be used to
 51  
     * create parsers when requested. Instantiating the factory up
 52  
     * front also checks that the framework is fully supported.
 53  
     */
 54  2
    public DocumentProvider() {
 55  2
       this.factory = DocumentBuilderFactory.newInstance();
 56  2
       this.factory.setNamespaceAware(true);
 57  2
    }
 58  
    
 59  
    /**
 60  
     * This provides an <code>EventReader</code> that will read from
 61  
     * the specified input stream. When reading from an input stream
 62  
     * the character encoding should be taken from the XML prolog or
 63  
     * it should default to the UTF-8 character encoding.
 64  
     * 
 65  
     * @param source this is the stream to read the document with
 66  
     * 
 67  
     * @return this is used to return the event reader implementation
 68  
     */
 69  
    public EventReader provide(InputStream source) throws Exception {
 70  10007
       return provide(new InputSource(source));
 71  
    }
 72  
    
 73  
    /**
 74  
     * This provides an <code>EventReader</code> that will read from
 75  
     * the specified reader. When reading from a reader the character
 76  
     * encoding should be the same as the source XML document.
 77  
     * 
 78  
     * @param source this is the reader to read the document with
 79  
     * 
 80  
     * @return this is used to return the event reader implementation
 81  
     */
 82  
    public EventReader provide(Reader source) throws Exception {
 83  64067
       return provide(new InputSource(source));
 84  
    }   
 85  
    
 86  
    /**
 87  
     * This provides an <code>EventReader</code> that will read from
 88  
     * the specified source. When reading from a source the character
 89  
     * encoding should be the same as the source XML document.
 90  
     * 
 91  
     * @param source this is the source to read the document with
 92  
     * 
 93  
     * @return this is used to return the event reader implementation
 94  
     */
 95  
    private EventReader provide(InputSource source) throws Exception {
 96  74074
       DocumentBuilder builder = factory.newDocumentBuilder();       
 97  74074
       Document document = builder.parse(source);
 98  
       
 99  74070
       return new DocumentReader(document);   
 100  
    }
 101  
 }