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.CadiException;
35 import org.onap.aaf.cadi.Access.Level;
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 class Writer {
160 private PrintStream ps;
161 private Writer(final boolean append) throws FileNotFoundException {
162 ps = new PrintStream(new FileOutputStream(csv,append));
164 public void row(Object ... objs) {
166 boolean first = true;
167 for(Object o : objs) {
174 } else if(o instanceof String[]) {
175 for(String str : (String[])o) {
186 private void print(String s) {
187 boolean quote = s.matches(".*[,|\"].*");
190 ps.print(s.replace("\"", "\"\"")
192 .replace("\\", "\\\\"));
201 * Note: CSV files do not actually support Comments as a standard, but it is useful
204 public void comment(String comment, Object ... objs) {
206 ps.printf(comment,objs);
210 public void flush() {
214 public void close() {
218 public String toString() {
219 return csv.getAbsolutePath();
223 public void delete() {
227 public String toString() {
228 return csv.getAbsolutePath();