001    /*----------------------------------------------------------------------------
002     This file is part of deegree, http://deegree.org/
003     Copyright (C) 2001-2009 by:
004       Department of Geography, University of Bonn
005     and
006       lat/lon GmbH
007    
008     This library is free software; you can redistribute it and/or modify it under
009     the terms of the GNU Lesser General Public License as published by the Free
010     Software Foundation; either version 2.1 of the License, or (at your option)
011     any later version.
012     This library is distributed in the hope that it will be useful, but WITHOUT
013     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
014     FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
015     details.
016     You should have received a copy of the GNU Lesser General Public License
017     along with this library; if not, write to the Free Software Foundation, Inc.,
018     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019    
020     Contact information:
021    
022     lat/lon GmbH
023     Aennchenstr. 19, 53177 Bonn
024     Germany
025     http://lat-lon.de/
026    
027     Department of Geography, University of Bonn
028     Prof. Dr. Klaus Greve
029     Postfach 1147, 53001 Bonn
030     Germany
031     http://www.geographie.uni-bonn.de/deegree/
032    
033     e-mail: info@deegree.org
034    ----------------------------------------------------------------------------*/
035    package org.deegree.tools.importer;
036    
037    import java.io.BufferedOutputStream;
038    import java.io.File;
039    import java.io.FileOutputStream;
040    import java.io.IOException;
041    import java.util.ArrayList;
042    
043    import org.deegree.framework.log.ILogger;
044    import org.deegree.framework.log.LoggerFactory;
045    
046    /**
047     *
048     * The <code>FileExporter</code> writes a new file.
049     *
050     * @author <a href="mailto:buesching@lat-lon.de">Lyn Buesching</a>
051     * @author last edited by: $Author: buesching $
052     *
053     * @version $Revision: 1.1 $, $Date: 2007-10-10 14:21:26 $
054     *
055     */
056    public class FileExporter implements Exporter {
057    
058        private static final ILogger LOG = LoggerFactory.getLogger( FileExporter.class );
059    
060        /**
061         * @param objectToExport
062         *            contains a ListArray of objects, where the first object contains an Array of bytes
063         *            to export and the second object the information of the target to export
064         * @return true, when expor is successful; otherwise false
065         */
066        public boolean export( Object objectToExport ) {
067            Boolean result = false;
068            byte[] byteArray = (byte[]) ( (ArrayList<Object>) objectToExport ).get( 0 );
069            String target = (String) ( (ArrayList<Object>) objectToExport ).get( 1 );
070            LOG.logInfo( Messages.getString( "FileExporter.EXPORT", target ) );
071            File file = new File( target );
072            if ( !file.exists() ) {
073                try {
074                    FileOutputStream fileOS = new FileOutputStream( file );
075                    BufferedOutputStream bufferedOS = new BufferedOutputStream( fileOS );
076                    bufferedOS.write( byteArray, 0, byteArray.length );
077                    bufferedOS.close();
078                    fileOS.close();
079                    result = true;
080                } catch ( IOException e ) {
081                    LOG.logError( Messages.getString( "FileExporter.ERROR_WRITE_FILE", target, e.getMessage() ) );
082                    e.printStackTrace();
083                }
084            } else {
085                LOG.logInfo( Messages.getString( "FileExporter.ERROR_EXPORT_FILE", target ) );
086            }
087            return result;
088        }
089    
090    }