001/*
002 * (C) Copyright 2006-2012 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 */
018
019package org.nuxeo.template.processors.convert;
020
021import java.io.Serializable;
022import java.util.ArrayList;
023import java.util.List;
024import java.util.Map;
025
026import org.nuxeo.ecm.core.api.Blob;
027import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
028import org.nuxeo.ecm.core.convert.api.ConversionException;
029import org.nuxeo.ecm.core.convert.api.ConversionService;
030import org.nuxeo.ecm.core.convert.api.ConverterCheckResult;
031import org.nuxeo.ecm.core.convert.extension.ConverterDescriptor;
032import org.nuxeo.ecm.core.convert.extension.ExternalConverter;
033import org.nuxeo.runtime.api.Framework;
034
035/**
036 * Converter used to bridge MarkDown and OpenOffice conversions
037 *
038 * @author Tiry (tdelprat@nuxeo.com)
039 */
040public class AnyToODTConverter implements ExternalConverter {
041
042    protected ConversionService getConversionService() {
043        return Framework.getLocalService(ConversionService.class);
044    }
045
046    protected List<String> getConverterChain(String srcMT) {
047        List<String> subConverters = new ArrayList<String>();
048
049        if (srcMT == null) {
050            return null;
051        }
052
053        if (srcMT.equals("text/x-web-markdown")) {
054            subConverters.add("md2html");
055        }
056
057        subConverters.add("sdttext2odt");
058
059        return subConverters;
060    }
061
062    public BlobHolder convert(BlobHolder blobHolder, Map<String, Serializable> parameters) throws ConversionException {
063
064        Blob sourceBlob = blobHolder.getBlob();
065
066        List<String> subConverters = getConverterChain(sourceBlob.getMimeType());
067
068        if (subConverters == null) {
069            throw new ConversionException("Can not find suitable underlying converters to handle html preview");
070        }
071
072        BlobHolder result = blobHolder;
073
074        for (String converterName : subConverters) {
075            result = getConversionService().convert(converterName, result, parameters);
076        }
077        return result;
078    }
079
080    public void init(ConverterDescriptor descriptor) {
081    }
082
083    public ConverterCheckResult isConverterAvailable() {
084        try {
085            return getConversionService().isConverterAvailable("sdttext2odt");
086        } catch (ConversionException e) {
087            ConverterCheckResult result = new ConverterCheckResult();
088            result.setAvailable(false);
089            return result;
090        }
091    }
092
093}