001    package net.sourceforge.mp3dings.export;
002    
003    /* Mp3dings - manage mp3 meta-information
004     * Copyright (C) 2003 Moritz Ringler
005     * $Id: WinampXMLPlaylistExporter.java 115 2007-11-05 23:46:25Z 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 a WinampXML playlist (.b4s).
027     * @author Moritz Ringler
028     * @version $Revision: 115 $ ($Date: 2007-11-06 00:46:25 +0100 (Di, 06 Nov 2007) $)
029     */
030    public class WinampXMLPlaylistExporter extends AbstractExporter implements Serializable{
031        static final long serialVersionUID = -6317898388247842582L;
032    
033        private String xmldecl ="<?xml version=\"1.0\" " +
034                "encoding='UTF-8' standalone=\"yes\"?>";
035        private String doctag = "WinampXML";
036        private String generator = "Generated by: Mp3dings";
037        private String indent = "";
038        private String tab = "    ";
039    
040        /** Creates a new WinampXMLPlaylistExporter. **/
041        public WinampXMLPlaylistExporter(){
042            super();
043            setKeys(null,
044                    new Field[]{
045                        Field.FULLPATH,
046                        Field.ARTIST,
047                        Field.FILENAME,
048                        Field.TITLE,
049                        Field.LENGTH
050                    },
051                    null);
052            setEncoding("UTF-8");
053        }
054    
055        /**Returns a single <code>&lt;entry&gt;</code> tag. The output looks as
056         * follows:
057         * <pre>
058         *  &lt;entry Playstring="[filepath]"&gt;
059         *      &lt;Name&gt;[artist] - [title]&lt;/Name&gt;
060         *      &lt;Length&gt;[length in ms]&lt;/Length&gt;
061         *  &lt;/entry&gt;
062         * </pre>
063         * @param fileIndex ignored
064        * @param blockdata a map containing the record data
065         */
066        public String getBlock(int fileIndex, Map blockdata){
067            /* format file path */
068            String x = "file:" + (String) blockdata.get(Field.FULLPATH);
069            x = escape(x);
070            if (x == null){
071                return null;
072            }
073            StringBuffer sb = new StringBuffer(indent);
074            sb.append("<entry Playstring=\"").append(x).append("\">\n");
075            /* format artist and title */
076            x = (String) blockdata.get(Field.ARTIST) + " - " + blockdata.get(Field.TITLE);
077            x = escape(x);
078            if (x != null){
079                sb.append(indent).append(tab);
080                sb.append("<Name>").append(x).append("</Name>\n");
081            }
082            /* format length */
083            Number length = (Number) blockdata.get(Field.LENGTH);
084            x = (length == null)? "" : ""+(length.longValue() * 1000);
085            x = escape(x);
086            if (x != null){
087                sb.append(indent).append(tab);
088                sb.append("<Length>").append(x).append("</Length>\n");
089            }
090            sb.append(indent).append("</entry>\n");
091            return sb.toString();
092        }
093    
094        /**Returns the xml declaration, the opening <code>&lt;WinampXML&gt;</code>
095         * doctag and the opening <code>&lt;playlist&gt;</code> tag. The output
096         * looks as follows:
097         * <pre>
098         *  &lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
099         *  &lt;WinampXML&gt;
100         *          &lt;playlist
101         *              num_entries="[number of datasets]"
102         *              label="Mp3dings Playlist"&gt;
103         * </pre>
104         */
105        public String getHeader(){
106            StringBuffer sb = new StringBuffer(xmldecl);
107            sb.append("\n");
108            sb.append("<").append(doctag).append(">\n");
109            indent += tab;
110            sb.append(indent).append("<!--").append(generator).append("-->\n");
111            sb.append(indent).append("<playlist num_entries=\"");
112            sb.append(getNumberOfEntries()).append("\" ");
113            sb.append("label=\"Mp3dings Playlist\">\n");
114            indent += tab;
115            return sb.toString();
116        }
117    
118        /**Returns the closing &lt;WinampXML&gt; doctag and the
119         * closing &lt;playlist&gt; tag. The output looks as follows:
120         * <pre>
121         *      &lt;/playlist&gt;
122         *  &lt;/WinampXML&gt;
123         * </pre>
124         * @param numEntries ignored
125         */
126        public String getFooter(int numEntries){
127            StringBuffer sb = new StringBuffer(tab);
128            sb.append("</playlist>\n");
129            sb.append("</").append(doctag).append(">\n");
130            return sb.toString();
131        }
132    
133        /** Returns a string description of this exporter's target file type.
134        * @return the name of the target file type. Currently this is
135        * "Winamp XML Playlist (*.b4s)", but this may change in future
136        * versions.
137        */
138        public String getName(){
139            return "Winamp XML Playlist (*.b4s)";
140        }
141    
142        /** Returns the file extension for this exporter's target file type.
143        * @return a file extension including the leading dot. Currently this is
144        * ".b4s", but this may change in future versions.
145        */
146        public String getExtension(){
147            return ".b4s";
148        }
149    
150        /** Escapes XML special characters and non-ascii characters in the
151        * specified string. */
152        public static String escape(String s){
153            StringBuffer buf = new StringBuffer();
154            int i;
155            for (i=0; i<s.length(); ++i) {
156                char ch = s.charAt(i);
157                int chi = (int) ch;
158                switch (chi){
159                    case 34: buf.append("&quot;"); break;
160                    case 38: buf.append("&amp;");break;
161                    case 60: buf.append("&lt;");break;
162                    case 62: buf.append("&gt;");break;
163                    default:
164                    if(chi > 128){
165                        buf.append("&#").append(chi).append(";");
166                    } else {
167                        buf.append(ch);
168                    }
169                }
170            }
171            return buf.toString();
172       }
173    }