1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.util;
16  
17  import com.liferay.portal.kernel.io.unsync.UnsyncStringWriter;
18  import com.liferay.portal.kernel.util.DiffHtml;
19  import com.liferay.portal.kernel.util.LocaleUtil;
20  import com.liferay.portal.kernel.util.StringPool;
21  
22  import java.io.Reader;
23  
24  import java.util.Locale;
25  
26  import javax.xml.transform.TransformerFactory;
27  import javax.xml.transform.sax.SAXTransformerFactory;
28  import javax.xml.transform.sax.TransformerHandler;
29  import javax.xml.transform.stream.StreamResult;
30  
31  import org.outerj.daisy.diff.HtmlCleaner;
32  import org.outerj.daisy.diff.XslFilter;
33  import org.outerj.daisy.diff.html.HTMLDiffer;
34  import org.outerj.daisy.diff.html.HtmlSaxDiffOutput;
35  import org.outerj.daisy.diff.html.TextNodeComparator;
36  import org.outerj.daisy.diff.html.dom.DomTreeBuilder;
37  
38  import org.xml.sax.ContentHandler;
39  import org.xml.sax.InputSource;
40  import org.xml.sax.helpers.AttributesImpl;
41  
42  /**
43   * <a href="DiffHtmlImpl.java.html"><b><i>View Source</i></b></a>
44   *
45   * <p>
46   * This class can compare two different versions of HTML code. It detects
47   * changes to an entire HTML page such as removal or addition of characters or
48   * images.
49   * </p>
50   *
51   * @author Julio Camarero
52   */
53  public class DiffHtmlImpl implements DiffHtml {
54  
55      /**
56       * This is a diff method with default values.
57       *
58       * @return a string containing the HTML code of the source text showing the
59       *         differences with the target text
60       */
61      public String diff(Reader source, Reader target) throws Exception {
62          InputSource oldSource = new InputSource(source);
63          InputSource newSource = new InputSource(target);
64  
65          UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter(true);
66  
67          SAXTransformerFactory saxTransformerFactory =
68              (SAXTransformerFactory)TransformerFactory.newInstance();
69  
70          TransformerHandler tranformHandler =
71              saxTransformerFactory.newTransformerHandler();
72  
73          tranformHandler.setResult(new StreamResult(unsyncStringWriter));
74  
75          XslFilter xslFilter = new XslFilter();
76  
77          ContentHandler contentHandler = xslFilter.xsl(
78              tranformHandler,
79              "com/liferay/portal/util/dependencies/diff_html.xsl");
80  
81          HtmlCleaner htmlCleaner = new HtmlCleaner();
82  
83          DomTreeBuilder oldDomTreeBuilder = new DomTreeBuilder();
84  
85          htmlCleaner.cleanAndParse(oldSource, oldDomTreeBuilder);
86  
87          Locale locale = LocaleUtil.getDefault();
88  
89          TextNodeComparator leftTextNodeComparator = new TextNodeComparator(
90              oldDomTreeBuilder, locale);
91  
92          DomTreeBuilder newDomTreeBuilder = new DomTreeBuilder();
93  
94          htmlCleaner.cleanAndParse(newSource, newDomTreeBuilder);
95  
96          TextNodeComparator rightTextNodeComparator = new TextNodeComparator(
97              newDomTreeBuilder, locale);
98  
99          contentHandler.startDocument();
100         contentHandler.startElement(
101             StringPool.BLANK, _DIFF_REPORT, _DIFF_REPORT, new AttributesImpl());
102         contentHandler.startElement(
103             StringPool.BLANK, _DIFF, _DIFF, new AttributesImpl());
104 
105         HtmlSaxDiffOutput htmlSaxDiffOutput = new HtmlSaxDiffOutput(
106             contentHandler, _DIFF);
107 
108         HTMLDiffer htmlDiffer = new HTMLDiffer(htmlSaxDiffOutput);
109 
110         htmlDiffer.diff(leftTextNodeComparator, rightTextNodeComparator);
111 
112         contentHandler.endElement(StringPool.BLANK, _DIFF, _DIFF);
113         contentHandler.endElement(StringPool.BLANK, _DIFF_REPORT, _DIFF_REPORT);
114         contentHandler.endDocument();
115 
116         unsyncStringWriter.flush();
117 
118         return unsyncStringWriter.toString();
119     }
120 
121     private static final String _DIFF = "diff";
122 
123     private static final String _DIFF_REPORT = "diffreport";
124 
125 }