001/* 002 * (C) Copyright 2006-2018 Nuxeo (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.IOException; 022import java.io.Serializable; 023import java.util.Map; 024 025import org.apache.commons.lang3.StringUtils; 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.ConverterNotRegistered; 030 031/** 032 * Text converter for content diff. 033 * <p> 034 * Uses the "any2text" converter. 035 * 036 * @author Antoine Taillefer ([email protected]) 037 * @since 5.6 038 */ 039public class ContentDiffTextConverter extends AbstractContentDiffConverter { 040 041 private static final String ANY_2_TEXT_CONVERTER_NAME = "any2text"; 042 043 public BlobHolder convert(BlobHolder blobHolder, Map<String, Serializable> parameters) throws ConversionException { 044 045 BlobHolder convertedBlobHolder = convert(ANY_2_TEXT_CONVERTER_NAME, blobHolder, parameters); 046 047 String convertedBlobString = null; 048 try { 049 Blob convertedBlob = convertedBlobHolder.getBlob(); 050 if (convertedBlob != null) { 051 convertedBlobString = convertedBlob.getString(); 052 } 053 } catch (IOException e) { 054 throw new ConversionException("Error while getting converted blob string."); 055 } 056 if (StringUtils.isEmpty(convertedBlobString)) { 057 // Converted blob is an empty string, this means no text 058 // converter was found (see FullTextConverter). Throw 059 // appropriate exception. 060 String srcMimeType = null; 061 Blob blob = blobHolder.getBlob(); 062 if (blob != null) { 063 srcMimeType = blob.getMimeType(); 064 } 065 throw new ConverterNotRegistered( 066 String.format("for sourceMimeType = %s, destinationMimeType = text/plain", srcMimeType)); 067 } 068 return convertedBlobHolder; 069 } 070 071}