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.Serializable;
021import java.util.Map;
022
023import org.apache.commons.logging.Log;
024import org.apache.commons.logging.LogFactory;
025import org.nuxeo.ecm.core.api.Blobs;
026import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
027import org.nuxeo.ecm.core.api.blobholder.SimpleBlobHolder;
028import org.nuxeo.ecm.core.convert.api.ConversionException;
029import org.nuxeo.ecm.core.convert.api.ConversionService;
030import org.nuxeo.ecm.core.convert.extension.Converter;
031import org.nuxeo.ecm.core.convert.extension.ConverterDescriptor;
032import org.nuxeo.runtime.api.Framework;
033
034/**
035 * Converter that tries to find a way to extract full text content according to input mime-type.
036 *
037 * @author tiry
038 */
039public class FullTextConverter implements Converter {
040
041    private static final String TEXT_PLAIN_MT = "text/plain";
042
043    private static final Log log = LogFactory.getLog(FullTextConverter.class);
044
045    protected ConverterDescriptor descriptor;
046
047    @Override
048    public BlobHolder convert(BlobHolder blobHolder, Map<String, Serializable> parameters) throws ConversionException {
049
050        String srcMT = blobHolder.getBlob().getMimeType();
051
052        if (TEXT_PLAIN_MT.equals(srcMT)) {
053            // no need to convert !
054            return blobHolder;
055        }
056
057        ConversionService cs = Framework.getLocalService(ConversionService.class);
058
059        String converterName = cs.getConverterName(srcMT, TEXT_PLAIN_MT);
060
061        if (converterName != null) {
062            if (converterName.equals(descriptor.getConverterName())) {
063                // Should never happen !
064                log.debug("Existing from converter to avoid a loop");
065                return new SimpleBlobHolder(Blobs.createBlob(""));
066            }
067            return cs.convert(converterName, blobHolder, parameters);
068        } else {
069            log.debug("Unable to find full text extractor for source mime type" + srcMT);
070            return new SimpleBlobHolder(Blobs.createBlob(""));
071        }
072    }
073
074    @Override
075    public void init(ConverterDescriptor descriptor) {
076        this.descriptor = descriptor;
077    }
078
079}