r2307 - trunk/nuiton-csv/src/main/java/org/nuiton/util/csv
Author: tchemit Date: 2012-03-19 14:36:02 +0100 (Mon, 19 Mar 2012) New Revision: 2307 Url: http://nuiton.org/repositories/revision/nuiton-utils/2307 Log: Evolution #2014: Open Export api Modified: trunk/nuiton-csv/src/main/java/org/nuiton/util/csv/Export.java Modified: trunk/nuiton-csv/src/main/java/org/nuiton/util/csv/Export.java =================================================================== --- trunk/nuiton-csv/src/main/java/org/nuiton/util/csv/Export.java 2012-02-29 13:31:25 UTC (rev 2306) +++ trunk/nuiton-csv/src/main/java/org/nuiton/util/csv/Export.java 2012-03-19 13:36:02 UTC (rev 2307) @@ -32,6 +32,7 @@ import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; +import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; @@ -71,10 +72,19 @@ /** Logger. */ private static final Log log = LogFactory.getLog(Export.class); - protected ExportModel<E> model; + /** Export model. */ + protected final ExportModel<E> model; + /** Data to export. */ protected Iterable<E> data; + /** + * Cell separator. + * + * @see ExportModel#getSeparator() + */ + protected final String separator; + public static <E> Export<E> newExport(ExportModel<E> model, Iterable<E> data) { return new Export<E>(model, data); @@ -118,12 +128,27 @@ protected Export(ExportModel<E> model, Iterable<E> data) { this.model = model; this.data = data; + separator = String.valueOf(model.getSeparator()); } public void write(Writer writer) throws Exception { - String separator = String.valueOf(model.getSeparator()); - // add headers + // write csv header + writeHeader(writer); + + // obtain export columns + Iterable<ExportableColumn<E, Object>> columns = + model.getColumnsForExport(); + + for (E row : data) { + + // write the row for this data + writeRow(writer, columns, row); + } + } + + protected void writeHeader(Writer writer) throws IOException { + List<String> headerNames = new LinkedList<String>(); for (ExportableColumn<E, ?> column : model.getColumnsForExport()) { headerNames.add(column.getHeaderName()); @@ -137,27 +162,24 @@ log.debug("will export " + ((Collection<E>) data).size() + " lines"); } } + } - Iterable<ExportableColumn<E, Object>> columnsForExport = - model.getColumnsForExport(); - - for (E object : data) { - for (ExportableColumn<E, Object> column : - columnsForExport) { - Object value = column.getValue(object); - String formattedValue = column.formatValue(value); - if (formattedValue == null) { - throw new NullPointerException( - "column for header " + column.getHeaderName() + - " returned a null value." + column.toString()); - } - formattedValue = - StringUtil.escapeCsvValue(formattedValue, separator); - writer.write(formattedValue); - writer.write(separator); + protected void writeRow(Writer writer, + Iterable<ExportableColumn<E, Object>> columns, + E row) throws Exception { + for (ExportableColumn<E, Object> column : columns) { + Object cell = column.getValue(row); + String formattedCell = column.formatValue(cell); + if (formattedCell == null) { + throw new NullPointerException( + "column for header " + column.getHeaderName() + + " returned a null value." + column.toString()); } - writer.write('\n'); + formattedCell = StringUtil.escapeCsvValue(formattedCell, separator); + writer.write(formattedCell); + writer.write(separator); } + writer.write('\n'); } public void write(OutputStream outputStream, Charset charset) throws Exception { @@ -194,25 +216,19 @@ return result; } - /** - * @deprecated since 2.4.3, use {@link #write(java.io.File)} instead. - */ + /** @deprecated since 2.4.3, use {@link #write(java.io.File)} instead. */ @Deprecated public void exportToFile(File file) throws Exception { write(file); } - /** - * @deprecated since 2.4.3, use {@link #write(java.io.Writer)} instead. - */ + /** @deprecated since 2.4.3, use {@link #write(java.io.Writer)} instead. */ @Deprecated public void startExport(Writer writer) throws Exception { write(writer); } - /** - * @deprecated since 2.4.3, use {@link #toString(java.nio.charset.Charset)} instead. - */ + /** @deprecated since 2.4.3, use {@link #toString(java.nio.charset.Charset)} instead. */ @Deprecated public String startExportAsString() throws Exception { return toString(Charset.defaultCharset()); @@ -220,8 +236,8 @@ /** * @deprecated since 2.4.3. It's not the role of the API to give an InputStream - * you can use {@link #toString(java.nio.charset.Charset)} and - * {@link org.apache.commons.io.IOUtils#toInputStream(String)} + * you can use {@link #toString(java.nio.charset.Charset)} and + * {@link org.apache.commons.io.IOUtils#toInputStream(String)} */ @Deprecated public InputStream startExport() throws Exception {
participants (1)
-
tchemit@users.nuiton.org