001/*
002 * (C) Copyright 2006-2012 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 */
020
021package org.nuxeo.template.processors.convert;
022
023import java.io.Serializable;
024import java.util.ArrayList;
025import java.util.List;
026import java.util.Map;
027
028import org.nuxeo.ecm.core.api.Blob;
029import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
030import org.nuxeo.ecm.core.convert.api.ConversionException;
031import org.nuxeo.ecm.core.convert.api.ConversionService;
032import org.nuxeo.ecm.core.convert.api.ConverterCheckResult;
033import org.nuxeo.ecm.core.convert.extension.ConverterDescriptor;
034import org.nuxeo.ecm.core.convert.extension.ExternalConverter;
035import org.nuxeo.runtime.api.Framework;
036
037/**
038 * Converter used to bridge MarkDown and OpenOffice conversions
039 *
040 * @author Tiry (tdelprat@nuxeo.com)
041 */
042public class AnyToODTConverter implements ExternalConverter {
043
044    protected ConversionService getConversionService() {
045        return Framework.getLocalService(ConversionService.class);
046    }
047
048    protected List<String> getConverterChain(String srcMT) {
049        List<String> subConverters = new ArrayList<String>();
050
051        if (srcMT == null) {
052            return null;
053        }
054
055        if (srcMT.equals("text/x-web-markdown")) {
056            subConverters.add("md2html");
057        }
058
059        subConverters.add("sdttext2odt");
060
061        return subConverters;
062    }
063
064    public BlobHolder convert(BlobHolder blobHolder, Map<String, Serializable> parameters) throws ConversionException {
065
066        Blob sourceBlob = blobHolder.getBlob();
067
068        List<String> subConverters = getConverterChain(sourceBlob.getMimeType());
069
070        if (subConverters == null) {
071            throw new ConversionException("Can not find suitable underlying converters to handle html preview");
072        }
073
074        BlobHolder result = blobHolder;
075
076        for (String converterName : subConverters) {
077            result = getConversionService().convert(converterName, result, parameters);
078        }
079        return result;
080    }
081
082    public void init(ConverterDescriptor descriptor) {
083    }
084
085    public ConverterCheckResult isConverterAvailable() {
086        try {
087            return getConversionService().isConverterAvailable("sdttext2odt");
088        } catch (ConversionException e) {
089            ConverterCheckResult result = new ConverterCheckResult();
090            result.setAvailable(false);
091            return result;
092        }
093    }
094
095}