001/* 002 * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Nuxeo - initial API and implementation 016 * 017 * $Id: XlsMimetypeSniffer.java 20310 2007-06-11 15:54:14Z lgodard $ 018 */ 019 020package org.nuxeo.ecm.platform.mimetype.detectors; 021 022import java.io.File; 023import java.io.FileInputStream; 024import java.io.FileNotFoundException; 025import java.io.IOException; 026import java.util.Map; 027 028import net.sf.jmimemagic.MagicDetector; 029 030import org.apache.commons.logging.Log; 031import org.apache.commons.logging.LogFactory; 032import org.apache.poi.hssf.usermodel.HSSFWorkbook; 033import org.nuxeo.common.utils.FileUtils; 034 035public class XlsMimetypeSniffer implements MagicDetector { 036 037 private static final Log log = LogFactory.getLog(XlsMimetypeSniffer.class); 038 039 public String getDisplayName() { 040 return "XLS MimeType Detector"; 041 } 042 043 public String[] getHandledExtensions() { 044 return new String[] { "xls" }; 045 } 046 047 public String[] getHandledTypes() { 048 return new String[] { "application/vnd.ms-excel", "application/msexcel", "application/vnd.microsoft-excel" }; 049 } 050 051 public String getName() { 052 return "xlsdetector"; 053 } 054 055 public String getVersion() { 056 return "0.1"; 057 } 058 059 public String[] process(byte[] data, int offset, int length, long bitmask, char comparator, String mimeType, 060 Map params) { 061 062 String[] mimetypes = { "" }; 063 File file = null; 064 065 try { 066 file = File.createTempFile("magicdetector", ".xls"); 067 FileUtils.writeFile(file, data); 068 mimetypes = guessExcel(file); 069 } catch (IOException e) { 070 log.error(e, e); 071 } finally { 072 if (file != null) { 073 file.delete(); 074 } 075 } 076 077 return mimetypes; 078 } 079 080 public String[] process(File file, int offset, int length, long bitmask, char comparator, String mimeType, 081 Map params) { 082 083 return guessExcel(file); 084 } 085 086 public String[] guessExcel(File file) { 087 088 String[] mimetypes = {}; 089 090 try { 091 FileInputStream stream = new FileInputStream(file); 092 HSSFWorkbook workbook = new HSSFWorkbook(stream); 093 if (workbook.getNumberOfSheets() != 0) { 094 mimetypes = getHandledTypes(); 095 } 096 } catch (FileNotFoundException e) { 097 // This is not an excel file 098 log.debug("MimeType detector : Not an excel file"); 099 } catch (IOException e) { 100 // This is not an excel file 101 log.debug("MimeType detector : Not an excel file"); 102 } catch (IllegalArgumentException e) { 103 log.debug("MimeType detector : Not an excel file"); 104 } 105 106 return mimetypes; 107 } 108 109}