001: /* ====================================================================
002: * Copyright (c) 1998 - 2004 David F. Glasser. All rights
003: * reserved.
004: *
005: * This file is part of the QueryForm Database Tool.
006: *
007: * The QueryForm Database Tool is free software; you can redistribute it
008: * and/or modify it under the terms of the GNU General Public License as
009: * published by the Free Software Foundation; either version 2 of the
010: * License, or (at your option) any later version.
011: *
012: * The QueryForm Database Tool is distributed in the hope that it will
013: * be useful, but WITHOUT ANY WARRANTY; without even the implied
014: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
015: * See the GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with the QueryForm Database Tool; if not, write to:
019: *
020: * The Free Software Foundation, Inc.,
021: * 59 Temple Place, Suite 330
022: * Boston, MA 02111-1307 USA
023: *
024: * or visit http://www.gnu.org.
025: *
026: * ====================================================================
027: *
028: * This product includes software developed by the
029: * Apache Software Foundation (http://www.apache.org/).
030: *
031: * ====================================================================
032: */
033: package org.glasser.qform;
034:
035: import javax.swing.*;
036: import org.glasser.util.*;
037: import org.glasser.sql.*;
038: import org.glasser.swing.*;
039: import java.awt.event.*;
040: import java.awt.*;
041: import javax.swing.border.*;
042: import javax.swing.event.*;
043: import java.text.SimpleDateFormat;
044:
045: public class CsvExportPanel extends ExportPanel {
046:
047: private static final Formatter numFormatter = new Formatter() {
048:
049: public String getFormattedString(Object obj) {
050: if (obj == null)
051: return "";
052: else
053: return obj.toString();
054: }
055:
056: public String toString() {
057: return "<Copy Value>";
058: }
059: };
060:
061: private static final Formatter nullFormatter = new Formatter() {
062:
063: public String getFormattedString(Object obj) {
064: return "";
065: }
066:
067: public String toString() {
068: return "<Empty String>";
069: }
070: };
071:
072: private static class TimestampFormatter implements Formatter {
073:
074: public final static int TIME_ESCAPE = 0;
075:
076: public final static int DATE_ESCAPE = 1;
077:
078: public final static int TIMESTAMP_ESCAPE = 2;
079:
080: public final static int TIME_STRING = 3;
081:
082: public final static int DATE_STRING = 4;
083:
084: public final static int TIMESTAMP_STRING = 5;
085:
086: private int formatStyle = TIMESTAMP_STRING;
087:
088: private final static SimpleDateFormat TIME_FORMATTER = new SimpleDateFormat(
089: "HH:mm:ss");
090:
091: private final static SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat(
092: "yyyy-MM-dd");
093:
094: private final static SimpleDateFormat TIMESTAMP_FORMATTER = new SimpleDateFormat(
095: "yyyy-MM-dd HH:mm:ss");
096:
097: public TimestampFormatter() {
098: }
099:
100: public TimestampFormatter(int formatStyle) {
101: this .formatStyle = formatStyle;
102: }
103:
104: public String getFormattedString(Object obj) {
105: if (obj == null)
106: return "NULL";
107:
108: java.util.Date date = (java.util.Date) obj;
109:
110: switch (formatStyle) {
111: case TIME_ESCAPE:
112: return "{t '" + TIME_FORMATTER.format(date) + "'}";
113: case DATE_ESCAPE:
114: return "{d '" + DATE_FORMATTER.format(date) + "'}";
115: case TIMESTAMP_ESCAPE:
116: return "{ts '" + TIMESTAMP_FORMATTER.format(date)
117: + "'}";
118: case TIME_STRING:
119: return "'" + TIME_FORMATTER.format(date) + "'";
120: case DATE_STRING:
121: return "'" + DATE_FORMATTER.format(date) + "'";
122: default:
123: return "'" + TIMESTAMP_FORMATTER.format(date) + "'";
124:
125: }
126: }
127:
128: public String toString() {
129:
130: switch (formatStyle) {
131: case TIME_ESCAPE:
132: return "{t 'hh:mm:ss'}";
133: case DATE_ESCAPE:
134: return "{d 'YYYY-MM-DD'}";
135: case TIMESTAMP_ESCAPE:
136: return "{ts 'YYYY-MM-DD hh:mm:ss'}";
137: case TIME_STRING:
138: return "'hh:mm:ss'";
139: case DATE_STRING:
140: return "'YYYY-MM-DD'";
141: default: // TIMESTAMP_STRING
142: return "'YYYY-MM-DD hh:mm:ss'";
143:
144: }
145: }
146:
147: }
148:
149: /**
150: * This escapes a field for an Excel-style CSV file.
151: */
152: private static class CsvFormatter implements Formatter {
153:
154: public String getFormattedString(Object obj) {
155:
156: if (obj == null)
157: return "";
158: String val = (String) obj;
159: val = val.trim();
160:
161: // First, scan the string for characters that require escaping.
162: // This way we can avoid creating the StringBuffer object if we don't need to.
163: char[] chars = val.toCharArray();
164: boolean needsEscaping = false;
165: for (int j = 0; j < chars.length; j++) {
166: if (chars[j] == ',' || chars[j] == '\n'
167: || chars[j] == '"') {
168: needsEscaping = true;
169: break;
170: }
171: }
172:
173: // if no bad characters were found, return the orginal value
174: if (needsEscaping == false)
175: return val;
176:
177: // otherwise, the string needs escaping
178: StringBuffer buffer = new StringBuffer(val.length() + 20);
179: buffer.append('"');
180: for (int j = 0; j < chars.length; j++) {
181: if (chars[j] == '"') {
182: buffer.append('"');
183: } else if (chars[j] == '\r' && chars.length > j + 1
184: && chars[j + 1] == '\n') {
185: // "\r\n" is normalized to '\n', so skip past the '\r'.
186: j++;
187: }
188:
189: buffer.append(chars[j]);
190: }
191:
192: buffer.append('"');
193:
194: return buffer.toString();
195: }
196:
197: public String toString() {
198: return "<Copy Value>";
199: }
200:
201: }
202:
203: private static CsvFormatter charFormatter = new CsvFormatter();
204:
205: private static final Object[] charChoices = { "", charFormatter,
206: nullFormatter };
207:
208: private static final Object[] numChoices = { "", numFormatter,
209: nullFormatter };
210:
211: private static final Object[] binChoices = { "", nullFormatter };
212:
213: private static final Object[] dateTimeChoices = {
214: "",
215: nullFormatter,
216: new TimestampFormatter(TimestampFormatter.TIMESTAMP_STRING),
217: new TimestampFormatter(TimestampFormatter.DATE_STRING),
218: new TimestampFormatter(TimestampFormatter.TIME_STRING),
219: new TimestampFormatter(TimestampFormatter.TIMESTAMP_ESCAPE),
220: new TimestampFormatter(TimestampFormatter.DATE_ESCAPE),
221: new TimestampFormatter(TimestampFormatter.TIME_ESCAPE) };
222:
223: public CsvExportPanel(TableInfo ti) {
224: super (ti);
225: }
226:
227: protected Object[] getFormatterChoices(int type) {
228: if (DBUtil.isCharType(type)) {
229: return charChoices;
230: } else if (DBUtil.isNumericType(type)) {
231: return numChoices;
232: } else if (DBUtil.isDateTimeType(type)) {
233: return dateTimeChoices;
234: } else {
235: return binChoices;
236: }
237:
238: }
239:
240: }
|