001/* 002 * (C) Copyright 2006-2008 Nuxeo SAS (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 * Alexandre Russel 016 * 017 * $Id$ 018 */ 019 020package org.nuxeo.ecm.diff.content.adapter; 021 022import java.io.IOException; 023import java.io.StringWriter; 024import java.util.ArrayList; 025import java.util.List; 026import java.util.Locale; 027 028import javax.xml.transform.TransformerConfigurationException; 029import javax.xml.transform.TransformerFactory; 030import javax.xml.transform.sax.SAXTransformerFactory; 031import javax.xml.transform.sax.TransformerHandler; 032import javax.xml.transform.stream.StreamResult; 033 034import org.apache.commons.lang.StringUtils; 035import org.apache.commons.logging.Log; 036import org.apache.commons.logging.LogFactory; 037import org.nuxeo.ecm.core.api.Blob; 038import org.nuxeo.ecm.core.api.Blobs; 039import org.nuxeo.ecm.core.api.DocumentModel; 040import org.nuxeo.ecm.core.api.blobholder.BlobHolder; 041import org.nuxeo.ecm.diff.content.ContentDiffException; 042import org.nuxeo.ecm.diff.content.ContentDiffHelper; 043import org.nuxeo.ecm.platform.web.common.vh.VirtualHostHelper; 044import org.outerj.daisy.diff.HtmlCleaner; 045import org.outerj.daisy.diff.XslFilter; 046import org.outerj.daisy.diff.html.HTMLDiffer; 047import org.outerj.daisy.diff.html.HtmlSaxDiffOutput; 048import org.outerj.daisy.diff.html.TextNodeComparator; 049import org.outerj.daisy.diff.html.dom.DomTreeBuilder; 050import org.xml.sax.ContentHandler; 051import org.xml.sax.InputSource; 052import org.xml.sax.SAXException; 053import org.xml.sax.helpers.AttributesImpl; 054 055/** 056 * @author Antoine Taillefer 057 * @since 5.6 058 */ 059public class HtmlContentDiffer implements MimeTypeContentDiffer { 060 061 private static final Log LOGGER = LogFactory.getLog(HtmlContentDiffer.class); 062 063 protected static final String NUXEO_DEFAULT_CONTEXT_PATH = "/nuxeo"; 064 065 @Override 066 public List<Blob> getContentDiff(DocumentModel leftDoc, DocumentModel rightDoc, String xpath, Locale locale) 067 throws ContentDiffException { 068 Blob leftBlob = null; 069 Blob rightBlob = null; 070 BlobHolder leftBlobHolder = null; 071 BlobHolder rightBlobHolder = null; 072 if (StringUtils.isBlank(xpath) || ContentDiffHelper.DEFAULT_XPATH.equals(xpath)) { 073 leftBlobHolder = leftDoc.getAdapter(BlobHolder.class); 074 rightBlobHolder = rightDoc.getAdapter(BlobHolder.class); 075 } else { 076 leftBlobHolder = ContentDiffHelper.getBlobHolder(leftDoc, xpath); 077 rightBlobHolder = ContentDiffHelper.getBlobHolder(rightDoc, xpath); 078 } 079 if (leftBlobHolder == null || rightBlobHolder == null) { 080 throw new ContentDiffException("Can not make a content diff of documents without a blob"); 081 } 082 leftBlob = leftBlobHolder.getBlob(); 083 rightBlob = rightBlobHolder.getBlob(); 084 if (leftBlob == null || rightBlob == null) { 085 throw new ContentDiffException("Can not make a content diff of documents without a blob"); 086 } 087 return getContentDiff(leftBlob, rightBlob, locale); 088 } 089 090 @Override 091 public List<Blob> getContentDiff(Blob leftBlob, Blob rightBlob, Locale locale) throws ContentDiffException { 092 093 try { 094 List<Blob> blobResults = new ArrayList<Blob>(); 095 StringWriter sw = new StringWriter(); 096 097 SAXTransformerFactory stf = (SAXTransformerFactory) TransformerFactory.newInstance(); 098 TransformerHandler transformHandler = stf.newTransformerHandler(); 099 transformHandler.setResult(new StreamResult(sw)); 100 101 XslFilter htmlHeaderXslFilter = new XslFilter(); 102 103 StringBuilder sb = new StringBuilder("xslfilter/htmldiffheader"); 104 sb.append("_"); 105 sb.append(locale.getLanguage()); 106 sb.append(".xsl"); 107 String htmlHeaderXslPath = sb.toString(); 108 ContentHandler postProcess; 109 try { 110 postProcess = htmlHeaderXslFilter.xsl(transformHandler, htmlHeaderXslPath); 111 } catch (IllegalStateException ise) { 112 LOGGER.error(String.format( 113 "Could not find the HTML diff header xsl file '%s', falling back on the default one.", 114 htmlHeaderXslPath), ise); 115 postProcess = htmlHeaderXslFilter.xsl(transformHandler, "xslfilter/htmldiffheader.xsl"); 116 } 117 118 String prefix = "diff"; 119 120 HtmlCleaner cleaner = new HtmlCleaner(); 121 122 InputSource leftIS = new InputSource(leftBlob.getStream()); 123 InputSource rightIS = new InputSource(rightBlob.getStream()); 124 125 DomTreeBuilder leftHandler = new DomTreeBuilder(); 126 cleaner.cleanAndParse(leftIS, leftHandler); 127 TextNodeComparator leftComparator = new TextNodeComparator(leftHandler, locale); 128 129 DomTreeBuilder rightHandler = new DomTreeBuilder(); 130 cleaner.cleanAndParse(rightIS, rightHandler); 131 TextNodeComparator rightComparator = new TextNodeComparator(rightHandler, locale); 132 133 postProcess.startDocument(); 134 postProcess.startElement("", "diffreport", "diffreport", new AttributesImpl()); 135 postProcess.startElement("", "diff", "diff", new AttributesImpl()); 136 HtmlSaxDiffOutput output = new HtmlSaxDiffOutput(postProcess, prefix); 137 138 HTMLDiffer differ = new HTMLDiffer(output); 139 differ.diff(leftComparator, rightComparator); 140 141 postProcess.endElement("", "diff", "diff"); 142 postProcess.endElement("", "diffreport", "diffreport"); 143 postProcess.endDocument(); 144 145 String stringBlob = sw.toString().replaceAll(NUXEO_DEFAULT_CONTEXT_PATH, 146 VirtualHostHelper.getContextPathProperty()); 147 Blob mainBlob = Blobs.createBlob(stringBlob); 148 sw.close(); 149 150 mainBlob.setFilename("contentDiff.html"); 151 mainBlob.setMimeType("text/html"); 152 153 blobResults.add(mainBlob); 154 return blobResults; 155 156 } catch (TransformerConfigurationException | SAXException | IOException e) { 157 throw new ContentDiffException(e); 158 } 159 } 160}