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
private File csv;
private Access access;
private boolean processAll;
+ private char delimiter = ',';
public CSV(Access access, File file) {
this.access = access;
processAll = false;
}
+ public CSV setDelimiter(char delimiter) {
+ this.delimiter = delimiter;
+ return this;
+ }
+
+ public String name() {
+ return csv.getName();
+ }
+
public CSV processAll() {
processAll = true;
return this;
List<String> row = new ArrayList<>();
boolean quotes=false;
boolean escape=false;
- char c;
+ char c = 0;
for(int i=0;i<line.length();++i) {
switch(c=line.charAt(i)) {
case '"':
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) {
+ if(sb.length()>0 || c==',') {
row.add(sb.toString());
sb.setLength(0);
}
return new Writer(append);
}
- public class Writer {
+ public interface RowSetter {
+ public void row(Object ... objs);
+ }
+
+ public static class Saver implements RowSetter {
+ List<String> 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<String> asList() {
+ List<String> 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;
* Note: CSV files do not actually support Comments as a standard, but it is useful
* @param comment
*/
- public void comment(String comment) {
+ public void comment(String comment, Object ... objs) {
ps.print("# ");
- ps.println(comment);
+ ps.printf(comment,objs);
+ ps.println();
}
public void flush() {
}
public void close() {
+ flush();
ps.close();
}