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