Permalink
Browse files

Added CSVFormat.

CSVFormat is implicitly used from CSVReader#open.
It is like scala.io.Codec.
  • Loading branch information...
tototoshi committed May 5, 2013
1 parent 747dd5e commit 23a0a38891d393646d6c2cce5d18bc1f45527461
@@ -0,0 +1,33 @@
+/*
+* Copyright 2013 Toshiyuki Takahashi
+*
+* Licensed 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 com.github.tototoshi.csv
+
+
+trait CSVFormat {
+
+ val separator: Char
+
+ val quote: Char
+
+ val escape: Char
+
+ val numberOfLinesToSkip: Int
+
+ val strictQuotes: Boolean
+
+ val ignoreLeadingWhiteSpace: Boolean
+
+}
@@ -21,10 +21,10 @@ import java.io._
import scala.collection.JavaConversions._
import java.util.NoSuchElementException
import au.com.bytecode.opencsv
-import opencsv.CSVParser
class CSVReader protected (private val underlying: JCSVReader) {
+ @deprecated("0.8.0")
def apply[A](f: Iterator[Seq[String]] => A): A = {
try {
f(this.iterator)
@@ -80,35 +80,31 @@ class CSVReader protected (private val underlying: JCSVReader) {
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)
@deprecated("Use #open instead", "0.5.0")
- def apply(reader: Reader): CSVReader = openFromReader(reader)
+ def apply(reader: Reader): CSVReader = open(reader)(defaultCSVFormat)
- def openFromReader(reader: Reader, separator: Char = CSVParser.DEFAULT_SEPARATOR, quote: Char = CSVParser.DEFAULT_QUOTE_CHARACTER, numberOfLinesToSkip: Int = JCSVReader.DEFAULT_SKIP_LINES): CSVReader =
- new CSVReader(new JCSVReader(reader, separator, quote, numberOfLinesToSkip))
+ def open(reader: Reader)(implicit format: CSVFormat): CSVReader =
+ new CSVReader(new JCSVReader(reader, format.separator, format.quote, format.numberOfLinesToSkip))
- def openFromFile(file: File, encoding: String = "UTF-8", separator: Char = CSVParser.DEFAULT_SEPARATOR, quote: Char = CSVParser.DEFAULT_QUOTE_CHARACTER, numberOfLinesToSkip: Int = JCSVReader.DEFAULT_SKIP_LINES): CSVReader = {
- val fin = new FileInputStream(file)
- val reader = new InputStreamReader(fin, encoding)
- openFromReader(reader, separator, quote, numberOfLinesToSkip)
+ def open(file: File)(implicit format: CSVFormat): CSVReader = {
+ open(file, this.DEFAULT_ENCODING)(format)
}
- def openFromPath(filename: String, encoding: String = "UTF-8", separator: Char = CSVParser.DEFAULT_SEPARATOR, quote: Char = CSVParser.DEFAULT_QUOTE_CHARACTER, numberOfLinesToSkip: Int = JCSVReader.DEFAULT_SKIP_LINES) : CSVReader =
- openFromFile(new File(filename), encoding, separator, quote, numberOfLinesToSkip)
-
- def open(reader: Reader): CSVReader = openFromReader(reader)
-
- def open(file: File): CSVReader = open(file, "UTF-8")
-
- def open(file: File, encoding: String): CSVReader = {
+ def open(file: File, encoding: String)(implicit format: CSVFormat): CSVReader = {
val fin = new FileInputStream(file)
val reader = new InputStreamReader(fin, encoding)
- open(reader)
+ open(reader)(format)
}
- def open(file: String): CSVReader = open(new File(file), "UTF-8")
+ def open(filename: String)(implicit format: CSVFormat) : CSVReader =
+ open(new File(filename), this.DEFAULT_ENCODING)(format)
+
+ def open(filename: String, encoding: String)(implicit format: CSVFormat) : CSVReader =
+ open(new File(filename), encoding)(format)
- def open(file: String, encoding: String): CSVReader = open(new File(file), encoding)
}
@@ -0,0 +1,34 @@
+/*
+* Copyright 2013 Toshiyuki Takahashi
+*
+* Licensed 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 com.github.tototoshi.csv
+
+import au.com.bytecode.opencsv.{ CSVParser, CSVReader => JCSVReader }
+
+trait DefaultCSVFormat extends CSVFormat {
+
+ val separator: Char = CSVParser.DEFAULT_SEPARATOR
+
+ val quote: Char = CSVParser.DEFAULT_QUOTE_CHARACTER
+
+ val escape: 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
+
+}
@@ -17,4 +17,7 @@
package com.github.tototoshi
package object csv {
+
+ implicit val defaultCSVFormat = new DefaultCSVFormat {}
+
}
@@ -4,21 +4,17 @@ import java.io.{ FileReader, File }
import org.scalatest.FunSpec
import org.scalatest.matchers._
+import javax.swing.colorchooser.DefaultColorSelectionModel
class CSVReaderSpec extends FunSpec with ShouldMatchers with Using {
+
def fixture = new {
}
describe("CSVReader") {
describe ("#apply") {
- it ("should provide the syntax of loan pattern") {
- val content = CSVReader.open("src/test/resources/simple.csv") { lines =>
- lines.map(_.mkString).mkString
- }
- content should be ("abcdef")
- }
it ("should close csv reader") {
val reader = CSVReader.open("src/test/resources/simple.csv")
reader.apply { lines => () }
@@ -51,7 +47,11 @@ class CSVReaderSpec extends FunSpec with ShouldMatchers with Using {
it("should be constructed with separators") {
var res: List[String] = Nil
- using (CSVReader.openFromPath("src/test/resources/hash-separated.csv", separator = '#')) { reader =>
+ implicit object format extends DefaultCSVFormat {
+ override val separator: Char = '#'
+ }
+
+ using (CSVReader.open("src/test/resources/hash-separated.csv")) { reader =>
reader foreach { fields =>
res = res ++ fields
}
@@ -61,15 +61,25 @@ class CSVReaderSpec extends FunSpec with ShouldMatchers with Using {
}
it("should be consutrcted with separators and quotes") {
- using (CSVReader.openFromPath("src/test/resources/hash-separated-dollar-quote.csv", separator = '#', quote = '$')) { reader => {
+ implicit object format extends DefaultCSVFormat {
+ override val separator: Char = '#'
+ override val quote: Char = '$'
+ }
+
+ using (CSVReader.open("src/test/resources/hash-separated-dollar-quote.csv")) { reader => {
val map = reader.allWithHeaders()
map(0)("Foo ") should be ("a")
}
}
}
it("should be constructed with separators, quotes, and line skipping") {
- using (CSVReader.openFromPath("src/test/resources/beginning-junk-hash-separated-dollar-quote.csv", separator = '#', quote = '$', numberOfLinesToSkip = 3)) { reader => {
+ implicit object format extends DefaultCSVFormat {
+ override val separator: Char = '#'
+ override val quote: Char = '$'
+ override val numberOfLinesToSkip: Int = 3
+ }
+ using (CSVReader.open("src/test/resources/beginning-junk-hash-separated-dollar-quote.csv")) { reader => {
val map = reader.allWithHeaders()
map(0)("Foo ") should be ("a")
}

0 comments on commit 23a0a38

Please sign in to comment.