001: /*
002: * $Header: /cvsroot/mvnforum/myvietnam/src/net/myvietnam/mvncore/util/CSVUtil.java,v 1.2 2007/10/02 09:39:19 phuongpdd Exp $
003: * $Author: phuongpdd $
004: * $Revision: 1.2 $
005: * $Date: 2007/10/02 09:39:19 $
006: *
007: * ====================================================================
008: *
009: * Copyright (C) 2002-2007 by MyVietnam.net
010: *
011: * All copyright notices regarding MyVietnam and MyVietnam CoreLib
012: * MUST remain intact in the scripts and source code.
013: *
014: * This library is free software; you can redistribute it and/or
015: * modify it under the terms of the GNU Lesser General Public
016: * License as published by the Free Software Foundation; either
017: * version 2.1 of the License, or (at your option) any later version.
018: *
019: * This library is distributed in the hope that it will be useful,
020: * but WITHOUT ANY WARRANTY; without even the implied warranty of
021: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
022: * Lesser General Public License for more details.
023: *
024: * You should have received a copy of the GNU Lesser General Public
025: * License along with this library; if not, write to the Free Software
026: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
027: *
028: * Correspondence and Marketing Questions can be sent to:
029: * info at MyVietnam net
030: *
031: * @author: MyVietnam.net developers
032: */
033: package net.myvietnam.mvncore.util;
034:
035: import java.io.*;
036:
037: public class CSVUtil {
038:
039: protected Writer out;
040: protected boolean autoFlush = true;
041: protected boolean error = false;
042: protected char delimiterChar = ',';
043: protected char quoteChar = '"';
044: protected boolean newLine = true;
045: protected char commentStart = '#';
046:
047: public CSVUtil(OutputStream out) {
048: this .out = new OutputStreamWriter(out);
049: }
050:
051: private String escapeAndQuote(String value) {
052: int count = 2;
053: for (int i = 0; i < value.length(); i++) {
054: char c = value.charAt(i);
055: switch (c) {
056: case '\n':
057: case '\r':
058: case '\\': {
059: count++;
060: }
061: break;
062: default: {
063: if (c == quoteChar) {
064: count++;
065: }
066: }
067: break;
068: }
069: }
070: StringBuffer sb = new StringBuffer(value.length() + count);
071: sb.append(quoteChar);
072: for (int i = 0; i < value.length(); i++) {
073: char c = value.charAt(i);
074: switch (c) {
075: case '\n': {
076: sb.append("\\n");
077: }
078: break;
079: case '\r': {
080: sb.append("\\r");
081: }
082: break;
083: case '\\': {
084: sb.append("\\\\");
085: }
086: break;
087: default: {
088: if (c == quoteChar) {
089: sb.append("\\" + quoteChar);
090: } else {
091: sb.append(c);
092: }
093: }
094: }
095: }
096: sb.append(quoteChar);
097: return (sb.toString());
098: }
099:
100: //abc, def
101: public void write(String value) throws IOException {
102: try {
103: if (value == null) {
104: value = "";
105: }
106: boolean quote = false;
107: if (value.length() > 0) {
108: char c = value.charAt(0);
109: if (newLine
110: && (c < '0' || (c > '9' && c < 'A')
111: || (c > 'Z' && c < 'a') || (c > 'z'))) {
112: quote = true;
113: }
114: if (c == ' ' || c == '\f' || c == '\t') {
115: quote = true;
116: }
117: for (int i = 0; i < value.length(); i++) {
118: c = value.charAt(i);
119: if ((c == quoteChar) || (c == delimiterChar)
120: || (c == '\n') || (c == '\r')) {
121: quote = true;
122: }
123: }
124: if ((c == ' ') || (c == '\f') || (c == '\t')) {
125: quote = true;
126: }
127: } else if (newLine) {
128: quote = true;
129: }
130: if (newLine) {
131: newLine = false;
132: } else {
133: out.write(delimiterChar);
134: }
135: if (quote) {
136: out.write(escapeAndQuote(value));
137: } else {
138: out.write(value);
139: }
140: if (autoFlush)
141: flush();
142: } catch (IOException iox) {
143: error = true;
144: throw iox;
145: }
146: }
147:
148: public void writeln(String value) throws IOException {
149: try {
150: write(value);
151: writeln();
152: } catch (IOException iox) {
153: error = true;
154: throw iox;
155: }
156: }
157:
158: public void writeln() throws IOException {
159: try {
160: out.write("\n");
161: if (autoFlush)
162: flush();
163: newLine = true;
164: } catch (IOException iox) {
165: error = true;
166: throw iox;
167: }
168: }
169:
170: public void writeln(String[] values) throws IOException {
171: try {
172: print(values);
173: writeln();
174: } catch (IOException iox) {
175: error = true;
176: throw iox;
177: }
178: }
179:
180: public void write(String[] values) throws IOException {
181: try {
182: for (int i = 0; i < values.length; i++) {
183: write(values[i]);
184: }
185: } catch (IOException iox) {
186: error = true;
187: throw iox;
188: }
189: }
190:
191: public void writeln(String[][] values) throws IOException {
192: try {
193: for (int i = 0; i < values.length; i++) {
194: writeln(values[i]);
195: }
196: if (values.length == 0) {
197: writeln();
198: }
199: } catch (IOException iox) {
200: error = true;
201: throw iox;
202: }
203: }
204:
205: public void writelnComment(String comment) throws IOException {
206: try {
207: if (comment == null)
208: comment = "";
209: if (!newLine) {
210: writeln();
211: }
212: out.write(commentStart);
213: out.write(' ');
214: for (int i = 0; i < comment.length(); i++) {
215: char c = comment.charAt(i);
216: switch (c) {
217: case '\r': {
218: if (i + 1 < comment.length()
219: && comment.charAt(i + 1) == '\n') {
220: i++;
221: }
222: } //break intentionally excluded.
223: case '\n': {
224: writeln();
225: out.write(commentStart);
226: out.write(' ');
227: }
228: break;
229: default: {
230: out.write(c);
231: }
232: break;
233: }
234: }
235: writeln();
236: } catch (IOException iox) {
237: error = true;
238: throw iox;
239: }
240: }
241:
242: public void println(String value) {
243: try {
244: writeln(value);
245: } catch (IOException iox) {
246: error = true;
247: }
248: }
249:
250: public void println() {
251: try {
252: writeln();
253: } catch (IOException iox) {
254: error = true;
255: }
256: }
257:
258: public void println(String[] values) {
259: try {
260: writeln(values);
261: } catch (IOException iox) {
262: error = true;
263: }
264: }
265:
266: public void print(String[] values) {
267: try {
268: write(values);
269: } catch (IOException iox) {
270: error = true;
271: }
272: }
273:
274: public void println(String[][] values) {
275: try {
276: writeln(values);
277: } catch (IOException iox) {
278: error = true;
279: }
280: }
281:
282: public void printlnComment(String comment) {
283: try {
284: writelnComment(comment);
285: } catch (IOException iox) {
286: error = true;
287: }
288: }
289:
290: public void print(String value) {
291: try {
292: write(value);
293: } catch (IOException iox) {
294: error = true;
295: }
296: }
297:
298: public void flush() throws IOException {
299: out.flush();
300: }
301:
302: public boolean checkError() {
303: try {
304: if (error) {
305: return true;
306: }
307: flush();
308: if (error) {
309: return true;
310: }
311: if (out instanceof PrintWriter) {
312: error = ((PrintWriter) out).checkError();
313: }
314: } catch (IOException iox) {
315: error = true;
316: }
317: return error;
318: }
319: }
|