Class CSVDataExporter
- java.lang.Object
-
- org.apache.wicket.extensions.markup.html.repeater.data.table.export.AbstractDataExporter
-
- org.apache.wicket.extensions.markup.html.repeater.data.table.export.CSVDataExporter
-
- All Implemented Interfaces:
Serializable,IDataExporter,org.apache.wicket.util.io.IClusterable
public class CSVDataExporter extends AbstractDataExporter
AnIDataExporterthat exports data to a CSV file. This class allows for customization of the exact CSV format, including setting the delimiter, the text quoting character and the character set.This class will export CSV files in a format consistent with RFC4180 by default.
- Author:
- Jesse Long
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description CSVDataExporter()Creates a new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <T> voidexportData(org.apache.wicket.markup.repeater.data.IDataProvider<T> dataProvider, List<IExportableColumn<T,?>> columns, OutputStream outputStream)Exports the data provided by theIDataProviderto theOutputStream.StringgetCharacterSet()Returns the character set encoding to be used when exporting data.StringgetContentType()Returns the content type of the exported data.protected org.apache.wicket.IConverterLocatorgetConverterLocator()Get the locator of converters.chargetDelimiter()Returns the delimiter to be used for separating fields.chargetQuoteCharacter()Returns the character to be used for quoting fields.booleanisExportHeadersEnabled()Indicates if header exporting is enabled.protected StringquoteValue(String value)Quotes a value for export to CSV.CSVDataExportersetCharacterSet(String characterSet)Sets the character set encoding to be used when exporting data.CSVDataExportersetDelimiter(char delimiter)Sets the delimiter to be used to separate fields.CSVDataExportersetExportHeadersEnabled(boolean exportHeadersEnabled)Turns on or off export headers functionality.CSVDataExportersetQuoteCharacter(char quoteCharacter)Sets the character to be used to quote fields.protected <T> org.apache.wicket.model.IModel<T>wrapModel(org.apache.wicket.model.IModel<T> model)Wrap the given model--
Methods inherited from class org.apache.wicket.extensions.markup.html.repeater.data.table.export.AbstractDataExporter
getDataFormatNameModel, getFileNameExtension, setContentType, setDataFormatNameModel, setFileNameExtension
-
-
-
-
Constructor Detail
-
CSVDataExporter
public CSVDataExporter()
Creates a new instance.
-
-
Method Detail
-
setDelimiter
public CSVDataExporter setDelimiter(char delimiter)
Sets the delimiter to be used to separate fields. The default delimiter is a colon.- Parameters:
delimiter- The delimiter to be used to separate fields.- Returns:
this, for chaining.
-
getDelimiter
public char getDelimiter()
Returns the delimiter to be used for separating fields.- Returns:
- the delimiter to be used for separating fields.
-
getCharacterSet
public String getCharacterSet()
Returns the character set encoding to be used when exporting data.- Returns:
- the character set encoding to be used when exporting data.
-
setCharacterSet
public CSVDataExporter setCharacterSet(String characterSet)
Sets the character set encoding to be used when exporting data. This defaults to UTF-8.- Parameters:
characterSet- The character set encoding to be used when exporting data.- Returns:
this, for chaining.
-
getQuoteCharacter
public char getQuoteCharacter()
Returns the character to be used for quoting fields.- Returns:
- the character to be used for quoting fields.
-
setQuoteCharacter
public CSVDataExporter setQuoteCharacter(char quoteCharacter)
Sets the character to be used to quote fields. This defaults to double quotes,- Parameters:
quoteCharacter- The character to be used to quote fields.- Returns:
this, for chaining.
-
getContentType
public String getContentType()
Returns the content type of the exported data. For CSV, this is normally "text/csv". This methods adds the character set and header values, in accordance with RFC4180.- Specified by:
getContentTypein interfaceIDataExporter- Overrides:
getContentTypein classAbstractDataExporter- Returns:
- the content type of the exported data.
-
setExportHeadersEnabled
public CSVDataExporter setExportHeadersEnabled(boolean exportHeadersEnabled)
Turns on or off export headers functionality. If this is set totrue, then the first line of the export will contain the column headers. This defaults totrue.- Parameters:
exportHeadersEnabled- A boolean indicating whether or not headers should be exported.- Returns:
this, for chaining.
-
isExportHeadersEnabled
public boolean isExportHeadersEnabled()
Indicates if header exporting is enabled. Defaults totrue.- Returns:
- a boolean indicating if header exporting is enabled.
-
quoteValue
protected String quoteValue(String value)
Quotes a value for export to CSV. According to RFC4180, this should just duplicate all occurrences of the quote character and wrap the result in the quote character.- Parameters:
value- The value to be quoted.- Returns:
- a quoted copy of the value.
-
exportData
public <T> void exportData(org.apache.wicket.markup.repeater.data.IDataProvider<T> dataProvider, List<IExportableColumn<T,?>> columns, OutputStream outputStream) throws IOException
Description copied from interface:IDataExporterExports the data provided by theIDataProviderto theOutputStream.- Type Parameters:
T- The type of each row of data provided by theIDataProvider.- Parameters:
dataProvider- TheIDataProviderfrom which to retrieve the data.columns- TheIExportableColumnto use to describe the data.outputStream- TheOutputStreamto which to write the exported data.- Throws:
IOException- If an error occurs.
-
getConverterLocator
protected org.apache.wicket.IConverterLocator getConverterLocator()
Get the locator of converters.- Returns:
- locator
-
wrapModel
protected <T> org.apache.wicket.model.IModel<T> wrapModel(org.apache.wicket.model.IModel<T> model)
Wrap the given model-- Parameters:
model-- Returns:
-
-