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.CadiException;
36 * Read CSV file for various purposes
38 * @author Instrumental(Jonathan)
44 public CSV(File file) {
48 public CSV(String csvFilename) {
49 csv = new File(csvFilename);
54 * Create your code to accept the List<String> row.
56 * Your code may keep the List... CSV does not hold onto it.
58 * @author Instrumental(Jonathan)
61 public interface Visitor {
62 void visit(List<String> row) throws IOException, CadiException;
65 public void visit(Visitor visitor) throws IOException, CadiException {
66 BufferedReader br = new BufferedReader(new FileReader(csv));
69 StringBuilder sb = new StringBuilder();
70 while((line = br.readLine())!=null) {
72 if(!line.startsWith("#") && line.length()>0) {
73 // System.out.println(line); uncomment to debug
74 List<String> row = new ArrayList<>();
78 for(int i=0;i<line.length();++i) {
79 switch(c=line.charAt(i)) {
82 if(i<line.length()-1) { // may look ahead
83 if('"' == line.charAt(i+1)) {
108 row.add(sb.toString());
117 row.add(sb.toString());
128 public Writer writer() throws FileNotFoundException {
129 return new Writer(false);
132 public Writer writer(boolean append) throws FileNotFoundException {
133 return new Writer(append);
136 public class Writer {
137 private PrintStream ps;
138 private Writer(final boolean append) throws FileNotFoundException {
139 ps = new PrintStream(new FileOutputStream(csv,append));
141 public void row(Object ... strings) {
142 if(strings.length>0) {
143 boolean first = true;
146 for(Object o : strings) {
153 quote = s.matches(".*[,|\"].*");
156 ps.print(s.replace("\"", "\"\"")
158 .replace("\\", "\\\\"));
169 * Note: CSV files do not actually support Comments as a standard, but it is useful
172 public void comment(String comment) {
177 public void flush() {
181 public void close() {
185 public String toString() {
186 return csv.getAbsolutePath();
190 public void delete() {
194 public String toString() {
195 return csv.getAbsolutePath();