Update license files, sonar plugin and fix tests
[aai/aai-common.git] / aai-core / src / main / java / org / openecomp / aai / util / AAICSVWriter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.openecomp.aai
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 /**
22  * 
23  */
24 package org.openecomp.aai.util;
25
26 import java.io.IOException;
27 import java.io.PrintWriter;
28 import java.io.Writer;
29
30 import com.opencsv.CSVWriter;
31
32 /**
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.
35  *
36  
37  */
38 public class AAICSVWriter extends CSVWriter {
39
40         private String separatorStr;
41         private char overridequotechar;
42         private String overridelineEnd;
43         private Writer rawWriter;
44         private PrintWriter pw;
45            
46         /**
47          * Instantiates a new AAICSV writer.
48          *
49          * @param writer the writer
50          */
51         public AAICSVWriter(Writer writer) {
52                 super(writer);
53                 // TODO Auto-generated constructor stub
54         }
55
56          /**
57          * Constructs AAICSVWriter with supplied separator string and quote char.
58          *
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
63          */
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);
71            }
72            
73            /**
74          * String contains special characters.
75          *
76          * @param line the line
77          * @return true, if successful
78          */
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");
81                    }
82            
83            /**
84             * Close the underlying stream writer flushing any buffered content.
85             *
86             * @throws IOException if bad things happen
87             */
88            public void close() throws IOException {
89               flush();
90               pw.close();
91               rawWriter.close();
92            }
93            
94         /**
95             * Writes the next line to the file.
96             *
97             * @param nextLine         a string array with each comma-separated element as a separate
98             *                         entry.
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.
101             */
102            public void writeNext(String[] nextLine, boolean applyQuotesToAll) {
103
104               if (nextLine == null)
105                  return;
106
107               StringBuilder sb = new StringBuilder(INITIAL_STRING_SIZE);
108               for (int i = 0; i < nextLine.length; i++) {
109
110                  if (i != 0) {
111                     sb.append(separatorStr);
112                  }
113
114                  String nextElement = nextLine[i];
115
116                  if (nextElement == null)
117                     continue;
118
119                  Boolean stringContainsSpecialCharacters = stringContainsSpecialCharacters(nextElement);
120
121                  if ((applyQuotesToAll || stringContainsSpecialCharacters) && overridequotechar != NO_QUOTE_CHARACTER)
122                     sb.append(overridequotechar);
123
124                  if (stringContainsSpecialCharacters) {
125                     sb.append(processLine(nextElement));
126                  } else {
127                     sb.append(nextElement);
128                  }
129
130                  if ((applyQuotesToAll || stringContainsSpecialCharacters) && overridequotechar != NO_QUOTE_CHARACTER)
131                     sb.append(overridequotechar);
132               }
133
134               sb.append(overridelineEnd);
135               pw.write(sb.toString());
136            }
137
138            
139            /**
140          * Writes the next line to the file ignoring all exceptions.
141          *
142          * @param nextLine         a string array with each comma-separated element as a separate
143          *                         entry.
144          */
145            public void writeColumn(String[] nextLine) {
146
147               if (nextLine == null)
148                  return;
149
150               StringBuilder sb = new StringBuilder(INITIAL_STRING_SIZE);
151               for (int i = 0; i < nextLine.length; i++) {
152
153
154                  String nextElement = nextLine[i];
155
156                  if (nextElement == null)
157                     continue;
158
159              sb.append(nextElement);
160                  
161
162               }
163
164               sb.append(overridelineEnd);
165               pw.write(sb.toString());
166            }
167 }