001    /**
002     * iFish - An iRiver iHP jukebox database creation tool
003     *
004     * Copyright (C) 2009 Richard "Shred" Körber
005     *   http://ifish.shredzone.org
006     *
007     * This program 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 3 of the License, or
010     * (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, see <http://www.gnu.org/licenses/>.
019     */
020    package net.shredzone.ifish.actions;
021    
022    import java.awt.event.ActionEvent;
023    import java.awt.event.KeyEvent;
024    import java.io.FileWriter;
025    import java.io.IOException;
026    import java.util.HashMap;
027    import java.util.Iterator;
028    import java.util.Map;
029    
030    import javax.swing.JFileChooser;
031    import javax.swing.KeyStroke;
032    
033    import net.shredzone.ifish.IFish;
034    import net.shredzone.ifish.IFishPane;
035    import net.shredzone.ifish.db.Entry;
036    import net.shredzone.ifish.db.Playlist;
037    import net.shredzone.ifish.db.PlaylistDb;
038    import net.shredzone.ifish.gui.ExceptionDialog;
039    import net.shredzone.ifish.gui.NaviDbTableModel;
040    import net.shredzone.ifish.i18n.L;
041    import net.shredzone.ifish.pool.ImgPool;
042    import net.shredzone.jshred.io.XMLWriter;
043    
044    /**
045     * Exports the database into some file, as XML.
046     *
047     * @author  Richard Körber &lt;dev@shredzone.de&gt;
048     * @version $Id: ExportDatabaseAction.java 291 2009-04-28 21:29:27Z shred $
049     */
050    public class ExportDatabaseAction extends IFishAction {
051      private static final long serialVersionUID = 3258126942791348273L;
052    
053      /**
054       * Create a new ExportDatabaseAction.
055       *
056       * @param   fish        Related IFishPane
057       */
058      public ExportDatabaseAction( IFishPane fish ) {
059        super(
060          fish,
061          L.tr("action.db.export"),
062          ImgPool.get("export.png"),
063          L.tr("action.db.export.tt"),
064          KeyStroke.getKeyStroke( KeyEvent.VK_S, ActionEvent.CTRL_MASK|ActionEvent.SHIFT_MASK )
065        );
066      }
067      
068      /**
069       * The action implementation itself. Do not call directly.
070       */
071      @Override
072      protected void action() {
073        //--- Choose a file ---
074        JFileChooser chooser = new JFileChooser();
075        if( JFileChooser.APPROVE_OPTION == chooser.showSaveDialog( fish ) ) {
076          
077          String filename = chooser.getSelectedFile().getAbsolutePath();
078          if(! filename.toLowerCase().endsWith(".xml") )
079            filename += ".xml";
080          
081          XMLWriter xml = null;
082          try {
083            //--- Open the file ---
084            FileWriter fw = new FileWriter( filename );
085            xml = new XMLWriter( fw );
086    
087            //--- Start the document ---
088            xml.startDocument();
089            xml.writeComment( "Generated by iFish, http://www.shredzone.net/go/ifish" );
090    
091            //--- This is an IFish export ---
092            xml.startElement( "ifish", "version", "1" );
093            
094            //--- Basisdaten ---
095            xml.startElement( "info" );
096            xml.startElement( "generator" );
097            xml.writeContent( "iFish v"+net.shredzone.ifish.Version.VERSION );
098            xml.endElement();
099            xml.startElement( "os" );
100            xml.writeContent( System.getProperty("os.name")+" ("+System.getProperty("os.version")+")" );
101            xml.endElement();
102            xml.startElement( "basedir" );
103            xml.writeContent( prefs.getDirectory() );
104            xml.endElement();
105            xml.endElement();
106    
107            //--- Start writing the database ---
108            NaviDbTableModel model = fish.getDatabase();
109            xml.startElement( "ifishdb" );
110            int cnt = model.getRowCount();
111            for( int ix=0; ix<cnt; ix++ ) {
112              Entry entry = model.getEntryAt( ix );
113              xml.startElement( "entry" );
114              
115              xml.startElement( "file" );
116              xml.writeContent( entry.getFileName() );
117              xml.endElement();
118              
119              writeIfFilled( xml, "artist", entry.getArtist() );
120              writeIfFilled( xml, "album", entry.getAlbum() );
121              writeIfFilled( xml, "title", entry.getTitle() );
122              writeIfFilled( xml, "genre", entry.getGenre() );
123    
124              xml.endElement();  // entry
125            }
126            xml.endElement();   // ifishdb
127    
128            //--- Start writing the playlists ---
129            PlaylistDb plDb = fish.getPlaylist();
130            if( plDb.getSize() > 0 ) {
131              xml.startElement( "playlists" );
132              Iterator<Playlist> it = plDb.iterator();
133              while( it.hasNext() ) {
134                Playlist pl = it.next();
135                Map<String, String> args = new HashMap<String, String>();
136                args.put( "name", pl.getName() );
137                if( pl.isStatic() ) args.put( "static", "yes" );
138                xml.startElement( "playlist", args );
139                Iterator<Entry> it2 = pl.getEntries().iterator();
140                while( it2.hasNext() ) {
141                  Entry entry = (Entry) it2.next();
142                  xml.startElement( "file" );
143                  xml.writeContent( entry.getFileName() );
144                  xml.endElement();   // file
145                }
146                xml.endElement();    // playlist
147              }
148              xml.endElement();   // playlists
149            }
150            
151            //--- Close everything ---
152            xml.endElement();   // ifish
153            xml.endDocument();
154          }catch( Exception e ) {
155            ExceptionDialog.show( fish, L.tr("a.exportdb.error"), e );
156          }finally {
157            if( xml!=null ) {
158              try {
159                xml.close();
160              }catch( IOException e2 ) {}
161            }
162          }
163        }
164      }
165      
166      private void writeIfFilled( XMLWriter xml, String tag, String content )
167      throws IOException {
168        if(! content.trim().equals("") ) {
169          xml.startElement( tag );
170          xml.writeContent( content );
171          xml.endElement();
172        }
173      }
174    
175    }
176