001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.util;
016    
017    import com.liferay.portal.kernel.io.unsync.UnsyncStringWriter;
018    import com.liferay.portal.kernel.util.DiffHtml;
019    import com.liferay.portal.kernel.util.LocaleUtil;
020    import com.liferay.portal.kernel.util.StringPool;
021    
022    import java.io.Reader;
023    
024    import java.util.Locale;
025    
026    import javax.xml.transform.TransformerFactory;
027    import javax.xml.transform.sax.SAXTransformerFactory;
028    import javax.xml.transform.sax.TransformerHandler;
029    import javax.xml.transform.stream.StreamResult;
030    
031    import org.outerj.daisy.diff.HtmlCleaner;
032    import org.outerj.daisy.diff.XslFilter;
033    import org.outerj.daisy.diff.html.HTMLDiffer;
034    import org.outerj.daisy.diff.html.HtmlSaxDiffOutput;
035    import org.outerj.daisy.diff.html.TextNodeComparator;
036    import org.outerj.daisy.diff.html.dom.DomTreeBuilder;
037    
038    import org.xml.sax.ContentHandler;
039    import org.xml.sax.InputSource;
040    import org.xml.sax.helpers.AttributesImpl;
041    
042    /**
043     * This class can compare two different versions of HTML code. It detects
044     * changes to an entire HTML page such as removal or addition of characters or
045     * images.
046     *
047     * @author Julio Camarero
048     */
049    public class DiffHtmlImpl implements DiffHtml {
050    
051            /**
052             * This is a diff method with default values.
053             *
054             * @param  source the source text
055             * @param  target the modified version of the source text
056             * @return a string containing the HTML code of the source text showing the
057             *         differences with the target text
058             * @throws Exception if an exception occurred
059             */
060            public String diff(Reader source, Reader target) throws Exception {
061                    InputSource oldSource = new InputSource(source);
062                    InputSource newSource = new InputSource(target);
063    
064                    UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter();
065    
066                    SAXTransformerFactory saxTransformerFactory =
067                            (SAXTransformerFactory)TransformerFactory.newInstance();
068    
069                    TransformerHandler tranformHandler =
070                            saxTransformerFactory.newTransformerHandler();
071    
072                    tranformHandler.setResult(new StreamResult(unsyncStringWriter));
073    
074                    XslFilter xslFilter = new XslFilter();
075    
076                    ContentHandler contentHandler = xslFilter.xsl(
077                            tranformHandler,
078                            "com/liferay/portal/util/dependencies/diff_html.xsl");
079    
080                    HtmlCleaner htmlCleaner = new HtmlCleaner();
081    
082                    DomTreeBuilder oldDomTreeBuilder = new DomTreeBuilder();
083    
084                    htmlCleaner.cleanAndParse(oldSource, oldDomTreeBuilder);
085    
086                    Locale locale = LocaleUtil.getDefault();
087    
088                    TextNodeComparator leftTextNodeComparator = new TextNodeComparator(
089                            oldDomTreeBuilder, locale);
090    
091                    DomTreeBuilder newDomTreeBuilder = new DomTreeBuilder();
092    
093                    htmlCleaner.cleanAndParse(newSource, newDomTreeBuilder);
094    
095                    TextNodeComparator rightTextNodeComparator = new TextNodeComparator(
096                            newDomTreeBuilder, locale);
097    
098                    contentHandler.startDocument();
099                    contentHandler.startElement(
100                            StringPool.BLANK, _DIFF_REPORT, _DIFF_REPORT, new AttributesImpl());
101                    contentHandler.startElement(
102                            StringPool.BLANK, _DIFF, _DIFF, new AttributesImpl());
103    
104                    HtmlSaxDiffOutput htmlSaxDiffOutput = new HtmlSaxDiffOutput(
105                            contentHandler, _DIFF);
106    
107                    HTMLDiffer htmlDiffer = new HTMLDiffer(htmlSaxDiffOutput);
108    
109                    htmlDiffer.diff(leftTextNodeComparator, rightTextNodeComparator);
110    
111                    contentHandler.endElement(StringPool.BLANK, _DIFF, _DIFF);
112                    contentHandler.endElement(StringPool.BLANK, _DIFF_REPORT, _DIFF_REPORT);
113                    contentHandler.endDocument();
114    
115                    unsyncStringWriter.flush();
116    
117                    return unsyncStringWriter.toString();
118            }
119    
120            private static final String _DIFF = "diff";
121    
122            private static final String _DIFF_REPORT = "diffreport";
123    
124    }