001: /*
002: *
003: * Copyright (c) 2004 SourceTap - www.sourcetap.com
004: *
005: * The contents of this file are subject to the SourceTap Public License
006: * ("License"); You may not use this file except in compliance with the
007: * License. You may obtain a copy of the License at http://www.sourcetap.com/license.htm
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
010: * the specific language governing rights and limitations under the License.
011: *
012: * The above copyright notice and this permission notice shall be included
013: * in all copies or substantial portions of the Software.
014: *
015: */
016:
017: package com.sourcetap.sfa.util;
018:
019: import java.io.BufferedReader;
020: import java.io.FileWriter;
021: import java.io.IOException;
022: import java.io.Reader;
023:
024: import java.util.ArrayList;
025: import java.util.List;
026: import java.util.StringTokenizer;
027:
028: /**
029: *
030: * @author Administrator
031: * @version
032: */
033: public class CsvConverter {
034: private static String DELIM = ",";
035: private String[] headers = null;
036: private ArrayList data = new ArrayList();
037:
038: public CsvConverter(Reader in) {
039: String line = "";
040: boolean doHeader = true;
041: StringTokenizer st = null;
042:
043: try {
044: BufferedReader br = new BufferedReader(in);
045:
046: while ((line = br.readLine()) != null) {
047: if (line == null) {
048: throw new IOException("Empty Data Source");
049: }
050:
051: if (doHeader) {
052: headers = breakCSVStringApart(line);
053: doHeader = false;
054: } else {
055: String[] rowArray = breakCSVStringApart(line);
056:
057: if ((rowArray.length < headers.length)
058: && (rowArray.length < 2)) {
059: //skip as blank row
060: } else {
061: data.add(rowArray);
062: }
063: }
064: }
065: } catch (IOException e) {
066: e.printStackTrace();
067: } finally {
068: try {
069: in.close();
070: } catch (Exception e) {
071: ;
072: }
073: }
074: }
075:
076: /**
077: * DOCUMENT ME!
078: *
079: * @return
080: *
081: * @throws IOException
082: */
083: public String[] getHeader() throws IOException {
084: return headers;
085: }
086:
087: /**
088: * DOCUMENT ME!
089: *
090: * @return
091: *
092: * @throws IOException
093: */
094: public ArrayList getData() throws IOException {
095: return data;
096: }
097:
098: /**
099: * DOCUMENT ME!
100: *
101: * @param fileName
102: */
103: public void writeToFile(String fileName) {
104: try {
105: FileWriter bwOut = new FileWriter(fileName);
106:
107: //write headers
108: for (int i = 0; i < headers.length; i++) {
109: bwOut.write(createCSVField(headers[i]));
110:
111: if (i != (headers.length - 1)) {
112: bwOut.write(",");
113: }
114: }
115:
116: bwOut.write("\n");
117:
118: //write data
119: for (int i = 0; i < data.size(); i++) {
120: String[] dataArray = (String[]) data.get(i);
121:
122: for (int j = 0; j < dataArray.length; j++) {
123: bwOut.write(createCSVField(dataArray[j]));
124:
125: if (j != (dataArray.length - 1)) {
126: bwOut.write(",");
127: }
128: }
129:
130: bwOut.write("\n");
131: }
132:
133: bwOut.close();
134: } catch (IOException e) {
135: e.printStackTrace();
136: }
137: }
138:
139: /**
140: * DOCUMENT ME!
141: *
142: * @param in
143: *
144: * @return
145: */
146: public String[] breakCSVStringApart(String in) {
147: StringBuffer curString = new StringBuffer();
148: List strings = new ArrayList();
149: boolean escaped = false;
150: boolean inquotedstring = false;
151:
152: for (int i = 0; i < in.length(); i++) {
153: char c = in.charAt(i);
154:
155: switch (c) {
156: case ',':
157:
158: if (inquotedstring) {
159: curString.append(',');
160: } else {
161: strings.add(curString.toString().trim());
162: curString = new StringBuffer();
163: }
164:
165: case ' ':
166:
167: // end word
168: //if (inquotedstring) {
169: curString.append(' ');
170:
171: //}
172: break;
173:
174: case '\t':
175:
176: // end word
177: if (inquotedstring) {
178: curString.append('\t');
179: }
180:
181: break;
182:
183: case '"':
184:
185: if (escaped) {
186: curString.append('"');
187: escaped = false;
188: } else if (inquotedstring) {
189: inquotedstring = false;
190:
191: //strings.add(curString.toString());
192: //curString = new StringBuffer();
193: } else {
194: inquotedstring = true;
195: }
196:
197: break;
198:
199: case '\\':
200:
201: if (escaped) {
202: curString.append("\\");
203: escaped = false;
204: } else {
205: escaped = true;
206: }
207:
208: break;
209:
210: default:
211:
212: if (escaped) {
213: switch (c) {
214: case 'n':
215: curString.append('\n');
216:
217: break;
218:
219: case 't':
220: curString.append('\t');
221:
222: break;
223:
224: case 'r':
225: curString.append('\r');
226:
227: break;
228:
229: default:
230: break;
231: }
232:
233: escaped = false;
234: } else {
235: curString.append(c);
236: }
237: }
238: }
239:
240: if (curString.length() > 0) {
241: strings.add(curString.toString().trim());
242: }
243:
244: return (String[]) strings.toArray(new String[0]);
245: }
246:
247: /**
248: * DOCUMENT ME!
249: *
250: * @param in
251: *
252: * @return
253: */
254: public static String createCSVField(String in) {
255: StringBuffer curString = new StringBuffer();
256: boolean needsQuotes = false;
257:
258: for (int i = 0; i < in.length(); i++) {
259: char c = in.charAt(i);
260:
261: switch (c) {
262: case '\n':
263: curString.append("\\n");
264:
265: break;
266:
267: case '\t':
268: curString.append("\\t");
269:
270: break;
271:
272: case '\r':
273: curString.append("\\r");
274:
275: break;
276:
277: case ',':
278: curString.append(",");
279: needsQuotes = true;
280:
281: break;
282:
283: case '\\':
284: curString.append("\\\\");
285:
286: break;
287:
288: case '"':
289: curString.append("\\\"");
290:
291: break;
292:
293: default:
294: curString.append(c);
295:
296: break;
297: }
298: }
299:
300: if (needsQuotes) {
301: return "\"" + curString.toString() + "\"";
302: } else {
303: return curString.toString();
304: }
305: }
306: }
|