KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > comparison > CmsHtmlDifferenceDialog


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/comparison/CmsHtmlDifferenceDialog.java,v $
3  * Date : $Date: 2006/03/28 07:53:22 $
4  * Version: $Revision: 1.3 $
5  *
6  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
7  * All rights reserved.
8  *
9  * This source code is the intellectual property of Alkacon Software GmbH.
10  * It is PROPRIETARY and CONFIDENTIAL.
11  * Use of this source code is subject to license terms.
12  *
13  * In order to use this source code, you need written permission from
14  * Alkacon Software GmbH. Redistribution of this source code, in modified
15  * or unmodified form, is not allowed unless written permission by
16  * Alkacon Software GmbH has been given.
17  *
18  * ALKACON SOFTWARE GMBH MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY
19  * OF THIS SOURCE CODE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
20  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
21  * PURPOSE, OR NON-INFRINGEMENT. ALKACON SOFTWARE GMBH SHALL NOT BE LIABLE FOR ANY
22  * DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
23  * THIS SOURCE CODE OR ITS DERIVATIVES.
24  *
25  * For further information about Alkacon Software GmbH, please see the
26  * company website: http://www.alkacon.com
27  */

28
29 package org.opencms.workplace.comparison;
30
31 import com.alkacon.diff.Diff;
32
33 import org.opencms.i18n.CmsEncoder;
34 import org.opencms.jsp.CmsJspActionElement;
35 import org.opencms.util.CmsHtml2TextConverter;
36 import org.opencms.util.CmsStringUtil;
37 import org.opencms.workplace.CmsWorkplaceSettings;
38
39 import javax.servlet.http.HttpServletRequest JavaDoc;
40 import javax.servlet.http.HttpServletResponse JavaDoc;
41 import javax.servlet.jsp.JspWriter JavaDoc;
42 import javax.servlet.jsp.PageContext JavaDoc;
43
44 /**
45  * Provides a GUI for the file comparison dialog.<p>
46  *
47  * @author Jan Baudisch
48  *
49  * @version $Revision: 1.3 $
50  *
51  * @since 6.0.0
52  */

53 public class CmsHtmlDifferenceDialog extends CmsDifferenceDialog {
54
55     /** constant indicating that the html is to be compared.<p> */
56     public static final String JavaDoc MODE_HTML = "html";
57
58     /** constant indicating that a textual representation of the html is to be compared.<p> */
59     public static final String JavaDoc MODE_TEXT = "text";
60
61     /** request parameter for the textmode.<p> */
62     public static final String JavaDoc PARAM_TEXTMODE = "textmode";
63
64     /**
65      * Default constructor.<p>
66      *
67      * @param jsp an initialized JSP action element
68      */

69     public CmsHtmlDifferenceDialog(CmsJspActionElement jsp) {
70
71         super(jsp);
72     }
73
74     /**
75      * Public constructor with JSP variables.<p>
76      *
77      * @param context the JSP page context
78      * @param req the JSP request
79      * @param res the JSP response
80      */

81     public CmsHtmlDifferenceDialog(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
82
83         this(new CmsJspActionElement(context, req, res));
84     }
85
86     /**
87      * Performs the dialog actions depending on the initialized action and displays the dialog form.<p>
88      *
89      * @throws Exception if writing to the JSP out fails
90      */

91     public void displayDialog() throws Exception JavaDoc {
92
93         if (getAction() == ACTION_CANCEL) {
94             actionCloseDialog();
95         }
96         JspWriter JavaDoc out = getJsp().getJspContext().getOut();
97         out.print("<link rel='stylesheet' type='text/css' HREF='");
98         out.print(getStyleUri(getJsp()));
99         out.println("diff.css'>");
100         out.println(dialogContentStart(getParamTitle()));
101         out.print("<form name='diff-form' method='post' action='");
102         out.print(getDialogUri());
103         out.println("'>");
104         out.println(allParamsAsHidden());
105         out.println("</form>");
106         out.println("<p>");
107         out.println(getDiffOnlyButtonsHtml());
108         
109         String JavaDoc onClic1 = "javascript:document.forms['diff-form'].textmode.value = '";
110         onClic1 += MODE_TEXT;
111         onClic1 += "'; document.forms['diff-form'].submit();";
112         String JavaDoc onClic2 = "javascript:document.forms['diff-form'].textmode.value = '";
113         onClic2 += MODE_HTML;
114         onClic2 += "'; document.forms['diff-form'].submit();";
115         out.println(getTwoButtonsHtml(Messages.get().container(Messages.GUI_DIFF_MODE_TEXT_0).key(getLocale()),
116             Messages.get().container(Messages.GUI_DIFF_MODE_HTML_0).key(getLocale()), onClic1, onClic2,
117             MODE_HTML.equals(getParamTextmode())
118         ));
119         
120         out.println("</p>");
121         out.println(dialogBlockStart(null));
122         out.println("<table cellspacing='0' cellpadding='0' class='xmlTable'>\n<tr><td><pre style='overflow:auto'>");
123         try {
124             CmsHtmlDifferenceConfiguration conf = new CmsHtmlDifferenceConfiguration(
125                 getMode() == CmsDiffViewMode.ALL ? -1 : getLinesBeforeSkip(),
126                 getLocale());
127             String JavaDoc originalSource = getOriginalSource();
128             String JavaDoc copySource = getCopySource();
129             if (MODE_TEXT.equals(getParamTextmode())) {
130                 originalSource = CmsHtml2TextConverter.html2text(originalSource, CmsEncoder.ENCODING_ISO_8859_1);
131                 copySource = CmsHtml2TextConverter.html2text(copySource, CmsEncoder.ENCODING_ISO_8859_1);
132             }
133             String JavaDoc diff = Diff.diffAsHtml(originalSource, copySource, conf);
134             if (CmsStringUtil.isNotEmpty(diff)) {
135                 out.println(diff);
136             } else {
137                 String JavaDoc htmlDiff = Diff.diffAsHtml(getOriginalSource(), getCopySource(), conf);
138                 if (CmsStringUtil.isNotEmpty(htmlDiff)) {
139                     // extracted text is equal, but html differs
140
out.println(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(CmsStringUtil.substitute(
145                         CmsStringUtil.escapeHtml(originalSource),
146                         "<br/>",
147                         "")));
148                 }
149             }
150         } catch (Exception JavaDoc e) {
151             out.print(e);
152         }
153         out.println("</pre></td></tr>\n</table>");
154         out.println(dialogBlockEnd());
155         out.println(dialogContentEnd());
156         out.println(dialogEnd());
157         out.println(bodyEnd());
158         out.println(htmlEnd());
159     }
160
161     /**
162      * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
163      */

164     protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest JavaDoc request) {
165
166         super.initWorkplaceRequestValues(settings, request);
167         if (CmsStringUtil.isEmptyOrWhitespaceOnly(getParamTextmode())) {
168             // ensure a valid mode is set
169
setParamTextmode(MODE_TEXT);
170         }
171     }
172
173     /**
174      *
175      * @see org.opencms.workplace.comparison.A_CmsDiffViewDialog#validateParamaters()
176      */

177     protected void validateParamaters() {
178
179         // noop
180
}
181 }
Popular Tags