001/**
002 * Copyright (C) 2014  Universidade de Aveiro, DETI/IEETA, Bioinformatics Group - http://bioinformatics.ua.pt/
003 *
004 * This file is part of Dicoogle/dicoogle-sdk-ext.
005 *
006 * Dicoogle/dicoogle-sdk-ext is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * Dicoogle/dicoogle-sdk-ext is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with Dicoogle.  If not, see <http://www.gnu.org/licenses/>.
018 */
019package pt.ua.dicoogle.sdk.index.handlers;
020
021import java.io.File;
022import java.io.IOException;
023import java.util.Properties;
024import pt.ua.dicoogle.sdk.index.IDoc;
025
026
027
028/**
029 * Handles different filetypes based on information from Properties
030 * Lmitation : The file must have an extension of any form
031 * @author marco
032 */
033public class ExtensionFileHandler {
034
035    private Properties handlerProps;
036    
037    /**
038     * Creates a new Extension File Handler
039     * @param p Properties that allow the correct class to be called for each extension
040     */
041    public ExtensionFileHandler(Properties p) throws IOException
042    {
043        handlerProps = p;
044    }
045    
046    /**
047     * Handle a file, obtaining a Lucene Document from it, if it
048     * knows how to process this file type. Since we are interest in
049     * indexing files, the path is also saved as a field
050     * @param file File to be handled
051     * @return Lucene Friendly Document
052     */
053    public IDoc getDocument(File file) throws FileHandlerException, FileAlreadyExistsException {
054        IDoc doc = null;
055        String name = file.getName();
056        String Error = "Cannot create instance of : ";
057        if (name.contains(".DS_Store"))
058        {
059            return null;
060        }
061        try
062        {
063            Class handlerClass = Class.forName("pt.ua.dicoogle.core.index.LuceneSupport.DICOM.DicomDocumentNG");
064            DocumentHandler handler = (DocumentHandler) handlerClass.newInstance();
065            doc = handler.getDocument(file);
066        }
067        catch (DocumentHandlerException ex)
068        {
069            throw new FileHandlerException(Error + "pt.ua.dicoogle.core.index.LuceneSupport.DICOM.DicomDocumentNG", ex);
070        }
071
072        catch (FileAlreadyExistsException ex)
073        {
074            throw ex;
075        }
076
077        catch (InstantiationException ex)
078        {
079            throw new FileHandlerException(Error + "pt.ua.dicoogle.core.index.LuceneSupport.DICOM.DicomDocumentNG", ex);
080        }
081        catch (IllegalAccessException ex)
082        {
083            throw new FileHandlerException(Error + "pt.ua.dicoogle.core.index.LuceneSupport.DICOM.DicomDocumentNG", ex);
084        }
085        catch (ClassNotFoundException ex)
086        {
087            throw new FileHandlerException(Error + "pt.ua.dicoogle.core.index.LuceneSupport.DICOM.DicomDocumentNG", ex);
088        }
089
090        return doc;
091    }
092}