X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=cadi%2Fcore%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Faaf%2Fcadi%2Futil%2FCSV.java;h=be6d73d4558e56b4e2f1aa3fe32deeacf95cb51e;hb=57effd69fb15e0c73f85296d10ff6b358a6cbebb;hp=a395887882c84e591bccff1b338e2a00a58f3a00;hpb=d9c0bb04b77f6a637f1fc07b69c90898d672bd34;p=aaf%2Fauthz.git diff --git a/cadi/core/src/main/java/org/onap/aaf/cadi/util/CSV.java b/cadi/core/src/main/java/org/onap/aaf/cadi/util/CSV.java index a3958878..be6d73d4 100644 --- a/cadi/core/src/main/java/org/onap/aaf/cadi/util/CSV.java +++ b/cadi/core/src/main/java/org/onap/aaf/cadi/util/CSV.java @@ -31,8 +31,8 @@ import java.util.ArrayList; import java.util.List; import org.onap.aaf.cadi.Access; -import org.onap.aaf.cadi.CadiException; import org.onap.aaf.cadi.Access.Level; +import org.onap.aaf.cadi.CadiException; /** * Read CSV file for various purposes @@ -44,6 +44,7 @@ public class CSV { private File csv; private Access access; private boolean processAll; + private char delimiter = ','; public CSV(Access access, File file) { this.access = access; @@ -57,6 +58,11 @@ public class CSV { processAll = false; } + public CSV setDelimiter(char delimiter) { + this.delimiter = delimiter; + return this; + } + public String name() { return csv.getName(); } @@ -116,16 +122,17 @@ public class CSV { escape = true; } break; - case ',': - if(quotes) { - sb.append(c); + default: + if(delimiter==c) { + if(quotes) { + sb.append(c); + } else { + row.add(sb.toString()); + sb.setLength(0); + } } else { - row.add(sb.toString()); - sb.setLength(0); + sb.append(c); } - break; - default: - sb.append(c); } } if(sb.length()>0 || c==',') { @@ -156,11 +163,44 @@ public class CSV { return new Writer(append); } - public class Writer { + public interface RowSetter { + public void row(Object ... objs); + } + + public static class Saver implements RowSetter { + List ls= new ArrayList<>(); + + @Override + public void row(Object ... objs) { + if(objs.length>0) { + for(Object o : objs) { + if(o != null) { + if(o instanceof String[]) { + for(String str : (String[])o) { + ls.add(str); + } + } else { + ls.add(o.toString()); + } + } + } + } + } + + public List asList() { + List rv = ls; + ls = new ArrayList<>(); + return rv; + } + } + + public class Writer implements RowSetter { private PrintStream ps; private Writer(final boolean append) throws FileNotFoundException { ps = new PrintStream(new FileOutputStream(csv,append)); } + + @Override public void row(Object ... objs) { if(objs.length>0) { boolean first = true; @@ -212,6 +252,7 @@ public class CSV { } public void close() { + flush(); ps.close(); }