001/*
002 * (C) Copyright 2015 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 <ataillefer@nuxeo.com>
018 *     Thibaud Arguillere
019 */
020
021package org.nuxeo.diff.pictures;
022
023import java.io.StringWriter;
024import java.util.ArrayList;
025import java.util.List;
026import java.util.Locale;
027
028import org.nuxeo.ecm.core.api.Blob;
029import org.nuxeo.ecm.core.api.Blobs;
030import org.nuxeo.ecm.core.api.DocumentModel;
031import org.nuxeo.ecm.diff.content.ContentDiffException;
032import org.nuxeo.ecm.diff.content.adapter.MimeTypeContentDiffer;
033
034/**
035 * ImageMagickContentDiffer
036 *
037 * @since 7.4
038 */
039public class ImageMagickContentDiffer implements MimeTypeContentDiffer {
040
041    @Override
042    public List<Blob> getContentDiff(DocumentModel leftDoc, DocumentModel rightDoc, String xpath, Locale locale)
043            throws ContentDiffException {
044
045        try {
046            List<Blob> blobResults = new ArrayList<Blob>();
047            StringWriter sw = new StringWriter();
048
049            String html = DiffPictures.buildDiffHtml(leftDoc, rightDoc, xpath);
050            sw.write(html);
051
052            String stringBlob = sw.toString();
053            Blob mainBlob = Blobs.createBlob(stringBlob);
054            sw.close();
055
056            mainBlob.setFilename("contentDiff.html");
057            mainBlob.setMimeType("text/html");
058
059            blobResults.add(mainBlob);
060            return blobResults;
061
062        } catch (Exception e) {
063            throw new ContentDiffException(e);
064        }
065    }
066
067    @Override
068    public List<Blob> getContentDiff(Blob leftBlob, Blob rightBlob, Locale locale) throws ContentDiffException {
069        throw new UnsupportedOperationException("ImageMagickContentDiffer can handle only DocumentModel");
070    }
071}