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 CSV processAll() {
65 * Create your code to accept the List<String> row.
67 * Your code may keep the List... CSV does not hold onto it.
69 * @author Instrumental(Jonathan)
72 public interface Visitor {
73 void visit(List<String> row) throws IOException, CadiException;
76 public void visit(Visitor visitor) throws IOException, CadiException {
77 BufferedReader br = new BufferedReader(new FileReader(csv));
80 StringBuilder sb = new StringBuilder();
81 while((line = br.readLine())!=null) {
83 if(!line.startsWith("#") && line.length()>0) {
84 // System.out.println(line); uncomment to debug
85 List<String> row = new ArrayList<>();
89 for(int i=0;i<line.length();++i) {
90 switch(c=line.charAt(i)) {
93 if(i<line.length()-1) { // may look ahead
94 if('"' == line.charAt(i+1)) {
119 row.add(sb.toString());
128 row.add(sb.toString());
133 } catch (CadiException e) {
135 access.log(Level.ERROR,e);
147 public Writer writer() throws FileNotFoundException {
148 return new Writer(false);
151 public Writer writer(boolean append) throws FileNotFoundException {
152 return new Writer(append);
155 public class Writer {
156 private PrintStream ps;
157 private Writer(final boolean append) throws FileNotFoundException {
158 ps = new PrintStream(new FileOutputStream(csv,append));
160 public void row(Object ... objs) {
162 boolean first = true;
163 for(Object o : objs) {
170 } else if(o instanceof String[]) {
171 for(String str : (String[])o) {
182 private void print(String s) {
183 boolean quote = s.matches(".*[,|\"].*");
186 ps.print(s.replace("\"", "\"\"")
188 .replace("\\", "\\\\"));
197 * Note: CSV files do not actually support Comments as a standard, but it is useful
200 public void comment(String comment) {
205 public void flush() {
209 public void close() {
213 public String toString() {
214 return csv.getAbsolutePath();
218 public void delete() {
222 public String toString() {
223 return csv.getAbsolutePath();