Improve Batches
[aaf/authz.git] / cadi / core / src / main / java / org / onap / aaf / cadi / util / CSV.java
index c7fada7..1d60ae5 100644 (file)
@@ -30,6 +30,8 @@ import java.io.PrintStream;
 import java.util.ArrayList;
 import java.util.List;
 
+import org.onap.aaf.cadi.Access;
+import org.onap.aaf.cadi.Access.Level;
 import org.onap.aaf.cadi.CadiException;
 
 /**
@@ -40,17 +42,30 @@ import org.onap.aaf.cadi.CadiException;
  */
 public class CSV {
        private File csv;
+       private Access access;
+       private boolean processAll;
        
-       public CSV(File file) {
+       public CSV(Access access, File file) {
+               this.access = access;
                csv = file;
+               processAll = false;
        }
        
-       public CSV(String csvFilename) {
+       public CSV(Access access, String csvFilename) {
+               this.access = access;
                csv = new File(csvFilename);
+               processAll = false;
        }
        
+       public String name() {
+               return csv.getName();
+       }
 
-       /**
+       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.
@@ -74,7 +89,7 @@ public class CSV {
                                        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 '"':
@@ -113,11 +128,19 @@ public class CSV {
                                                                sb.append(c);
                                                }
                                        }
-                                       if(sb.length()>0) {
+                                       if(sb.length()>0 || c==',') {
                                                row.add(sb.toString());
                                                sb.setLength(0);
                                        }
-                                       visitor.visit(row);
+                                       try {
+                                               visitor.visit(row);
+                                       } catch (CadiException e) {
+                                               if(processAll) {
+                                                       access.log(Level.ERROR,e);
+                                               } else {
+                                                       throw e;
+                                               }
+                                       }
                                }
                        }
                } finally {
@@ -133,45 +156,88 @@ 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<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));
                }
-               public void row(Object ... strings) {
-                       if(strings.length>0) {
+               
+               @Override
+               public void row(Object ... objs) {
+                       if(objs.length>0) {
                                boolean first = true;
-                               boolean quote;
-                               String s;
-                               for(Object o : strings) {
+                               for(Object o : objs) {
                                        if(first) {
                                                first = false;
                                        } else {
                                                ps.append(',');
                                        }
-                                       s = o.toString();
-                                       quote = s.matches(".*[,|\"].*");
-                                       if(quote) {
-                                               ps.append('"');
-                                               ps.print(s.replace("\"", "\"\"")
-                                                                 .replace("'", "''")
-                                                                 .replace("\\", "\\\\"));
-                                               ps.append('"');
+                                       if(o == null) {
+                                       } else if(o instanceof String[]) {
+                                               for(String str : (String[])o) {
+                                                       print(str);
+                                               }
                                        } else {
-                                               ps.append(s);
+                                               print(o.toString());
                                        }
                                }
                                ps.println();
                        }
                }
                
+               private void print(String s) {
+                       boolean quote = s.matches(".*[,|\"].*");
+                       if(quote) {
+                               ps.append('"');
+                               ps.print(s.replace("\"", "\"\"")
+                                                 .replace("'", "''")
+                                                 .replace("\\", "\\\\"));
+                               ps.append('"');
+                       } else {
+                               ps.append(s);
+                       }
+
+                       
+               }
                /**
                 * 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() {