001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.workplace.comparison;
029
030import com.alkacon.diff.Diff;
031
032import org.opencms.i18n.CmsEncoder;
033import org.opencms.jsp.CmsJspActionElement;
034import org.opencms.util.CmsHtml2TextConverter;
035import org.opencms.util.CmsStringUtil;
036import org.opencms.workplace.CmsWorkplaceSettings;
037
038import javax.servlet.http.HttpServletRequest;
039import javax.servlet.http.HttpServletResponse;
040import javax.servlet.jsp.JspWriter;
041import javax.servlet.jsp.PageContext;
042
043/**
044 * Provides a GUI for the file comparison dialog.<p>
045 *
046 * @since 6.0.0
047 */
048public class CmsHtmlDifferenceDialog extends CmsDifferenceDialog {
049
050    /** constant indicating that the html is to be compared.<p> */
051    public static final String MODE_HTML = "html";
052
053    /** constant indicating that a textual representation of the html is to be compared.<p> */
054    public static final String MODE_TEXT = "text";
055
056    /** request parameter for the textmode.<p> */
057    public static final String PARAM_TEXTMODE = "textmode";
058
059    /**
060     * Default constructor.<p>
061     *
062     * @param jsp an initialized JSP action element
063     */
064    public CmsHtmlDifferenceDialog(CmsJspActionElement jsp) {
065
066        super(jsp);
067    }
068
069    /**
070     * Public constructor with JSP variables.<p>
071     *
072     * @param context the JSP page context
073     * @param req the JSP request
074     * @param res the JSP response
075     */
076    public CmsHtmlDifferenceDialog(PageContext context, HttpServletRequest req, HttpServletResponse res) {
077
078        this(new CmsJspActionElement(context, req, res));
079    }
080
081    /**
082     * Performs the dialog actions depending on the initialized action and displays the dialog form.<p>
083     *
084     * @throws Exception if writing to the JSP out fails
085     */
086    @Override
087    public void displayDialog() throws Exception {
088
089        if (getAction() == ACTION_CANCEL) {
090            actionCloseDialog();
091        }
092        JspWriter out = getJsp().getJspContext().getOut();
093        out.print("<link rel='stylesheet' type='text/css' href='");
094        out.print(getStyleUri(getJsp()));
095        out.println("diff.css'>");
096        out.println(dialogContentStart(getParamTitle()));
097        out.print("<form name='diff-form' method='post' action='");
098        out.print(getDialogUri());
099        out.println("'>");
100        out.println(allParamsAsHidden());
101        out.println("</form>");
102        out.println("<p>");
103        out.println(getDiffOnlyButtonsHtml());
104
105        String onClic1 = "javascript:document.forms['diff-form'].textmode.value = '";
106        onClic1 += MODE_TEXT;
107        onClic1 += "'; document.forms['diff-form'].submit();";
108        String onClic2 = "javascript:document.forms['diff-form'].textmode.value = '";
109        onClic2 += MODE_HTML;
110        onClic2 += "'; document.forms['diff-form'].submit();";
111        out.println(
112            getTwoButtonsHtml(
113                Messages.get().container(Messages.GUI_DIFF_MODE_TEXT_0).key(getLocale()),
114                Messages.get().container(Messages.GUI_DIFF_MODE_HTML_0).key(getLocale()),
115                onClic1,
116                onClic2,
117                MODE_HTML.equals(getParamTextmode())));
118
119        out.println("</p>");
120        out.println(dialogBlockStart(null));
121        out.println("<table cellspacing='0' cellpadding='0' class='xmlTable'>\n<tr><td><pre style='overflow:auto'>");
122        try {
123            CmsHtmlDifferenceConfiguration conf = new CmsHtmlDifferenceConfiguration(
124                getMode() == CmsDiffViewMode.ALL ? -1 : getLinesBeforeSkip(),
125                getLocale());
126            String originalSource = getOriginalSource();
127            String copySource = getCopySource();
128            if (MODE_TEXT.equals(getParamTextmode())) {
129                originalSource = CmsHtml2TextConverter.html2text(originalSource, CmsEncoder.ENCODING_ISO_8859_1);
130                copySource = CmsHtml2TextConverter.html2text(copySource, CmsEncoder.ENCODING_ISO_8859_1);
131            }
132            String diff = Diff.diffAsHtml(originalSource, copySource, conf);
133            if (CmsStringUtil.isNotEmpty(diff)) {
134                out.println(diff);
135            } else {
136                String htmlDiff = Diff.diffAsHtml(getOriginalSource(), getCopySource(), conf);
137                if (CmsStringUtil.isNotEmpty(htmlDiff)) {
138                    // extracted text is equal, but html differs
139                    out.println(
140                        Messages.get().container(Messages.GUI_COMPARE_IDENTICAL_TEXT_DIFFERENT_HTML_0).key(
141                            getLocale()));
142                } else if (getMode() == CmsDiffViewMode.ALL) {
143                    // print original source, if there are no differences
144                    out.println(wrapLinesWithUnchangedStyle(
145                        CmsStringUtil.substitute(CmsStringUtil.escapeHtml(originalSource), "<br/>", "")));
146                }
147            }
148        } catch (Exception e) {
149            out.print(e);
150        }
151        out.println("</pre></td></tr>\n</table>");
152        out.println(dialogBlockEnd());
153        out.println(dialogContentEnd());
154        out.println(dialogEnd());
155        out.println(bodyEnd());
156        out.println(htmlEnd());
157    }
158
159    /**
160     * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
161     */
162    @Override
163    protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) {
164
165        super.initWorkplaceRequestValues(settings, request);
166        if (CmsStringUtil.isEmptyOrWhitespaceOnly(getParamTextmode())) {
167            // ensure a valid mode is set
168            setParamTextmode(MODE_TEXT);
169        }
170    }
171
172    /**
173     *
174     * @see org.opencms.workplace.comparison.A_CmsDiffViewDialog#validateParamaters()
175     */
176    @Override
177    protected void validateParamaters() {
178
179        // noop
180    }
181}