Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
Checking mergeability… Don't worry, you can still create the pull request.
  • 3 commits
  • 8 files changed
  • 0 commit comments
  • 1 contributor
Commits on May 05, 2013
@tototoshi Added CSVFormat.
CSVFormat is implicitly used from CSVReader#open.
It is like scala.io.Codec.
23a0a38
@tototoshi Applied CSVFormat to CSVWriter too.
15e1543
@tototoshi Deprecate loan pattern support 94003fd
View
21 README.md
@@ -124,16 +124,6 @@ scala> reader.allWithHeaders()
res0: List[Map[String,String]] = List(Map(Foo -> a, Bar -> b, Baz -> c), Map(Foo -> d, Bar -> e, Baz -> f))
````
-#### Using loan pattern
-
-```scala
-scala> CSVReader.open("src/test/resources/simple.csv") { lines =>
- | lines.foreach(println)
- | }
-List(a, b, c)
-List(d, e, f)
-```
-
### Writing example
#### import
@@ -184,17 +174,6 @@ scala> writer.writeRow(List("4", "5", "6"))
scala> writer.close()
```
-#### Using loan pattern
-
-```scala
-scala> CSVWriter.open("a.csv") { writer =>
- | writer.writeRow(List(1, 2, 3))
- | }
-
-scala> io.Source.fromFile("a.csv").mkString
-res0: String =
-""1","2","3"
-```
## License
[Apache 2.0](http://www.apache.org/licenses/LICENSE-2.0)
View
34 src/main/scala/com/github/tototoshi/csv/CSVFormat.scala
@@ -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
+
+
+trait CSVFormat {
+
+ val separator: Char
+
+ val quoteChar: Char
+
+ val escapeChar: Char
+
+ val numberOfLinesToSkip: Int
+
+ val strictQuotes: Boolean
+
+ val ignoreLeadingWhiteSpace: Boolean
+
+ val lineEnd: String
+}
View
36 src/main/scala/com/github/tototoshi/csv/CSVReader.scala
@@ -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("No longer supported", "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)
+ 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 = 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.quoteChar, 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)
}
View
28 src/main/scala/com/github/tototoshi/csv/CSVWriter.scala
@@ -24,6 +24,7 @@ class CSVWriter protected (writer: Writer) {
private val underlying: JCSVWriter = new JCSVWriter(writer)
+ @deprecated("No longer supported","0.8.0")
def apply[A](f: CSVWriter => A): A = {
try {
f(this)
@@ -54,30 +55,33 @@ 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): CSVWriter = new CSVWriter(writer)
+ def open(writer: Writer)(implicit format: CSVFormat): 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)
}
View
36 src/main/scala/com/github/tototoshi/csv/DefaultCSVFormat.scala
@@ -0,0 +1,36 @@
+/*
+* 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, CSVWriter => JCSVWriter }
+
+trait DefaultCSVFormat extends CSVFormat {
+
+ val separator: Char = CSVParser.DEFAULT_SEPARATOR
+
+ val quoteChar: Char = CSVParser.DEFAULT_QUOTE_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
+
+}
View
3  src/main/scala/com/github/tototoshi/csv/package.scala
@@ -17,4 +17,7 @@
package com.github.tototoshi
package object csv {
+
+ implicit val defaultCSVFormat = new DefaultCSVFormat {}
+
}
View
28 src/test/scala/com/github/tototoshi/csv/CSVReaderSpec.scala
@@ -4,8 +4,10 @@ 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 {
}
@@ -13,12 +15,6 @@ class CSVReaderSpec extends FunSpec with ShouldMatchers with Using {
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,7 +61,12 @@ 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 quoteChar: Char = '$'
+ }
+
+ using (CSVReader.open("src/test/resources/hash-separated-dollar-quote.csv")) { reader => {
val map = reader.allWithHeaders()
map(0)("Foo ") should be ("a")
}
@@ -69,7 +74,12 @@ class CSVReaderSpec extends FunSpec with ShouldMatchers with Using {
}
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 quoteChar: 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")
}
View
7 src/test/scala/com/github/tototoshi/csv/CSVWriterSpec.scala
@@ -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 => ())

No commit comments for this range

Something went wrong with that request. Please try again.