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: MsoXML2007MimetypeSniffer.java 20591 2007-06-16 16:26:40Z sfermigier $
018 */
019
020package org.nuxeo.ecm.platform.mimetype.detectors;
021
022import java.io.File;
023import java.io.IOException;
024import java.util.Map;
025import java.util.zip.ZipFile;
026
027import net.sf.jmimemagic.MagicDetector;
028
029import org.apache.commons.logging.Log;
030import org.apache.commons.logging.LogFactory;
031import org.nuxeo.common.utils.FileUtils;
032
033public class MsoXML2007MimetypeSniffer implements MagicDetector {
034
035    private static final Log log = LogFactory.getLog(MsoXML2007MimetypeSniffer.class);
036
037    public String getDisplayName() {
038        return "Microsoft 2007 files MimeType Detector";
039    }
040
041    public String[] getHandledExtensions() {
042        return new String[] { "docm", "docx", "dotm", "dotx", "ppsm", "ppsx", "pptm", "pptx", "xlsb", "xlsm", "xlsx",
043                "xps" };
044    }
045
046    public String[] getHandledTypes() {
047        return new String[] { "application/vnd.ms-word.document.macroEnabled.12",
048                "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
049                "application/vnd.ms-word.template.macroEnabled.12",
050                "application/vnd.openxmlformats-officedocument.wordprocessingml.template",
051                "application/vnd.ms-powerpoint.slideshow.macroEnabled.12",
052                "application/vnd.openxmlformats-officedocument.presentationml.slideshow",
053                "application/vnd.ms-powerpoint.presentation.macroEnabled.12",
054                "application/vnd.openxmlformats-officedocument.presentationml.presentation",
055                "application/vnd.ms-excel.sheet.binary.macroEnabled.12",
056                "application/vnd.ms-excel.sheet.macroEnabled.12",
057                "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "application/vnd.ms-xpsdocument", };
058    }
059
060    public String getName() {
061        return "mso2007detector";
062    }
063
064    public String getVersion() {
065        return "0.1";
066    }
067
068    public String[] process(byte[] data, int offset, int length, long bitmask, char comparator, String mimeType,
069            Map params) {
070        String[] mimetypes = {};
071        File file = null;
072        try {
073            file = File.createTempFile("magicdetector", ".xml");
074            FileUtils.writeFile(file, data);
075            mimetypes = guessMsoXML2007(file);
076        } catch (IOException e) {
077            log.error(e);
078        } finally {
079            if (file != null) {
080                file.delete();
081            }
082        }
083        return mimetypes;
084    }
085
086    public String[] process(File file, int offset, int length, long bitmask, char comparator, String mimeType,
087            Map params) {
088        return guessMsoXML2007(file);
089    }
090
091    public String[] guessMsoXML2007(File file) {
092        String[] mimetype = {};
093        try {
094            // unzip
095            ZipFile zip = new ZipFile(file);
096            // look at mimetype
097        } catch (IOException e) {
098            log.error(e, e);
099        }
100        return mimetype;
101    }
102
103}