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