- /**
- * Create your code to accept the List<String> row.
- *
- * Your code may keep the List... CSV does not hold onto it.
- *
- * @author Instrumental(Jonathan)
- *
- */
- public interface Visitor {
- void visit(List<String> row) throws IOException, CadiException;
- }
-
- public void visit(Visitor visitor) throws IOException, CadiException {
- BufferedReader br = new BufferedReader(new FileReader(csv));
- try {
- String line;
- StringBuilder sb = new StringBuilder();
- while((line = br.readLine())!=null) {
- line=line.trim();
- if(!line.startsWith("#") && line.length()>0) {
-// System.out.println(line); uncomment to debug
- List<String> row = new ArrayList<>();
- boolean quotes=false;
- boolean escape=false;
- char c;
- for(int i=0;i<line.length();++i) {
- switch(c=line.charAt(i)) {
- case '"':
- if(quotes) {
- if(i<line.length()-1) { // may look ahead
- if('"' == line.charAt(i+1)) {
- sb.append(c);
- ++i;
- } else {
- quotes = false;
- }
- } else {
- quotes=false;
- }
- } else {
- quotes=true;
- }
- break;
- case '\\':
- if(escape) {
- sb.append(c);
- escape = false;
- } else {
- escape = true;
- }
- break;
- case ',':
- if(quotes) {
- sb.append(c);
- } else {
- row.add(sb.toString());
- sb.setLength(0);
- }
- break;
- default:
- sb.append(c);
- }
- }
- if(sb.length()>0) {
- row.add(sb.toString());
- sb.setLength(0);
- }
- visitor.visit(row);
- }
- }
- } finally {
- br.close();
- }
- }
-
- public Writer writer() throws FileNotFoundException {
- return new Writer(false);
- }
+ public CSV processAll() {
+ processAll = true;
+ return this;
+ }
+ /*
+ * Create your code to accept the List<String> row.
+ *
+ * Your code may keep the List... CSV does not hold onto it.
+ *
+ * @author Instrumental(Jonathan)
+ *
+ */
+ public interface Visitor {
+ void visit(List<String> row) throws IOException, CadiException;
+ }
+
+ public void visit(Visitor visitor) throws IOException, CadiException {
+ BufferedReader br = new BufferedReader(new FileReader(csv));
+ try {
+ String line;
+ StringBuilder sb = new StringBuilder();
+ while(go && (line = br.readLine())!=null) {
+ line=line.trim();
+ if(!line.startsWith("#") && line.length()>0) {
+// System.out.println(line); uncomment to debug
+ List<String> row = new ArrayList<>();
+ boolean quotes=false;
+ boolean escape=false;
+ char c = 0;
+ for(int i=0;i<line.length();++i) {
+ switch(c=line.charAt(i)) {
+ case '"':
+ if(quotes) {
+ if(i<line.length()-1) { // may look ahead
+ if('"' == line.charAt(i+1)) {
+ sb.append(c);
+ ++i;
+ } else {
+ quotes = false;
+ }
+ } else {
+ quotes=false;
+ }
+ } else {
+ quotes=true;
+ }
+ break;
+ case '\\':
+ if(escape) {
+ sb.append(c);
+ escape = false;
+ } else {
+ escape = true;
+ }
+ break;
+ case 'n':
+ if(escape) {
+ sb.append("\\n");
+ escape=false;
+ } else {
+ sb.append(c);
+ }
+ break;
+ default:
+ if(delimiter==c) {
+ if(quotes) {
+ sb.append(c);
+ } else {
+ row.add(sb.toString());
+ sb.setLength(0);
+ }
+ } else {
+ sb.append(c);
+ }
+ }
+ }
+ if(sb.length()>0 || c==',') {
+ row.add(sb.toString());
+ sb.setLength(0);
+ }
+ try {
+ visitor.visit(row);
+ } catch (CadiException e) {
+ if(processAll) {
+ access.log(Level.ERROR,e);
+ } else {
+ throw e;
+ }
+ }
+ }
+ }
+ } finally {
+ br.close();
+ }
+ }
+
+ public Writer writer() throws FileNotFoundException {
+ return new Writer(false);
+ }