2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 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=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
25 package org.onap.aai.util;
27 import java.io.IOException;
28 import java.io.PrintWriter;
29 import java.io.Writer;
31 import com.opencsv.CSVWriter;
34 * had to overwrite the separate character to separate string
35 * Based on the public - A very simple CSV writer released under a commercial-friendly license.
39 public class AAICSVWriter extends CSVWriter {
41 private String separatorStr;
42 private char overridequotechar;
43 private String overridelineEnd;
44 private Writer rawWriter;
45 private PrintWriter pw;
48 * Instantiates a new AAICSV writer.
50 * @param writer the writer
52 public AAICSVWriter(Writer writer) {
54 // TODO Auto-generated constructor stub
58 * Constructs AAICSVWriter with supplied separator string and quote char.
60 * @param writer the writer to an underlying CSV source.
61 * @param overrideseparator the overrideseparator
62 * @param quotechar the character to use for quoted elements
63 * @param lineEnd the line feed terminator to use
65 public AAICSVWriter(Writer writer, String overrideseparator, char quotechar, String lineEnd) {
66 super(writer, CSVWriter.DEFAULT_SEPARATOR, quotechar, DEFAULT_ESCAPE_CHARACTER, lineEnd);
67 separatorStr = overrideseparator;
68 overridequotechar = quotechar;
69 overridelineEnd = lineEnd;
70 this.rawWriter = writer;
71 this.pw = new PrintWriter(writer);
75 * String contains special characters.
77 * @param line the line
78 * @return true, if successful
80 private boolean stringContainsSpecialCharacters(String line) {
81 return line.indexOf(overridequotechar) != -1 || line.indexOf(DEFAULT_ESCAPE_CHARACTER) != -1 || line.indexOf(separatorStr) != -1 || line.contains("\n") || line.contains("\r");
85 * Close the underlying stream writer flushing any buffered content.
87 * @throws IOException if bad things happen
89 public void close() throws IOException {
96 * Writes the next line to the file.
98 * @param nextLine a string array with each comma-separated element as a separate
100 * @param applyQuotesToAll true if all values are to be quoted. false applies quotes only
101 * to values which contain the separator, escape, quote or new line characters.
103 public void writeNext(String[] nextLine, boolean applyQuotesToAll) {
105 if (nextLine == null)
108 StringBuilder sb = new StringBuilder(INITIAL_STRING_SIZE);
109 for (int i = 0; i < nextLine.length; i++) {
112 sb.append(separatorStr);
115 String nextElement = nextLine[i];
117 if (nextElement == null)
120 Boolean stringContainsSpecialCharacters = stringContainsSpecialCharacters(nextElement);
122 if ((applyQuotesToAll || stringContainsSpecialCharacters) && overridequotechar != NO_QUOTE_CHARACTER)
123 sb.append(overridequotechar);
125 if (stringContainsSpecialCharacters) {
126 sb.append(processLine(nextElement));
128 sb.append(nextElement);
131 if ((applyQuotesToAll || stringContainsSpecialCharacters) && overridequotechar != NO_QUOTE_CHARACTER)
132 sb.append(overridequotechar);
135 sb.append(overridelineEnd);
136 pw.write(sb.toString());
141 * Writes the next line to the file ignoring all exceptions.
143 * @param nextLine a string array with each comma-separated element as a separate
146 public void writeColumn(String[] nextLine) {
148 if (nextLine == null)
151 StringBuilder sb = new StringBuilder(INITIAL_STRING_SIZE);
152 for (int i = 0; i < nextLine.length; i++) {
155 String nextElement = nextLine[i];
157 if (nextElement == null)
160 sb.append(nextElement);
165 sb.append(overridelineEnd);
166 pw.write(sb.toString());