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