KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/editors/CmsDefaultPageEditor.java,v $
3  * Date : $Date: 2006/09/22 15:17:03 $
4  * Version: $Revision: 1.23 $
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.file.CmsFile;
35 import org.opencms.file.CmsPropertyDefinition;
36 import org.opencms.file.CmsRequestContext;
37 import org.opencms.i18n.CmsEncoder;
38 import org.opencms.i18n.CmsLocaleManager;
39 import org.opencms.jsp.CmsJspActionElement;
40 import org.opencms.main.CmsException;
41 import org.opencms.main.CmsLog;
42 import org.opencms.main.OpenCms;
43 import org.opencms.util.CmsHtmlConverter;
44 import org.opencms.util.CmsStringUtil;
45 import org.opencms.xml.CmsXmlException;
46 import org.opencms.xml.page.CmsXmlPage;
47 import org.opencms.xml.page.CmsXmlPageFactory;
48
49 import java.io.IOException JavaDoc;
50 import java.util.ArrayList JavaDoc;
51 import java.util.List JavaDoc;
52 import java.util.Locale JavaDoc;
53 import java.util.Properties JavaDoc;
54
55 import javax.servlet.ServletException JavaDoc;
56 import javax.servlet.jsp.JspException JavaDoc;
57
58 import org.apache.commons.logging.Log;
59
60 /**
61  * Provides methods for building editors for the CmsDefaultPage page type.<p>
62  *
63  * Extend this class for all editors that work with the CmsDefaultPage.<p>
64  *
65  * @author Andreas Zahner
66  *
67  * @version $Revision: 1.23 $
68  *
69  * @since 6.0.0
70  */

