Skip to content

Instantly share code, notes, and snippets.

@linkin-park
Created September 5, 2018 23:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save linkin-park/a3a89b6a8844a1b40906c19b990f1505 to your computer and use it in GitHub Desktop.
Save linkin-park/a3a89b6a8844a1b40906c19b990f1505 to your computer and use it in GitHub Desktop.
CSV_Modular_1
package com.detector;
import java.util.ArrayList;
import java.util.List;
public class CSVProcessor {
final String doubleQuoteRegex = "^\"\".*";
private String[] process(String buffer) {
List<String> values = new ArrayList<String>();
if (buffer == null || buffer.isEmpty()) {
return values.toArray(new String[] {});
}
StreamIterator charToken = new StreamIterator();
charToken.setBuffer(buffer);
// initially set to false,
boolean isDoubleQuoteSkip = false;
while (charToken.hasNext()) {
char ch = charToken.getNext();
if (ch == '\"') {
isDoubleQuoteSkip = doubleQuoteProcess(isDoubleQuoteSkip,charToken,values);
} else if (isDoubleQuoteSkip != true && ch == ',') {
values.add("");
} else {
add(values, ch);
}
}
return values.toArray(new String[] {});
}
/**
* @param isDoubleQuoteSkip
* @param charToken
* @param values
* @return
*/
private boolean doubleQuoteProcess(boolean isDoubleQuoteSkip,StreamIterator charToken,List<String> values){
if (isDoubleQuoteSkip
&& charToken.currentString()
.matches(doubleQuoteRegex)) {
// add this values based on the basic rule
// double quote is followed by double quote
add(values, '"');
// skip the next double quote
charToken.getNext();
}else{
// true <-> false
isDoubleQuoteSkip = !isDoubleQuoteSkip;
}
return isDoubleQuoteSkip;
}
public void add(List<String> values, char ch) {
// intialization
if (values.size() == 0) {
values.add("");
}
// append values to existing data
values.set(values.size() - 1, values.get(values.size() - 1) + ch);
}
public String[] csvToText(String buffer) {
return process(buffer);
}
public static void main(String[] args) {
CSVProcessor csvProcessor = new CSVProcessor();
String[] values = csvProcessor
.csvToText("1999,Chevy,\"Venture \"\"Extended Edition, Very Large\"\"\",,5000.00");
for (String val : values) {
System.out.println(val);
}
System.out.println("**************");
values = csvProcessor.csvToText("1996,Jeep,Grand Cherokee,\"MUST SELL!\nair, moon roof, loaded\",4799.00");
for (String val : values) {
System.out.println(val);
}
}
}
class StreamIterator{
private char next;
private char peek;
private int counter = -1;
private String buffer;
/**
* @return the counter
*/
public int getCounter() {
return counter;
}
/**
* @param counter the counter to set
*/
public void setCounter(int counter) {
this.counter = counter;
}
/**
* @return the buffer
*/
public String getBuffer() {
return buffer;
}
/**
* @param buffer the buffer to set
*/
public void setBuffer(String buffer) {
this.buffer = buffer;
counterReset();
}
public void counterReset(){
counter = -1;
}
/**
* @return the next
*/
public char getNext() {
processNext();
return next;
}
/**
* @return the peek
*/
public char getPeek() {
processPeek();
return peek;
}
public void processNext(){
try{
next = buffer.charAt(++counter);
}catch(ArrayIndexOutOfBoundsException e){
next = '\0';
}
}
public void processPeek(){
try{
peek = buffer.charAt(counter+1);
}catch(ArrayIndexOutOfBoundsException e){
peek = '\0';
}
}
public boolean hasNext(){
if(buffer == null) return false;
return counter+1<buffer.length();
}
public String currentString(){
return buffer.substring(counter);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment