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: MsoXmlMimetypeSniffer.java 15407 2007-04-03 18:57:25Z sfermigier $ 018 */ 019 020package org.nuxeo.ecm.platform.mimetype.detectors; 021 022import java.io.File; 023import java.io.IOException; 024import java.util.Map; 025 026import net.sf.jmimemagic.MagicDetector; 027 028import org.apache.commons.logging.Log; 029import org.apache.commons.logging.LogFactory; 030import org.nuxeo.common.utils.FileUtils; 031 032public class MsoXmlMimetypeSniffer implements MagicDetector { 033 034 private static final Log log = LogFactory.getLog(MsoXmlMimetypeSniffer.class); 035 036 public String getDisplayName() { 037 return "XML Microsoft 2003 MimeType Detector"; 038 } 039 040 public String[] getHandledExtensions() { 041 return new String[] { "xml" }; 042 } 043 044 public String[] getHandledTypes() { 045 return new String[] { "application/vnd.ms-excel", "application/msword", }; 046 } 047 048 public String getName() { 049 return "msoxml2003detector"; 050 } 051 052 public String getVersion() { 053 return "0.1"; 054 } 055 056 public String[] process(byte[] data, int offset, int length, long bitmask, char comparator, String mimeType, 057 Map params) { 058 String[] mimetypes = {}; 059 File file = null; 060 try { 061 file = File.createTempFile("magicdetector", ".xml"); 062 FileUtils.writeFile(file, data); 063 mimetypes = guessMsoXml(file); 064 } catch (IOException e) { 065 log.error(e); 066 } finally { 067 if (file != null) { 068 file.delete(); 069 } 070 } 071 return mimetypes; 072 } 073 074 public String[] process(File file, int offset, int length, long bitmask, char comparator, String mimeType, 075 Map params) { 076 return guessMsoXml(file); 077 } 078 079 public String[] guessMsoXml(File file) { 080 String[] mimetype = {}; 081 try { 082 String content = FileUtils.readFile(file); 083 if (content.contains("<?mso-application progid=\"Word.Document\"?>")) { 084 String[] type = { getHandledTypes()[1] }; 085 mimetype = type; 086 } else { 087 if (content.contains("<?mso-application progid=\"Excel.Sheet\"?>")) { 088 String[] type = { getHandledTypes()[0] }; 089 mimetype = type; 090 } 091 } 092 } catch (IOException e) { 093 log.error(e); 094 } 095 return mimetype; 096 } 097 098}