KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > displaytag > export > CsvView


1 /**
2  * Licensed under the Artistic License; you may not use this file
3  * except in compliance with the License.
4  * You may obtain a copy of the License at
5  *
6  * http://displaytag.sourceforge.net/license.html
7  *
8  * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
9  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
10  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11  */

12 package org.displaytag.export;
13
14 import org.apache.commons.lang.StringUtils;
15 import org.displaytag.model.TableModel;
16
17
18 /**
19  * Export view for comma separated value exporting.
20  * @author Fabrizio Giustina
21  * @version $Revision: 934 $ ($Author: fgiust $)
22  */

23 public class CsvView extends BaseExportView
24 {
25
26     /**
27      * @see org.displaytag.export.BaseExportView#setParameters(TableModel, boolean, boolean, boolean)
28      */

29     public void setParameters(TableModel tableModel, boolean exportFullList, boolean includeHeader,
30         boolean decorateValues)
31     {
32         super.setParameters(tableModel, exportFullList, includeHeader, decorateValues);
33     }
34
35     /**
36      * @see org.displaytag.export.BaseExportView#getRowEnd()
37      */

38     protected String JavaDoc getRowEnd()
39     {
40         return "\n"; //$NON-NLS-1$
41
}
42
43     /**
44      * @see org.displaytag.export.BaseExportView#getCellEnd()
45      */

46     protected String JavaDoc getCellEnd()
47     {
48         return ","; //$NON-NLS-1$
49
}
50
51     /**
52      * @see org.displaytag.export.BaseExportView#getAlwaysAppendCellEnd()
53      */

54     protected boolean getAlwaysAppendCellEnd()
55     {
56         return false;
57     }
58
59     /**
60      * @see org.displaytag.export.BaseExportView#getAlwaysAppendRowEnd()
61      */

62     protected boolean getAlwaysAppendRowEnd()
63     {
64         return true;
65     }
66
67     /**
68      * @see org.displaytag.export.ExportView#getMimeType()
69      */

70     public String JavaDoc getMimeType()
71     {
72         return "text/csv"; //$NON-NLS-1$
73
}
74
75     /**
76      * Escaping for csv format.
77      * <ul>
78      * <li>Quotes inside quoted strings are escaped with a /</li>
79      * <li>Fields containings newlines or , are surrounded by "</li>
80      * </ul>
81      * Note this is the standard CVS format and it's not handled well by excel.
82      * @see org.displaytag.export.BaseExportView#escapeColumnValue(java.lang.Object)
83      */

84     protected String JavaDoc escapeColumnValue(Object JavaDoc value)
85     {
86         String JavaDoc stringValue = StringUtils.trim(value.toString());
87         if (!StringUtils.containsNone(stringValue, new char[]{'\n', ','}))
88         {
89             return "\"" + //$NON-NLS-1$
90
StringUtils.replace(stringValue, "\"", "\\\"") + "\""; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
91
}
92
93         return stringValue;
94     }
95
96 }
97
Popular Tags