Changes from Batch Test
[aaf/authz.git] / cadi / core / src / main / java / org / onap / aaf / cadi / util / CSV.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * ===========================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END====================================================
19  */
20
21 package org.onap.aaf.cadi.util;
22
23 import java.io.BufferedReader;
24 import java.io.File;
25 import java.io.FileNotFoundException;
26 import java.io.FileOutputStream;
27 import java.io.FileReader;
28 import java.io.IOException;
29 import java.io.PrintStream;
30 import java.util.ArrayList;
31 import java.util.List;
32
33 import org.onap.aaf.cadi.Access;
34 import org.onap.aaf.cadi.Access.Level;
35 import org.onap.aaf.cadi.CadiException;
36
37 /**
38  * Read CSV file for various purposes
39  * 
40  * @author Instrumental(Jonathan)
41  *
42  */
43 public class CSV {
44         private File csv;
45         private Access access;
46         private boolean processAll;
47         private char delimiter = ',';
48         
49         public CSV(Access access, File file) {
50                 this.access = access;
51                 csv = file;
52                 processAll = false;
53         }
54         
55         public CSV(Access access, String csvFilename) {
56                 this.access = access;
57                 csv = new File(csvFilename);
58                 processAll = false;
59         }
60         
61         public CSV setDelimiter(char delimiter) {
62                 this.delimiter = delimiter;
63                 return this;
64         }
65         
66         public String name() {
67                 return csv.getName();
68         }
69
70         public CSV processAll() {
71                 processAll = true;
72                 return this;
73         }
74         /*
75          * Create your code to accept the List<String> row.
76          * 
77          * Your code may keep the List... CSV does not hold onto it.
78          * 
79          * @author Instrumental(Jonathan)
80          *
81          */
82         public interface Visitor {
83                 void visit(List<String> row) throws IOException, CadiException;
84         }
85         
86         public void visit(Visitor visitor) throws IOException, CadiException {
87                 BufferedReader br = new BufferedReader(new FileReader(csv));
88                 try {
89                         String line;
90                         StringBuilder sb = new StringBuilder();
91                         while((line = br.readLine())!=null) {
92                                 line=line.trim();
93                                 if(!line.startsWith("#") && line.length()>0) {
94 //                                      System.out.println(line);  uncomment to debug
95                                         List<String> row = new ArrayList<>();
96                                         boolean quotes=false;
97                                         boolean escape=false;
98                                         char c = 0;
99                                         for(int i=0;i<line.length();++i) {
100                                                 switch(c=line.charAt(i)) {
101                                                         case '"':
102                                                                 if(quotes) {
103                                                                         if(i<line.length()-1) { // may look ahead
104                                                                                 if('"' == line.charAt(i+1)) {
105                                                                                         sb.append(c);
106                                                                                         ++i;
107                                                                                 } else {
108                                                                                         quotes = false;
109                                                                                 }
110                                                                         } else {
111                                                                                 quotes=false;
112                                                                         }
113                                                                 } else {
114                                                                         quotes=true;
115                                                                 }
116                                                                 break;
117                                                         case '\\':
118                                                                 if(escape) {
119                                                                         sb.append(c);
120                                                                         escape = false;
121                                                                 } else {
122                                                                         escape = true;
123                                                                 }
124                                                                 break;
125                                                         default:
126                                                                 if(delimiter==c) {
127                                                                         if(quotes) {
128                                                                                 sb.append(c);
129                                                                         } else {
130                                                                                 row.add(sb.toString());
131                                                                                 sb.setLength(0);
132                                                                         }
133                                                                 } else {
134                                                                         sb.append(c);
135                                                                 }
136                                                 }
137                                         }
138                                         if(sb.length()>0 || c==',') {
139                                                 row.add(sb.toString());
140                                                 sb.setLength(0);
141                                         }
142                                         try {
143                                                 visitor.visit(row);
144                                         } catch (CadiException e) {
145                                                 if(processAll) {
146                                                         access.log(Level.ERROR,e);
147                                                 } else {
148                                                         throw e;
149                                                 }
150                                         }
151                                 }
152                         }
153                 } finally {
154                         br.close();
155                 }
156         }
157         
158         public Writer writer() throws FileNotFoundException {
159                 return new Writer(false);
160         }
161
162         public Writer writer(boolean append) throws FileNotFoundException {
163                 return new Writer(append);
164         }
165
166         public interface RowSetter {
167                 public void row(Object ... objs);
168         }
169         
170         public static class Saver implements RowSetter {
171                 List<String> ls= new ArrayList<>();
172                 
173                 @Override
174                 public void row(Object ... objs) {
175                         if(objs.length>0) {
176                                 for(Object o : objs) {
177                                         if(o != null) {
178                                                 if(o instanceof String[]) {
179                                                         for(String str : (String[])o) {
180                                                                 ls.add(str);
181                                                         }
182                                                 } else {
183                                                         ls.add(o.toString());
184                                                 }
185                                         }
186                                 }
187                         }
188                 }
189                 
190                 public List<String> asList() {
191                         List<String> rv = ls;
192                         ls = new ArrayList<>();
193                         return rv;
194                 }
195         }
196
197         public class Writer implements RowSetter {
198                 private PrintStream ps;
199                 private Writer(final boolean append) throws FileNotFoundException {
200                         ps = new PrintStream(new FileOutputStream(csv,append));
201                 }
202                 
203                 @Override
204                 public void row(Object ... objs) {
205                         if(objs.length>0) {
206                                 boolean first = true;
207                                 for(Object o : objs) {
208                                         if(first) {
209                                                 first = false;
210                                         } else {
211                                                 ps.append(',');
212                                         }
213                                         if(o == null) {
214                                         } else if(o instanceof String[]) {
215                                                 for(String str : (String[])o) {
216                                                         print(str);
217                                                 }
218                                         } else {
219                                                 print(o.toString());
220                                         }
221                                 }
222                                 ps.println();
223                         }
224                 }
225                 
226                 private void print(String s) {
227                         boolean quote = s.matches(".*[,|\"].*");
228                         if(quote) {
229                                 ps.append('"');
230                                 ps.print(s.replace("\"", "\"\"")
231                                                   .replace("'", "''")
232                                                   .replace("\\", "\\\\"));
233                                 ps.append('"');
234                         } else {
235                                 ps.append(s);
236                         }
237
238                         
239                 }
240                 /**
241                  * Note: CSV files do not actually support Comments as a standard, but it is useful
242                  * @param comment
243                  */
244                 public void comment(String comment, Object ... objs) {
245                         ps.print("# ");
246                         ps.printf(comment,objs);
247                         ps.println();
248                 }
249                 
250                 public void flush() {
251                         ps.flush();
252                 }
253                 
254                 public void close() {
255                         flush();
256                         ps.close();
257                 }
258                 
259                 public String toString() {
260                         return csv.getAbsolutePath();
261                 }
262         }
263
264         public void delete() {
265                 csv.delete();
266         }
267         
268         public String toString() {
269                 return csv.getAbsolutePath();
270         }
271
272 }