001/*
002 * (C) Copyright 2002-20012 Nuxeo SA (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 *     Antoine Taillefer
016 */
017package org.nuxeo.ecm.diff.content.converters;
018
019import java.io.Serializable;
020import java.util.Map;
021
022import org.apache.commons.logging.Log;
023import org.apache.commons.logging.LogFactory;
024import org.nuxeo.ecm.core.api.Blob;
025import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
026import org.nuxeo.ecm.core.convert.api.ConversionException;
027import org.nuxeo.ecm.core.convert.api.ConversionService;
028import org.nuxeo.ecm.core.convert.api.ConverterNotRegistered;
029import org.nuxeo.runtime.api.Framework;
030
031/**
032 * HTML converter for content diff.
033 * <p>
034 * Uses the converter registered with sourceMimeType = mime type of the {@code blobHolder} and destinationMimeType =
035 * {@code text/html}.
036 *
037 * @author Antoine Taillefer (ataillefer@nuxeo.com)
038 * @since 5.6
039 */
040public class ContentDiffHtmlConverter extends AbstractContentDiffConverter {
041
042    private static final Log LOGGER = LogFactory.getLog(ContentDiffHtmlConverter.class);
043
044    private static final String HTML_MIME_TYPE = "text/html";
045
046    private static final String ANY_2_HTML_CONVERTER_NAME = "any2html";
047
048    private static final String OFFICE_2_HTML_CONVERTER_NAME = "office2html";
049
050    public BlobHolder convert(BlobHolder blobHolder, Map<String, Serializable> parameters) throws ConversionException {
051
052        String converterName = null;
053
054        // Fetch blob from blob holder
055        Blob blob = blobHolder.getBlob();
056        if (blob == null) {
057            LOGGER.warn("Trying to convert a blob holder that has a null blob. Nothing to do, returning the blob holder.");
058            return blobHolder;
059        }
060
061        // Get HTML converter name from blob mime type
062        String mimeType = blob.getMimeType();
063        ConversionService cs = Framework.getLocalService(ConversionService.class);
064        converterName = cs.getConverterName(mimeType, HTML_MIME_TYPE);
065        // We don't want to use the "any2html" converter contributed for the
066        // preview in the case of non pdf blobs since it uses the following
067        // conversion chain : any2pdf --> pdf2html.
068        // In this case we want to use the "office2html" converter which
069        // gives a better result when applying the HTMLContentDiffer on the
070        // converted HTML.
071        if (ANY_2_HTML_CONVERTER_NAME.equals(converterName) && !"application/pdf".equals(mimeType)) {
072            converterName = OFFICE_2_HTML_CONVERTER_NAME;
073        }
074
075        // No converter found, throw appropriate exception
076        if (converterName == null) {
077            throw new ConverterNotRegistered(String.format("for sourceMimeType = %s, destinationMimeType = %s",
078                    mimeType, HTML_MIME_TYPE));
079        }
080
081        return convert(converterName, blobHolder, parameters);
082    }
083
084}