Codebase list libcsv-java / 5d6ef76
Import Upstream version 2.1 Markus Koschany 7 years ago
4 changed file(s) with 80 addition(s) and 30 deletion(s). Raw diff Collapse all Expand all
77 </target>
88 <target name="compile" depends="init" description="compile the source ">
99 <!-- Compile the java code from ${src} into ${build} -->
10 <javac srcdir="${src}" destdir="${build}" />
10 <javac srcdir="${src}" destdir="${build}" encoding="ISO_8859_1" source="1.4" includeantruntime="false">
11 <exclude name="**/AllTests.java" />
12 </javac>
1113 </target>
1214 <target name="dist" depends="compile" description="generate the distribution">
1315 <jar jarfile="javacsv.jar" basedir="${build}" />
1414 access="public">
1515 <packageset dir="${src}" />
1616 <doctitle><![CDATA[<h1>JavaCSV</h1>]]></doctitle>
17 <bottom><![CDATA[<i>http://sourceforge.net/projects/javacsv/</i>]]></bottom>
17 <bottom><![CDATA[<a href="http://sourceforge.net"><img src="http://sflogo.sourceforge.net/sflogo.php?group_id=33066&amp;type=1" width="88" height="31" border="0" /></a>]]></bottom>
1818 <link href="http://java.sun.com/j2se/1.4.1/docs/api/"/>
1919 </javadoc>
2020 </target>
0 /*
1 * Java CSV is a stream based library for reading and writing
2 * CSV and other delimited data.
3 *
4 * Copyright (C) Bruce Dunwiddie bruce@csvreader.com
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 */
020 import java.io.BufferedReader;
121 import java.io.ByteArrayInputStream;
222 import java.io.ByteArrayOutputStream;
2747
2848 public class AllTests {
2949 public static void main(String[] args) throws Exception {
30 Class testClass = AllTests.class;
50 Class<AllTests> testClass = AllTests.class;
3151 ArrayList<Method> setups = new ArrayList<Method>();
3252 ArrayList<Method> tearDowns = new ArrayList<Method>();
3353
6282 setup.invoke(instance, (Object[]) null);
6383 }
6484
65 Class expectedException = testAnnotation.expected();
85 Class<?> expectedException = testAnnotation.expected();
6686
6787 // can't for the life of me get eclipse to be able to
6888 // resolve Test.None directly
651671 new OutputStreamWriter(new FileOutputStream("temp.csv"),
652672 Charset.forName("UTF-8"))), ',');
653673 // writer will trim all whitespace and put this in quotes to preserve
654 // it's existance
674 // it's existence
655675 writer.write(" \t \t");
656676 writer.close();
657677
14231443 @Test
14241444 public void test76() throws Exception {
14251445 CsvReader reader = CsvReader.parse("user_id,name\r\n1,Bruce");
1426 Assert.assertEquals(null, reader.getHeaders());
1446 Assert.assertNull(reader.getHeaders());
14271447 Assert.assertEquals(-1, reader.getIndex("user_id"));
14281448 Assert.assertEquals("", reader.getHeader(0));
14291449 Assert.assertTrue(reader.readHeaders());
14341454 Assert.assertEquals("user_id", headers[0]);
14351455 Assert.assertEquals("name", headers[1]);
14361456 reader.setHeaders(null);
1437 Assert.assertEquals(null, reader.getHeaders());
1457 Assert.assertNull(reader.getHeaders());
14381458 Assert.assertEquals(-1, reader.getIndex("user_id"));
14391459 Assert.assertEquals("", reader.getHeader(0));
14401460 reader.close();
15751595 @Test
15761596 public void test88() throws Exception {
15771597 try {
1578 CsvReader reader = new CsvReader((String) null, ',', Charset
1598 new CsvReader((String) null, ',', Charset
15791599 .forName("ISO-8859-1"));
15801600 } catch (Exception ex) {
15811601 assertException(new IllegalArgumentException(
15861606 @Test
15871607 public void test89() throws Exception {
15881608 try {
1589 CsvReader reader = new CsvReader("temp.csv", ',', null);
1609 new CsvReader("temp.csv", ',', null);
15901610 } catch (Exception ex) {
15911611 assertException(new IllegalArgumentException(
15921612 "Parameter charset can not be null."), ex);
15961616 @Test
15971617 public void test90() throws Exception {
15981618 try {
1599 CsvReader reader = new CsvReader((Reader) null, ',');
1619 new CsvReader((Reader) null, ',');
16001620 } catch (Exception ex) {
16011621 assertException(new IllegalArgumentException(
16021622 "Parameter inputStream can not be null."), ex);
16481668 @Test
16491669 public void test112() throws Exception {
16501670 try {
1651 CsvWriter writer = new CsvWriter((String) null, ',', Charset
1671 new CsvWriter((String) null, ',', Charset
16521672 .forName("ISO-8859-1"));
16531673 } catch (Exception ex) {
16541674 assertException(new IllegalArgumentException("Parameter fileName can not be null."), ex);
16581678 @Test
16591679 public void test113() throws Exception {
16601680 try {
1661 CsvWriter writer = new CsvWriter("test.csv", ',', (Charset) null);
1681 new CsvWriter("test.csv", ',', (Charset) null);
16621682 } catch (Exception ex) {
16631683 assertException(new IllegalArgumentException("Parameter charset can not be null."), ex);
16641684 }
16671687 @Test
16681688 public void test114() throws Exception {
16691689 try {
1670 CsvWriter writer = new CsvWriter((Writer) null, ',');
1690 new CsvWriter((Writer) null, ',');
16711691 } catch (Exception ex) {
16721692 assertException(new IllegalArgumentException("Parameter outputStream can not be null."), ex);
16731693 }
21582178 public void test149() throws Exception {
21592179 try
21602180 {
2161 CsvReader reader = new CsvReader("C:\\somefilethatdoesntexist.csv");
2181 new CsvReader("C:\\somefilethatdoesntexist.csv");
21622182 }
21632183 catch (Exception ex)
21642184 {
21792199 reader.readRecord();
21802200 } catch (IOException ex) {
21812201 // make sure stream that caused exception
2182 // has been sent a dipose call
2202 // has been sent a dispose call
21832203 Assert.assertTrue(fail.DisposeCalled);
21842204 exceptionThrown = true;
21852205 Assert.assertEquals("Read failed.", ex.getMessage());
22162236 DisposeCalled = true;
22172237 }
22182238 }
2239
2240 @Test
2241 public void Test174() throws IOException {
2242 // verifies that data is eventually automatically flushed
2243 CsvWriter writer = new CsvWriter("temp.csv");
2244
2245 for (int i = 0; i < 10000; i++)
2246 {
2247 writer.write("stuff");
2248 writer.endRecord();
2249 }
2250
2251 CsvReader reader = new CsvReader("temp.csv");
2252
2253 Assert.assertTrue(reader.readRecord());
2254
2255 Assert.assertEquals("stuff", reader.get(0));
2256
2257 writer.close();
2258 reader.close();
2259
2260 new File("temp.csv").delete();
2261 }
22192262 }
2323 import java.io.IOException;
2424 import java.io.OutputStream;
2525 import java.io.OutputStreamWriter;
26 import java.io.PrintWriter;
26 import java.io.BufferedWriter;
2727 import java.io.Writer;
2828 import java.nio.charset.Charset;
2929
3131 * A stream based writer for writing delimited text data to a file or a stream.
3232 */
3333 public class CsvWriter {
34 private PrintWriter outputStream = null;
35
34 private Writer outputStream = null;
35
3636 private String fileName = null;
3737
3838 private boolean firstColumn = true;
4747 private boolean initialized = false;
4848
4949 private boolean closed = false;
50
51 /**
52 * Double up the text qualifier to represent an occurance of the text
50
51 private String systemRecordDelimiter = System.getProperty("line.separator");
52
53 /**
54 * Double up the text qualifier to represent an occurrence of the text
5355 * qualifier.
5456 */
5557 public static final int ESCAPE_MODE_DOUBLED = 1;
5658
5759 /**
5860 * Use a backslash character before the text qualifier to represent an
59 * occurance of the text qualifier.
61 * occurrence of the text qualifier.
6062 */
6163 public static final int ESCAPE_MODE_BACKSLASH = 2;
6264
112114 throw new IllegalArgumentException("Parameter outputStream can not be null.");
113115 }
114116
115 this.outputStream = new PrintWriter(outputStream);
117 this.outputStream = outputStream;
116118 userSettings.Delimiter = delimiter;
117119 initialized = true;
118120 }
386388 if (useCustomRecordDelimiter) {
387389 outputStream.write(userSettings.RecordDelimiter);
388390 } else {
389 outputStream.println();
390 }
391
391 outputStream.write(systemRecordDelimiter);
392 }
393
392394 firstColumn = true;
393395 }
394396
446448 if (useCustomRecordDelimiter) {
447449 outputStream.write(userSettings.RecordDelimiter);
448450 } else {
449 outputStream.println();
451 outputStream.write(systemRecordDelimiter);
450452 }
451453
452454 firstColumn = true;
458460 private void checkInit() throws IOException {
459461 if (!initialized) {
460462 if (fileName != null) {
461 outputStream = new PrintWriter(new OutputStreamWriter(
463 outputStream = new BufferedWriter(new OutputStreamWriter(
462464 new FileOutputStream(fileName), charset));
463465 }
464466
469471 /**
470472 * Clears all buffers for the current writer and causes any buffered data to
471473 * be written to the underlying device.
472 */
473 public void flush() {
474 * @exception IOException
475 * Thrown if an error occurs while writing data to the
476 * destination stream.
477 */
478 public void flush() throws IOException {
474479 outputStream.flush();
475480 }
476481