2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 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=========================================================
24 package org.openecomp.aai.util;
26 import java.io.IOException;
27 import java.io.PrintWriter;
28 import java.io.Writer;
30 import com.opencsv.CSVWriter;
33 * had to overwrite the separate character to separate string
34 * Based on the public - A very simple CSV writer released under a commercial-friendly license.
38 public class AAICSVWriter extends CSVWriter {
40 private String separatorStr;
41 private char overridequotechar;
42 private String overridelineEnd;
43 private Writer rawWriter;
44 private PrintWriter pw;
47 * Instantiates a new AAICSV writer.
49 * @param writer the writer
51 public AAICSVWriter(Writer writer) {
53 // TODO Auto-generated constructor stub
57 * Constructs AAICSVWriter with supplied separator string and quote char.
59 * @param writer the writer to an underlying CSV source.
60 * @param overrideseparator the overrideseparator
61 * @param quotechar the character to use for quoted elements
62 * @param lineEnd the line feed terminator to use
64 public AAICSVWriter(Writer writer, String overrideseparator, char quotechar, String lineEnd) {
65 super(writer, CSVWriter.DEFAULT_SEPARATOR, quotechar, DEFAULT_ESCAPE_CHARACTER, lineEnd);
66 separatorStr = overrideseparator;
67 overridequotechar = quotechar;
68 overridelineEnd = lineEnd;
69 this.rawWriter = writer;
70 this.pw = new PrintWriter(writer);
74 * String contains special characters.
76 * @param line the line
77 * @return true, if successful
79 private boolean stringContainsSpecialCharacters(String line) {
80 return line.indexOf(overridequotechar) != -1 || line.indexOf(DEFAULT_ESCAPE_CHARACTER) != -1 || line.indexOf(separatorStr) != -1 || line.contains("\n") || line.contains("\r");
84 * Close the underlying stream writer flushing any buffered content.
86 * @throws IOException if bad things happen
88 public void close() throws IOException {
95 * Writes the next line to the file.
97 * @param nextLine a string array with each comma-separated element as a separate
99 * @param applyQuotesToAll true if all values are to be quoted. false applies quotes only
100 * to values which contain the separator, escape, quote or new line characters.
102 public void writeNext(String[] nextLine, boolean applyQuotesToAll) {
104 if (nextLine == null)
107 StringBuilder sb = new StringBuilder(INITIAL_STRING_SIZE);
108 for (int i = 0; i < nextLine.length; i++) {
111 sb.append(separatorStr);
114 String nextElement = nextLine[i];
116 if (nextElement == null)
119 Boolean stringContainsSpecialCharacters = stringContainsSpecialCharacters(nextElement);
121 if ((applyQuotesToAll || stringContainsSpecialCharacters) && overridequotechar != NO_QUOTE_CHARACTER)
122 sb.append(overridequotechar);
124 if (stringContainsSpecialCharacters) {
125 sb.append(processLine(nextElement));
127 sb.append(nextElement);
130 if ((applyQuotesToAll || stringContainsSpecialCharacters) && overridequotechar != NO_QUOTE_CHARACTER)
131 sb.append(overridequotechar);
134 sb.append(overridelineEnd);
135 pw.write(sb.toString());
140 * Writes the next line to the file ignoring all exceptions.
142 * @param nextLine a string array with each comma-separated element as a separate
145 public void writeColumn(String[] nextLine) {
147 if (nextLine == null)
150 StringBuilder sb = new StringBuilder(INITIAL_STRING_SIZE);
151 for (int i = 0; i < nextLine.length; i++) {
154 String nextElement = nextLine[i];
156 if (nextElement == null)
159 sb.append(nextElement);
164 sb.append(overridelineEnd);
165 pw.write(sb.toString());