Remove Tabs, per Jococo
[aaf/authz.git] / cadi / core / src / main / java / org / onap / aaf / cadi / util / CSV.java
index 35d85b9..fbaa4a5 100644 (file)
@@ -41,240 +41,240 @@ import org.onap.aaf.cadi.CadiException;
  *
  */
 public class CSV {
-       private File csv;
-       private Access access;
-       private boolean processAll;
-       private char delimiter = ',';
-       
-       public CSV(Access access, File file) {
-               this.access = access;
-               csv = file;
-               processAll = false;
-       }
-       
-       public CSV(Access access, String csvFilename) {
-               this.access = access;
-               csv = new File(csvFilename);
-               processAll = false;
-       }
-       
-       public CSV setDelimiter(char delimiter) {
-               this.delimiter = delimiter;
-               return this;
-       }
-       
-       public String name() {
-               return csv.getName();
-       }
+    private File csv;
+    private Access access;
+    private boolean processAll;
+    private char delimiter = ',';
+    
+    public CSV(Access access, File file) {
+        this.access = access;
+        csv = file;
+        processAll = false;
+    }
+    
+    public CSV(Access access, String csvFilename) {
+        this.access = access;
+        csv = new File(csvFilename);
+        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;
-       }
-       /*
-        * 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 = 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);
-       }
+    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((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);
+    }
 
-       public Writer writer(boolean append) throws FileNotFoundException {
-               return new Writer(append);
-       }
+    public Writer writer(boolean append) throws FileNotFoundException {
+        return new Writer(append);
+    }
 
-       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 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;
-                               for(Object o : objs) {
-                                       if(first) {
-                                               first = false;
-                                       } else {
-                                               ps.append(delimiter);
-                                       }
-                                       if(o == null) {
-                                       } else if(o instanceof String[]) {
-                                               for(String str : (String[])o) {
-                                                       print(str);
-                                               }
-                                       } else {
-                                               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);
-                       }
+    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;
+                for(Object o : objs) {
+                    if(first) {
+                        first = false;
+                    } else {
+                        ps.append(delimiter);
+                    }
+                    if(o == null) {
+                    } else if(o instanceof String[]) {
+                        for(String str : (String[])o) {
+                            print(str);
+                        }
+                    } else {
+                        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, Object ... objs) {
-                       ps.print("# ");
-                       ps.printf(comment,objs);
-                       ps.println();
-               }
-               
-               public void flush() {
-                       ps.flush();
-               }
-               
-               public void close() {
-                       flush();
-                       ps.close();
-               }
-               
-               public String toString() {
-                       return csv.getAbsolutePath();
-               }
-       }
+            
+        }
+        /**
+         * Note: CSV files do not actually support Comments as a standard, but it is useful
+         * @param comment
+         */
+        public void comment(String comment, Object ... objs) {
+            ps.print("# ");
+            ps.printf(comment,objs);
+            ps.println();
+        }
+        
+        public void flush() {
+            ps.flush();
+        }
+        
+        public void close() {
+            flush();
+            ps.close();
+        }
+        
+        public String toString() {
+            return csv.getAbsolutePath();
+        }
+    }
 
-       public void delete() {
-               csv.delete();
-       }
-       
-       public String toString() {
-               return csv.getAbsolutePath();
-       }
+    public void delete() {
+        csv.delete();
+    }
+    
+    public String toString() {
+        return csv.getAbsolutePath();
+    }
 
 }