001/*
002 * (C) Copyright 2006-2009 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$
018 */
019
020package org.nuxeo.ecm.platform.preview.converters;
021
022import java.io.Serializable;
023import java.util.ArrayList;
024import java.util.List;
025import java.util.Map;
026
027import org.nuxeo.ecm.core.api.Blob;
028import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
029import org.nuxeo.ecm.core.convert.api.ConversionException;
030import org.nuxeo.ecm.core.convert.api.ConversionService;
031import org.nuxeo.ecm.core.convert.api.ConverterCheckResult;
032import org.nuxeo.ecm.core.convert.extension.ConverterDescriptor;
033import org.nuxeo.ecm.core.convert.extension.ExternalConverter;
034import org.nuxeo.runtime.api.Framework;
035
036public class HtmlPreviewConverter implements ExternalConverter {
037
038    protected static ConversionService cs;
039
040    protected static Boolean canUsePDF2Html;
041
042    protected static Boolean canUseOOo2Html;
043
044    protected static ConversionService getConversionService() {
045        if (cs == null) {
046            cs = Framework.getLocalService(ConversionService.class);
047        }
048        return cs;
049    }
050
051    protected static boolean getCanUsePDF2Html() {
052        if (canUsePDF2Html == null) {
053            try {
054                canUsePDF2Html = getConversionService().isConverterAvailable("pdf2html").isAvailable();
055            } catch (ConversionException e) {
056                return false;
057            }
058        }
059        return canUsePDF2Html;
060    }
061
062    protected static boolean getCanUseOOo2Html() {
063        if (canUseOOo2Html == null) {
064            try {
065                canUseOOo2Html = getConversionService().isConverterAvailable("office2html").isAvailable();
066            } catch (ConversionException e) {
067                return false;
068            }
069        }
070        return canUseOOo2Html;
071    }
072
073    protected List<String> getConverterChain(String srcMT) {
074        List<String> subConverters = new ArrayList<String>();
075
076        if (srcMT == null) {
077            return null;
078        }
079
080        if (srcMT.equals("text/html") || srcMT.equals("text/xml") || srcMT.equals("text/xhtml")) {
081            return subConverters;
082        }
083
084        if (getCanUsePDF2Html()) {
085            if (srcMT.equals("application/pdf")) {
086                subConverters.add("pdf2html");
087            } else {
088                subConverters.add("any2pdf");
089                subConverters.add("pdf2html");
090            }
091        } else {
092            if (getCanUseOOo2Html()) {
093                subConverters.add("office2html");
094            } else {
095                return null;
096            }
097        }
098
099        return subConverters;
100    }
101
102    @Override
103    public BlobHolder convert(BlobHolder blobHolder, Map<String, Serializable> parameters) throws ConversionException {
104
105        Blob sourceBlob = blobHolder.getBlob();
106
107        List<String> subConverters = getConverterChain(sourceBlob.getMimeType());
108
109        if (subConverters == null) {
110            throw new ConversionException("Can not find suitable underlying converters to handle html preview");
111        }
112
113        BlobHolder result = blobHolder;
114
115        for (String converterName : subConverters) {
116            result = getConversionService().convert(converterName, result, parameters);
117        }
118        Blob blob = result.getBlob();
119        if (blob != null && blob.getEncoding() == null) {
120            blob.setEncoding("UTF-8");
121        }
122        return result;
123    }
124
125    @Override
126    public void init(ConverterDescriptor descriptor) {
127        // TODO Auto-generated method stub
128    }
129
130    @Override
131    public ConverterCheckResult isConverterAvailable() {
132        ConverterCheckResult result = new ConverterCheckResult();
133        result.setAvailable(getCanUseOOo2Html() || getCanUsePDF2Html());
134        return result;
135    }
136
137}