001/* 002 * Copyright (c) 2005 Einar Pehrson <einar@pehrson.nu>. 003 * 004 * This file is part of 005 * CleanSheets - a spreadsheet application for the Java platform. 006 * 007 * CleanSheets is free software; you can redistribute it and/or modify 008 * it under the terms of the GNU General Public License as published by 009 * the Free Software Foundation; either version 2 of the License, or 010 * (at your option) any later version. 011 * 012 * CleanSheets is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 015 * GNU General Public License for more details. 016 * 017 * You should have received a copy of the GNU General Public License 018 * along with CleanSheets; if not, write to the Free Software 019 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 020 */ 021package csheets.io; 022 023import java.io.BufferedReader; 024import java.io.BufferedWriter; 025import java.io.IOException; 026import java.io.InputStream; 027import java.io.InputStreamReader; 028import java.io.OutputStream; 029import java.io.OutputStreamWriter; 030import java.io.PrintWriter; 031import java.io.Reader; 032import java.util.LinkedList; 033import java.util.List; 034 035import csheets.core.Spreadsheet; 036import csheets.core.Workbook; 037 038/** 039 * A codec for comma-separated files. 040 * @author Einar Pehrson 041 */ 042public class CSVCodec implements Codec { 043 044 /** The string used to separate the content of different cells */ 045 public static final String SEPARATOR = ";"; 046 047 /** 048 * Creates a new CSV codec. 049 */ 050 public CSVCodec() {} 051 052 public Workbook read(InputStream stream) throws IOException { 053 // Wraps stream 054 Reader streamReader = new InputStreamReader(stream); 055 BufferedReader reader = new BufferedReader(streamReader); 056 057 // Reads content of rows 058 String line; 059 int columns = 0; 060 List<String[]> rows = new LinkedList<String[]>(); 061 while ((line = reader.readLine()) != null) { 062 String[] row = line.split(SEPARATOR); 063 rows.add(row); 064 if (row.length > columns) 065 columns = row.length; 066 } 067 068 // Builds content matrix 069 String[][] content = new String[rows.size()][columns]; 070 int i = 0; 071 for (String[] row : rows) 072 content[i++] = row; 073 074 // Frees resources 075 reader.close(); 076 streamReader.close(); 077 stream.close(); 078 079 return new Workbook(content); 080 } 081 082 public void write(Workbook workbook, OutputStream stream) throws IOException { 083 System.out.println("Writing!"); 084 // Wraps stream 085 PrintWriter writer = new PrintWriter(new BufferedWriter( 086 new OutputStreamWriter(stream))); 087 088 // Writes content of rows 089 Spreadsheet sheet = workbook.getSpreadsheet(0); 090 for (int row = 0; row < sheet.getRowCount(); row++) { 091 for (int column = 0; column < sheet.getColumnCount(); column++) 092 if (column + 1 < sheet.getColumnCount()) 093 writer.print(sheet.getCell(column, row).getContent() 094 + SEPARATOR); 095 if (row + 1 < sheet.getRowCount()) 096 writer.println(); 097 } 098 099 100 // Frees resources 101 writer.close(); 102 stream.close(); 103 System.out.println("Done!"); 104 } 105}