Skip to content

Commit 535fbb9

Browse files
committed
initial commit
0 parents  commit 535fbb9

29 files changed

+1243
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
target
2+
bin
3+
*~
4+
.settings

LICENSE

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Copyright (c) 2011, Element d.o.o.
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions are met:
6+
7+
1. Redistributions of source code must retain the above copyright notice,
8+
this list of conditions and the following disclaimer.
9+
2. Redistributions in binary form must reproduce the above copyright notice,
10+
this list of conditions and the following disclaimer in the documentation
11+
and/or other materials provided with the distribution.
12+
3. Neither the name of the Element d.o.o. nor the names of its contributors
13+
may be used to endorse or promote products derived from this software
14+
without specific prior written permission.
15+
16+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20+
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
POSSIBILITY OF SUCH DAMAGE.

README

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
PostgreSQL JDBC wrapper for Scala + Java utilities.

csvs/bla.cvs

251 Bytes
Binary file not shown.

csvs/test1.csv

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
"""";";;";""""sss
2+
2;22;";;;""";sss
3+
;;;""""sss
4+
""""

csvs/test2.csv

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
1;2
2+
123;"3;"""
3+
2en;dva;"tri\n"
4+
3en;dva;"Tri;;;"";;"
5+
4enfsdddddddddddddddddddddddddddddddddddddddddddddfdsfsfdsffffffffffffffffffffffffffffffffffffffffffddddddddddddddddddddddddddd;dva;tri;""""""""""""""
6+
7+
;2;3
8+
2;

csvs/test3.csv

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"""""1;2;3""";"""""";fdafdsa
2+
"4;5;6";""""";
3+
"""";;;";";jfdskajfd "
4+
"1;2;3";"""""";fdasfdsa
5+
"1;2;3";"""""";fdafdsa
6+
"4;5;6";""""";
7+
"""";;;";";jfdskajfd "
8+
"1;2;3";"""""";fdasfdsa

csvs/test4.csv

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"""""1;2;3""";"""""";fdafdsa;
2+
"4;5;6";""""";
3+
"""";;;";";jfdskajfd ";
4+
"1;2;3";"""""";fdasfdsa ;
5+
"1;2;3";"""""";fdafdsa;
6+
"4;5;6";""""";
7+
"""";;;";";jfdskajfd ";""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
8+
"1;2;3";"""""";fdasfdsa ;

doit-csv/.classpath

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" output="target/scala-2.9.1/classes" path="src/main/scala"/>
4+
<classpathentry kind="src" path="src/main/javaa"/>
5+
<classpathentry kind="src" output="target/scala-2.9.1/test-classes" path="src/test/scala"/>
6+
<classpathentry kind="con" path="org.scala-ide.sdt.launching.SCALA_CONTAINER"/>
7+
<classpathentry kind="lib" path="/home/marin/.ivy2/cache/org.scalatest/scalatest_2.9.1/jars/scalatest_2.9.1-1.6.1.jar"/>
8+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
9+
<classpathentry kind="output" path="bin"/>
10+
</classpath>