71 public abstract class CmsDefaultPageEditor extends CmsEditor {
72
73     /** Parameter name for the request parameter "element name". */
74     public static final String JavaDoc PARAM_ELEMENTNAME = "elementname";
75
76     /** Parameter name for the request parameter "old element name". */
77     public static final String JavaDoc PARAM_OLDELEMENTNAME = "oldelementname";
78
79     /** Name of the special body element from an XMLTemplate. */
80     public static final String JavaDoc XML_BODY_ELEMENT = "body";
81
82     /** The log object for this class. */
83     private static final Log LOG = CmsLog.getLog(CmsDefaultPageEditor.class);
84
85     /** File object used to read and write contents. */
86     protected CmsFile m_file;
87
88     /** Page object used from the action and init methods, be sure to initialize this e.g. in the initWorkplaceRequestValues method. */
89     protected CmsXmlPage m_page;
90
91     private List JavaDoc m_elementList;
92
93     /** The element locale. */
94     private Locale JavaDoc m_elementLocale;
95
96     private String JavaDoc m_paramElementname;
97     private String JavaDoc m_paramOldelementname;
98
99     /** option values for font select boxes. */
100     public static final String JavaDoc[] SELECTBOX_FONTS = {
101         "Arial",
102         "Arial Narrow",
103         "System",
104         "Times New Roman",
105         "Verdana",
106         "Monospace",
107         "SansSerif"};
108
109     /**
110      * Public constructor.<p>
111      *
112      * @param jsp an initialized JSP action element
113      */

114     public CmsDefaultPageEditor(CmsJspActionElement jsp) {
115
116         super(jsp);
117     }
118
119     /**
120      * Performs the change body action of the editor.<p>
121      */

122     public void actionChangeBodyElement() {
123
124         try {
125             // save eventually changed content of the editor to the temporary file
126
Locale JavaDoc oldLocale = CmsLocaleManager.getLocale(getParamOldelementlanguage());
127             performSaveContent(getParamOldelementname(), oldLocale);
128         } catch (CmsException e) {
129             // show error page
130
try {
131                 showErrorPage(this, e);
132             } catch (JspException JavaDoc exc) {
133                 // should usually never happen
134
if (LOG.isInfoEnabled()) {
135                     LOG.info(exc);
136                 }
137             }
138         }
139         // re-initialize the element name if the language has changed
140
if (!getParamElementlanguage().equals(getParamOldelementlanguage())) {
141             initBodyElementName(getParamOldelementname());
142         }
143         // get the new editor content
144
initContent();
145     }
146
147     /**
148      * Performs the cleanup body action of the editor.<p>
149      */

150     public void actionCleanupBodyElement() {
151
152         try {
153             // save eventually changed content of the editor to the temporary file
154
Locale JavaDoc oldLocale = CmsLocaleManager.getLocale(getParamOldelementlanguage());
155             performSaveContent(getParamOldelementname(), oldLocale);
156         } catch (CmsException e) {
157             // show error page
158
try {
159                 showErrorPage(this, e);
160             } catch (JspException JavaDoc exc) {
161                 // should usually never happen
162
if (LOG.isInfoEnabled()) {
163                     LOG.info(exc);
164                 }
165             }
166         }
167     }
168
169     /**
170      * @see org.opencms.workplace.editors.CmsEditor#actionClear(boolean)
171      */

172     public void actionClear(boolean forceUnlock) {
173
174         // delete the temporary file
175
deleteTempFile();
176         boolean directEditMode = Boolean.valueOf(getParamDirectedit()).booleanValue();
177         boolean modified = Boolean.valueOf(getParamModified()).booleanValue();
178         if (directEditMode || forceUnlock || !modified) {
179             // unlock the resource when in direct edit mode, force unlock is true or resource was not modified
180
try {
181                 getCms().unlockResource(getParamResource());
182             } catch (CmsException e) {
183                 // should usually never happen
184
if (LOG.isInfoEnabled()) {
185                     LOG.info(e);
186                 }
187             }
188         }
189     }
190
191     /**
192      * Performs the delete locale action.<p>
193      *
194      * @throws JspException if something goes wrong
195      */

196     public void actionDeleteElementLocale() throws JspException JavaDoc {
197
198         try {
199             Locale JavaDoc loc = getElementLocale();
200             m_page.removeLocale(loc);
201             //write the modified xml content
202
m_file.setContents(m_page.marshal());
203             m_file = getCms().writeFile(m_file);
204             List JavaDoc locales = m_page.getLocales();
205             if (locales.size() > 0) {
206                 // set first locale as new display locale
207
Locale JavaDoc newLoc = (Locale JavaDoc)locales.get(0);
208                 setParamElementlanguage(newLoc.toString());
209                 m_elementLocale = newLoc;
210             } else {
211                 if (LOG.isErrorEnabled()) {
212                     LOG.error(Messages.get().getBundle().key(Messages.LOG_GET_LOCALES_1, getParamResource()));
213                 }
214             }
215             initContent();
216         } catch (CmsXmlException e) {
217             // an error occured while trying to delete the locale, stop action
218
showErrorPage(e);
219         } catch (CmsException e) {
220             // should usually never happen
221
if (LOG.isInfoEnabled()) {
222                 LOG.info(e.getLocalizedMessage(), e);
223             }
224         }
225     }
226
227     /**
228      * Performs a configurable action performed by the editor.<p>
229      *
230      * The default action is: save resource, clear temporary files and publish the resource directly.<p>
231      *
232      * @throws IOException if a forward fails
233      * @throws JspException if including a JSP fails
234      * @throws ServletException if a forward fails
235      */

236     public void actionDirectEdit() throws IOException JavaDoc, JspException JavaDoc, ServletException JavaDoc {
237
238         // get the action class from the OpenCms runtime property
239
I_CmsEditorActionHandler actionClass = OpenCms.getWorkplaceManager().getEditorActionHandler();
240         if (actionClass == null) {
241             // error getting the action class, save content and exit the editor
242
actionSave();
243             actionExit();
244         } else {
245             actionClass.editorAction(this, getJsp());
246         }
247     }
248
249     /**
250      * Performs the exit editor action and deletes the temporary file.<p>
251      *
252      * @see org.opencms.workplace.editors.CmsEditor#actionExit()
253      */

254     public void actionExit() throws IOException JavaDoc, JspException JavaDoc, ServletException JavaDoc {
255
256         if (getAction() == ACTION_CANCEL) {
257             // save and exit was canceled
258
return;
259         }
260         // clear temporary file and unlock resource, if in directedit mode
261
actionClear(false);
262         // close the editor
263
actionClose();
264     }
265
266     /**
267      * Performs the preview page action in a new browser window.<p>
268      *
269      * @throws IOException if redirect fails
270      * @throws JspException if inclusion of error page fails
271      */

272     public void actionPreview() throws IOException JavaDoc, JspException JavaDoc {
273
274         try {
275             // save content of the editor to the temporary file
276
performSaveContent(getParamElementname(), getElementLocale());
277         } catch (CmsException e) {
278             // show error page
279
showErrorPage(this, e);
280         }
281
282         // redirect to the temporary file with current active element language
283
String JavaDoc param = "?" + org.opencms.i18n.CmsLocaleManager.PARAMETER_LOCALE + "=" + getParamElementlanguage();
284         sendCmsRedirect(getParamTempfile() + param);
285     }
286
287     /**
288      * @see org.opencms.workplace.editors.CmsEditor#actionSave()
289      */

290     public void actionSave() throws JspException JavaDoc {
291
292         try {
293
294             // save content to temporary file
295
performSaveContent(getParamElementname(), getElementLocale());
296             // copy the temporary file content back to the original file
297
commitTempFile();
298             // set the modified parameter
299
setParamModified(Boolean.TRUE.toString());
300         } catch (CmsException e) {
301             showErrorPage(e);
302         }
303
304         if (getAction() != ACTION_CANCEL) {
305             // save successful, set save action
306
setAction(ACTION_SAVE);
307         }
308     }
309
310     /**
311      * Builds the html String for the gallery buttons.<p>
312      *
313      * @param options the display configuration for the editor
314      * @param buttonStyle the style for the buttons
315      * @param displayOptions the display options for the editor
316      * @return the html String for the gallery buttons
317      */

318     public abstract String JavaDoc buildGalleryButtons(
319         CmsEditorDisplayOptions options,
320         int buttonStyle,
321         Properties JavaDoc displayOptions);
322
323     /**
324      * Builds the html String for the element language selector.<p>
325      *
326      * @param attributes optional attributes for the &lt;select&gt; tag
327      * @return the html for the element language selectbox
328      */

329     public String JavaDoc buildSelectElementLanguage(String JavaDoc attributes) {
330
331         return buildSelectElementLanguage(attributes, getParamTempfile(), getElementLocale());
332     }
333
334     /**
335      * Builds the html String for the element name selector.<p>
336      *
337      * @param attributes optional attributes for the &lt;select&gt; tag
338      * @return the html for the element name selectbox
339      */

340     public String JavaDoc buildSelectElementName(String JavaDoc attributes) {
341
342         // get the active page elements
343
List JavaDoc elementList = getElementList();
344
345         int counter = 0;
346         int currentIndex = -1;
347         List JavaDoc options = new ArrayList JavaDoc(elementList.size());
348         List JavaDoc values = new ArrayList JavaDoc(elementList.size());
349         String JavaDoc elementName = getParamElementname();
350         if (CmsStringUtil.isEmpty(elementName)) {
351             elementName = getParamOldelementname();
352         }
353         for (int i = 0; i < elementList.size(); i++) {
354             // get the current list element
355
CmsDialogElement element = (CmsDialogElement)elementList.get(i);
356
357             if (CmsStringUtil.isNotEmpty(elementName) && elementName.equals(element.getName())) {
358                 // current element is the displayed one, mark it as selected
359
currentIndex = counter;
360             }
361             if ((!m_page.hasValue(element.getName(), getElementLocale()) && element.isMandantory())
362                 || m_page.isEnabled(element.getName(), getElementLocale())) {
363                 // add element if it is not available or if it is enabled
364
options.add(element.getNiceName());
365                 values.add(element.getName());
366                 counter++;
367             }
368         }
369         return buildSelect(attributes, options, values, currentIndex, false);
370     }
371
372     /**
373      * Builds the html for the font face select box of a WYSIWYG editor.<p>
374      *
375      * @param attributes optional attributes for the &lt;select&gt; tag
376      * @return the html for the font face select box
377      */

378     public String JavaDoc buildSelectFonts(String JavaDoc attributes) {
379
380         List JavaDoc names = new ArrayList JavaDoc();
381         for (int i = 0; i < CmsDefaultPageEditor.SELECTBOX_FONTS.length; i++) {
382             String JavaDoc value = CmsDefaultPageEditor.SELECTBOX_FONTS[i];
383             names.add(value);
384         }
385         return buildSelect(attributes, names, names, -1, false);
386     }
387
388     /**
389      * Escapes the content and title parameters to display them in the editor form.<p>
390      *
391      * This method has to be called on the JSP right before the form display html is created.<p> *
392      */

393     public void escapeParams() {
394
395         // escape the content
396
setParamContent(CmsEncoder.escapeWBlanks(getParamContent(), CmsEncoder.ENCODING_UTF_8));
397     }
398
399     /**
400      * Returns the current element locale.<p>
401      *
402      * @return the current element locale
403      */

404     public Locale JavaDoc getElementLocale() {
405
406         if (m_elementLocale == null) {
407             m_elementLocale = CmsLocaleManager.getLocale(getParamElementlanguage());
408         }
409         return m_elementLocale;
410     }
411
412     /**
413      * Returns the current element name.<p>
414      *
415      * @return the current element name
416      */

417     public String JavaDoc getParamElementname() {
418
419         return m_paramElementname;
420     }
421
422     /**
423      * Returns the old element name.<p>
424      *
425      * @return the old element name
426      */

427     public String JavaDoc getParamOldelementname() {
428
429         return m_paramOldelementname;
430     }
431
432     /**
433      * Returns the OpenCms VFS uri of the style sheet of the current page.<p>
434      *
435      * @return the OpenCms VFS uri of the style sheet of the current page
436      */

437     public String JavaDoc getUriStyleSheet() {
438
439         String JavaDoc result = "";
440         try {
441             String JavaDoc currentTemplate = getUriTemplate();
442             if (!"".equals(currentTemplate)) {
443                 // read the stylesheet from the template file
444
result = getCms().readPropertyObject(currentTemplate, CmsPropertyDefinition.PROPERTY_TEMPLATE, false).getValue(
445                     "");
446             }
447         } catch (CmsException e) {
448             LOG.warn(Messages.get().getBundle().key(Messages.LOG_READ_TEMPLATE_PROP_STYLESHEET_FAILED_0), e);
449         }
450         return result;
451     }
452
453     /**
454      * Returns the OpenCms VFS uri of the template of the current page.<p>
455      *
456      * @return the OpenCms VFS uri of the template of the current page
457      */

458     public String JavaDoc getUriTemplate() {
459
460         String JavaDoc result = "";
461         try {
462             result = getCms().readPropertyObject(getParamTempfile(), CmsPropertyDefinition.PROPERTY_TEMPLATE, true).getValue(
463                 "");
464         } catch (CmsException e) {
465             LOG.warn(Messages.get().getBundle().key(Messages.LOG_READ_TEMPLATE_PROP_FAILED_0), e);
466         }
467         return result;
468     }
469
470     /**
471      * Sets the current element name.<p>
472      *
473      * @param elementName the current element name
474      */

475     public void setParamElementname(String JavaDoc elementName) {
476
477         m_paramElementname = elementName;
478     }
479
480     /**
481      * Sets the old element name.<p>
482      *
483      * @param oldElementName the old element name
484      */

485     public void setParamOldelementname(String JavaDoc oldElementName) {
486
487         m_paramOldelementname = oldElementName;
488     }
489
490     /**
491      * Returns the list of active elements of the page.<p>
492      *
493      * @return the list of active elements of the page
494      */

495     protected List JavaDoc getElementList() {
496
497         if (m_elementList == null) {
498             m_elementList = CmsDialogElements.computeElements(getCms(), m_page, getParamTempfile(), getElementLocale());
499         }
500         return m_elementList;
501     }
502
503     /**
504      * Initializes the body element language for the first call of the editor.<p>
505      */

506     protected void initBodyElementLanguage() {
507
508         List JavaDoc locales = m_page.getLocales();
509         Locale JavaDoc defaultLocale = (Locale JavaDoc)OpenCms.getLocaleManager().getDefaultLocales(
510             getCms(),
511             getCms().getSitePath(m_file)).get(0);
512
513         if (locales.size() == 0) {
514             // no body present, create default body
515
if (!m_page.hasValue(CmsDefaultPageEditor.XML_BODY_ELEMENT, defaultLocale)) {
516                 m_page.addValue(CmsDefaultPageEditor.XML_BODY_ELEMENT, defaultLocale);
517             }
518             try {
519                 m_file.setContents(m_page.marshal());
520                 getCms().writeFile(m_file);
521             } catch (CmsException e) {
522                 // show error page
523
try {
524                     showErrorPage(this, e);
525                 } catch (JspException JavaDoc exc) {
526                     // should usually never happen
527
if (LOG.isInfoEnabled()) {
528                         LOG.info(exc);
529                     }
530                 }
531             }
532             setParamElementlanguage(defaultLocale.toString());
533         } else {
534             // body present, get the language
535
if (locales.contains(defaultLocale)) {
536                 // get the body for the default language
537
setParamElementlanguage(defaultLocale.toString());
538             } else {
539                 // get the first body that can be found
540
setParamElementlanguage(locales.get(0).toString());
541             }
542
543         }
544     }
545
546     /**
547      * Initializes the body element name of the editor.<p>
548      *
549      * This has to be called after the element language has been set with setParamBodylanguage().<p>
550      *
551      * @param elementName the name of the element to initialize or null, if default element should be used
552      */

553     protected void initBodyElementName(String JavaDoc elementName) {
554
555         if (elementName == null
556             || (m_page.hasValue(elementName, getElementLocale()) && !m_page.isEnabled(elementName, getElementLocale()))) {
557             // elementName not specified or given element is disabled, determine default element
558
List JavaDoc allElements = m_page.getNames(getElementLocale());
559             int elementCount = allElements.size();
560             List JavaDoc elements = new ArrayList JavaDoc(elementCount);
561             for (int i = 0; i < elementCount; i++) {
562                 // filter disabled elements
563
if (m_page.isEnabled((String JavaDoc)allElements.get(i), getElementLocale())) {
564                     elements.add(allElements.get(i));
565                 }
566             }
567
568             // get the active page elements
569
List JavaDoc elementList = getElementList();
570             for (int i = 0; i < elementList.size(); i++) {
571                 CmsDialogElement checkElement = (CmsDialogElement)elementList.get(i);
572                 if (elements.contains(checkElement.getName())) {
573                     // get the first active element from the element list
574
setParamElementname(checkElement.getName());
575                     return;
576                 }
577             }
578
579             // no matching active element found
580
if (elements.contains(CmsDefaultPageEditor.XML_BODY_ELEMENT)) {
581                 // default legacy element present, use it
582
setParamElementname(CmsDefaultPageEditor.XML_BODY_ELEMENT);
583             } else {
584                 // use the first element from the element list
585
setParamElementname((String JavaDoc)elements.get(0));
586             }
587         } else {
588             // elementName specified and element is enabled or not present, set to elementName
589
setParamElementname(elementName);
590         }
591     }
592
593     /**
594      * This method has to be called after initializing the body element name and language.<p>
595      *
596      * @see org.opencms.workplace.editors.CmsEditor#initContent()
597      */

598     protected void initContent() {
599
600         if (CmsStringUtil.isNotEmpty(getParamContent())) {
601             if (CmsStringUtil.isNotEmpty(getParamElementname())
602                 && getParamElementname().equals(getParamOldelementname())) {
603                 if (CmsStringUtil.isNotEmpty(getParamElementlanguage())
604                     && getParamElementlanguage().equals(getParamOldelementlanguage())) {
605                     return;
606                 }
607             }
608         }
609         getCms().getRequestContext().setAttribute(CmsRequestContext.ATTRIBUTE_EDITOR, new Boolean JavaDoc(true));
610
611         String JavaDoc elementData;
612         if (m_page.hasValue(getParamElementname(), getElementLocale())) {
613             // element value is available in the page
614
elementData = m_page.getStringValue(getCms(), getParamElementname(), getElementLocale());
615         } else {
616             // value is not available in the page
617
if (Boolean.valueOf(getParamDirectedit()).booleanValue()) {
618                 // direct edit on a non-existing element: create new element with this name
619
m_page.addValue(getParamElementname(), getElementLocale());
620             }
621             elementData = "";
622         }
623         setParamContent(elementData);
624     }
625
626     /**
627      * Saves the editor content to the temporary file.<p>
628      *
629      * @param body the body name to write
630      * @param locale the body locale to write
631      * @throws CmsException if writing the file fails
632      */

633     protected void performSaveContent(String JavaDoc body, Locale JavaDoc locale) throws CmsException {
634
635         // prepare the content for saving
636
String JavaDoc content = prepareContent(true);
637
638         String JavaDoc contentConversion = m_page.getConversion();
639         // check if cleanup was selected in the editor, we have to add the cleanup parameter
640
if (EDITOR_CLEANUP.equals(getParamAction())) {
641             if ((contentConversion == null) || (contentConversion.equals(CmsHtmlConverter.PARAM_DISABLED))) {
642                 // if the current conversion mode is "false" only, we have to remove the "false" value and set it to "cleanup", as "false" will be stronger than all other values
643
contentConversion = CmsHtmlConverter.PARAM_WORD;
644             } else {
645                 // add "cleanup" to the already existing values
646
contentConversion += ";" + CmsHtmlConverter.PARAM_WORD;
647             }
648         }
649         m_page.setConversion(contentConversion);
650
651         // create the element if necessary and if content is present
652
if (!m_page.hasValue(body, locale) && !"".equals(content)) {
653             m_page.addValue(body, locale);
654         }
655
656         // get the enabled state of the element
657
boolean enabled = m_page.isEnabled(body, locale);
658
659         // set the element data
660
if (m_page.hasValue(body, locale)) {
661             m_page.setStringValue(getCms(), body, locale, content);
662         }
663
664         // write the file
665
m_file.setContents(m_page.marshal());
666         m_file = getCms().writeFile(m_file);
667
668         // content might have been modified during write operation
669
m_page = CmsXmlPageFactory.unmarshal(getCms(), m_file);
670         if (m_page.hasValue(body, locale)) {
671             getCms().getRequestContext().setAttribute(CmsRequestContext.ATTRIBUTE_EDITOR, new Boolean JavaDoc(true));
672             content = m_page.getStringValue(getCms(), body, locale);
673             if (content == null) {
674                 content = "";
675             }
676             setParamContent(content);
677             prepareContent(false);
678             m_page.setEnabled(body, locale, enabled);
679         }
680     }
681
682     /**
683      * Manipulates the content String for different editor views and the save operation.<p>
684      *
685      * @param save if set to true, the result String is not escaped and the content parameter is not updated
686      * @return the prepared content String
687      */

688     protected abstract String JavaDoc prepareContent(boolean save);
689
690 }
Popular Tags