001    package net.sourceforge.mp3dings.export;
002    
003    /* Mp3dings - manage mp3 meta-information
004    * Copyright (C) 2003 Moritz Ringler
005    * $Id: CSVExporter.java 13 2007-07-27 09:52:39Z ringler $
006    *
007    * This program is free software; you can redistribute it and/or
008    * modify it under the terms of the GNU General Public License
009    * as published by the Free Software Foundation; either version 2
010    * of the License, or (at your option) any later version.
011    *
012    * This program 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 this program; if not, write to the Free Software
019    * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
020    */
021    
022    import java.util.Map;
023    import java.io.Serializable;
024    
025    /**Exports data as a comma separated file (csv, spreadsheet
026    * interchange format).
027    * @author Moritz Ringler
028    * @version $Revision: 13 $ ($Date: 2007-07-27 11:52:39 +0200 (Fr, 27 Jul 2007) $)
029    */
030    public class CSVExporter extends AbstractExporter implements Serializable{
031        static final long serialVersionUID = -7839804949142088844L;
032    
033        private char separator='\t';
034    
035        /** Creates a new csv exporter with the encoding set to ISO-8859-1.*/
036        public CSVExporter(){
037            super();
038            setEncoding("ISO-8859-1");
039            setFileFallback(false);
040        }
041    
042        /** Changes the character that will separate the fields in a record.
043        * If this character occurs in one of the exported values it will be
044        * replaced by a blank ' '.<br>
045        * The default separator is a tab '\t' (ASCII character 9).
046        */
047        public void setSeparator(char delim){
048            separator = delim;
049        }
050    
051        /** Replaces all occurrances of the delimiter in the string s
052        * by a blank ' '. If the delimiter is a blank it will be replaced
053        * by an underscore '_'.
054        * @param s the input string
055        * @return the input string with the delimiter character replaced
056        */
057        private String elimDelim(String s){
058            return (separator == ' ')
059            ? s.replace(separator,'_')
060            : s.replace(separator,' ');
061        }
062    
063        /** Replaces line breaks (both \r\n and \n) in the string s
064        *  by a blank.
065        **/
066        private String elimLineBreak(String s){
067            return s.replaceAll("(?:\r)?\n"," ");
068        }
069    
070        /**Produces a single data record for the exported file. Blockdata will
071        * be merged into a single string separated by the current
072        * {@link #setSeparator(char) separator character}. The order of the
073        * entries is determined
074        * by the order of the keys in the array returned by {@link #getBlockKeys}.
075        * @param fileIndex ignored
076        * @param blockdata the data of a single record as a map. The map will not
077        * be changed by this method, and can be reused for further calls to this
078        * method.
079        * @return a single data record as a single line string including
080        * a final line terminator
081        */
082        public String getBlock(int fileIndex, Map blockdata){
083            StringBuffer sb = new StringBuffer();
084            Object[] keys = getBlockKeys();
085            Object val;
086            for(int i=0; i<keys.length; i++){
087                val = blockdata.get(keys[i]);
088                sb.append((val == null)?"":elimLineBreak(elimDelim(val.toString())));
089                sb.append(separator);
090            }
091            sb.deleteCharAt(sb.length() - 1);
092            return sb.append("\n").toString();
093        }
094    
095        /** Produces a header for the exported csv file consisting of a single
096        * line containing the column names separated by the current separator
097        * character.
098        * @return the single-line csv header including a final line terminator
099        */
100        protected String getHeader(){
101            return getBlock(0, getHeaderData());
102        }
103    
104        /** Produces an empty footer.
105        * @param numEntries ignored
106        * @return an empty string
107        */
108        protected String getFooter(int numEntries){
109            return "";
110        }
111    
112        /** Returns a string description of this exporter's target type.
113        * @return the name of the target type. Currently this is
114        * "Comma separated file (*.csv)", but this may change in future
115        * versions.
116        */
117        public String getName(){
118            return "Comma separated file (*.csv)";
119        }
120    
121        /** Returns the file extension for this exporter's target file type.
122        * @return a file extension including the leading dot. Currently this is
123        * ".csv", but this may change in future versions.
124        */
125        public String getExtension(){
126            return ".csv";
127        }
128    }