1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.util;
24  
25  import com.liferay.portal.kernel.util.DiffHtml;
26  import com.liferay.portal.kernel.util.LocaleUtil;
27  import com.liferay.portal.kernel.util.StringPool;
28  
29  import java.io.Reader;
30  import java.io.StringWriter;
31  
32  import java.util.Locale;
33  
34  import javax.xml.transform.TransformerFactory;
35  import javax.xml.transform.sax.SAXTransformerFactory;
36  import javax.xml.transform.sax.TransformerHandler;
37  import javax.xml.transform.stream.StreamResult;
38  
39  import org.outerj.daisy.diff.HtmlCleaner;
40  import org.outerj.daisy.diff.XslFilter;
41  import org.outerj.daisy.diff.html.HTMLDiffer;
42  import org.outerj.daisy.diff.html.HtmlSaxDiffOutput;
43  import org.outerj.daisy.diff.html.TextNodeComparator;
44  import org.outerj.daisy.diff.html.dom.DomTreeBuilder;
45  
46  import org.xml.sax.ContentHandler;
47  import org.xml.sax.InputSource;
48  import org.xml.sax.helpers.AttributesImpl;
49  
50  /**
51   * <a href="DiffHtmlImpl.java.html"><b><i>View Source</i></b></a>
52   *
53   * <p>
54   * This class can compare two different versions of HTML code. It detects
55   * changes to an entire HTML page such as removal or addition of characters or
56   * images.
57   * </p>
58   *
59   * @author Julio Camarero
60   *
61   */
62  public class DiffHtmlImpl implements DiffHtml {
63  
64      /**
65       * This is a diff method with default values.
66       *
67       * @param       source the <code>Reader</code> of the source text, this can
68       *              be for example, an instance of FileReader or StringReader
69       * @param       target the <code>Reader</code> of the target text
70       * @return      a string containing the HTML code of the source text
71       *              showing the differences with the target text
72       */
73      public String diff(Reader source, Reader target) throws Exception {
74          InputSource oldSource = new InputSource(source);
75          InputSource newSource = new InputSource(target);
76  
77          StringWriter stringWriter = new StringWriter();
78  
79          SAXTransformerFactory saxTransformerFactory =
80              (SAXTransformerFactory)TransformerFactory.newInstance();
81  
82          TransformerHandler tranformHandler =
83              saxTransformerFactory.newTransformerHandler();
84  
85          tranformHandler.setResult(new StreamResult(stringWriter));
86  
87          XslFilter xslFilter = new XslFilter();
88  
89          ContentHandler contentHandler = xslFilter.xsl(
90              tranformHandler,
91              "com/liferay/portal/util/dependencies/diff_html.xsl");
92  
93          HtmlCleaner htmlCleaner = new HtmlCleaner();
94  
95          DomTreeBuilder oldDomTreeBuilder = new DomTreeBuilder();
96  
97          htmlCleaner.cleanAndParse(oldSource, oldDomTreeBuilder);
98  
99          Locale locale = LocaleUtil.getDefault();
100 
101         TextNodeComparator leftTextNodeComparator = new TextNodeComparator(
102             oldDomTreeBuilder, locale);
103 
104         DomTreeBuilder newDomTreeBuilder = new DomTreeBuilder();
105 
106         htmlCleaner.cleanAndParse(newSource, newDomTreeBuilder);
107 
108         TextNodeComparator rightTextNodeComparator = new TextNodeComparator(
109             newDomTreeBuilder, locale);
110 
111         contentHandler.startDocument();
112         contentHandler.startElement(
113             StringPool.BLANK, _DIFF_REPORT, _DIFF_REPORT, new AttributesImpl());
114         contentHandler.startElement(
115             StringPool.BLANK, _DIFF, _DIFF, new AttributesImpl());
116 
117         HtmlSaxDiffOutput htmlSaxDiffOutput = new HtmlSaxDiffOutput(
118             contentHandler, _DIFF);
119 
120         HTMLDiffer htmlDiffer = new HTMLDiffer(htmlSaxDiffOutput);
121 
122         htmlDiffer.diff(leftTextNodeComparator, rightTextNodeComparator);
123 
124         contentHandler.endElement(StringPool.BLANK, _DIFF, _DIFF);
125         contentHandler.endElement(StringPool.BLANK, _DIFF_REPORT, _DIFF_REPORT);
126         contentHandler.endDocument();
127 
128         stringWriter.flush();
129 
130         return stringWriter.toString();
131     }
132 
133     private static final String _DIFF = "diff";
134 
135     private static final String _DIFF_REPORT = "diffreport";
136 
137 }