Coverage Report - net.sf.jmatchparser.util.csv.XMLCSVWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
XMLCSVWriter
72%
21/29
75%
3/4
1,8
 
 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  
 import java.io.OutputStream;
 37  
 
 38  
 import javax.xml.parsers.DocumentBuilderFactory;
 39  
 import javax.xml.parsers.ParserConfigurationException;
 40  
 import javax.xml.transform.TransformerFactory;
 41  
 import javax.xml.transform.dom.DOMSource;
 42  
 import javax.xml.transform.stream.StreamResult;
 43  
 
 44  
 import org.w3c.dom.Document;
 45  
 import org.w3c.dom.Element;
 46  
 import org.w3c.dom.Node;
 47  
 
 48  
 /**
 49  
  * Class to create an XML file as if it was a CSV (Comma Separated Value) file.
 50  
  */
 51  
 public class XMLCSVWriter extends AbstractCSVWriter {
 52  
 
 53  
         private final OutputStream out;
 54  
         private final Node root;
 55  
         private final String rowTag;
 56  
         private final String[] cellTags;
 57  
 
 58  
         /**
 59  
          * Create a new {@link XMLCSVWriter} that writes to the given DOM document.
 60  
          * Rows will become child elements given root element that have the row tag
 61  
          * as name, and cells will become children of the row, with the cell tags
 62  
          * (assigned round robin) as name.
 63  
          * 
 64  
          * @param root
 65  
          *            the root node
 66  
          * @param rowTag
 67  
          *            the name of row elements
 68  
          * @param cellTags
 69  
          *            the names of cell elements
 70  
          * 
 71  
          */
 72  0
         public XMLCSVWriter(Node root, String rowTag, String... cellTags) {
 73  0
                 this.out = null;
 74  0
                 this.root = root;
 75  0
                 this.rowTag = rowTag;
 76  0
                 this.cellTags = cellTags;
 77  0
         }
 78  
 
 79  
         /**
 80  
          * Create a new {@link XMLCSVWriter} that writes to the given DOM document.
 81  
          * Rows will become child elements of the document element that have the row
 82  
          * tag as name, and cells will become children of the row, with the cell
 83  
          * tags (assigned round robin) as name.
 84  
          * 
 85  
          * @param out
 86  
          *            Output stream to write to
 87  
          * @param rootTag
 88  
          *            the name of the root element
 89  
          * @param rowTag
 90  
          *            the name of row elements
 91  
          * @param cellTags
 92  
          *            the names of cell elements
 93  
          */
 94  2
         public XMLCSVWriter(OutputStream out, String rootTag, String rowTag, String... cellTags) throws ParserConfigurationException {
 95  2
                 this.out = out;
 96  2
                 Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
 97  2
                 doc.appendChild(doc.createElement(rootTag));
 98  2
                 this.rowTag = rowTag;
 99  2
                 this.cellTags = cellTags;
 100  2
                 this.root = doc.getDocumentElement();
 101  2
         }
 102  
 
 103  
         @Override
 104  
         public void flush() throws IOException {
 105  2
         }
 106  
 
 107  
         @Override
 108  
         public void close() throws IOException {
 109  2
                 if (out != null) {
 110  
                         try {
 111  2
                                 TransformerFactory.newInstance().newTransformer().transform(new DOMSource(root.getOwnerDocument()), new StreamResult(out));
 112  2
                                 out.close();
 113  0
                         } catch (Exception ex) {
 114  0
                                 throw new IOException("Unable to save XML file", ex);
 115  2
                         }
 116  
                 }
 117  2
         }
 118  
 
 119  
         @Override
 120  
         public void write(String... record) throws IOException {
 121  4
                 Element row = root.getOwnerDocument().createElement(rowTag);
 122  12
                 for (int i = 0; i < record.length; i++) {
 123  8
                         Element cell = root.getOwnerDocument().createElement(cellTags[i % cellTags.length]);
 124  8
                         cell.appendChild(root.getOwnerDocument().createTextNode(record[i]));
 125  8
                         row.appendChild(cell);
 126  
                 }
 127  4
                 root.appendChild(row);
 128  4
         }
 129  
 }