001    /**
002     * Copyright (c) 2000-2013 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.security.pacl.DoPrivileged;
019    import com.liferay.portal.kernel.util.DiffHtml;
020    import com.liferay.portal.kernel.util.LocaleUtil;
021    import com.liferay.portal.kernel.util.StringPool;
022    
023    import java.io.Reader;
024    
025    import java.util.Locale;
026    
027    import javax.xml.transform.TransformerFactory;
028    import javax.xml.transform.sax.SAXTransformerFactory;
029    import javax.xml.transform.sax.TransformerHandler;
030    import javax.xml.transform.stream.StreamResult;
031    
032    import org.outerj.daisy.diff.HtmlCleaner;
033    import org.outerj.daisy.diff.XslFilter;
034    import org.outerj.daisy.diff.html.HTMLDiffer;
035    import org.outerj.daisy.diff.html.HtmlSaxDiffOutput;
036    import org.outerj.daisy.diff.html.TextNodeComparator;
037    import org.outerj.daisy.diff.html.dom.DomTreeBuilder;
038    
039    import org.xml.sax.ContentHandler;
040    import org.xml.sax.InputSource;
041    import org.xml.sax.helpers.AttributesImpl;
042    
043    /**
044     * This class can compare two different versions of HTML code. It detects
045     * changes to an entire HTML page such as removal or addition of characters or
046     * images.
047     *
048     * @author Julio Camarero
049     */
050    @DoPrivileged
051    public class DiffHtmlImpl implements DiffHtml {
052    
053            /**
054             * This is a diff method with default values.
055             *
056             * @param  source the source text
057             * @param  target the modified version of the source text
058             * @return a string containing the HTML code of the source text showing the
059             *         differences with the target text
060             * @throws Exception if an exception occurred
061             */
062            public String diff(Reader source, Reader target) throws Exception {
063                    InputSource oldSource = new InputSource(source);
064                    InputSource newSource = new InputSource(target);
065    
066                    UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter();
067    
068                    SAXTransformerFactory saxTransformerFactory =
069                            (SAXTransformerFactory)TransformerFactory.newInstance();
070    
071                    TransformerHandler tranformHandler =
072                            saxTransformerFactory.newTransformerHandler();
073    
074                    tranformHandler.setResult(new StreamResult(unsyncStringWriter));
075    
076                    XslFilter xslFilter = new XslFilter();
077    
078                    ContentHandler contentHandler = xslFilter.xsl(
079                            tranformHandler,
080                            "com/liferay/portal/util/dependencies/diff_html.xsl");
081    
082                    HtmlCleaner htmlCleaner = new HtmlCleaner();
083    
084                    DomTreeBuilder oldDomTreeBuilder = new DomTreeBuilder();
085    
086                    htmlCleaner.cleanAndParse(oldSource, oldDomTreeBuilder);
087    
088                    Locale locale = LocaleUtil.getDefault();
089    
090                    TextNodeComparator leftTextNodeComparator = new TextNodeComparator(
091                            oldDomTreeBuilder, locale);
092    
093                    DomTreeBuilder newDomTreeBuilder = new DomTreeBuilder();
094    
095                    htmlCleaner.cleanAndParse(newSource, newDomTreeBuilder);
096    
097                    TextNodeComparator rightTextNodeComparator = new TextNodeComparator(
098                            newDomTreeBuilder, locale);
099    
100                    contentHandler.startDocument();
101                    contentHandler.startElement(
102                            StringPool.BLANK, _DIFF_REPORT, _DIFF_REPORT, new AttributesImpl());
103                    contentHandler.startElement(
104                            StringPool.BLANK, _DIFF, _DIFF, new AttributesImpl());
105    
106                    HtmlSaxDiffOutput htmlSaxDiffOutput = new HtmlSaxDiffOutput(
107                            contentHandler, _DIFF);
108    
109                    HTMLDiffer htmlDiffer = new HTMLDiffer(htmlSaxDiffOutput);
110    
111                    htmlDiffer.diff(leftTextNodeComparator, rightTextNodeComparator);
112    
113                    contentHandler.endElement(StringPool.BLANK, _DIFF, _DIFF);
114                    contentHandler.endElement(StringPool.BLANK, _DIFF_REPORT, _DIFF_REPORT);
115                    contentHandler.endDocument();
116    
117                    unsyncStringWriter.flush();
118    
119                    return unsyncStringWriter.toString();
120            }
121    
122            private static final String _DIFF = "diff";
123    
124            private static final String _DIFF_REPORT = "diffreport";
125    
126    }