001    /**
002     * Copyright (c) 2000-present 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.diff;
016    
017    import com.liferay.portal.kernel.diff.DiffHtml;
018    import com.liferay.portal.kernel.io.unsync.UnsyncStringWriter;
019    import com.liferay.portal.kernel.security.pacl.DoPrivileged;
020    import com.liferay.portal.kernel.util.LocaleUtil;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.StringUtil;
023    
024    import java.io.Reader;
025    
026    import java.util.Locale;
027    
028    import javax.xml.transform.TransformerFactory;
029    import javax.xml.transform.sax.SAXTransformerFactory;
030    import javax.xml.transform.sax.TransformerHandler;
031    import javax.xml.transform.stream.StreamResult;
032    
033    import org.outerj.daisy.diff.HtmlCleaner;
034    import org.outerj.daisy.diff.XslFilter;
035    import org.outerj.daisy.diff.html.HTMLDiffer;
036    import org.outerj.daisy.diff.html.HtmlSaxDiffOutput;
037    import org.outerj.daisy.diff.html.TextNodeComparator;
038    import org.outerj.daisy.diff.html.dom.DomTreeBuilder;
039    
040    import org.xml.sax.ContentHandler;
041    import org.xml.sax.InputSource;
042    import org.xml.sax.helpers.AttributesImpl;
043    
044    /**
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     *
049     * @author Julio Camarero
050     */
051    @DoPrivileged
052    public class DiffHtmlImpl implements DiffHtml {
053    
054            /**
055             * This is a diff method with default values.
056             *
057             * @param  source the source text
058             * @param  target the modified version of the source text
059             * @return a string containing the HTML code of the source text showing the
060             *         differences with the target text
061             * @throws Exception if an exception occurred
062             */
063            @Override
064            public String diff(Reader source, Reader target) throws Exception {
065                    if (source == null) {
066                            throw new NullPointerException("Source is null");
067                    }
068    
069                    if (target == null) {
070                            throw new NullPointerException("Target is null");
071                    }
072    
073                    InputSource oldSource = new InputSource(source);
074                    InputSource newSource = new InputSource(target);
075    
076                    UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter();
077    
078                    SAXTransformerFactory saxTransformerFactory =
079                            (SAXTransformerFactory)TransformerFactory.newInstance();
080    
081                    TransformerHandler tranformHandler =
082                            saxTransformerFactory.newTransformerHandler();
083    
084                    tranformHandler.setResult(new StreamResult(unsyncStringWriter));
085    
086                    XslFilter xslFilter = new XslFilter();
087    
088                    ContentHandler contentHandler = xslFilter.xsl(
089                            tranformHandler,
090                            "com/liferay/portal/util/dependencies/diff_html.xsl");
091    
092                    HtmlCleaner htmlCleaner = new HtmlCleaner();
093    
094                    DomTreeBuilder oldDomTreeBuilder = new DomTreeBuilder();
095    
096                    htmlCleaner.cleanAndParse(oldSource, oldDomTreeBuilder);
097    
098                    Locale locale = LocaleUtil.getDefault();
099    
100                    TextNodeComparator leftTextNodeComparator = new TextNodeComparator(
101                            oldDomTreeBuilder, locale);
102    
103                    DomTreeBuilder newDomTreeBuilder = new DomTreeBuilder();
104    
105                    htmlCleaner.cleanAndParse(newSource, newDomTreeBuilder);
106    
107                    TextNodeComparator rightTextNodeComparator = new TextNodeComparator(
108                            newDomTreeBuilder, locale);
109    
110                    contentHandler.startDocument();
111                    contentHandler.startElement(
112                            StringPool.BLANK, _DIFF_REPORT, _DIFF_REPORT, new AttributesImpl());
113                    contentHandler.startElement(
114                            StringPool.BLANK, _DIFF, _DIFF, new AttributesImpl());
115    
116                    HtmlSaxDiffOutput htmlSaxDiffOutput = new HtmlSaxDiffOutput(
117                            contentHandler, _DIFF);
118    
119                    HTMLDiffer htmlDiffer = new HTMLDiffer(htmlSaxDiffOutput);
120    
121                    htmlDiffer.diff(leftTextNodeComparator, rightTextNodeComparator);
122    
123                    contentHandler.endElement(StringPool.BLANK, _DIFF, _DIFF);
124                    contentHandler.endElement(StringPool.BLANK, _DIFF_REPORT, _DIFF_REPORT);
125                    contentHandler.endDocument();
126    
127                    unsyncStringWriter.flush();
128    
129                    String string = unsyncStringWriter.toString();
130    
131                    if (string.startsWith("<?xml")) {
132                            int index = string.indexOf("?>");
133    
134                            string = string.substring(index + 2);
135                    }
136    
137                    return string;
138            }
139    
140            @Override
141            public String replaceStyles(String html) {
142                    return StringUtil.replace(
143                            html,
144                            new String[] {
145                                    "changeType=\"diff-added-image\"",
146                                    "changeType=\"diff-changed-image\"",
147                                    "changeType=\"diff-removed-image\"",
148                                    "class=\"diff-html-added\"", "class=\"diff-html-changed\"",
149                                    "class=\"diff-html-removed\""
150                            },
151                            new String[] {
152                                    "style=\"border: 10px solid #CFC;\"",
153                                    "style=\"border: 10px solid blue;\"",
154                                    "style=\"border: 10px solid #FDC6C6;\"",
155                                    "style=\"background-color: #CFC;\"",
156                                    "style=\"border-bottom: 2px dotted blue;\"",
157                                    "style=\"background-color: #FDC6C6; text-decoration: " +
158                                            "line-through;\""
159                            }
160                    );
161            }
162    
163            private static final String _DIFF = "diff";
164    
165            private static final String _DIFF_REPORT = "diffreport";
166    
167    }