Added: websites/production/commons/content/proper/commons-csv/jacoco/org.apache.commons.csv/CSVParser.html ============================================================================== --- websites/production/commons/content/proper/commons-csv/jacoco/org.apache.commons.csv/CSVParser.html (added) +++ websites/production/commons/content/proper/commons-csv/jacoco/org.apache.commons.csv/CSVParser.html Thu Aug 14 19:48:06 2014 @@ -0,0 +1 @@ +<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../.resources/report.css" type="text/css"/><link rel="shortcut icon" href="../.resources/report.gif" type="image/gif"/><title>CSVParser</title><script type="text/javascript" src="../.resources/sort.js"></script></head><body onload="initialSort(['breadcrumb'])"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../.sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">Apache Commons CSV</a> > <a href="index.html" class="el_package">org.apache.commons.csv</a> > <span class="el_class">CSVParser</span></div><h1>CSVParser</h1><table class="coverage" cellspacing="0" id="coveragetable"><thead><tr><td class="sortable" id="a" onclic k="toggleSort(this)">Element</td><td class="down sortable bar" id="b" onclick="toggleSort(this)">Missed Instructions</td><td class="sortable ctr2" id="c" onclick="toggleSort(this)">Cov.</td><td class="sortable bar" id="d" onclick="toggleSort(this)">Missed Branches</td><td class="sortable ctr2" id="e" onclick="toggleSort(this)">Cov.</td><td class="sortable ctr1" id="f" onclick="toggleSort(this)">Missed</td><td class="sortable ctr2" id="g" onclick="toggleSort(this)">Cxty</td><td class="sortable ctr1" id="h" onclick="toggleSort(this)">Missed</td><td class="sortable ctr2" id="i" onclick="toggleSort(this)">Lines</td><td class="sortable ctr1" id="j" onclick="toggleSort(this)">Missed</td><td class="sortable ctr2" id="k" onclick="toggleSort(this)">Methods</td></tr></thead><tfoot><tr><td>Total</td><td class="bar">29 of 395</td><td class="ctr2">93%</td><td class="bar">6 of 50</td><td class="ctr2">88%</td><td class="ctr1">6</td><td class="ctr2">41</td><td class="ctr1">2</td><td class="ctr2">87 </td><td class="ctr1">0</td><td class="ctr2">14</td></tr></tfoot><tbody><tr><td id="a10"><a href="CSVParser.java.html#L445" class="el_method">nextRecord()</a></td><td class="bar" id="b0"><img src="../.resources/redbar.gif" width="27" height="10" title="29" alt="29"/><img src="../.resources/greenbar.gif" width="92" height="10" title="99" alt="99"/></td><td class="ctr2" id="c13">77%</td><td class="bar" id="d1"><img src="../.resources/redbar.gif" width="10" height="10" title="2" alt="2"/><img src="../.resources/greenbar.gif" width="70" height="10" title="14" alt="14"/></td><td class="ctr2" id="e3">88%</td><td class="ctr1" id="f1">2</td><td class="ctr2" id="g1">11</td><td class="ctr1" id="h0">2</td><td class="ctr2" id="i0">26</td><td class="ctr1" id="j0">0</td><td class="ctr2" id="k0">1</td></tr><tr><td id="a7"><a href="CSVParser.java.html#L340" class="el_method">initializeHeader()</a></td><td class="bar" id="b1"><img src="../.resources/greenbar.gif" width="91" height="10" title="98" al t="98"/></td><td class="ctr2" id="c0">100%</td><td class="bar" id="d0"><img src="../.resources/redbar.gif" width="15" height="10" title="3" alt="3"/><img src="../.resources/greenbar.gif" width="105" height="10" title="21" alt="21"/></td><td class="ctr2" id="e4">88%</td><td class="ctr1" id="f0">3</td><td class="ctr2" id="g0">13</td><td class="ctr1" id="h1">0</td><td class="ctr2" id="i1">24</td><td class="ctr1" id="j1">0</td><td class="ctr2" id="k1">1</td></tr><tr><td id="a2"><a href="CSVParser.java.html#L219" class="el_method">CSVParser(Reader, CSVFormat)</a></td><td class="bar" id="b2"><img src="../.resources/greenbar.gif" width="33" height="10" title="36" alt="36"/></td><td class="ctr2" id="c1">100%</td><td class="bar" id="d6"/><td class="ctr2" id="e6">n/a</td><td class="ctr1" id="f3">0</td><td class="ctr2" id="g6">1</td><td class="ctr1" id="h2">0</td><td class="ctr2" id="i2">9</td><td class="ctr1" id="j2">0</td><td class="ctr2" id="k2">1</td></tr><tr><td id="a0"><a href="CSVParser .java.html#L252" class="el_method">addRecordValue()</a></td><td class="bar" id="b3"><img src="../.resources/greenbar.gif" width="27" height="10" title="29" alt="29"/></td><td class="ctr2" id="c2">100%</td><td class="bar" id="d3"><img src="../.resources/greenbar.gif" width="20" height="10" title="4" alt="4"/></td><td class="ctr2" id="e0">100%</td><td class="ctr1" id="f4">0</td><td class="ctr2" id="g2">3</td><td class="ctr1" id="h3">0</td><td class="ctr2" id="i3">6</td><td class="ctr1" id="j3">0</td><td class="ctr2" id="k3">1</td></tr><tr><td id="a13"><a href="CSVParser.java.html#L202" class="el_method">parse(URL, Charset, CSVFormat)</a></td><td class="bar" id="b4"><img src="../.resources/greenbar.gif" width="18" height="10" title="20" alt="20"/></td><td class="ctr2" id="c3">100%</td><td class="bar" id="d7"/><td class="ctr2" id="e7">n/a</td><td class="ctr1" id="f5">0</td><td class="ctr2" id="g7">1</td><td class="ctr1" id="h4">0</td><td class="ctr2" id="i4">4</td><td class="ctr1" id="j 4">0</td><td class="ctr2" id="k4">1</td></tr><tr><td id="a11"><a href="CSVParser.java.html#L155" class="el_method">parse(File, Charset, CSVFormat)</a></td><td class="bar" id="b5"><img src="../.resources/greenbar.gif" width="17" height="10" title="19" alt="19"/></td><td class="ctr2" id="c4">100%</td><td class="bar" id="d8"/><td class="ctr2" id="e8">n/a</td><td class="ctr1" id="f6">0</td><td class="ctr2" id="g8">1</td><td class="ctr1" id="h5">0</td><td class="ctr2" id="i6">3</td><td class="ctr1" id="j5">0</td><td class="ctr2" id="k5">1</td></tr><tr><td id="a6"><a href="CSVParser.java.html#L326" class="el_method">getRecords()</a></td><td class="bar" id="b6"><img src="../.resources/greenbar.gif" width="15" height="10" title="16" alt="16"/></td><td class="ctr2" id="c5">100%</td><td class="bar" id="d4"><img src="../.resources/greenbar.gif" width="10" height="10" title="2" alt="2"/></td><td class="ctr2" id="e1">100%</td><td class="ctr1" id="f7">0</td><td class="ctr2" id="g3">2</td><td clas s="ctr1" id="h6">0</td><td class="ctr2" id="i5">4</td><td class="ctr1" id="j6">0</td><td class="ctr2" id="k6">1</td></tr><tr><td id="a12"><a href="CSVParser.java.html#L175" class="el_method">parse(String, CSVFormat)</a></td><td class="bar" id="b7"><img src="../.resources/greenbar.gif" width="14" height="10" title="15" alt="15"/></td><td class="ctr2" id="c6">100%</td><td class="bar" id="d9"/><td class="ctr2" id="e9">n/a</td><td class="ctr1" id="f8">0</td><td class="ctr2" id="g9">1</td><td class="ctr1" id="h7">0</td><td class="ctr2" id="i7">3</td><td class="ctr1" id="j7">0</td><td class="ctr2" id="k7">1</td></tr><tr><td id="a4"><a href="CSVParser.java.html#L295" class="el_method">getHeaderMap()</a></td><td class="bar" id="b8"><img src="../.resources/greenbar.gif" width="10" height="10" title="11" alt="11"/></td><td class="ctr2" id="c7">100%</td><td class="bar" id="d5"><img src="../.resources/greenbar.gif" width="10" height="10" title="2" alt="2"/></td><td class="ctr2" id="e2">100%</td ><td class="ctr1" id="f9">0</td><td class="ctr2" id="g4">2</td><td >class="ctr1" id="h8">0</td><td class="ctr2" id="i9">1</td><td class="ctr1" >id="j8">0</td><td class="ctr2" id="k8">1</td></tr><tr><td id="a1"><a >href="CSVParser.java.html#L268" class="el_method">close()</a></td><td >class="bar" id="b9"><img src="../.resources/greenbar.gif" width="6" >height="10" title="7" alt="7"/></td><td class="ctr2" id="c8">100%</td><td >class="bar" id="d2"><img src="../.resources/redbar.gif" width="5" height="10" >title="1" alt="1"/><img src="../.resources/greenbar.gif" width="5" >height="10" title="1" alt="1"/></td><td class="ctr2" id="e5">50%</td><td >class="ctr1" id="f2">1</td><td class="ctr2" id="g5">2</td><td class="ctr1" >id="h9">0</td><td class="ctr2" id="i8">3</td><td class="ctr1" >id="j9">0</td><td class="ctr2" id="k9">1</td></tr><tr><td id="a9"><a >href="CSVParser.java.html#L390" class="el_method">iterator()</a></td><td >class="bar" id="b10"><img src="../.resources/greenbar.gif" width="4" height=" 10" title="5" alt="5"/></td><td class="ctr2" id="c9">100%</td><td class="bar" id="d10"/><td class="ctr2" id="e10">n/a</td><td class="ctr1" id="f10">0</td><td class="ctr2" id="g10">1</td><td class="ctr1" id="h10">0</td><td class="ctr2" id="i10">1</td><td class="ctr1" id="j10">0</td><td class="ctr2" id="k10">1</td></tr><tr><td id="a3"><a href="CSVParser.java.html#L284" class="el_method">getCurrentLineNumber()</a></td><td class="bar" id="b11"><img src="../.resources/greenbar.gif" width="3" height="10" title="4" alt="4"/></td><td class="ctr2" id="c10">100%</td><td class="bar" id="d11"/><td class="ctr2" id="e11">n/a</td><td class="ctr1" id="f11">0</td><td class="ctr2" id="g11">1</td><td class="ctr1" id="h11">0</td><td class="ctr2" id="i11">1</td><td class="ctr1" id="j11">0</td><td class="ctr2" id="k11">1</td></tr><tr><td id="a8"><a href="CSVParser.java.html#L378" class="el_method">isClosed()</a></td><td class="bar" id="b12"><img src="../.resources/greenbar.gif" width="3" height="10" titl e="4" alt="4"/></td><td class="ctr2" id="c11">100%</td><td class="bar" id="d12"/><td class="ctr2" id="e12">n/a</td><td class="ctr1" id="f12">0</td><td class="ctr2" id="g12">1</td><td class="ctr1" id="h12">0</td><td class="ctr2" id="i12">1</td><td class="ctr1" id="j12">0</td><td class="ctr2" id="k12">1</td></tr><tr><td id="a5"><a href="CSVParser.java.html#L309" class="el_method">getRecordNumber()</a></td><td class="bar" id="b13"><img src="../.resources/greenbar.gif" width="2" height="10" title="3" alt="3"/></td><td class="ctr2" id="c12">100%</td><td class="bar" id="d13"/><td class="ctr2" id="e13">n/a</td><td class="ctr1" id="f13">0</td><td class="ctr2" id="g13">1</td><td class="ctr1" id="h13">0</td><td class="ctr2" id="i13">1</td><td class="ctr1" id="j13">0</td><td class="ctr2" id="k13">1</td></tr></tbody></table><div class="footer"><span class="right">Created with <a href="http://www.eclemma.org/jacoco">JaCoCo</a> 0.7.1.201405082137</span></div></body></html> \ No newline at end of file
Propchange: websites/production/commons/content/proper/commons-csv/jacoco/org.apache.commons.csv/CSVParser.html ------------------------------------------------------------------------------ svn:eol-style = native Added: websites/production/commons/content/proper/commons-csv/jacoco/org.apache.commons.csv/CSVParser.java.html ============================================================================== --- websites/production/commons/content/proper/commons-csv/jacoco/org.apache.commons.csv/CSVParser.java.html (added) +++ websites/production/commons/content/proper/commons-csv/jacoco/org.apache.commons.csv/CSVParser.java.html Thu Aug 14 19:48:06 2014 @@ -0,0 +1,489 @@ +<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../.resources/report.css" type="text/css"/><link rel="shortcut icon" href="../.resources/report.gif" type="image/gif"/><title>CSVParser.java</title><link rel="stylesheet" href="../.resources/prettify.css" type="text/css"/><script type="text/javascript" src="../.resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../.sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">Apache Commons CSV</a> > <a href="index.source.html" class="el_package">org.apache.commons.csv</a> > <span class="el_source">CSVParser.java</span></div><h1>CSVParser.j ava</h1><pre class="source lang-java linenums">/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.csv; + +import java.io.Closeable; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.Reader; +import java.io.StringReader; +import java.net.URL; +import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.NoSuchElementException; + +import static org.apache.commons.csv.Token.Type.*; + +/** + * Parses CSV files according to the specified format. + * + * Because CSV appears in many different dialects, the parser supports many formats by allowing the + * specification of a {@link CSVFormat}. + * + * The parser works record wise. It is not possible to go back, once a record has been parsed from the input stream. + * + * <h2>Creating instances</h2> + * <p> + * There are several static factory methods that can be used to create instances for various types of resources: + * </p> + * <ul> + * <li>{@link #parse(java.io.File, Charset, CSVFormat)}</li> + * <li>{@link #parse(String, CSVFormat)}</li> + * <li>{@link #parse(java.net.URL, java.nio.charset.Charset, CSVFormat)}</li> + * </ul> + * <p> + * Alternatively parsers can also be created by passing a {@link Reader} directly to the sole constructor. + * + * For those who like fluent APIs, parsers can be created using {@link CSVFormat#parse(java.io.Reader)} as a shortcut: + * </p> + * <pre> + * for(CSVRecord record : CSVFormat.EXCEL.parse(in)) { + * ... + * } + * </pre> + * + * <h2>Parsing record wise</h2> + * <p> + * To parse a CSV input from a file, you write: + * </p> + * + * <pre> + * File csvData = new File(&quot;/path/to/csv&quot;); + * CSVParser parser = CSVParser.parse(csvData, CSVFormat.RFC4180); + * for (CSVRecord csvRecord : parser) { + * ... + * } + * </pre> + * + * <p> + * This will read the parse the contents of the file using the + * <a href="http://tools.ietf.org/html/rfc4180" target="_blank">RFC 4180</a> format. + * </p> + * + * <p> + * To parse CSV input in a format like Excel, you write: + * </p> + * + * <pre> + * CSVParser parser = CSVParser.parse(csvData, CSVFormat.EXCEL); + * for (CSVRecord csvRecord : parser) { + * ... + * } + * </pre> + * + * <p> + * If the predefined formats don't match the format at hands, custom formats can be defined. More information about + * customising CSVFormats is available in {@link CSVFormat CSVFormat JavaDoc}. + * </p> + * + * <h2>Parsing into memory</h2> + * <p> + * If parsing record wise is not desired, the contents of the input can be read completely into memory. + * </p> + * + * <pre> + * Reader in = new StringReader(&quot;a;b\nc;d&quot;); + * CSVParser parser = new CSVParser(in, CSVFormat.EXCEL); + * List&lt;CSVRecord&gt; list = parser.getRecords(); + * </pre> + * + * <p> + * There are two constraints that have to be kept in mind: + * </p> + * + * <ol> + * <li>Parsing into memory starts at the current position of the parser. If you have already parsed records from + * the input, those records will not end up in the in memory representation of your CSV data.</li> + * <li>Parsing into memory may consume a lot of system resources depending on the input. For example if you're + * parsing a 150MB file of CSV data the contents will be read completely into memory.</li> + * </ol> + * + * <h2>Notes</h2> + * <p> + * Internal parser state is completely covered by the format and the reader-state. + * </p> + * + * @version $Id: CSVParser.java 1617069 2014-08-10 08:53:42Z britter $ + * + * @see <a href="package-summary.html">package documentation for more details</a> + */ +public final class CSVParser implements Iterable<CSVRecord>, Closeable { + + /** + * Creates a parser for the given {@link File}. + * + * <p><strong>Note:</strong> This method internally creates a FileReader using + * {@link java.io.FileReader#FileReader(java.io.File)} which in turn relies on the default encoding of the JVM that + * is executing the code. If this is insufficient create a URL to the file and use + * {@link #parse(URL, Charset, CSVFormat)}</p> + * + * @param file + * a CSV file. Must not be null. + * @param charset + * A charset + * @param format + * the CSVFormat used for CSV parsing. Must not be null. + * @return a new parser + * @throws IllegalArgumentException + * If the parameters of the format are inconsistent or if either file or format are null. + * @throws IOException + * If an I/O error occurs + */ + public static CSVParser parse(final File file, final Charset charset, final CSVFormat format) throws IOException { +<span class="fc" id="L155"> Assertions.notNull(file, "file");</span> +<span class="fc" id="L156"> Assertions.notNull(format, "format");</span> + // Use the default Charset explicitly +<span class="fc" id="L158"> return new CSVParser(new InputStreamReader(new FileInputStream(file), charset), format);</span> + } + + /** + * Creates a parser for the given {@link String}. + * + * @param string + * a CSV string. Must not be null. + * @param format + * the CSVFormat used for CSV parsing. Must not be null. + * @return a new parser + * @throws IllegalArgumentException + * If the parameters of the format are inconsistent or if either string or format are null. + * @throws IOException + * If an I/O error occurs + */ + public static CSVParser parse(final String string, final CSVFormat format) throws IOException { +<span class="fc" id="L175"> Assertions.notNull(string, "string");</span> +<span class="fc" id="L176"> Assertions.notNull(format, "format");</span> + +<span class="fc" id="L178"> return new CSVParser(new StringReader(string), format);</span> + } + + /** + * Creates a parser for the given URL. + * + * <p> + * If you do not read all records from the given {@code url}, you should call {@link #close()} on the parser, unless + * you close the {@code url}. + * </p> + * + * @param url + * a URL. Must not be null. + * @param charset + * the charset for the resource. Must not be null. + * @param format + * the CSVFormat used for CSV parsing. Must not be null. + * @return a new parser + * @throws IllegalArgumentException + * If the parameters of the format are inconsistent or if either url, charset or format are null. + * @throws IOException + * If an I/O error occurs + */ + public static CSVParser parse(final URL url, final Charset charset, final CSVFormat format) throws IOException { +<span class="fc" id="L202"> Assertions.notNull(url, "url");</span> +<span class="fc" id="L203"> Assertions.notNull(charset, "charset");</span> +<span class="fc" id="L204"> Assertions.notNull(format, "format");</span> + +<span class="fc" id="L206"> return new CSVParser(new InputStreamReader(url.openStream(), charset), format);</span> + } + + // the following objects are shared to reduce garbage + + private final CSVFormat format; + + /** A mapping of column names to column indices */ + private final Map<String, Integer> headerMap; + + private final Lexer lexer; + + /** A record buffer for getRecord(). Grows as necessary and is reused. */ +<span class="fc" id="L219"> private final List<String> record = new ArrayList<String>();</span> + + private long recordNumber; + +<span class="fc" id="L223"> private final Token reusableToken = new Token();</span> + + /** + * Customized CSV parser using the given {@link CSVFormat} + * + * <p> + * If you do not read all records from the given {@code reader}, you should call {@link #close()} on the parser, + * unless you close the {@code reader}. + * </p> + * + * @param reader + * a Reader containing CSV-formatted input. Must not be null. + * @param format + * the CSVFormat used for CSV parsing. Must not be null. + * @throws IllegalArgumentException + * If the parameters of the format are inconsistent or if either reader or format are null. + * @throws IOException + * If there is a problem reading the header or skipping the first record + */ +<span class="fc" id="L242"> public CSVParser(final Reader reader, final CSVFormat format) throws IOException {</span> +<span class="fc" id="L243"> Assertions.notNull(reader, "reader");</span> +<span class="fc" id="L244"> Assertions.notNull(format, "format");</span> + +<span class="fc" id="L246"> this.format = format;</span> +<span class="fc" id="L247"> this.lexer = new Lexer(format, new ExtendedBufferedReader(reader));</span> +<span class="fc" id="L248"> this.headerMap = this.initializeHeader();</span> +<span class="fc" id="L249"> }</span> + + private void addRecordValue() { +<span class="fc" id="L252"> final String input = this.reusableToken.content.toString();</span> +<span class="fc" id="L253"> final String nullString = this.format.getNullString();</span> +<span class="fc bfc" id="L254" title="All 2 branches covered."> if (nullString == null) {</span> +<span class="fc" id="L255"> this.record.add(input);</span> + } else { +<span class="fc bfc" id="L257" title="All 2 branches covered."> this.record.add(input.equalsIgnoreCase(nullString) ? null : input);</span> + } +<span class="fc" id="L259"> }</span> + + /** + * Closes resources. + * + * @throws IOException + * If an I/O error occurs + */ + public void close() throws IOException { +<span class="pc bpc" id="L268" title="1 of 2 branches missed."> if (this.lexer != null) {</span> +<span class="fc" id="L269"> this.lexer.close();</span> + } +<span class="fc" id="L271"> }</span> + + /** + * Returns the current line number in the input stream. + * + * <p> + * <strong>ATTENTION:</strong> If your CSV input has multi-line values, the returned number does not correspond to + * the record number. + * </p> + * + * @return current line number + */ + public long getCurrentLineNumber() { +<span class="fc" id="L284"> return this.lexer.getCurrentLineNumber();</span> + } + + /** + * Returns a copy of the header map that iterates in column order. + * <p> + * The map keys are column names. The map values are 0-based indices. + * </p> + * @return a copy of the header map that iterates in column order. + */ + public Map<String, Integer> getHeaderMap() { +<span class="fc bfc" id="L295" title="All 2 branches covered."> return this.headerMap == null ? null : new LinkedHashMap<String, Integer>(this.headerMap);</span> + } + + /** + * Returns the current record number in the input stream. + * + * <p> + * <strong>ATTENTION:</strong> If your CSV input has multi-line values, the returned number does not correspond to + * the line number. + * </p> + * + * @return current line number + */ + public long getRecordNumber() { +<span class="fc" id="L309"> return this.recordNumber;</span> + } + + /** + * Parses the CSV input according to the given format and returns the content as a list of + * {@link CSVRecord CSVRecords}. + * + * <p> + * The returned content starts at the current parse-position in the stream. + * </p> + * + * @return list of {@link CSVRecord CSVRecords}, may be empty + * @throws IOException + * on parse error or input read-failure + */ + public List<CSVRecord> getRecords() throws IOException { + CSVRecord rec; +<span class="fc" id="L326"> List<CSVRecord> records = new ArrayList<CSVRecord>();</span> +<span class="fc bfc" id="L327" title="All 2 branches covered."> while ((rec = this.nextRecord()) != null) {</span> +<span class="fc" id="L328"> records.add(rec);</span> + } +<span class="fc" id="L330"> return records;</span> + } + + /** + * Initializes the name to index mapping if the format defines a header. + * + * @return null if the format has no header. + * @throws IOException if there is a problem reading the header or skipping the first record + */ + private Map<String, Integer> initializeHeader() throws IOException { +<span class="fc" id="L340"> Map<String, Integer> hdrMap = null;</span> +<span class="fc" id="L341"> final String[] formatHeader = this.format.getHeader();</span> +<span class="fc bfc" id="L342" title="All 2 branches covered."> if (formatHeader != null) {</span> +<span class="fc" id="L343"> hdrMap = new LinkedHashMap<String, Integer>();</span> + +<span class="fc" id="L345"> String[] headerRecord = null;</span> +<span class="fc bfc" id="L346" title="All 2 branches covered."> if (formatHeader.length == 0) {</span> + // read the header from the first line of the file +<span class="fc" id="L348"> final CSVRecord nextRecord = this.nextRecord();</span> +<span class="pc bpc" id="L349" title="1 of 2 branches missed."> if (nextRecord != null) {</span> +<span class="fc" id="L350"> headerRecord = nextRecord.values();</span> + } +<span class="fc" id="L352"> } else {</span> +<span class="fc bfc" id="L353" title="All 2 branches covered."> if (this.format.getSkipHeaderRecord()) {</span> +<span class="fc" id="L354"> this.nextRecord();</span> + } +<span class="fc" id="L356"> headerRecord = formatHeader;</span> + } + + // build the name to index mappings +<span class="pc bpc" id="L360" title="1 of 2 branches missed."> if (headerRecord != null) {</span> +<span class="fc bfc" id="L361" title="All 2 branches covered."> for (int i = 0; i < headerRecord.length; i++) {</span> +<span class="fc" id="L362"> final String header = headerRecord[i];</span> +<span class="fc" id="L363"> final boolean containsHeader = hdrMap.containsKey(header);</span> +<span class="fc bfc" id="L364" title="All 4 branches covered."> final boolean emptyHeader = header == null || header.trim().isEmpty();</span> +<span class="pc bpc" id="L365" title="1 of 6 branches missed."> if (containsHeader &&</span> +<span class="fc bfc" id="L366" title="All 2 branches covered."> (!emptyHeader || (emptyHeader && !this.format.getAllowMissingColumnNames()))) {</span> +<span class="fc" id="L367"> throw new IllegalArgumentException("The header contains a duplicate name: \"" + header +</span> +<span class="fc" id="L368"> "\" in " + Arrays.toString(headerRecord));</span> + } +<span class="fc" id="L370"> hdrMap.put(header, Integer.valueOf(i));</span> + } + } + } +<span class="fc" id="L374"> return hdrMap;</span> + } + + public boolean isClosed() { +<span class="fc" id="L378"> return this.lexer.isClosed();</span> + } + + /** + * Returns an iterator on the records. + * + * <p>IOExceptions occurring during the iteration are wrapped in a + * RuntimeException. + * If the parser is closed a call to {@code next()} will throw a + * NoSuchElementException.</p> + */ + public Iterator<CSVRecord> iterator() { +<span class="fc" id="L390"> return new Iterator<CSVRecord>() {</span> + private CSVRecord current; + + private CSVRecord getNextRecord() { + try { +<span class="fc" id="L395"> return CSVParser.this.nextRecord();</span> +<span class="nc" id="L396"> } catch (final IOException e) {</span> + // TODO: This is not great, throw an ISE instead? +<span class="nc" id="L398"> throw new RuntimeException(e);</span> + } + } + + public boolean hasNext() { +<span class="fc bfc" id="L403" title="All 2 branches covered."> if (CSVParser.this.isClosed()) {</span> +<span class="fc" id="L404"> return false;</span> + } +<span class="fc bfc" id="L406" title="All 2 branches covered."> if (this.current == null) {</span> +<span class="fc" id="L407"> this.current = this.getNextRecord();</span> + } + +<span class="fc bfc" id="L410" title="All 2 branches covered."> return this.current != null;</span> + } + + public CSVRecord next() { +<span class="fc bfc" id="L414" title="All 2 branches covered."> if (CSVParser.this.isClosed()) {</span> +<span class="fc" id="L415"> throw new NoSuchElementException("CSVParser has been closed");</span> + } +<span class="fc" id="L417"> CSVRecord next = this.current;</span> +<span class="fc" id="L418"> this.current = null;</span> + +<span class="fc bfc" id="L420" title="All 2 branches covered."> if (next == null) {</span> + // hasNext() wasn't called before +<span class="fc" id="L422"> next = this.getNextRecord();</span> +<span class="fc bfc" id="L423" title="All 2 branches covered."> if (next == null) {</span> +<span class="fc" id="L424"> throw new NoSuchElementException("No more CSV records available");</span> + } + } + +<span class="fc" id="L428"> return next;</span> + } + + public void remove() { +<span class="fc" id="L432"> throw new UnsupportedOperationException();</span> + } + }; + } + + /** + * Parses the next record from the current point in the stream. + * + * @return the record as an array of values, or {@code null} if the end of the stream has been reached + * @throws IOException + * on parse error or input read-failure + */ + CSVRecord nextRecord() throws IOException { +<span class="fc" id="L445"> CSVRecord result = null;</span> +<span class="fc" id="L446"> this.record.clear();</span> +<span class="fc" id="L447"> StringBuilder sb = null;</span> + do { +<span class="fc" id="L449"> this.reusableToken.reset();</span> +<span class="fc" id="L450"> this.lexer.nextToken(this.reusableToken);</span> +<span class="pc bpc" id="L451" title="2 of 6 branches missed."> switch (this.reusableToken.type) {</span> + case TOKEN: +<span class="fc" id="L453"> this.addRecordValue();</span> +<span class="fc" id="L454"> break;</span> + case EORECORD: +<span class="fc" id="L456"> this.addRecordValue();</span> +<span class="fc" id="L457"> break;</span> + case EOF: +<span class="fc bfc" id="L459" title="All 2 branches covered."> if (this.reusableToken.isReady) {</span> +<span class="fc" id="L460"> this.addRecordValue();</span> + } + break; + case INVALID: +<span class="nc" id="L464"> throw new IOException("(line " + this.getCurrentLineNumber() + ") invalid parse sequence");</span> + case COMMENT: // Ignored currently +<span class="fc bfc" id="L466" title="All 2 branches covered."> if (sb == null) { // first comment for this record</span> +<span class="fc" id="L467"> sb = new StringBuilder();</span> + } else { +<span class="fc" id="L469"> sb.append(Constants.LF);</span> + } +<span class="fc" id="L471"> sb.append(this.reusableToken.content);</span> +<span class="fc" id="L472"> this.reusableToken.type = TOKEN; // Read another token</span> +<span class="fc" id="L473"> break;</span> + default: +<span class="nc" id="L475"> throw new IllegalStateException("Unexpected Token type: " + this.reusableToken.type);</span> + } +<span class="fc bfc" id="L477" title="All 2 branches covered."> } while (this.reusableToken.type == TOKEN);</span> + +<span class="fc bfc" id="L479" title="All 2 branches covered."> if (!this.record.isEmpty()) {</span> +<span class="fc" id="L480"> this.recordNumber++;</span> +<span class="fc bfc" id="L481" title="All 2 branches covered."> final String comment = sb == null ? null : sb.toString();</span> +<span class="fc" id="L482"> result = new CSVRecord(this.record.toArray(new String[this.record.size()]), this.headerMap, comment,</span> + this.recordNumber); + } +<span class="fc" id="L485"> return result;</span> + } + +} +</pre><div class="footer"><span class="right">Created with <a href="http://www.eclemma.org/jacoco">JaCoCo</a> 0.7.1.201405082137</span></div></body></html> \ No newline at end of file Propchange: websites/production/commons/content/proper/commons-csv/jacoco/org.apache.commons.csv/CSVParser.java.html ------------------------------------------------------------------------------ svn:eol-style = native Added: websites/production/commons/content/proper/commons-csv/jacoco/org.apache.commons.csv/CSVPrinter$1.html ============================================================================== --- websites/production/commons/content/proper/commons-csv/jacoco/org.apache.commons.csv/CSVPrinter$1.html (added) +++ websites/production/commons/content/proper/commons-csv/jacoco/org.apache.commons.csv/CSVPrinter$1.html Thu Aug 14 19:48:06 2014 @@ -0,0 +1 @@ +<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../.resources/report.css" type="text/css"/><link rel="shortcut icon" href="../.resources/report.gif" type="image/gif"/><title>CSVPrinter.new Object() {...}</title><script type="text/javascript" src="../.resources/sort.js"></script></head><body onload="initialSort(['breadcrumb'])"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../.sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">Apache Commons CSV</a> > <a href="index.html" class="el_package">org.apache.commons.csv</a> > <span class="el_class">CSVPrinter.new Object() {...}</span></div><h1>CSVPrinter.new Object() {...}</h1><table class="coverage" cellspacing="0" id=" coveragetable"><thead><tr><td class="sortable" id="a" onclick="toggleSort(this)">Element</td><td class="down sortable bar" id="b" onclick="toggleSort(this)">Missed Instructions</td><td class="sortable ctr2" id="c" onclick="toggleSort(this)">Cov.</td><td class="sortable bar" id="d" onclick="toggleSort(this)">Missed Branches</td><td class="sortable ctr2" id="e" onclick="toggleSort(this)">Cov.</td><td class="sortable ctr1" id="f" onclick="toggleSort(this)">Missed</td><td class="sortable ctr2" id="g" onclick="toggleSort(this)">Cxty</td><td class="sortable ctr1" id="h" onclick="toggleSort(this)">Missed</td><td class="sortable ctr2" id="i" onclick="toggleSort(this)">Lines</td><td class="sortable ctr1" id="j" onclick="toggleSort(this)">Missed</td><td class="sortable ctr2" id="k" onclick="toggleSort(this)">Methods</td></tr></thead><tfoot><tr><td>Total</td><td class="bar">4 of 33</td><td class="ctr2">88%</td><td class="bar">0 of 0</td><td class="ctr2">n/a</td><td class="ctr1">0</td><td class ="ctr2">1</td><td class="ctr1">0</td><td class="ctr2">1</td><td class="ctr1">0</td><td class="ctr2">1</td></tr></tfoot><tbody><tr><td id="a0"><a href="CSVPrinter.java.html#L189" class="el_method">static {...}</a></td><td class="bar" id="b0"><img src="../.resources/redbar.gif" width="14" height="10" title="4" alt="4"/><img src="../.resources/greenbar.gif" width="105" height="10" title="29" alt="29"/></td><td class="ctr2" id="c0">88%</td><td class="bar" id="d0"/><td class="ctr2" id="e0">n/a</td><td class="ctr1" id="f0">0</td><td class="ctr2" id="g0">1</td><td class="ctr1" id="h0">0</td><td class="ctr2" id="i0">1</td><td class="ctr1" id="j0">0</td><td class="ctr2" id="k0">1</td></tr></tbody></table><div class="footer"><span class="right">Created with <a href="http://www.eclemma.org/jacoco">JaCoCo</a> 0.7.1.201405082137</span></div></body></html> \ No newline at end of file Propchange: websites/production/commons/content/proper/commons-csv/jacoco/org.apache.commons.csv/CSVPrinter$1.html ------------------------------------------------------------------------------ svn:eol-style = native Added: websites/production/commons/content/proper/commons-csv/jacoco/org.apache.commons.csv/CSVPrinter.html ============================================================================== --- websites/production/commons/content/proper/commons-csv/jacoco/org.apache.commons.csv/CSVPrinter.html (added) +++ websites/production/commons/content/proper/commons-csv/jacoco/org.apache.commons.csv/CSVPrinter.html Thu Aug 14 19:48:06 2014 @@ -0,0 +1 @@ +<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../.resources/report.css" type="text/css"/><link rel="shortcut icon" href="../.resources/report.gif" type="image/gif"/><title>CSVPrinter</title><script type="text/javascript" src="../.resources/sort.js"></script></head><body onload="initialSort(['breadcrumb'])"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../.sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">Apache Commons CSV</a> > <a href="index.html" class="el_package">org.apache.commons.csv</a> > <span class="el_class">CSVPrinter</span></div><h1>CSVPrinter</h1><table class="coverage" cellspacing="0" id="coveragetable"><thead><tr><td class="sortable" id="a" onc lick="toggleSort(this)">Element</td><td class="down sortable bar" id="b" onclick="toggleSort(this)">Missed Instructions</td><td class="sortable ctr2" id="c" onclick="toggleSort(this)">Cov.</td><td class="sortable bar" id="d" onclick="toggleSort(this)">Missed Branches</td><td class="sortable ctr2" id="e" onclick="toggleSort(this)">Cov.</td><td class="sortable ctr1" id="f" onclick="toggleSort(this)">Missed</td><td class="sortable ctr2" id="g" onclick="toggleSort(this)">Cxty</td><td class="sortable ctr1" id="h" onclick="toggleSort(this)">Missed</td><td class="sortable ctr2" id="i" onclick="toggleSort(this)">Lines</td><td class="sortable ctr1" id="j" onclick="toggleSort(this)">Missed</td><td class="sortable ctr2" id="k" onclick="toggleSort(this)">Methods</td></tr></thead><tfoot><tr><td>Total</td><td class="bar">44 of 631</td><td class="ctr2">93%</td><td class="bar">10 of 120</td><td class="ctr2">92%</td><td class="ctr1">8</td><td class="ctr2">77</td><td class="ctr1">6</td><td class="ctr 2">160</td><td class="ctr1">0</td><td class="ctr2">15</td></tr></tfoot><tbody><tr><td id="a8"><a href="CSVPrinter.java.html#L298" class="el_method">printComment(String)</a></td><td class="bar" id="b0"><img src="../.resources/redbar.gif" width="9" height="10" title="16" alt="16"/><img src="../.resources/greenbar.gif" width="34" height="10" title="59" alt="59"/></td><td class="ctr2" id="c13">79%</td><td class="bar" id="d0"><img src="../.resources/redbar.gif" width="14" height="10" title="6" alt="6"/><img src="../.resources/greenbar.gif" width="16" height="10" title="7" alt="7"/></td><td class="ctr2" id="e12">54%</td><td class="ctr1" id="f0">4</td><td class="ctr2" id="g2">8</td><td class="ctr1" id="h0">3</td><td class="ctr2" id="i2">18</td><td class="ctr1" id="j0">0</td><td class="ctr2" id="k0">1</td></tr><tr><td id="a7"><a href="CSVPrinter.java.html#L177" class="el_method">printAndQuote(Object, CharSequence, int, int)</a></td><td class="bar" id="b1"><img src="../.resources/redbar.gif" width="7" height="10" title="12" alt="12"/><img src="../.resources/greenbar.gif" width="112" height="10" title="191" alt="191"/></td><td class="ctr2" id="c11">94%</td><td class="bar" id="d1"><img src="../.resources/redbar.gif" width="2" height="10" title="1" alt="1"/><img src="../.resources/greenbar.gif" width="117" height="10" title="50" alt="50"/></td><td class="ctr2" id="e9">98%</td><td class="ctr1" id="f1">1</td><td class="ctr2" id="g0">28</td><td class="ctr1" id="h1">1</td><td class="ctr2" id="i0">53</td><td class="ctr1" id="j1">0</td><td class="ctr2" id="k1">1</td></tr><tr><td id="a13"><a href="CSVPrinter.java.html#L456" class="el_method">printRecords(Object[])</a></td><td class="bar" id="b2"><img src="../.resources/redbar.gif" width="4" height="10" title="8" alt="8"/><img src="../.resources/greenbar.gif" width="20" height="10" title="34" alt="34"/></td><td class="ctr2" id="c12">81%</td><td class="bar" id="d2"><img src="../.resources/redbar.gif" width="2" height="10" title="1 " alt="1"/><img src="../.resources/greenbar.gif" width="11" height="10" title="5" alt="5"/></td><td class="ctr2" id="e10">83%</td><td class="ctr1" id="f2">1</td><td class="ctr2" id="g3">4</td><td class="ctr1" id="h2">1</td><td class="ctr2" id="i6">7</td><td class="ctr1" id="j2">0</td><td class="ctr2" id="k2">1</td></tr><tr><td id="a12"><a href="CSVPrinter.java.html#L413" class="el_method">printRecords(Iterable)</a></td><td class="bar" id="b3"><img src="../.resources/redbar.gif" width="4" height="10" title="8" alt="8"/><img src="../.resources/greenbar.gif" width="16" height="10" title="28" alt="28"/></td><td class="ctr2" id="c14">78%</td><td class="bar" id="d3"><img src="../.resources/redbar.gif" width="2" height="10" title="1" alt="1"/><img src="../.resources/greenbar.gif" width="11" height="10" title="5" alt="5"/></td><td class="ctr2" id="e11">83%</td><td class="ctr1" id="f3">1</td><td class="ctr2" id="g4">4</td><td class="ctr1" id="h3">1</td><td class="ctr2" id="i5">8</td><td clas s="ctr1" id="j3">0</td><td class="ctr2" id="k3">1</td></tr><tr><td id="a6"><a href="CSVPrinter.java.html#L136" class="el_method">printAndEscape(CharSequence, int, int)</a></td><td class="bar" id="b4"><img src="../.resources/greenbar.gif" width="49" height="10" title="84" alt="84"/></td><td class="ctr2" id="c0">100%</td><td class="bar" id="d5"><img src="../.resources/greenbar.gif" width="42" height="10" title="18" alt="18"/></td><td class="ctr2" id="e0">100%</td><td class="ctr1" id="f5">0</td><td class="ctr2" id="g1">10</td><td class="ctr1" id="h4">0</td><td class="ctr2" id="i1">22</td><td class="ctr1" id="j4">0</td><td class="ctr2" id="k4">1</td></tr><tr><td id="a5"><a href="CSVPrinter.java.html#L118" class="el_method">print(Object, CharSequence, int, int)</a></td><td class="bar" id="b5"><img src="../.resources/greenbar.gif" width="26" height="10" title="44" alt="44"/></td><td class="ctr2" id="c1">100%</td><td class="bar" id="d6"><img src="../.resources/greenbar.gif" width="14" heig ht="10" title="6" alt="6"/></td><td class="ctr2" id="e1">100%</td><td class="ctr1" id="f6">0</td><td class="ctr2" id="g5">4</td><td class="ctr1" id="h5">0</td><td class="ctr2" id="i3">9</td><td class="ctr1" id="j5">0</td><td class="ctr2" id="k5">1</td></tr><tr><td id="a1"><a href="CSVPrinter.java.html#L43" class="el_method">CSVPrinter(Appendable, CSVFormat)</a></td><td class="bar" id="b6"><img src="../.resources/greenbar.gif" width="15" height="10" title="26" alt="26"/></td><td class="ctr2" id="c2">100%</td><td class="bar" id="d9"><img src="../.resources/greenbar.gif" width="4" height="10" title="2" alt="2"/></td><td class="ctr2" id="e2">100%</td><td class="ctr1" id="f7">0</td><td class="ctr2" id="g8">2</td><td class="ctr1" id="h6">0</td><td class="ctr2" id="i4">9</td><td class="ctr1" id="j6">0</td><td class="ctr2" id="k6">1</td></tr><tr><td id="a4"><a href="CSVPrinter.java.html#L107" class="el_method">print(Object)</a></td><td class="bar" id="b7"><img src="../.resources/greenbar.gi f" width="14" height="10" title="24" alt="24"/></td><td class="ctr2" id="c3">100%</td><td class="bar" id="d7"><img src="../.resources/greenbar.gif" width="9" height="10" title="4" alt="4"/></td><td class="ctr2" id="e3">100%</td><td class="ctr1" id="f8">0</td><td class="ctr2" id="g6">3</td><td class="ctr1" id="h7">0</td><td class="ctr2" id="i7">7</td><td class="ctr1" id="j7">0</td><td class="ctr2" id="k7">1</td></tr><tr><td id="a14"><a href="CSVPrinter.java.html#L477" class="el_method">printRecords(ResultSet)</a></td><td class="bar" id="b8"><img src="../.resources/greenbar.gif" width="13" height="10" title="23" alt="23"/></td><td class="ctr2" id="c4">100%</td><td class="bar" id="d8"><img src="../.resources/greenbar.gif" width="9" height="10" title="4" alt="4"/></td><td class="ctr2" id="e4">100%</td><td class="ctr1" id="f9">0</td><td class="ctr2" id="g7">3</td><td class="ctr1" id="h8">0</td><td class="ctr2" id="i8">6</td><td class="ctr1" id="j8">0</td><td class="ctr2" id="k8">1</td></ tr><tr><td id="a11"><a href="CSVPrinter.java.html#L375" class="el_method">printRecord(Object[])</a></td><td class="bar" id="b9"><img src="../.resources/greenbar.gif" width="13" height="10" title="22" alt="22"/></td><td class="ctr2" id="c5">100%</td><td class="bar" id="d10"><img src="../.resources/greenbar.gif" width="4" height="10" title="2" alt="2"/></td><td class="ctr2" id="e5">100%</td><td class="ctr1" id="f10">0</td><td class="ctr2" id="g9">2</td><td class="ctr1" id="h9">0</td><td class="ctr2" id="i11">4</td><td class="ctr1" id="j9">0</td><td class="ctr2" id="k9">1</td></tr><tr><td id="a10"><a href="CSVPrinter.java.html#L355" class="el_method">printRecord(Iterable)</a></td><td class="bar" id="b10"><img src="../.resources/greenbar.gif" width="9" height="10" title="16" alt="16"/></td><td class="ctr2" id="c6">100%</td><td class="bar" id="d11"><img src="../.resources/greenbar.gif" width="4" height="10" title="2" alt="2"/></td><td class="ctr2" id="e6">100%</td><td class="ctr1" id="f1 1">0</td><td class="ctr2" id="g10">2</td><td class="ctr1" id="h10">0</td><td class="ctr2" id="i9">5</td><td class="ctr1" id="j10">0</td><td class="ctr2" id="k10">1</td></tr><tr><td id="a9"><a href="CSVPrinter.java.html#L334" class="el_method">println()</a></td><td class="bar" id="b11"><img src="../.resources/greenbar.gif" width="8" height="10" title="15" alt="15"/></td><td class="ctr2" id="c7">100%</td><td class="bar" id="d12"><img src="../.resources/greenbar.gif" width="4" height="10" title="2" alt="2"/></td><td class="ctr2" id="e7">100%</td><td class="ctr1" id="f12">0</td><td class="ctr2" id="g11">2</td><td class="ctr1" id="h11">0</td><td class="ctr2" id="i10">5</td><td class="ctr1" id="j11">0</td><td class="ctr2" id="k11">1</td></tr><tr><td id="a0"><a href="CSVPrinter.java.html#L79" class="el_method">close()</a></td><td class="bar" id="b12"><img src="../.resources/greenbar.gif" width="5" height="10" title="9" alt="9"/></td><td class="ctr2" id="c8">100%</td><td class="bar" id="d13 "><img src="../.resources/greenbar.gif" width="4" height="10" title="2" alt="2"/></td><td class="ctr2" id="e8">100%</td><td class="ctr1" id="f13">0</td><td class="ctr2" id="g12">2</td><td class="ctr1" id="h12">0</td><td class="ctr2" id="i12">3</td><td class="ctr1" id="j12">0</td><td class="ctr2" id="k12">1</td></tr><tr><td id="a2"><a href="CSVPrinter.java.html#L91" class="el_method">flush()</a></td><td class="bar" id="b13"><img src="../.resources/greenbar.gif" width="5" height="10" title="9" alt="9"/></td><td class="ctr2" id="c9">100%</td><td class="bar" id="d4"><img src="../.resources/redbar.gif" width="2" height="10" title="1" alt="1"/><img src="../.resources/greenbar.gif" width="2" height="10" title="1" alt="1"/></td><td class="ctr2" id="e13">50%</td><td class="ctr1" id="f4">1</td><td class="ctr2" id="g13">2</td><td class="ctr1" id="h13">0</td><td class="ctr2" id="i13">3</td><td class="ctr1" id="j13">0</td><td class="ctr2" id="k13">1</td></tr><tr><td id="a3"><a href="CSVPrinter.j ava.html#L492" class="el_method">getOut()</a></td><td class="bar" id="b14"><img src="../.resources/greenbar.gif" width="1" height="10" title="3" alt="3"/></td><td class="ctr2" id="c10">100%</td><td class="bar" id="d14"/><td class="ctr2" id="e14">n/a</td><td class="ctr1" id="f14">0</td><td class="ctr2" id="g14">1</td><td class="ctr1" id="h14">0</td><td class="ctr2" id="i14">1</td><td class="ctr1" id="j14">0</td><td class="ctr2" id="k14">1</td></tr></tbody></table><div class="footer"><span class="right">Created with <a href="http://www.eclemma.org/jacoco">JaCoCo</a> 0.7.1.201405082137</span></div></body></html> \ No newline at end of file Propchange: websites/production/commons/content/proper/commons-csv/jacoco/org.apache.commons.csv/CSVPrinter.html ------------------------------------------------------------------------------ svn:eol-style = native Added: websites/production/commons/content/proper/commons-csv/jacoco/org.apache.commons.csv/CSVPrinter.java.html ============================================================================== --- websites/production/commons/content/proper/commons-csv/jacoco/org.apache.commons.csv/CSVPrinter.java.html (added) +++ websites/production/commons/content/proper/commons-csv/jacoco/org.apache.commons.csv/CSVPrinter.java.html Thu Aug 14 19:48:06 2014 @@ -0,0 +1,495 @@ +<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../.resources/report.css" type="text/css"/><link rel="shortcut icon" href="../.resources/report.gif" type="image/gif"/><title>CSVPrinter.java</title><link rel="stylesheet" href="../.resources/prettify.css" type="text/css"/><script type="text/javascript" src="../.resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../.sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">Apache Commons CSV</a> > <a href="index.source.html" class="el_package">org.apache.commons.csv</a> > <span class="el_source">CSVPrinter.java</span></div><h1>CSVPrinte r.java</h1><pre class="source lang-java linenums">/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.csv; + +import static org.apache.commons.csv.Constants.COMMENT; +import static org.apache.commons.csv.Constants.CR; +import static org.apache.commons.csv.Constants.LF; +import static org.apache.commons.csv.Constants.SP; + +import java.io.Closeable; +import java.io.Flushable; +import java.io.IOException; +import java.sql.ResultSet; +import java.sql.SQLException; + +/** + * Prints values in a CSV format. + * + * @version $Id: CSVPrinter.java 1617076 2014-08-10 09:23:01Z britter $ + */ +public final class CSVPrinter implements Flushable, Closeable { + + /** The place that the values get written. */ + private final Appendable out; + private final CSVFormat format; + + /** True if we just began a new record. */ +<span class="fc" id="L43"> private boolean newRecord = true;</span> + + /** + * Creates a printer that will print values to the given stream following the CSVFormat. + * <p> + * Currently, only a pure encapsulation format or a pure escaping format is supported. Hybrid formats (encapsulation + * and escaping with a different character) are not supported. + * </p> + * + * @param out + * stream to which to print. Must not be null. + * @param format + * the CSV format. Must not be null. + * @throws IOException + * thrown if the optional header cannot be printed. + * @throws IllegalArgumentException + * thrown if the parameters of the format are inconsistent or if either out or format are null. + */ +<span class="fc" id="L61"> public CSVPrinter(final Appendable out, final CSVFormat format) throws IOException {</span> +<span class="fc" id="L62"> Assertions.notNull(out, "out");</span> +<span class="fc" id="L63"> Assertions.notNull(format, "format");</span> + +<span class="fc" id="L65"> this.out = out;</span> +<span class="fc" id="L66"> this.format = format;</span> + // TODO: Is it a good idea to do this here instead of on the first call to a print method? + // It seems a pain to have to track whether the header has already been printed or not. +<span class="fc bfc" id="L69" title="All 2 branches covered."> if (format.getHeader() != null) {</span> +<span class="fc" id="L70"> this.printRecord((Object[]) format.getHeader());</span> + } +<span class="fc" id="L72"> }</span> + + // ====================================================== + // printing implementation + // ====================================================== + + public void close() throws IOException { +<span class="fc bfc" id="L79" title="All 2 branches covered."> if (out instanceof Closeable) {</span> +<span class="fc" id="L80"> ((Closeable) out).close();</span> + } +<span class="fc" id="L82"> }</span> + + /** + * Flushes the underlying stream. + * + * @throws IOException + * If an I/O error occurs + */ + public void flush() throws IOException { +<span class="pc bpc" id="L91" title="1 of 2 branches missed."> if (out instanceof Flushable) {</span> +<span class="fc" id="L92"> ((Flushable) out).flush();</span> + } +<span class="fc" id="L94"> }</span> + + /** + * Prints the string as the next value on the line. The value will be escaped or encapsulated as needed. + * + * @param value + * value to be output. + * @throws IOException + * If an I/O error occurs + */ + public void print(final Object value) throws IOException { + // null values are considered empty + String strValue; +<span class="fc bfc" id="L107" title="All 2 branches covered."> if (value == null) {</span> +<span class="fc" id="L108"> final String nullString = format.getNullString();</span> +<span class="fc bfc" id="L109" title="All 2 branches covered."> strValue = nullString == null ? Constants.EMPTY : nullString;</span> +<span class="fc" id="L110"> } else {</span> +<span class="fc" id="L111"> strValue = value.toString();</span> + } +<span class="fc" id="L113"> this.print(value, strValue, 0, strValue.length());</span> +<span class="fc" id="L114"> }</span> + + private void print(final Object object, final CharSequence value, + final int offset, final int len) throws IOException { +<span class="fc bfc" id="L118" title="All 2 branches covered."> if (!newRecord) {</span> +<span class="fc" id="L119"> out.append(format.getDelimiter());</span> + } +<span class="fc bfc" id="L121" title="All 2 branches covered."> if (format.isQuoteCharacterSet()) {</span> + // the original object is needed so can check for Number +<span class="fc" id="L123"> printAndQuote(object, value, offset, len);</span> +<span class="fc bfc" id="L124" title="All 2 branches covered."> } else if (format.isEscapeCharacterSet()) {</span> +<span class="fc" id="L125"> printAndEscape(value, offset, len);</span> + } else { +<span class="fc" id="L127"> out.append(value, offset, offset + len);</span> + } +<span class="fc" id="L129"> newRecord = false;</span> +<span class="fc" id="L130"> }</span> + + /* + * Note: must only be called if escaping is enabled, otherwise will generate NPE + */ + private void printAndEscape(final CharSequence value, final int offset, final int len) throws IOException { +<span class="fc" id="L136"> int start = offset;</span> +<span class="fc" id="L137"> int pos = offset;</span> +<span class="fc" id="L138"> final int end = offset + len;</span> + +<span class="fc" id="L140"> final char delim = format.getDelimiter();</span> +<span class="fc" id="L141"> final char escape = format.getEscapeCharacter().charValue();</span> + +<span class="fc bfc" id="L143" title="All 2 branches covered."> while (pos < end) {</span> +<span class="fc" id="L144"> char c = value.charAt(pos);</span> +<span class="fc bfc" id="L145" title="All 8 branches covered."> if (c == CR || c == LF || c == delim || c == escape) {</span> + // write out segment up until this char +<span class="fc bfc" id="L147" title="All 2 branches covered."> if (pos > start) {</span> +<span class="fc" id="L148"> out.append(value, start, pos);</span> + } +<span class="fc bfc" id="L150" title="All 2 branches covered."> if (c == LF) {</span> +<span class="fc" id="L151"> c = 'n';</span> +<span class="fc bfc" id="L152" title="All 2 branches covered."> } else if (c == CR) {</span> +<span class="fc" id="L153"> c = 'r';</span> + } + +<span class="fc" id="L156"> out.append(escape);</span> +<span class="fc" id="L157"> out.append(c);</span> + +<span class="fc" id="L159"> start = pos + 1; // start on the current char after this one</span> + } + +<span class="fc" id="L162"> pos++;</span> +<span class="fc" id="L163"> }</span> + + // write last segment +<span class="fc bfc" id="L166" title="All 2 branches covered."> if (pos > start) {</span> +<span class="fc" id="L167"> out.append(value, start, pos);</span> + } +<span class="fc" id="L169"> }</span> + + /* + * Note: must only be called if quoting is enabled, otherwise will generate NPE + */ + // the original object is needed so can check for Number + private void printAndQuote(final Object object, final CharSequence value, + final int offset, final int len) throws IOException { +<span class="fc" id="L177"> boolean quote = false;</span> +<span class="fc" id="L178"> int start = offset;</span> +<span class="fc" id="L179"> int pos = offset;</span> +<span class="fc" id="L180"> final int end = offset + len;</span> + +<span class="fc" id="L182"> final char delimChar = format.getDelimiter();</span> +<span class="fc" id="L183"> final char quoteChar = format.getQuoteCharacter().charValue();</span> + +<span class="fc" id="L185"> QuoteMode quoteModePolicy = format.getQuoteMode();</span> +<span class="fc bfc" id="L186" title="All 2 branches covered."> if (quoteModePolicy == null) {</span> +<span class="fc" id="L187"> quoteModePolicy = QuoteMode.MINIMAL;</span> + } +<span class="pc bpc" id="L189" title="1 of 5 branches missed."> switch (quoteModePolicy) {</span> + case ALL: +<span class="fc" id="L191"> quote = true;</span> +<span class="fc" id="L192"> break;</span> + case NON_NUMERIC: +<span class="fc bfc" id="L194" title="All 2 branches covered."> quote = !(object instanceof Number);</span> +<span class="fc" id="L195"> break;</span> + case NONE: + // Use the existing escaping code +<span class="fc" id="L198"> printAndEscape(value, offset, len);</span> +<span class="fc" id="L199"> return;</span> + case MINIMAL: +<span class="fc bfc" id="L201" title="All 2 branches covered."> if (len <= 0) {</span> + // always quote an empty token that is the first + // on the line, as it may be the only thing on the + // line. If it were not quoted in that case, + // an empty line has no tokens. +<span class="fc bfc" id="L206" title="All 2 branches covered."> if (newRecord) {</span> +<span class="fc" id="L207"> quote = true;</span> + } + } else { +<span class="fc" id="L210"> char c = value.charAt(pos);</span> + + // TODO where did this rule come from? +<span class="fc bfc" id="L213" title="All 14 branches covered."> if (newRecord && (c < '0' || (c > '9' && c < 'A') || (c > 'Z' && c < 'a') || (c > 'z'))) {</span> +<span class="fc" id="L214"> quote = true;</span> +<span class="fc bfc" id="L215" title="All 2 branches covered."> } else if (c <= COMMENT) {</span> + // Some other chars at the start of a value caused the parser to fail, so for now + // encapsulate if we start in anything less than '#'. We are being conservative + // by including the default comment char too. +<span class="fc" id="L219"> quote = true;</span> + } else { +<span class="fc bfc" id="L221" title="All 2 branches covered."> while (pos < end) {</span> +<span class="fc" id="L222"> c = value.charAt(pos);</span> +<span class="fc bfc" id="L223" title="All 8 branches covered."> if (c == LF || c == CR || c == quoteChar || c == delimChar) {</span> +<span class="fc" id="L224"> quote = true;</span> +<span class="fc" id="L225"> break;</span> + } +<span class="fc" id="L227"> pos++;</span> + } + +<span class="fc bfc" id="L230" title="All 2 branches covered."> if (!quote) {</span> +<span class="fc" id="L231"> pos = end - 1;</span> +<span class="fc" id="L232"> c = value.charAt(pos);</span> + // Some other chars at the end caused the parser to fail, so for now + // encapsulate if we end in anything less than ' ' +<span class="fc bfc" id="L235" title="All 2 branches covered."> if (c <= SP) {</span> +<span class="fc" id="L236"> quote = true;</span> + } + } + } + } + +<span class="fc bfc" id="L242" title="All 2 branches covered."> if (!quote) {</span> + // no encapsulation needed - write out the original value +<span class="fc" id="L244"> out.append(value, start, end);</span> +<span class="fc" id="L245"> return;</span> + } + break; + default: +<span class="nc" id="L249"> throw new IllegalStateException("Unexpected Quote value: " + quoteModePolicy);</span> + } + +<span class="fc bfc" id="L252" title="All 2 branches covered."> if (!quote) {</span> + // no encapsulation needed - write out the original value +<span class="fc" id="L254"> out.append(value, start, end);</span> +<span class="fc" id="L255"> return;</span> + } + + // we hit something that needed encapsulation +<span class="fc" id="L259"> out.append(quoteChar);</span> + + // Pick up where we left off: pos should be positioned on the first character that caused + // the need for encapsulation. +<span class="fc bfc" id="L263" title="All 2 branches covered."> while (pos < end) {</span> +<span class="fc" id="L264"> final char c = value.charAt(pos);</span> +<span class="fc bfc" id="L265" title="All 2 branches covered."> if (c == quoteChar) {</span> + // write out the chunk up until this point + + // add 1 to the length to write out the encapsulator also +<span class="fc" id="L269"> out.append(value, start, pos + 1);</span> + // put the next starting position on the encapsulator so we will + // write it out again with the next string (effectively doubling it) +<span class="fc" id="L272"> start = pos;</span> + } +<span class="fc" id="L274"> pos++;</span> +<span class="fc" id="L275"> }</span> + + // write the last segment +<span class="fc" id="L278"> out.append(value, start, pos);</span> +<span class="fc" id="L279"> out.append(quoteChar);</span> +<span class="fc" id="L280"> }</span> + + /** + * Prints a comment on a new line among the delimiter separated values. + * + * <p> + * Comments will always begin on a new line and occupy a least one full line. The character specified to start + * comments and a space will be inserted at the beginning of each new line in the comment. + * </p> + * + * If comments are disabled in the current CSV format this method does nothing. + * + * @param comment + * the comment to output + * @throws IOException + * If an I/O error occurs + */ + public void printComment(final String comment) throws IOException { +<span class="fc bfc" id="L298" title="All 2 branches covered."> if (!format.isCommentMarkerSet()) {</span> +<span class="fc" id="L299"> return;</span> + } +<span class="pc bpc" id="L301" title="1 of 2 branches missed."> if (!newRecord) {</span> +<span class="nc" id="L302"> println();</span> + } +<span class="fc" id="L304"> out.append(format.getCommentMarker().charValue());</span> +<span class="fc" id="L305"> out.append(SP);</span> +<span class="fc bfc" id="L306" title="All 2 branches covered."> for (int i = 0; i < comment.length(); i++) {</span> +<span class="fc" id="L307"> final char c = comment.charAt(i);</span> +<span class="pc bpc" id="L308" title="1 of 3 branches missed."> switch (c) {</span> + case CR: +<span class="nc bnc" id="L310" title="All 4 branches missed."> if (i + 1 < comment.length() && comment.charAt(i + 1) == LF) {</span> +<span class="nc" id="L311"> i++;</span> + } + //$FALL-THROUGH$ break intentionally excluded. + case LF: +<span class="fc" id="L315"> println();</span> +<span class="fc" id="L316"> out.append(format.getCommentMarker().charValue());</span> +<span class="fc" id="L317"> out.append(SP);</span> +<span class="fc" id="L318"> break;</span> + default: +<span class="fc" id="L320"> out.append(c);</span> + break; + } + } +<span class="fc" id="L324"> println();</span> +<span class="fc" id="L325"> }</span> + + /** + * Outputs the record separator. + * + * @throws IOException + * If an I/O error occurs + */ + public void println() throws IOException { +<span class="fc" id="L334"> final String recordSeparator = format.getRecordSeparator();</span> +<span class="fc bfc" id="L335" title="All 2 branches covered."> if (recordSeparator != null) {</span> +<span class="fc" id="L336"> out.append(recordSeparator);</span> + } +<span class="fc" id="L338"> newRecord = true;</span> +<span class="fc" id="L339"> }</span> + + /** + * Prints the given values a single record of delimiter separated values followed by the record separator. + * + * <p> + * The values will be quoted if needed. Quotes and newLine characters will be escaped. This method adds the record + * separator to the output after printing the record, so there is no need to call {@link #println()}. + * </p> + * + * @param values + * values to output. + * @throws IOException + * If an I/O error occurs + */ + public void printRecord(final Iterable<?> values) throws IOException { +<span class="fc bfc" id="L355" title="All 2 branches covered."> for (final Object value : values) {</span> +<span class="fc" id="L356"> print(value);</span> +<span class="fc" id="L357"> }</span> +<span class="fc" id="L358"> println();</span> +<span class="fc" id="L359"> }</span> + + /** + * Prints the given values a single record of delimiter separated values followed by the record separator. + * + * <p> + * The values will be quoted if needed. Quotes and newLine characters will be escaped. This method adds the record + * separator to the output after printing the record, so there is no need to call {@link #println()}. + * </p> + * + * @param values + * values to output. + * @throws IOException + * If an I/O error occurs + */ + public void printRecord(final Object... values) throws IOException { +<span class="fc bfc" id="L375" title="All 2 branches covered."> for (final Object value : values) {</span> +<span class="fc" id="L376"> print(value);</span> + } +<span class="fc" id="L378"> println();</span> +<span class="fc" id="L379"> }</span> + + /** + * Prints all the objects in the given collection handling nested collections/arrays as records. + * + * <p>If the given collection only contains simple objects, this method will print a single record like + * {@link #printRecord(Iterable)}. If the given collections contains nested collections/arrays those nested elements + * will each be printed as records using {@link #printRecord(Object...)}.</p> + * + * <p>Given the following data structure:</p> + * <pre> + * <code> + * List&lt;String[]&gt; data = ... + * data.add(new String[]{ "A", "B", "C" }); + * data.add(new String[]{ "1", "2", "3" }); + * data.add(new String[]{ "A1", "B2", "C3" }); + * </code> + * </pre> + * + * <p>Calling this method will print:</p> + * <pre> + * <code> + * A, B, C + * 1, 2, 3 + * A1, B2, C3 + * </code> + * </pre> + * + * @param values + * the values to print. + * @throws IOException + * If an I/O error occurs + */ + public void printRecords(final Iterable<?> values) throws IOException { +<span class="fc bfc" id="L413" title="All 2 branches covered."> for (final Object value : values) {</span> +<span class="fc bfc" id="L414" title="All 2 branches covered."> if (value instanceof Object[]) {</span> +<span class="fc" id="L415"> this.printRecord((Object[]) value);</span> +<span class="pc bpc" id="L416" title="1 of 2 branches missed."> } else if (value instanceof Iterable) {</span> +<span class="fc" id="L417"> this.printRecord((Iterable<?>) value);</span> + } else { +<span class="nc" id="L419"> this.printRecord(value);</span> + } +<span class="fc" id="L421"> }</span> +<span class="fc" id="L422"> }</span> + + /** + * Prints all the objects in the given array handling nested collections/arrays as records. + * + * <p>If the given array only contains simple objects, this method will print a single record like + * {@link #printRecord(Object...)}. If the given collections contains nested collections/arrays those nested + * elements will each be printed as records using {@link #printRecord(Object...)}.</p> + * + * <p>Given the following data structure:</p> + * <pre> + * <code> + * String[][] data = new String[3][] + * data[0] = String[]{ "A", "B", "C" }; + * data[1] = new String[]{ "1", "2", "3" }; + * data[2] = new String[]{ "A1", "B2", "C3" }; + * </code> + * </pre> + * + * <p>Calling this method will print:</p> + * <pre> + * <code> + * A, B, C + * 1, 2, 3 + * A1, B2, C3 + * </code> + * </pre> + * + * @param values + * the values to print. + * @throws IOException + * If an I/O error occurs + */ + public void printRecords(final Object... values) throws IOException { +<span class="fc bfc" id="L456" title="All 2 branches covered."> for (final Object value : values) {</span> +<span class="fc bfc" id="L457" title="All 2 branches covered."> if (value instanceof Object[]) {</span> +<span class="fc" id="L458"> this.printRecord((Object[]) value);</span> +<span class="pc bpc" id="L459" title="1 of 2 branches missed."> } else if (value instanceof Iterable) {</span> +<span class="fc" id="L460"> this.printRecord((Iterable<?>) value);</span> + } else { +<span class="nc" id="L462"> this.printRecord(value);</span> + } + } +<span class="fc" id="L465"> }</span> + + /** + * Prints all the objects in the given JDBC result set. + * + * @param resultSet result set + * the values to print. + * @throws IOException + * If an I/O error occurs + * @throws SQLException if a database access error occurs + */ + public void printRecords(final ResultSet resultSet) throws SQLException, IOException { +<span class="fc" id="L477"> final int columnCount = resultSet.getMetaData().getColumnCount();</span> +<span class="fc bfc" id="L478" title="All 2 branches covered."> while (resultSet.next()) {</span> +<span class="fc bfc" id="L479" title="All 2 branches covered."> for (int i = 1; i <= columnCount; i++) {</span> +<span class="fc" id="L480"> print(resultSet.getString(i));</span> + } +<span class="fc" id="L482"> println();</span> + } +<span class="fc" id="L484"> }</span> + + /** + * Gets the target Appendable. + * + * @return the target Appendable. + */ + public Appendable getOut() { +<span class="fc" id="L492"> return this.out;</span> + } +} +</pre><div class="footer"><span class="right">Created with <a href="http://www.eclemma.org/jacoco">JaCoCo</a> 0.7.1.201405082137</span></div></body></html> \ No newline at end of file Propchange: websites/production/commons/content/proper/commons-csv/jacoco/org.apache.commons.csv/CSVPrinter.java.html ------------------------------------------------------------------------------ svn:eol-style = native