001    package net.sourceforge.mp3dings.export;
002    
003    /* Mp3dings - manage mp3 meta-information
004    * Copyright (C) 2003 Moritz Ringler
005    * $Id: M3uExporter.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 net.sourceforge.mp3dings.Field;
024    import java.io.Serializable;
025    
026    /** Exports data as an M3u playlist.
027    * @author Moritz Ringler
028    * @version $Revision: 13 $ ($Date: 2007-07-27 11:52:39 +0200 (Fr, 27 Jul 2007) $)
029    */
030    public class SansaPLAExporter extends AbstractExporter implements Serializable{
031        private final static String CRLF = new String(new char[]{'\r','\n'});
032    
033        /** Creates a new SansaPLAExporter.
034        */
035        public SansaPLAExporter(){
036            super();
037            super.setKeys(null,
038            new Field[]{Field.FULLPATH}, null);
039            setEncoding("UTF-16LE");
040        }
041    
042        /**Returns a line making up one playlist entry.
043        * <pre>
044        *      HARP, path\to\file.mp3
045        * </pre>
046        * @param fileIndex ignored
047        * @param blockdata a map containing the record data
048        */
049        public String getBlock(int fileIndex, Map blockdata){
050            StringBuffer sb = new StringBuffer("HARP, ");
051            Object path = blockdata.get(Field.FULLPATH);
052            if(path != null){
053                sb.append(convertPath(path)).append(CRLF);
054            }
055            return sb.toString();
056        }
057    
058        private static String convertPath(Object path){
059            String result = (String) path;
060            final int len = result.length();
061            //see if path has MUSIC in it, strip everything before MUSIC
062            final int imusic = result.indexOf("MUSIC");
063            if(imusic >= 0){
064                result = result.substring(imusic);
065            //look for windows drive letter (say X), remove X:\
066            } else if (len > 3 &&
067                       result.charAt(1) == ':' &&
068                       result.charAt(2) == '\\')
069            {
070                result = result.substring(3);
071            //look for /dev/something/ and remove it
072            } else if (len > 5 && result.startsWith("/dev/")){
073                final int islash = result.indexOf('/', 5);
074                if(islash > 5 && islash + 1 < len){
075                    result = result.substring(islash + 1);
076                }
077            }
078            //convert slashes to backslashes
079            result = result.replace(java.io.File.separatorChar,'\\');
080            return result;
081        }
082    
083        /** Returns the three CRLF-terminated header lines:
084        * <pre>
085        *      PLP PLAYLIST
086        *      VERSION 1.20
087        *
088        * </pre>
089        */
090        public String getHeader(){
091            return "PLP PLAYLIST\r\nVERSION 1.20\r\n\r\n";
092        }
093    
094        /** Returns an empty string.*/
095        public String getFooter(int numEntries){
096            return "";
097        }
098    
099        /** Returns a string description of this exporter's target file type.
100        * @return the name of the target file type. Currently this is
101        * "M3u playlist (*.m3u)", but this may change in future
102        * versions.
103        */
104        public String getName(){
105            return "Sansa e2## series playlist (*.pla)";
106        }
107    
108        /** Returns the file extension for this exporter's target file type.
109        * @return a file extension including the leading dot. Currently this is
110        * ".pla", but this may change in future versions.
111        */
112        public String getExtension(){
113            return ".pla";
114        }
115    
116        /** Overriden to do nothing. This exporter uses no fields for the header and
117        * footer and only <code>FULLPATH</code> as blockKey.
118        */
119        public void setKeys(Object[] headerKeys, Object[] blockKeys, Object[] footerKeys){
120        }
121    }