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.BufferedInputStream;
038    import java.io.File;
039    import java.io.FileInputStream;
040    import java.io.FileNotFoundException;
041    import java.io.IOException;
042    import java.util.ArrayList;
043    import java.util.List;
044    
045    import org.deegree.framework.log.ILogger;
046    import org.deegree.framework.log.LoggerFactory;
047    
048    /**
049     *
050     * The <code>FileLoader</code> loads a file from given path.
051     *
052     * @author <a href="mailto:buesching@lat-lon.de">Lyn Buesching</a>
053     * @author last edited by: $Author: buesching $
054     *
055     * @version $Revision: 1.1 $, $Date: 2007-10-10 14:21:26 $
056     *
057     */
058    public class FileLoader implements Loader {
059    
060        private static final ILogger LOG = LoggerFactory.getLogger( FileLoader.class );
061    
062        /**
063         * @param objectToLoad
064         *            contains an Array of strings, where the first entry contains the source to load
065         *            and the second the target to export
066         * @return an ArrayList of objects, where the first object contains an Array of bytes to export
067         *         and the second object the information of the target to export
068         */
069        public Object loadObject( Object objectToLoad ) {
070            List<Object> result = new ArrayList<Object>();
071            String source = ( (String[]) objectToLoad )[0];
072            LOG.logInfo( Messages.getString( "FileLoader.LOAD", source ) );
073            File file = new File( source );
074            int fileLength = (int) file.length();
075            byte[] byteArray = new byte[fileLength];
076            try {
077                FileInputStream fileIS = new FileInputStream( file );
078                BufferedInputStream bufferedIS = new BufferedInputStream( fileIS );
079                bufferedIS.read( byteArray, 0, fileLength );
080                bufferedIS.close();
081                fileIS.close();
082                result.add( byteArray );
083                result.add( ( (String[]) objectToLoad )[1] );
084                return result;
085            } catch ( FileNotFoundException e ) {
086                e.printStackTrace();
087                return null;
088            } catch ( IOException e ) {
089                e.printStackTrace();
090                return null;
091            }
092        }
093    
094    }