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