Permalink
Browse files

Applied CSVFormat to CSVWriter too.

  • Loading branch information...
1 parent 23a0a38 commit df998b8f8e75e2feb9fe6c77d177a40e07934305 @tototoshi committed May 5, 2013
@@ -20,14 +20,15 @@ trait CSVFormat {
val separator: Char
- val quote: Char
+ val quoteChar: Char
- val escape: Char
+ val escapeChar: Char
val numberOfLinesToSkip: Int
val strictQuotes: Boolean
val ignoreLeadingWhiteSpace: Boolean
+ val lineEnd: String
}
@@ -24,7 +24,7 @@ import au.com.bytecode.opencsv
class CSVReader protected (private val underlying: JCSVReader) {
- @deprecated("0.8.0")
+ @deprecated("No longer supported", "0.8.0")
def apply[A](f: Iterator[Seq[String]] => A): A = {
try {
f(this.iterator)
@@ -83,13 +83,13 @@ object CSVReader {
val DEFAULT_ENCODING = "UTF-8"
@deprecated("Use #open instead", "0.5.0")
- def apply(file: File, encoding: String = "UTF-8"): CSVReader = open(file, encoding)
+ def apply(file: File, encoding: String = "UTF-8"): CSVReader = open(file, encoding)(defaultCSVFormat)
@deprecated("Use #open instead", "0.5.0")
def apply(reader: Reader): CSVReader = open(reader)(defaultCSVFormat)
def open(reader: Reader)(implicit format: CSVFormat): CSVReader =
- new CSVReader(new JCSVReader(reader, format.separator, format.quote, format.numberOfLinesToSkip))
+ new CSVReader(new JCSVReader(reader, format.separator, format.quoteChar, format.numberOfLinesToSkip))
def open(file: File)(implicit format: CSVFormat): CSVReader = {
open(file, this.DEFAULT_ENCODING)(format)
@@ -20,10 +20,9 @@ import scala.collection.JavaConversions._
import au.com.bytecode.opencsv.{ CSVWriter => JCSVWriter }
import java.io.{ File, Writer, FileOutputStream, OutputStreamWriter }
-class CSVWriter protected (writer: Writer) {
-
- private val underlying: JCSVWriter = new JCSVWriter(writer)
+class CSVWriter protected (protected val underlying: JCSVWriter) {
+ @deprecated("No longer supported","0.8.0")
def apply[A](f: CSVWriter => A): A = {
try {
f(this)
@@ -54,30 +53,36 @@ class CSVWriter protected (writer: Writer) {
object CSVWriter {
@deprecated("Use #open instead", "0.5.0")
- def apply(file: File, encoding: String = "UTF-8"): CSVWriter = open(file, false, encoding)
+ def apply(file: File, encoding: String = "UTF-8")(implicit format: CSVFormat): CSVWriter =
+ open(file, false, encoding)(defaultCSVFormat)
@deprecated("Use #open instead", "0.5.0")
- def apply(writer: Writer): CSVWriter = open(writer)
+ def apply(writer: Writer): CSVWriter = open(writer)(defaultCSVFormat)
+
+ def open(writer: Writer)(implicit format: CSVFormat): CSVWriter = {
+ val jcsvWriter = new JCSVWriter(writer, format.separator, format.quoteChar, format.escapeChar, format.lineEnd)
+ new CSVWriter(jcsvWriter)
+ }
- def open(writer: Writer): CSVWriter = new CSVWriter(writer)
- def open(file: File): CSVWriter = open(file, false, "UTF-8")
+ def open(file: File)(implicit format: CSVFormat): CSVWriter = open(file, false, "UTF-8")(format)
- def open(file: File, encoding: String): CSVWriter = open(file, false, encoding)
+ def open(file: File, encoding: String)(implicit format: CSVFormat): CSVWriter = open(file, false, encoding)(format)
- def open(file: File, append: Boolean): CSVWriter = open(file, append, "UTF-8")
+ def open(file: File, append: Boolean)(implicit format: CSVFormat): CSVWriter = open(file, append, "UTF-8")(format)
- def open(file: File, append: Boolean, encoding: String): CSVWriter = {
+ def open(file: File, append: Boolean, encoding: String)(implicit format: CSVFormat): CSVWriter = {
val fos = new FileOutputStream(file, append)
val writer = new OutputStreamWriter(fos, encoding)
- open(writer)
+ open(writer)(format)
}
- def open(file: String): CSVWriter = open(file, false, "UTF-8")
+ def open(file: String)(implicit format: CSVFormat): CSVWriter = open(file, false, "UTF-8")(format)
- def open(file: String, encoding: String): CSVWriter = open(file, false, encoding)
+ def open(file: String, encoding: String)(implicit format: CSVFormat): CSVWriter = open(file, false, encoding)(format)
- def open(file: String, append: Boolean): CSVWriter = open(file, append, "UTF-8")
+ def open(file: String, append: Boolean)(implicit format: CSVFormat): CSVWriter = open(file, append, "UTF-8")(format)
- def open(file: String, append: Boolean, encoding: String): CSVWriter = open(new File(file), append, encoding)
+ def open(file: String, append: Boolean, encoding: String)(implicit format: CSVFormat): CSVWriter =
+ open(new File(file), append, encoding)(format)
}
@@ -15,20 +15,22 @@
*/
package com.github.tototoshi.csv
-import au.com.bytecode.opencsv.{ CSVParser, CSVReader => JCSVReader }
+import au.com.bytecode.opencsv.{ CSVParser, CSVReader => JCSVReader, CSVWriter => JCSVWriter }
trait DefaultCSVFormat extends CSVFormat {
val separator: Char = CSVParser.DEFAULT_SEPARATOR
- val quote: Char = CSVParser.DEFAULT_QUOTE_CHARACTER
+ val quoteChar: Char = CSVParser.DEFAULT_QUOTE_CHARACTER
- val escape: Char = CSVParser.DEFAULT_ESCAPE_CHARACTER
+ val escapeChar: Char = CSVParser.DEFAULT_ESCAPE_CHARACTER
val numberOfLinesToSkip: Int = JCSVReader.DEFAULT_SKIP_LINES
val strictQuotes: Boolean = CSVParser.DEFAULT_STRICT_QUOTES
val ignoreLeadingWhiteSpace: Boolean = CSVParser.DEFAULT_IGNORE_LEADING_WHITESPACE
+ val lineEnd: String = JCSVWriter.DEFAULT_LINE_END
+
}
@@ -63,7 +63,7 @@ class CSVReaderSpec extends FunSpec with ShouldMatchers with Using {
it("should be consutrcted with separators and quotes") {
implicit object format extends DefaultCSVFormat {
override val separator: Char = '#'
- override val quote: Char = '$'
+ override val quoteChar: Char = '$'
}
using (CSVReader.open("src/test/resources/hash-separated-dollar-quote.csv")) { reader => {
@@ -76,7 +76,7 @@ class CSVReaderSpec extends FunSpec with ShouldMatchers with Using {
it("should be constructed with separators, quotes, and line skipping") {
implicit object format extends DefaultCSVFormat {
override val separator: Char = '#'
- override val quote: Char = '$'
+ override val quoteChar: Char = '$'
override val numberOfLinesToSkip: Int = 3
}
using (CSVReader.open("src/test/resources/beginning-junk-hash-separated-dollar-quote.csv")) { reader => {
@@ -22,13 +22,6 @@ class CSVWriterSpec extends FunSpec with ShouldMatchers with BeforeAndAfter with
describe("CSVWriter") {
describe ("#apply") {
- it ("should provide loan pattern") {
- CSVWriter.open("test.csv") { writer =>
- writer.writeRow(List(1, 2, 3))
- writer.writeRow(List(4, 5, 6))
- }
- readFileAsString("test.csv") should be ("\"1\",\"2\",\"3\"\n\"4\",\"5\",\"6\"\n")
- }
it ("should close csv writer") {
val writer = CSVWriter.open("test.csv")
writer(w => ())

0 comments on commit df998b8

Please sign in to comment.