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;
48 public CSV(Access access, File file) {
54 public CSV(Access access, String csvFilename) {
56 csv = new File(csvFilename);
60 public String name() {
64 public CSV processAll() {
69 * Create your code to accept the List<String> row.
71 * Your code may keep the List... CSV does not hold onto it.
73 * @author Instrumental(Jonathan)
76 public interface Visitor {
77 void visit(List<String> row) throws IOException, CadiException;
80 public void visit(Visitor visitor) throws IOException, CadiException {
81 BufferedReader br = new BufferedReader(new FileReader(csv));
84 StringBuilder sb = new StringBuilder();
85 while((line = br.readLine())!=null) {
87 if(!line.startsWith("#") && line.length()>0) {
88 // System.out.println(line); uncomment to debug
89 List<String> row = new ArrayList<>();
93 for(int i=0;i<line.length();++i) {
94 switch(c=line.charAt(i)) {
97 if(i<line.length()-1) { // may look ahead
98 if('"' == line.charAt(i+1)) {
123 row.add(sb.toString());
131 if(sb.length()>0 || c==',') {
132 row.add(sb.toString());
137 } catch (CadiException e) {
139 access.log(Level.ERROR,e);
151 public Writer writer() throws FileNotFoundException {
152 return new Writer(false);
155 public Writer writer(boolean append) throws FileNotFoundException {
156 return new Writer(append);
159 public interface RowSetter {
160 public void row(Object ... objs);
163 public static class Saver implements RowSetter {
164 List<String> ls= new ArrayList<>();
167 public void row(Object ... objs) {
169 for(Object o : objs) {
171 if(o instanceof String[]) {
172 for(String str : (String[])o) {
176 ls.add(o.toString());
183 public List<String> asList() {
184 List<String> rv = ls;
185 ls = new ArrayList<>();
190 public class Writer implements RowSetter {
191 private PrintStream ps;
192 private Writer(final boolean append) throws FileNotFoundException {
193 ps = new PrintStream(new FileOutputStream(csv,append));
197 public void row(Object ... objs) {
199 boolean first = true;
200 for(Object o : objs) {
207 } else if(o instanceof String[]) {
208 for(String str : (String[])o) {
219 private void print(String s) {
220 boolean quote = s.matches(".*[,|\"].*");
223 ps.print(s.replace("\"", "\"\"")
225 .replace("\\", "\\\\"));
234 * Note: CSV files do not actually support Comments as a standard, but it is useful
237 public void comment(String comment, Object ... objs) {
239 ps.printf(comment,objs);
243 public void flush() {
247 public void close() {
251 public String toString() {
252 return csv.getAbsolutePath();
256 public void delete() {
260 public String toString() {
261 return csv.getAbsolutePath();