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