doit-csv/.project

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<projectDescription>
2+
<name>doit-csv</name>
3+
<buildSpec>
4+
<buildCommand>
5+
<name>org.scala-ide.sdt.core.scalabuilder</name>
6+
</buildCommand>
7+
</buildSpec>
8+
<natures>
9+
<nature>org.scala-ide.sdt.core.scalanature</nature>
10+
<nature>org.eclipse.jdt.core.javanature</nature>
11+
</natures>
12+
</projectDescription>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package hr.element.doit.csvjava;
2+
3+
4+
import java.io.InputStream;
5+
import java.nio.charset.Charset;
6+
7+
public class CSVFactory {
8+
public final String delimiter;
9+
public final String newLine;
10+
public final String quotes;
11+
public final Charset encoding;
12+
13+
public static CSVFactory factory() {
14+
return new CSVFactory(";", "\n", "\"", Charset.forName("UTF-8"));
15+
}
16+
17+
private CSVFactory(final String delimiter, final String newLine,
18+
final String quotes, final Charset encoding) {
19+
this.delimiter = delimiter;
20+
this.newLine = newLine;
21+
this.quotes = quotes;
22+
this.encoding = encoding;
23+
}
24+
25+
public CSVFactory setDelimiter(final String delimiter) {
26+
return new CSVFactory(delimiter, newLine, quotes, encoding);
27+
}
28+
29+
public CSVFactory setDelimiter(final char delimiter) {
30+
return setDelimiter(String.valueOf(delimiter));
31+
}
32+
33+
public CSVFactory setNewLine(final String newLine) {
34+
return new CSVFactory(delimiter, newLine, quotes, encoding);
35+
}
36+
37+
public CSVFactory setNewLine(final char newLine) {
38+
return setNewLine(String.valueOf(newLine));
39+
}
40+
41+
public CSVFactory setEncoding(final String encoding) {
42+
return setEncoding(Charset.forName(encoding));
43+
}
44+
45+
public CSVFactory setEncoding(final Charset encoding) {
46+
return new CSVFactory(delimiter, newLine, quotes, encoding);
47+
}
48+
49+
public CSVFactory setQuotes(final String quotes) {
50+
return new CSVFactory(delimiter, newLine, quotes, encoding);
51+
}
52+
53+
public CSVFactory setQuotes(final char quotes) {
54+
return setQuotes(String.valueOf(quotes));
55+
}
56+
57+
// ----------------------------------------------------
58+
/*
59+
public CSVWriter getWriter(final OutputStream oS) {
60+
Writer w = new OutputStreamWriter(oS, encoding);
61+
return new CSVWriter(this, w);
62+
}
63+
*/
64+
// -----------------------------------------------------
65+
public CSVReader getReader(final InputStream iS) {
66+
return new CSVReader(this, iS);
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package hr.element.doit.csvjava;
2+
3+
4+
import hr.element.doit.csv.LineReader;
5+
6+
import java.io.InputStream;
7+
import java.io.InputStreamReader;
8+
import java.io.Reader;
9+
10+
import java.util.Iterator;
11+
12+
13+
14+
public class CSVReader implements Iterable<LineReader>, Iterator<LineReader> {
15+
16+
private final CSVFactory config;
17+
private final Reader reader;
18+
private LineReader currentLine;
19+
20+
CSVReader(final CSVFactory config, final InputStream iS) {
21+
this.config = config;
22+
reader = new InputStreamReader(iS, config.encoding);
23+
}
24+
25+
@Override
26+
public Iterator<LineReader> iterator() {
27+
return this;
28+
}
29+
30+
boolean hasMoreLines = true;
31+
32+
@Override
33+
public boolean hasNext() {
34+
if (!hasMoreLines) {
35+
return false;
36+
}
37+
38+
if (currentLine == null) {
39+
currentLine = new LineReader(config, reader);
40+
hasMoreLines = currentLine != null;
41+
}
42+
43+
return hasMoreLines;
44+
}
45+
46+
@Override
47+
public LineReader next(){
48+
if (!hasNext()) {
49+
return null;//throw new IOException("Called next on empty iterator!");
50+
}
51+
52+
final LineReader cL = currentLine;
53+
currentLine = null;
54+
return cL;
55+
}
56+
57+
@Override
58+
public void remove() {
59+
throw new UnsupportedOperationException("Could not remove element!");
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package hr.element.doit.csv;
2+
3+
import java.io.IOException;
4+
import java.io.Writer;
5+
import java.util.List;
6+
7+
public class CSVWriter {
8+
public final CSVFactory config;
9+
public final Writer writer;
10+
11+
CSVWriter(final CSVFactory config, final Writer w) {
12+
this.writer = w;
13+
this.config = config;
14+
}
15+
public CSVWriter write(String[] line) throws IOException{
16+
int i = 0;
17+
while(true){
18+
final String van = getStringToWrite(line[i]);
19+
writer.write(van);
20+
21+
if(++i == line.length) break;
22+
writer.write(config.delimiter);
23+
}
24+
writer.write(config.newLine);
25+
return this;
26+
}
27+
private String getStringToWrite(final String value) {
28+
29+
int quoteCount = 0; {
30+
final boolean hasNewLine = -1 != value.indexOf(config.newLine);
31+
final boolean hasDelimiter = -1 != value.indexOf(config.delimiter);
32+
int valuePivot = -1;
33+
while(true){
34+
valuePivot = value.indexOf(config.quotes, valuePivot + 1);
35+
if(valuePivot == -1) break ;
36+
quoteCount++;
37+
}
38+
39+
if ( !hasNewLine && !hasDelimiter && (quoteCount == 0))
40+
return value;
41+
}
42+
final int qouteLength = config.quotes.length();
43+
final int newValueLength = value.length() + (quoteCount + 2) * qouteLength;
44+
45+
final char[] newValue = new char[newValueLength];
46+
final char[] oldValue = value.toCharArray();
47+
final char[] qoutesArray = config.quotes.toCharArray();
48+
49+
System.arraycopy(qoutesArray, 0, newValue, 0, qouteLength);
50+
System.arraycopy(qoutesArray, 0, newValue, newValueLength - qouteLength, qouteLength);
51+
52+
int newValuePivot = qouteLength;
53+
int oldValueTailPivot = -1;
54+
55+
while(true) {
56+
final int oldValueHeadPivot = oldValueTailPivot < 0 ? 0 :oldValueTailPivot;
57+
58+
oldValueTailPivot = value.indexOf(config.quotes, oldValueTailPivot+1);
59+
60+
if (oldValueTailPivot == -1) {
61+
System.arraycopy(oldValue, oldValueHeadPivot, newValue, newValuePivot, oldValue.length - oldValueHeadPivot);
62+
break;
63+
}
64+
65+
final int copyLength = oldValueTailPivot - oldValueHeadPivot;
66+
System.arraycopy(oldValue, oldValueHeadPivot, newValue, newValuePivot, copyLength);
67+
newValuePivot += copyLength;
68+
69+
System.arraycopy(qoutesArray, 0, newValue, newValuePivot, qouteLength);
70+
newValuePivot += qouteLength;
71+
}
72+
return new String(newValue);
73+
}
74+
75+
public CSVWriter write(String[][] lines) throws IOException{
76+
for( String[] line : lines){
77+
write(line);
78+
}
79+
return this;
80+
}
81+
public CSVWriter write(List<String[]> lines) throws IOException{
82+
for( String[] line : lines){
83+
write(line);
84+
}
85+
return this;
86+
}
87+
public <T> CSVWriter writeAll(List<T> list){
88+
return this;
89+
}
90+
public CSVWriter writeHeader(){
91+
return this;
92+
}
93+
public void close() throws IOException{
94+
writer.close();
95+
}
96+
97+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package hr.element.doit.csv;
2+
3+
4+
import java.io.InputStream;
5+
import java.nio.charset.Charset;
6+
7+
public class CSVFactory {
8+
public final String delimiter;
9+
public final String newLine;
10+
public final String quotes;
11+
public final Charset encoding;
12+
13+
public static CSVFactory factory() {
14+
return new CSVFactory(";", "\n", "\"", Charset.forName("UTF-8"));
15+
}
16+
17+
private CSVFactory(final String delimiter, final String newLine,
18+
final String quotes, final Charset encoding) {
19+
this.delimiter = delimiter;
20+
this.newLine = newLine;
21+
this.quotes = quotes;
22+
this.encoding = encoding;
23+
}
24+
25+
public CSVFactory setDelimiter(final String delimiter) {
26+
return new CSVFactory(delimiter, newLine, quotes, encoding);
27+
}
28+
29+
public CSVFactory setDelimiter(final char delimiter) {
30+
return setDelimiter(String.valueOf(delimiter));
31+
}
32+
33+
public CSVFactory setNewLine(final String newLine) {
34+
return new CSVFactory(delimiter, newLine, quotes, encoding);
35+
}
36+
37+
public CSVFactory setNewLine(final char newLine) {
38+
return setNewLine(String.valueOf(newLine));
39+
}
40+
41+
public CSVFactory setEncoding(final String encoding) {
42+
return setEncoding(Charset.forName(encoding));
43+
}
44+
45+
public CSVFactory setEncoding(final Charset encoding) {
46+
return new CSVFactory(delimiter, newLine, quotes, encoding);
47+
}
48+
49+
public CSVFactory setQuotes(final String quotes) {
50+
return new CSVFactory(delimiter, newLine, quotes, encoding);
51+
}
52+
53+
public CSVFactory setQuotes(final char quotes) {
54+
return setQuotes(String.valueOf(quotes));
55+
}
56+
57+
// ----------------------------------------------------
58+
/*
59+
public CSVWriter getWriter(final OutputStream oS) {
60+
Writer w = new OutputStreamWriter(oS, encoding);
61+
return new CSVWriter(this, w);
62+
}
63+
*/
64+
// -----------------------------------------------------
65+
public CSVReader getReader(final InputStream iS) {
66+
return new CSVReader(this, iS);
67+
}
68+
}

0 commit comments

Comments
 (0)