001: /**********************************************************************************
002: *
003: * $Id: SpreadsheetDataFileWriterCsv.java 22226 2007-03-06 17:42:53Z ray@media.berkeley.edu $
004: *
005: ***********************************************************************************
006: *
007: * Copyright (c) 2007 The Regents of the University of California
008: *
009: * Licensed under the Educational Community License, Version 1.0 (the "License");
010: * you may not use this file except in compliance with the License.
011: * You may obtain a copy of the License at
012: *
013: * http://www.opensource.org/licenses/ecl1.php
014: *
015: * Unless required by applicable law or agreed to in writing, software
016: * distributed under the License is distributed on an "AS IS" BASIS,
017: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018: * See the License for the specific language governing permissions and
019: * limitations under the License.
020: *
021: **********************************************************************************/package org.sakaiproject.jsf.spreadsheet;
022:
023: import java.io.IOException;
024: import java.io.OutputStream;
025: import java.util.Iterator;
026: import java.util.List;
027:
028: import javax.servlet.http.HttpServletResponse;
029:
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032:
033: /**
034: * NOTE: CSV export capabilities are extremely limited! UTF-16 text (such as
035: * Chinese) is not supported correctly, for example. Use Excel-formatted output if at all
036: * possible.
037: */
038: public class SpreadsheetDataFileWriterCsv implements
039: SpreadsheetDataFileWriter {
040: private static final Log log = LogFactory
041: .getLog(SpreadsheetDataFileWriterCsv.class);
042:
043: public void writeDataToResponse(List<List<Object>> spreadsheetData,
044: String fileName, HttpServletResponse response) {
045: response.setContentType("text/comma-separated-values");
046: response.setHeader("Content-disposition",
047: "attachment; filename=" + fileName + ".csv");
048:
049: String csvString = getAsCsv(spreadsheetData);
050: response.setContentLength(csvString.length());
051: OutputStream out = null;
052: try {
053: out = response.getOutputStream();
054: out.write(csvString.getBytes());
055: out.flush();
056: } catch (IOException e) {
057: if (log.isErrorEnabled())
058: log.error(e);
059: } finally {
060: try {
061: if (out != null)
062: out.close();
063: } catch (IOException e) {
064: if (log.isErrorEnabled())
065: log.error(e);
066: }
067: }
068: }
069:
070: private String getAsCsv(List<List<Object>> spreadsheetData) {
071: String csvSep = ",";
072: StringBuilder sb = new StringBuilder();
073:
074: for (List<Object> rowData : spreadsheetData) {
075: Iterator<Object> dataIter = rowData.iterator();
076: while (dataIter.hasNext()) {
077: Object data = dataIter.next();
078: if (data != null) {
079: if (data instanceof String) {
080: appendQuoted(sb, (String) data);
081: } else {
082: sb.append(data);
083: }
084: }
085: if (dataIter.hasNext()) {
086: sb.append(csvSep);
087: } else {
088: sb.append("\n");
089: }
090: }
091: }
092:
093: return sb.toString();
094: }
095:
096: private StringBuilder appendQuoted(StringBuilder sb, String toQuote) {
097: if ((toQuote.indexOf(',') >= 0) || (toQuote.indexOf('"') >= 0)) {
098: String out = toQuote.replaceAll("\"", "\"\"");
099: if (log.isDebugEnabled())
100: log.debug("Turning '" + toQuote + "' to '" + out + "'");
101: sb.append("\"").append(out).append("\"");
102: } else {
103: sb.append(toQuote);
104: }
105: return sb;
106: }
107:
108: }
|