Coverage Report - org.simpleframework.xml.stream.NodeExtractor
 
Classes in this File Line Coverage Branch Coverage Complexity
NodeExtractor
100%
17/17
83%
5/6
2
 
 1  
 /*
 2  
  * NodeExtractor.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  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 14  
  * implied. See the License for the specific language governing 
 15  
  * permissions and limitations under the License.
 16  
  */
 17  
 
 18  
 package org.simpleframework.xml.stream;
 19  
 
 20  
 import static org.w3c.dom.Node.COMMENT_NODE;
 21  
 
 22  
 import java.util.LinkedList;
 23  
 
 24  
 import org.w3c.dom.Document;
 25  
 import org.w3c.dom.Node;
 26  
 import org.w3c.dom.NodeList;
 27  
 
 28  
 /**
 29  
  * The <code>NodeExtractor</code> object is used to extract nodes 
 30  
  * from a provided DOM document. This is used so that the nodes of
 31  
  * a given document can be read with queue like semantics, such 
 32  
  * that the first node encountered is the first node taken from 
 33  
  * the queue. Queue semantics help transform DOM documents to an
 34  
  * event stream much like the StAX framework.
 35  
  *  
 36  
  * @author Niall Gallagher
 37  
  */
 38  
 class NodeExtractor extends LinkedList<Node> {
 39  
 
 40  
    /**
 41  
     * Constructor for the <code>NodeExtractor</code> object. This
 42  
     * is used to instantiate an object that flattens a document
 43  
     * in to a queue so that the nodes can be used for streaming.
 44  
     * 
 45  
     * @param source this is the source document to be flattened
 46  
     */
 47  74070
    public NodeExtractor(Document source) {
 48  74070
       this.extract(source);
 49  74070
    }
 50  
    
 51  
    /**
 52  
     * This is used to extract the nodes of the document in such a
 53  
     * way that it can be navigated as a queue. In order to do this
 54  
     * each node encountered is pushed in to the queue so that
 55  
     * when finished the nodes can be dealt with as a stream.
 56  
     * 
 57  
     * @param source this is the source document to be flattened
 58  
     */
 59  
    private void extract(Document source) {
 60  74070
       Node node = source.getDocumentElement();
 61  
       
 62  74070
       if(node != null) {
 63  74070
          offer(node);
 64  74070
          extract(node);
 65  
       }
 66  74070
    }
 67  
 
 68  
    /**
 69  
     * This is used to extract the nodes of the element in such a
 70  
     * way that it can be navigated as a queue. In order to do this
 71  
     * each node encountered is pushed in to the queue so that
 72  
     * when finished the nodes can be dealt with as a stream.
 73  
     * 
 74  
     * @param source this is the source element to be flattened
 75  
     */
 76  
    private void extract(Node source) {
 77  5341683
       NodeList list = source.getChildNodes();
 78  5341683
       int length = list.getLength();
 79  
       
 80  10622033
       for(int i = 0; i < length; i++) {
 81  5280350
          Node node = list.item(i);
 82  5280350
          short type = node.getNodeType();
 83  
          
 84  5280350
          if(type != COMMENT_NODE) {
 85  5267613
             offer(node);
 86  5267613
             extract(node);
 87  
          }
 88  
       }
 89  5341683
    }
 90  
 }