KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > editors > CmsMSDHtmlEditor


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/editors/CmsMSDHtmlEditor.java,v $
3  * Date : $Date: 2006/04/28 15:20:52 $
4  * Version: $Revision: 1.17 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.workplace.editors;
33
34 import org.opencms.i18n.CmsEncoder;
35 import org.opencms.jsp.CmsJspActionElement;
36 import org.opencms.main.OpenCms;
37 import org.opencms.util.CmsStringUtil;
38
39 import java.util.ArrayList JavaDoc;
40 import java.util.List JavaDoc;
41 import java.util.Vector JavaDoc;
42 import java.util.regex.Matcher JavaDoc;
43 import java.util.regex.Pattern JavaDoc;
44
45 /**
46  * Creates the output for editing a CmsDefaultPage with the MS DHTML control editor.<p>
47  *
48  * The following editor uses this class:
49  * <ul>
50  * <li>/editors/msdhtml/editor.jsp
51  * </ul>
52  * <p>
53  *
54  * @author Andreas Zahner
55  *
56  * @version $Revision: 1.17 $
57  *
58  * @since 6.0.0
59  */

60 public class CmsMSDHtmlEditor extends CmsSimplePageEditor {
61
62     /** Constant for the editor type, must be the same as the editors subfolder name in the VFS. */
63     public static final String JavaDoc EDITOR_TYPE = "msdhtml";
64
65     /** Option localization keys for editor view select boxes. */
66     public static final String JavaDoc[] SELECTBOX_EDITORVIEWKEYS = {
67         Messages.GUI_EDITOR_MODE_WYSIWYG_0,
68         Messages.GUI_EDITOR_MODE_SOURCE_0};
69
70     /** Option values for editor view select boxes. */
71     public static final String JavaDoc[] SELECTBOX_EDITORVIEWS = {"edithtml", "edit"};
72
73     /** values for editor view select boxes. */
74     public static final int[] SELECTBOX_EDITORVIEWS_ALLOWED = {3, 2};
75
76     /** regex pattern to find all src attribs in img tags, plus all href attribs in anchor tags. */
77     private static final Pattern JavaDoc REGEX_LINKS = Pattern.compile(
78         "<(img|a)(\\s+)(.*?)(src|href)=(\"|\')(.*?)(\"|\')(.*?)>",
79         Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
80
81     /**
82      * Public constructor.<p>
83      *
84      * @param jsp an initialized JSP action element
85      */

86     public CmsMSDHtmlEditor(CmsJspActionElement jsp) {
87
88         super(jsp);
89     }
90
91     /**
92      * Builds the html String for the editor views available in the editor screens.<p>
93      *
94      * @param attributes optional attributes for the &lt;select&gt; tag
95      * @return the html for the editorview selectbox
96      */

97     public final String JavaDoc buildSelectViews(String JavaDoc attributes) {
98
99         Vector JavaDoc names = new Vector JavaDoc();
100         Vector JavaDoc values = new Vector JavaDoc();
101         // get the available views fron the constant
102
String JavaDoc[] contents = SELECTBOX_EDITORVIEWS;
103         for (int i = 0; i < contents.length; i++) {
104             String JavaDoc value = contents[i];
105             values.addElement(value);
106             String JavaDoc s = keyDefault(SELECTBOX_EDITORVIEWKEYS[i], value);
107             names.addElement(s);
108         }
109         int browserId = 0;
110         int loop = 1;
111         int allowedEditors = SELECTBOX_EDITORVIEWS_ALLOWED[browserId];
112         if ("script".equals(getParamElementname())) {
113             allowedEditors = allowedEditors & 510;
114         }
115         List JavaDoc namesFinal = new ArrayList JavaDoc(names.size());
116         List JavaDoc valuesFinal = new ArrayList JavaDoc(values.size());
117         for (int i = 0; i < names.size(); i++) {
118             if ((allowedEditors & loop) > 0) {
119                 valuesFinal.add(values.elementAt(i));
120                 namesFinal.add(names.elementAt(i));
121             }
122             loop <<= 1;
123         }
124         int currentIndex = valuesFinal.indexOf(getParamEditormode());
125         return buildSelect(attributes, namesFinal, valuesFinal, currentIndex, false);
126     }
127
128     /**
129      * @see org.opencms.workplace.editors.CmsDefaultPageEditor#escapeParams()
130      */

131     public void escapeParams() {
132
133         // This code fixes a very strange bug in the MS Dhtml Control.
134
// If the HTML source contains a link like this:
135
// <a HREF=
136
// "/some/url">Link</a>
137
// i.e. with a linebreak between the = and the " of the link href attribute, this
138
// causes the Dhtml control to insert additional buggy attributes in the link.
139
// Solution: Remove the linebreak in this case on the server (see next lines).
140
String JavaDoc result = CmsStringUtil.substitute(getParamContent(), "\r\n", "\n");
141         result = CmsStringUtil.substitute(result, "=\n\"", "=\"");
142         // escape the content
143
result = CmsEncoder.escapeWBlanks(result, CmsEncoder.ENCODING_UTF_8);
144         setParamContent(result);
145     }
146
147     /**
148      * @see org.opencms.workplace.editors.CmsEditor#getEditorResourceUri()
149      */

150     public final String JavaDoc getEditorResourceUri() {
151
152         return getSkinUri() + "editors/" + EDITOR_TYPE + "/";
153     }
154
155     /**
156      * Manipulates the content String for the different editor views and the save operation.<p>
157      *
158      * @param save if set to true, the content parameter is not updated
159      * @return the prepared content String
160      */

161     protected String JavaDoc prepareContent(boolean save) {
162
163         String JavaDoc content = getParamContent();
164         // extract content of <body>...</body> tag
165
content = CmsStringUtil.extractHtmlBody(content);
166         // remove unwanted "&amp;" from links
167
content = filterAnchors(content);
168
169         // ensure all chars in the content are valid for the selected encoding
170
content = CmsEncoder.adjustHtmlEncoding(content, getFileEncoding());
171
172         if (!("edit".equals(getParamEditormode()) || save)) {
173             // editor is in html mode, add tags for stylesheet
174
String JavaDoc stylesheet = getUriStyleSheet();
175
176             // create a head with stylesheet for template and base URL to display images correctly
177
String JavaDoc server = getJsp().getRequest().getScheme()
178                 + "://"
179                 + getJsp().getRequest().getServerName()
180                 + ":"
181                 + getJsp().getRequest().getServerPort();
182             StringBuffer JavaDoc result = new StringBuffer JavaDoc(content.length() + 1024);
183             result.append("<html><head>");
184             if (!"".equals(stylesheet)) {
185                 stylesheet = getJsp().link(stylesheet);
186                 result.append("<link HREF=\"");
187                 result.append(server);
188                 result.append(stylesheet);
189                 result.append("\" rel=\"stylesheet\" type=\"text/css\">");
190             }
191             result.append("<base HREF=\"");
192             result.append(server);
193             result.append(OpenCms.getSystemInfo().getOpenCmsContext());
194             result.append("\"></base></head><body>");
195             result.append(content);
196             result.append("</body></html>");
197             content = result.toString();
198         }
199         if (!save) {
200             // set the content parameter to the modified content
201
setParamContent(content);
202         }
203         return content.trim();
204     }
205
206     /**
207      * Filters the content String and removes unwanted "&amp;" Strings from anchor "href" or "src" attributes.<p>
208      *
209      * These unwanted "&amp;" Strings are produced by the MS DHTML editing control.<p>
210      *
211      * @param content the content of the editor
212      * @return filtered content
213      */

214     private String JavaDoc filterAnchors(String JavaDoc content) {
215
216         String JavaDoc anchor = null;
217         String JavaDoc newAnchor = null;
218
219         // don't forget to update the group index on the matcher after changing the regex below!
220
Matcher JavaDoc matcher = REGEX_LINKS.matcher(content);
221         while (matcher.find()) {
222             anchor = matcher.group(6);
223             newAnchor = CmsStringUtil.substitute(anchor, "&amp;", "&");
224             if (anchor.length() != newAnchor.length()) {
225                 // substitute only if anchor length has changed
226
content = CmsStringUtil.substitute(content, anchor, newAnchor);
227             }
228         }
229         return content;
230     }
231
232 }
233
Popular Tags