001/*
002 * (C) Copyright 2002-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 */
018package org.nuxeo.ecm.core.convert.plugins.text.extractors;
019
020import java.io.File;
021import java.io.FileInputStream;
022import java.io.FileOutputStream;
023import java.io.IOException;
024import java.io.InputStream;
025import java.io.OutputStream;
026import java.io.Serializable;
027import java.util.Map;
028
029import org.apache.commons.logging.Log;
030import org.apache.commons.logging.LogFactory;
031import org.apache.poi.hwpf.extractor.WordExtractor;
032import org.nuxeo.ecm.core.api.Blob;
033import org.nuxeo.ecm.core.api.Blobs;
034import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
035import org.nuxeo.ecm.core.convert.api.ConversionException;
036import org.nuxeo.ecm.core.convert.cache.SimpleCachableBlobHolder;
037import org.nuxeo.ecm.core.convert.extension.Converter;
038import org.nuxeo.ecm.core.convert.extension.ConverterDescriptor;
039
040/**
041 * @deprecated subsumed by MSOffice2TextConverter
042 */
043@Deprecated
044public class Word2TextConverter implements Converter {
045
046    private static final Log log = LogFactory.getLog(Word2TextConverter.class);
047
048    @Override
049    public BlobHolder convert(BlobHolder blobHolder, Map<String, Serializable> parameters) throws ConversionException {
050
051        File f = null;
052        OutputStream fas = null;
053
054        WordExtractor extractor = null;
055        try {
056            extractor = new WordExtractor(blobHolder.getBlob().getStream());
057            byte[] bytes = extractor.getText().getBytes();
058            f = File.createTempFile("po-word2text", ".txt");
059            fas = new FileOutputStream(f);
060            fas.write(bytes);
061
062            Blob blob;
063            try (InputStream in = new FileInputStream(f)) {
064                blob = Blobs.createBlob(in);
065            }
066            blob.setMimeType("text/plain");
067
068            return new SimpleCachableBlobHolder(blob);
069        } catch (IOException e) {
070            throw new ConversionException("Error during Word2Text conversion", e);
071        } finally {
072            if (extractor != null) {
073                try {
074                    extractor.close();
075                } catch (IOException e) {
076                    log.error(e, e);
077                }
078            }
079            if (fas != null) {
080                try {
081                    fas.close();
082                } catch (IOException e) {
083                    log.error(e, e);
084                }
085            }
086            if (f != null) {
087                f.delete();
088            }
089        }
090    }
091
092    @Override
093    public void init(ConverterDescriptor descriptor) {
094    }
095
096}