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 = ',';
49 public CSV(Access access, File file) {
55 public CSV(Access access, String csvFilename) {
57 csv = new File(csvFilename);
61 public CSV setDelimiter(char delimiter) {
62 this.delimiter = delimiter;
66 public String name() {
70 public CSV processAll() {
75 * Create your code to accept the List<String> row.
77 * Your code may keep the List... CSV does not hold onto it.
79 * @author Instrumental(Jonathan)
82 public interface Visitor {
83 void visit(List<String> row) throws IOException, CadiException;
86 public void visit(Visitor visitor) throws IOException, CadiException {
87 BufferedReader br = new BufferedReader(new FileReader(csv));
90 StringBuilder sb = new StringBuilder();
91 while((line = br.readLine())!=null) {
93 if(!line.startsWith("#") && line.length()>0) {
94 // System.out.println(line); uncomment to debug
95 List<String> row = new ArrayList<>();
99 for(int i=0;i<line.length();++i) {
100 switch(c=line.charAt(i)) {
103 if(i<line.length()-1) { // may look ahead
104 if('"' == line.charAt(i+1)) {
130 row.add(sb.toString());
138 if(sb.length()>0 || c==',') {
139 row.add(sb.toString());
144 } catch (CadiException e) {
146 access.log(Level.ERROR,e);
158 public Writer writer() throws FileNotFoundException {
159 return new Writer(false);
162 public Writer writer(boolean append) throws FileNotFoundException {
163 return new Writer(append);
166 public interface RowSetter {
167 public void row(Object ... objs);
170 public static class Saver implements RowSetter {
171 List<String> ls= new ArrayList<>();
174 public void row(Object ... objs) {
176 for(Object o : objs) {
178 if(o instanceof String[]) {
179 for(String str : (String[])o) {
183 ls.add(o.toString());
190 public List<String> asList() {
191 List<String> rv = ls;
192 ls = new ArrayList<>();
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));
204 public void row(Object ... objs) {
206 boolean first = true;
207 for(Object o : objs) {
214 } else if(o instanceof String[]) {
215 for(String str : (String[])o) {
226 private void print(String s) {
227 boolean quote = s.matches(".*[,|\"].*");
230 ps.print(s.replace("\"", "\"\"")
232 .replace("\\", "\\\\"));
241 * Note: CSV files do not actually support Comments as a standard, but it is useful
244 public void comment(String comment, Object ... objs) {
246 ps.printf(comment,objs);
250 public void flush() {
254 public void close() {
259 public String toString() {
260 return csv.getAbsolutePath();
264 public void delete() {
268 public String toString() {
269 return csv.getAbsolutePath();