Coverage Report - net.sf.jmatchparser.util.csv.XPathCSVReader
 
Classes in this File Line Coverage Branch Coverage Complexity
XPathCSVReader
91%
22/24
100%
6/6
2,2
 
 1  
 /*
 2  
  * Copyright (c) 2010 - 2011 Michael Schierl
 3  
  * 
 4  
  * All rights reserved.
 5  
  * 
 6  
  * Redistribution and use in source and binary forms, with or without
 7  
  * modification, are permitted provided that the following conditions
 8  
  * are met:
 9  
  * 
 10  
  * - Redistributions of source code must retain the above copyright notice,
 11  
  *   this list of conditions and the following disclaimer.
 12  
  *   
 13  
  * - Redistributions in binary form must reproduce the above copyright
 14  
  *   notice, this list of conditions and the following disclaimer in the
 15  
  *   documentation and/or other materials provided with the distribution.
 16  
  *   
 17  
  * - Neither name of the copyright holders nor the names of its
 18  
  *   contributors may be used to endorse or promote products derived from
 19  
  *   this software without specific prior written permission.
 20  
  *   
 21  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND THE CONTRIBUTORS
 22  
  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 23  
  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 24  
  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 25  
  * HOLDERS OR THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 26  
  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 27  
  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 28  
  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 29  
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 30  
  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 31  
  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 32  
  */
 33  
 package net.sf.jmatchparser.util.csv;
 34  
 
 35  
 import java.io.IOException;
 36  
 
 37  
 import javax.xml.xpath.XPath;
 38  
 import javax.xml.xpath.XPathConstants;
 39  
 import javax.xml.xpath.XPathExpression;
 40  
 import javax.xml.xpath.XPathExpressionException;
 41  
 import javax.xml.xpath.XPathFactory;
 42  
 
 43  
 import org.w3c.dom.Document;
 44  
 import org.w3c.dom.Node;
 45  
 import org.w3c.dom.NodeList;
 46  
 
 47  
 /**
 48  
  * Class to read data from an XML file as if it was a CSV (Comma Separated
 49  
  * Value) file, using {@link XPathExpression}s for the columns.
 50  
  */
 51  
 public class XPathCSVReader extends AbstractCSVReader {
 52  
 
 53  
         private final NodeList rows;
 54  
         private final XPathExpression[] columnExpressions;
 55  2
         private int nextIndex = 0;
 56  
 
 57  
         /**
 58  
          * Create a new {@link XPathCSVReader} that reads from the given DOM
 59  
          * document. The rowExpression is expected to return a {@link NodeList} of
 60  
          * rows, each columnExpression is evaluated on each of the rows to find the
 61  
          * column values.
 62  
          */
 63  
         public XPathCSVReader(Document doc, String rowExpression, String... columnExpressions) throws XPathExpressionException {
 64  2
                 this(doc, buildExpressions(rowExpression)[0], buildExpressions(columnExpressions));
 65  2
         }
 66  
 
 67  
         /**
 68  
          * Create a new {@link XPathCSVReader} that reads from the given DOM
 69  
          * document. The rowExpression is expected to return a {@link NodeList} of
 70  
          * rows, each columnExpression is evaluated on each of the rows to find the
 71  
          * column values.
 72  
          */
 73  2
         public XPathCSVReader(Document doc, XPathExpression rowExpression, XPathExpression... columnExpressions) throws XPathExpressionException {
 74  2
                 this.columnExpressions = columnExpressions;
 75  2
                 rows = (NodeList) rowExpression.evaluate(doc, XPathConstants.NODESET);
 76  2
         }
 77  
 
 78  1
         private static final XPath xpath = XPathFactory.newInstance().newXPath();
 79  
 
 80  
         private static XPathExpression[] buildExpressions(String... expressions) throws XPathExpressionException {
 81  4
                 XPathExpression[] result = new XPathExpression[expressions.length];
 82  12
                 for (int i = 0; i < result.length; i++) {
 83  8
                         result[i] = xpath.compile(expressions[i]);
 84  
                 }
 85  4
                 return result;
 86  
         }
 87  
 
 88  
         /**
 89  
          * Close this reader. This is a no-op.
 90  
          */
 91  
         @Override
 92  
         public void close() throws IOException {
 93  1
         }
 94  
 
 95  
         /**
 96  
          * Read a row from this XML file.
 97  
          */
 98  
         @Override
 99  
         public String[] read() throws IOException {
 100  6
                 if (nextIndex == rows.getLength())
 101  2
                         return null;
 102  4
                 Node row = rows.item(nextIndex);
 103  4
                 nextIndex++;
 104  4
                 String[] record = new String[columnExpressions.length];
 105  16
                 for (int i = 0; i < record.length; i++) {
 106  
                         try {
 107  12
                                 record[i] = columnExpressions[i].evaluate(row);
 108  0
                         } catch (XPathExpressionException ex) {
 109  0
                                 throw new IOException("Error while parsing " + columnExpressions[i], ex);
 110  12
                         }
 111  
                 }
 112  4
                 return record;
 113  
         }
 114  
 }