1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
/*-
* ============LICENSE_START=======================================================
* org.openecomp.aai
* ================================================================================
* Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
* ============LICENSE_END=========================================================
*/
/**
*
*/
package org.openecomp.aai.util;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import com.opencsv.CSVWriter;
/**
* had to overwrite the separate character to separate string
* Based on the public - A very simple CSV writer released under a commercial-friendly license.
*
*/
public class AAICSVWriter extends CSVWriter {
private String separatorStr;
private char overridequotechar;
private String overridelineEnd;
private Writer rawWriter;
private PrintWriter pw;
/**
* Instantiates a new AAICSV writer.
*
* @param writer the writer
*/
public AAICSVWriter(Writer writer) {
super(writer);
// TODO Auto-generated constructor stub
}
/**
* Constructs AAICSVWriter with supplied separator string and quote char.
*
* @param writer the writer to an underlying CSV source.
* @param overrideseparator the overrideseparator
* @param quotechar the character to use for quoted elements
* @param lineEnd the line feed terminator to use
*/
public AAICSVWriter(Writer writer, String overrideseparator, char quotechar, String lineEnd) {
super(writer, CSVWriter.DEFAULT_SEPARATOR, quotechar, DEFAULT_ESCAPE_CHARACTER, lineEnd);
separatorStr = overrideseparator;
overridequotechar = quotechar;
overridelineEnd = lineEnd;
this.rawWriter = writer;
this.pw = new PrintWriter(writer);
}
/**
* String contains special characters.
*
* @param line the line
* @return true, if successful
*/
private boolean stringContainsSpecialCharacters(String line) {
return line.indexOf(overridequotechar) != -1 || line.indexOf(DEFAULT_ESCAPE_CHARACTER) != -1 || line.indexOf(separatorStr) != -1 || line.contains("\n") || line.contains("\r");
}
/**
* Close the underlying stream writer flushing any buffered content.
*
* @throws IOException if bad things happen
*/
public void close() throws IOException {
flush();
pw.close();
rawWriter.close();
}
/**
* Writes the next line to the file.
*
* @param nextLine a string array with each comma-separated element as a separate
* entry.
* @param applyQuotesToAll true if all values are to be quoted. false applies quotes only
* to values which contain the separator, escape, quote or new line characters.
*/
public void writeNext(String[] nextLine, boolean applyQuotesToAll) {
if (nextLine == null)
return;
StringBuilder sb = new StringBuilder(INITIAL_STRING_SIZE);
for (int i = 0; i < nextLine.length; i++) {
if (i != 0) {
sb.append(separatorStr);
}
String nextElement = nextLine[i];
if (nextElement == null)
continue;
Boolean stringContainsSpecialCharacters = stringContainsSpecialCharacters(nextElement);
if ((applyQuotesToAll || stringContainsSpecialCharacters) && overridequotechar != NO_QUOTE_CHARACTER)
sb.append(overridequotechar);
if (stringContainsSpecialCharacters) {
sb.append(processLine(nextElement));
} else {
sb.append(nextElement);
}
if ((applyQuotesToAll || stringContainsSpecialCharacters) && overridequotechar != NO_QUOTE_CHARACTER)
sb.append(overridequotechar);
}
sb.append(overridelineEnd);
pw.write(sb.toString());
}
/**
* Writes the next line to the file ignoring all exceptions.
*
* @param nextLine a string array with each comma-separated element as a separate
* entry.
*/
public void writeColumn(String[] nextLine) {
if (nextLine == null)
return;
StringBuilder sb = new StringBuilder(INITIAL_STRING_SIZE);
for (int i = 0; i < nextLine.length; i++) {
String nextElement = nextLine[i];
if (nextElement == null)
continue;
sb.append(nextElement);
}
sb.append(overridelineEnd);
pw.write(sb.toString());
}
}
|