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)) {
138 row.add(sb.toString());
146 if(sb.length()>0 || c==',') {
147 row.add(sb.toString());
152 } catch (CadiException e) {
154 access.log(Level.ERROR,e);
166 public Writer writer() throws FileNotFoundException {
167 return new Writer(false);
170 public Writer writer(boolean append) throws FileNotFoundException {
171 return new Writer(append);
174 public interface RowSetter {
175 public void row(Object ... objs);
178 public static class Saver implements RowSetter {
179 List<String> ls= new ArrayList<>();
182 public void row(Object ... objs) {
184 for(Object o : objs) {
186 if(o instanceof String[]) {
187 for(String str : (String[])o) {
191 ls.add(o.toString());
198 public List<String> asList() {
199 List<String> rv = ls;
200 ls = new ArrayList<>();
205 public class Writer implements RowSetter {
206 private PrintStream ps;
207 private Writer(final boolean append) throws FileNotFoundException {
208 ps = new PrintStream(new FileOutputStream(csv,append));
212 public void row(Object ... objs) {
214 boolean first = true;
215 for(Object o : objs) {
219 ps.append(delimiter);
222 } else if(o instanceof String[]) {
223 for(String str : (String[])o) {
234 private void print(String s) {
235 boolean quote = s.matches(".*[,|\"].*");
238 ps.print(s.replace("\"", "\"\"")
240 .replace("\\", "\\\\"));
249 * Note: CSV files do not actually support Comments as a standard, but it is useful
252 public void comment(String comment, Object ... objs) {
254 ps.printf(comment,objs);
258 public void flush() {
262 public void close() {
267 public String toString() {
268 return csv.getAbsolutePath();
272 public void delete() {
276 public String toString() {
277 return csv.getAbsolutePath();