2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
21 package org.onap.aaf.cadi.util;
23 import java.io.BufferedReader;
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;
33 import org.onap.aaf.cadi.Access;
34 import org.onap.aaf.cadi.Access.Level;
35 import org.onap.aaf.cadi.CadiException;
38 * Read CSV file for various purposes
40 * @author Instrumental(Jonathan)
45 private Access access;
46 private boolean processAll;
47 private char delimiter = ',';
50 public CSV(Access access, File file) {
57 public CSV(Access access, String csvFilename) {
59 csv = new File(csvFilename);
64 public CSV setDelimiter(char delimiter) {
65 this.delimiter = delimiter;
69 public String name() {
73 public CSV processAll() {
78 * Create your code to accept the List<String> row.
80 * Your code may keep the List... CSV does not hold onto it.
82 * @author Instrumental(Jonathan)
85 public interface Visitor {
86 void visit(List<String> row) throws IOException, CadiException;
89 public void visit(Visitor visitor) throws IOException, CadiException {
90 BufferedReader br = new BufferedReader(new FileReader(csv));
93 StringBuilder sb = new StringBuilder();
94 while(go && (line = br.readLine())!=null) {
96 if(!line.startsWith("#") && line.length()>0) {
97 // System.out.println(line); uncomment to debug
98 List<String> row = new ArrayList<>();
100 boolean escape=false;
102 for(int i=0;i<line.length();++i) {
103 switch(c=line.charAt(i)) {
106 if(i<line.length()-1) { // may look ahead
107 if('"' == line.charAt(i+1)) {
141 row.add(sb.toString());
149 if(sb.length()>0 || c==',') {
150 row.add(sb.toString());
155 } catch (CadiException e) {
157 access.log(Level.ERROR,e);
169 public Writer writer() throws FileNotFoundException {
170 return new Writer(false);
173 public Writer writer(boolean append) throws FileNotFoundException {
174 return new Writer(append);
177 public interface RowSetter {
178 public void row(Object ... objs);
181 public static class Saver implements RowSetter {
182 List<String> ls= new ArrayList<>();
185 public void row(Object ... objs) {
187 for(Object o : objs) {
189 if(o instanceof String[]) {
190 for(String str : (String[])o) {
194 ls.add(o.toString());
201 public List<String> asList() {
202 List<String> rv = ls;
203 ls = new ArrayList<>();
208 public class Writer implements RowSetter {
209 private PrintStream ps;
210 private Writer(final boolean append) throws FileNotFoundException {
211 ps = new PrintStream(new FileOutputStream(csv,append));
215 public void row(Object ... objs) {
217 boolean first = true;
218 for(Object o : objs) {
222 ps.append(delimiter);
225 } else if(o instanceof String[]) {
226 for(String str : (String[])o) {
237 private void print(String s) {
238 boolean quote = s.matches(".*[,|\"].*");
241 ps.print(s.replace("\"", "\"\"")
243 .replace("\\", "\\\\"));
252 * Note: CSV files do not actually support Comments as a standard, but it is useful
255 public void comment(String comment, Object ... objs) {
257 ps.printf(comment,objs);
261 public void flush() {
265 public void close() {
270 public String toString() {
271 return csv.getAbsolutePath();
276 * Provides a way to stop processing records from inside a Visit
282 public void delete() {
286 public String toString() {
287 return csv.getAbsolutePath();