001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/jsp/CmsJspActionElement.java,v $
003: * Date : $Date: 2008-02-27 12:05:34 $
004: * Version: $Revision: 1.31 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software GmbH, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.jsp;
033:
034: import org.opencms.file.CmsFile;
035: import org.opencms.file.CmsProperty;
036: import org.opencms.flex.CmsFlexController;
037: import org.opencms.i18n.CmsMessageContainer;
038: import org.opencms.i18n.CmsMessages;
039: import org.opencms.loader.CmsImageScaler;
040: import org.opencms.loader.I_CmsResourceLoader;
041: import org.opencms.main.CmsException;
042: import org.opencms.main.OpenCms;
043: import org.opencms.security.CmsSecurityException;
044: import org.opencms.staticexport.CmsLinkManager;
045: import org.opencms.util.CmsStringUtil;
046: import org.opencms.workplace.editors.directedit.CmsDirectEditJspIncludeProvider;
047: import org.opencms.workplace.editors.directedit.CmsDirectEditMode;
048: import org.opencms.workplace.editors.directedit.I_CmsDirectEditProvider;
049:
050: import java.io.UnsupportedEncodingException;
051: import java.util.HashMap;
052: import java.util.Iterator;
053: import java.util.Locale;
054: import java.util.Map;
055:
056: import javax.servlet.http.HttpServletRequest;
057: import javax.servlet.http.HttpServletResponse;
058: import javax.servlet.jsp.JspException;
059: import javax.servlet.jsp.PageContext;
060:
061: /**
062: * Bean to be used in JSP scriptlet code that provides direct
063: * access to the functionality offered by the OpenCms taglib.<p>
064: *
065: * By instantiating a bean of this type and accessing the methods provided by
066: * the instance, all functionality of the OpenCms JSP taglib can be easily
067: * used from within JSP scriptlet code.<p>
068: *
069: * Initialize this bean at the beginning of your JSP like this:
070: * <pre>
071: * <jsp:useBean id="cms" class="org.opencms.jsp.CmsJspActionElement">
072: * <% cms.init(pageContext, request, response); %>
073: * </jsp:useBean>
074: * </pre>
075: *
076: * You can also access the current users <code>{@link org.opencms.file.CmsObject}</code>
077: * by using <code>{@link org.opencms.jsp.CmsJspBean#getCmsObject()}</code>.<p>
078: *
079: * All exceptions that occur when calling any method of this class are caught
080: * and written to the log output only, so that a template still has a chance of
081: * working at least in some elements.<p>
082: *
083: * @author Alexander Kandzior
084: *
085: * @version $Revision: 1.31 $
086: *
087: * @since 6.0.0
088: */
089: public class CmsJspActionElement extends CmsJspBean {
090:
091: /** Error message in case bean was not properly initialized. */
092: // cannot use a string: At class-loading time the
093: // user request context for localization is not at hand.
094: public static final CmsMessageContainer NOT_INITIALIZED = Messages
095: .get().container(Messages.GUI_ERR_ACTIONELEM_NOT_INIT_0);
096:
097: /** JSP navigation builder. */
098: private CmsJspNavBuilder m_navigation;
099:
100: /**
101: * Empty constructor, required for every JavaBean.
102: */
103: public CmsJspActionElement() {
104:
105: super ();
106: }
107:
108: /**
109: * Constructor, with parameters.
110: *
111: * @param context the JSP page context object
112: * @param req the JSP request
113: * @param res the JSP response
114: */
115: public CmsJspActionElement(PageContext context,
116: HttpServletRequest req, HttpServletResponse res) {
117:
118: super ();
119: init(context, req, res);
120: }
121:
122: /**
123: * Includes the direct edit scriptlet, same as
124: * using the <code><cms:editable /></code> tag.<p>
125: *
126: * The configured default direct edit provider is used.<p>
127: *
128: * @param isEditable include scriptlet only if true
129: *
130: * @throws JspException if something goes wrong
131: */
132: public void editable(boolean isEditable) throws JspException {
133:
134: if (isEditable) {
135: CmsJspTagEditable.editableTagAction(getJspContext(), null,
136: CmsDirectEditMode.AUTO, null);
137: }
138: }
139:
140: /**
141: * Includes the direct edit scriptlet, same as
142: * using the <code><cms:editable file="..." /></code>tag.<p>
143: *
144: * For backward compatibility, this always uses the JSP include based direct edit provider<p>.
145: *
146: * @param isEditable include scriptlet only if true
147: * @param filename file with scriptlet code
148: *
149: * @throws JspException if something goes wrong
150: */
151: public void editable(boolean isEditable, String filename)
152: throws JspException {
153:
154: if (isEditable) {
155: CmsJspTagEditable.editableTagAction(getJspContext(),
156: CmsDirectEditJspIncludeProvider.class.getName(),
157: CmsDirectEditMode.AUTO, filename);
158: }
159: }
160:
161: /**
162: * Includes the direct edit scriptlet, same as
163: * using the <code><cms:editable provider="..." mode="..." file="..." /></code>tag.<p>
164: *
165: * @param provider the direct edit provider class name
166: * @param mode the direct edit mode to use
167: * @param filename file with scriptlet code (may be <code>null</code>)
168: *
169: * @throws JspException if something goes wrong
170: */
171: public void editable(String provider, String mode, String filename)
172: throws JspException {
173:
174: CmsJspTagEditable.editableTagAction(getJspContext(), provider,
175: CmsDirectEditMode.valueOf(mode), filename);
176: }
177:
178: /**
179: * Insert the end HTML for the direct edit buttons in manual mode (if required).<p>
180: *
181: * Same as closing the <code></cms:editable</code> tag after opening one in manual mode.<p>
182: *
183: * @param needsClose result of {@link #editableManualOpen()} should be the value for this parameter
184: *
185: * @throws JspException if something goes wrong
186: */
187: public void editableManualClose(boolean needsClose)
188: throws JspException {
189:
190: if (needsClose) {
191: CmsJspTagEditable.editableTagAction(getJspContext(), null,
192: CmsDirectEditMode.MANUAL, null);
193: }
194: }
195:
196: /**
197: * Insert the start HTML for the direct edit buttons in manual mode.<p>
198: *
199: * Same as opening the <code><cms:editable mode="manual"></code> tag.<p>
200: *
201: * @return <code>true</code> if HTML was inserted that needs to be closed
202: *
203: * @throws JspException if something goes wrong
204: */
205: public boolean editableManualOpen() throws JspException {
206:
207: boolean result = false;
208: if (!CmsFlexController.isCmsOnlineRequest(getJspContext()
209: .getRequest())) {
210: // all this does NOT apply to the "online" project
211: I_CmsDirectEditProvider eb = CmsJspTagEditable
212: .getDirectEditProvider(getJspContext());
213: if ((eb != null)) {
214: // check if the provider support manual placement of buttons
215: if (eb.isManual(CmsDirectEditMode.MANUAL)) {
216: // the provider supports manual placement of buttons
217: result = true;
218: CmsJspTagEditable.editableTagAction(
219: getJspContext(), null,
220: CmsDirectEditMode.MANUAL, null);
221: }
222: }
223: }
224: return result;
225: }
226:
227: /**
228: * Returns the processed output of an OpenCms resource in a String.<p>
229: *
230: * @param target the target to process
231: * @return the processed output of an OpenCms resource in a String
232: */
233: public String getContent(String target) {
234:
235: return getContent(target, null, null);
236: }
237:
238: /**
239: * Returns the processed output of an element within an OpenCms resource.<p>
240: *
241: * @param target the target to process
242: * @param element name of the element
243: * @param locale locale of the element
244: * @return the processed output
245: */
246: public String getContent(String target, String element,
247: Locale locale) {
248:
249: I_CmsResourceLoader loader;
250: CmsFile file;
251: target = toAbsolute(target);
252:
253: try {
254: file = getCmsObject().readFile(target);
255: loader = OpenCms.getResourceManager().getLoader(file);
256: } catch (ClassCastException e) {
257: // no loader implementation found
258: return CmsMessages.formatUnknownKey(e.getMessage());
259: } catch (CmsException e) {
260: // file might not exist or no read permissions
261: return CmsMessages.formatUnknownKey(e.getMessage());
262: }
263:
264: try {
265: byte[] result = loader.dump(getCmsObject(), file, element,
266: locale, getRequest(), getResponse());
267: return new String(result, getRequestContext().getEncoding());
268: } catch (UnsupportedEncodingException uee) {
269: // encoding unsupported
270: return CmsMessages.formatUnknownKey(uee.getMessage());
271: } catch (Throwable t) {
272: // any other exception, check for hidden root cause first
273: Throwable cause = CmsFlexController
274: .getThrowable(getRequest());
275: if (cause == null) {
276: cause = t;
277: }
278: handleException(cause);
279: return CmsMessages.formatUnknownKey(cause.getMessage());
280: }
281: }
282:
283: /**
284: * Generates an initialized instance of {@link CmsMessages} for
285: * convenient access to localized resource bundles.<p>
286: *
287: * @param bundleName the name of the ResourceBundle to use
288: * @param locale the locale to use for localization
289: *
290: * @return CmsMessages a message bundle initialized with the provided values
291: */
292: public CmsMessages getMessages(String bundleName, Locale locale) {
293:
294: return new CmsMessages(bundleName, locale);
295: }
296:
297: /**
298: * Generates an initialized instance of {@link CmsMessages} for
299: * convenient access to localized resource bundles.<p>
300: *
301: * @param bundleName the name of the ResourceBundle to use
302: * @param language language identifier for the locale of the bundle
303: * @return CmsMessages a message bundle initialized with the provided values
304: */
305: public CmsMessages getMessages(String bundleName, String language) {
306:
307: return getMessages(bundleName, language, "", "", null);
308: }
309:
310: /**
311: * Generates an initialized instance of {@link CmsMessages} for
312: * convenient access to localized resource bundles.<p>
313: *
314: * @param bundleName the name of the ResourceBundle to use
315: * @param language language identifier for the locale of the bundle
316: * @param defaultLanguage default for the language, will be used
317: * if language is null or empty String "", and defaultLanguage is not null
318: * @return CmsMessages a message bundle initialized with the provided values
319: */
320: public CmsMessages getMessages(String bundleName, String language,
321: String defaultLanguage) {
322:
323: return getMessages(bundleName, language, "", "",
324: defaultLanguage);
325: }
326:
327: /**
328: * Generates an initialized instance of {@link CmsMessages} for
329: * convenient access to localized resource bundles.<p>
330: *
331: * @param bundleName the name of the ResourceBundle to use
332: * @param language language identifier for the locale of the bundle
333: * @param country 2 letter country code for the locale of the bundle
334: * @param variant a vendor or browser-specific variant code
335: * @param defaultLanguage default for the language, will be used
336: * if language is null or empty String "", and defaultLanguage is not null
337: * @return CmsMessages a message bundle initialized with the provided values
338: *
339: * @see java.util.ResourceBundle
340: * @see org.opencms.i18n.CmsMessages
341: */
342: public CmsMessages getMessages(String bundleName, String language,
343: String country, String variant, String defaultLanguage) {
344:
345: try {
346: if ((defaultLanguage != null)
347: && CmsStringUtil.isEmpty(language)) {
348: language = defaultLanguage;
349: }
350: if (language == null) {
351: language = "";
352: }
353: if (country == null) {
354: country = "";
355: }
356: if (variant == null) {
357: variant = "";
358: }
359: return getMessages(bundleName, new Locale(language,
360: country, variant));
361: } catch (Throwable t) {
362: handleException(t);
363: }
364: return null;
365: }
366:
367: /**
368: * Returns an initialized {@link CmsJspNavBuilder} instance.<p>
369: *
370: * @return CmsJspNavBuilder an initialized <code>CmsJspNavBuilder</code>
371: *
372: * @see org.opencms.jsp.CmsJspNavBuilder
373: */
374: public CmsJspNavBuilder getNavigation() {
375:
376: if (isNotInitialized()) {
377: return null;
378: }
379: if (m_navigation == null) {
380: m_navigation = new CmsJspNavBuilder(getCmsObject());
381: }
382: return m_navigation;
383: }
384:
385: /**
386: * Returns the HTML for an <code><img src="..." /></code> tag that includes the given image scaling parameters.<p>
387: *
388: * @param target the target URI of the file in the OpenCms VFS
389: * @param scaler the image scaler to use for scaling the image
390: * @param attributes a map of additional HTML attributes that are added to the output
391: *
392: * @return the HTML for an <code><img src></code> tag that includes the given image scaling parameters
393: */
394: public String img(String target, CmsImageScaler scaler,
395: Map attributes) {
396:
397: return img(target, scaler, attributes, false);
398: }
399:
400: /**
401: * Returns the HTML for an <code><img src="..." /></code> tag that includes the given image scaling parameters.<p>
402: *
403: * @param target the target URI of the file in the OpenCms VFS
404: * @param scaler the image scaler to use for scaling the image
405: * @param attributes a map of additional HTML attributes that are added to the output
406: * @param partialTag if <code>true</code>, the opening <code><img</code> and closing <code> /></code> is omitted
407: *
408: * @return the HTML for an <code><img src></code> tag that includes the given image scaling parameters
409: */
410: public String img(String target, CmsImageScaler scaler,
411: Map attributes, boolean partialTag) {
412:
413: try {
414: return CmsJspTagImage.imageTagAction(target, scaler,
415: attributes, partialTag, getRequest());
416: } catch (Throwable t) {
417: handleException(t);
418: }
419: CmsMessageContainer msgContainer = Messages.get().container(
420: Messages.GUI_ERR_IMG_SCALE_2, target,
421: scaler == null ? "null" : scaler.toString());
422: return getMessage(msgContainer);
423: }
424:
425: /**
426: * Include a sub-element without parameters from the OpenCms VFS, same as
427: * using the <code><cms:include file="***" /></code> tag.<p>
428: *
429: * @param target the target URI of the file in the OpenCms VFS (can be relative or absolute)
430: * @throws JspException in case there were problems including the target
431: *
432: * @see org.opencms.jsp.CmsJspTagInclude
433: */
434: public void include(String target) throws JspException {
435:
436: include(target, null, null);
437: }
438:
439: /**
440: * Include a named sub-element without parameters from the OpenCms VFS, same as
441: * using the <code><cms:include file="***" element="***" /></code> tag.<p>
442: *
443: * @param target the target URI of the file in the OpenCms VFS (can be relative or absolute)
444: * @param element the element (template selector) to display from the target
445: * @throws JspException in case there were problems including the target
446: *
447: * @see org.opencms.jsp.CmsJspTagInclude
448: */
449: public void include(String target, String element)
450: throws JspException {
451:
452: include(target, element, null);
453: }
454:
455: /**
456: * Include a named sub-element without parameters from the OpenCms VFS, same as
457: * using the <code><cms:include file="***" element="***" /></code> tag.<p>
458: *
459: * @param target the target URI of the file in the OpenCms VFS (can be relative or absolute)
460: * @param element the element (template selector) to display from the target
461: * @param editable flag to indicate if direct edit should be enabled for the element
462: * @throws JspException in case there were problems including the target
463: *
464: * @see org.opencms.jsp.CmsJspTagInclude
465: */
466: public void include(String target, String element, boolean editable)
467: throws JspException {
468:
469: include(target, element, editable, null);
470: }
471:
472: /**
473: * Include a named sub-element with parameters from the OpenCms VFS, same as
474: * using the <code><cms:include file="***" element="***" /></code> tag
475: * with parameters in the tag body.<p>
476: *
477: * The parameter map should be a map where the keys are Strings
478: * (the parameter names) and the values are of type String[].
479: * However, as a convenience feature,
480: * in case you provide just a String for the parameter value,
481: * it will automatically be translated to a String[1].<p>
482: *
483: * The handling of the <code>element</code> parameter depends on the
484: * included file type. Most often it is used as template selector.<p>
485: *
486: * <b>Important:</b> Exceptions that occur in the include process are NOT
487: * handled even if {@link #setSupressingExceptions(boolean)} was set to <code>true</code>.
488: *
489: * @param target the target URI of the file in the OpenCms VFS (can be relative or absolute)
490: * @param element the element (template selector) to display from the target
491: * @param editable flag to indicate if direct edit should be enabled for the element
492: * @param parameterMap a map of the request parameters
493: * @throws JspException in case there were problems including the target
494: *
495: * @see org.opencms.jsp.CmsJspTagInclude
496: */
497: public void include(String target, String element,
498: boolean editable, Map parameterMap) throws JspException {
499:
500: if (isNotInitialized()) {
501: return;
502: }
503: if (parameterMap != null) {
504: try {
505: HashMap modParameterMap = new HashMap(parameterMap
506: .size());
507: // ensure parameters are always of type String[] not just String
508: Iterator i = parameterMap.entrySet().iterator();
509: while (i.hasNext()) {
510: Map.Entry entry = (Map.Entry) i.next();
511: String key = (String) entry.getKey();
512: Object value = entry.getValue();
513: if (value instanceof String[]) {
514: modParameterMap.put(key, value);
515: } else {
516: if (value == null) {
517: value = "null";
518: }
519: String[] newValue = new String[] { value
520: .toString() };
521: modParameterMap.put(key, newValue);
522: }
523: }
524: parameterMap = modParameterMap;
525: } catch (UnsupportedOperationException e) {
526: // parameter map is immutable, just use it "as is"
527: }
528: }
529: CmsJspTagInclude.includeTagAction(getJspContext(), target,
530: element, editable, parameterMap, getRequest(),
531: getResponse());
532: }
533:
534: /**
535: * Include a named sub-element with parameters from the OpenCms VFS, same as
536: * using the <code><cms:include file="***" element="***" /></code> tag
537: * with parameters in the tag body.<p>
538: *
539: * @param target the target URI of the file in the OpenCms VFS (can be relative or absolute)
540: * @param element the element (template selector) to display from the target
541: * @param parameterMap a map of the request parameters
542: * @throws JspException in case there were problems including the target
543: *
544: * @see org.opencms.jsp.CmsJspTagInclude
545: */
546: public void include(String target, String element, Map parameterMap)
547: throws JspException {
548:
549: include(target, element, false, parameterMap);
550: }
551:
552: /**
553: * Includes a named sub-element suppressing all Exceptions that occur during the include,
554: * otherwise the same as using {@link #include(String, String, Map)}.<p>
555: *
556: * This is a convenience method that allows to include elements on a page without checking
557: * if they exist or not. If the target element does not exist, nothing is printed to
558: * the JSP output.<p>
559: *
560: * @param target the target URI of the file in the OpenCms VFS (can be relative or absolute)
561: * @param element the element (template selector) to display from the target
562: */
563: public void includeSilent(String target, String element) {
564:
565: try {
566: include(target, element, null);
567: } catch (Throwable t) {
568: // ignore
569: }
570: }
571:
572: /**
573: * Includes a named sub-element suppressing all Exceptions that occur during the include,
574: * otherwise the same as using {@link #include(String, String, Map)}.<p>
575: *
576: * This is a convenience method that allows to include elements on a page without checking
577: * if they exist or not. If the target element does not exist, nothing is printed to
578: * the JSP output.<p>
579: *
580: * @param target the target URI of the file in the OpenCms VFS (can be relative or absolute)
581: * @param element the element (template selector) to display from the target
582: * @param editable flag to indicate if direct edit should be enabled for the element
583: */
584: public void includeSilent(String target, String element,
585: boolean editable) {
586:
587: try {
588: include(target, element, editable, null);
589: } catch (Throwable t) {
590: // ignore
591: }
592: }
593:
594: /**
595: * Includes a named sub-element suppressing all Exceptions that occur during the include,
596: * otherwise the same as using {@link #include(String, String, Map)}.<p>
597: *
598: * This is a convenience method that allows to include elements on a page without checking
599: * if they exist or not. If the target element does not exist, nothing is printed to
600: * the JSP output.<p>
601: *
602: * @param target the target URI of the file in the OpenCms VFS (can be relative or absolute)
603: * @param element the element (template selector) to display from the target
604: * @param editable flag to indicate if direct edit should be enabled for the element
605: * @param parameterMap a map of the request parameters
606: */
607: public void includeSilent(String target, String element,
608: boolean editable, Map parameterMap) {
609:
610: try {
611: include(target, element, editable, parameterMap);
612: } catch (Throwable t) {
613: // ignore
614: }
615: }
616:
617: /**
618: * Includes a named sub-element suppressing all Exceptions that occur during the include,
619: * otherwise the same as using {@link #include(String, String, Map)}.<p>
620: *
621: * This is a convenience method that allows to include elements on a page without checking
622: * if they exist or not. If the target element does not exist, nothing is printed to
623: * the JSP output.<p>
624: *
625: * @param target the target URI of the file in the OpenCms VFS (can be relative or absolute)
626: * @param element the element (template selector) to display from the target
627: * @param parameterMap a map of the request parameters
628: */
629: public void includeSilent(String target, String element,
630: Map parameterMap) {
631:
632: try {
633: include(target, element, parameterMap);
634: } catch (Throwable t) {
635: // ignore
636: }
637: }
638:
639: /**
640: * Returns an OpenCms or JVM system info property value, same as using
641: * the <code><cms:info property="***" /></code> tag.<p>
642: *
643: * See the description of the class {@link CmsJspTagInfo} for a detailed list
644: * of available options for the property value.<p>
645: *
646: * @param property the property to look up
647: * @return String the value of the system property
648: * @see org.opencms.jsp.CmsJspTagInfo
649: */
650: public String info(String property) {
651:
652: try {
653: return CmsJspTagInfo.infoTagAction(property, getRequest());
654: } catch (Throwable t) {
655: handleException(t);
656: }
657: CmsMessageContainer msgContainer = Messages.get().container(
658: Messages.GUI_ERR_INFO_PROP_READ_1, property);
659: return getMessage(msgContainer);
660: }
661:
662: /**
663: * Returns an OpenCms workplace label.<p>
664: *
665: * You should consider using a standard
666: * {@link java.util.ResourceBundle java.util.ResourceBundle} instead of the
667: * OpenCms workplace language files.<p>
668: *
669: * @param label the label to look up
670: * @return label the value of the label
671: *
672: * @see org.opencms.jsp.CmsJspTagLabel
673: */
674: public String label(String label) {
675:
676: if (isNotInitialized()) {
677: return getMessage(NOT_INITIALIZED);
678: }
679: try {
680: return CmsJspTagLabel.wpLabelTagAction(label, getRequest());
681: } catch (Throwable t) {
682: handleException(t);
683: }
684: CmsMessageContainer msgContainer = Messages.get().container(
685: Messages.GUI_ERR_WORKPL_LABEL_READ_1, label);
686: return getMessage(msgContainer);
687: }
688:
689: /**
690: * Returns a link to a file in the OpenCms VFS
691: * that has been adjusted according to the web application path and the
692: * OpenCms static export rules.<p>
693: *
694: * Please note that the target is always assumed to be in the OpenCms VFS, so you can't use
695: * this method for links external to OpenCms.<p>
696: *
697: * Relative links are converted to absolute links, using the current element URI as base.<p>
698: *
699: * This is the same as using the <code><cms:link>***</cms:link></code> tag.<p>
700: *
701: * @param target the URI in the OpenCms VFS to link to
702: * @return the translated link
703: *
704: * @see org.opencms.jsp.CmsJspTagLink
705: */
706: public String link(String target) {
707:
708: if (isNotInitialized()) {
709: return getMessage(NOT_INITIALIZED);
710: }
711: try {
712: return CmsJspTagLink.linkTagAction(target, getRequest());
713: } catch (Throwable t) {
714: handleException(t);
715: }
716: CmsMessageContainer msgContainer = Messages.get().container(
717: Messages.GUI_ERR_GEN_LINK_1, target);
718: return getMessage(msgContainer);
719: }
720:
721: /**
722: * Returns all properties of the current file.<p>
723: *
724: * @return Map all properties of the current file
725: */
726: public Map properties() {
727:
728: return properties(null);
729: }
730:
731: /**
732: * Returns all properties of the selected file.<p>
733: *
734: * Please see the description of the class {@link org.opencms.jsp.CmsJspTagProperty} for
735: * valid options of the <code>file</code> parameter.<p>
736: *
737: * @param file the file (or folder) to look at for the properties
738: * @return Map all properties of the current file
739: * (and optional of the folders containing the file)
740: *
741: * @see org.opencms.jsp.CmsJspTagProperty
742: */
743: public Map properties(String file) {
744:
745: if (isNotInitialized()) {
746: return new HashMap();
747: }
748: Map value = new HashMap();
749: try {
750: if (file == null) {
751: file = CmsJspTagProperty.USE_URI;
752: }
753: switch (CmsJspTagProperty.ACTION_VALUES_LIST.indexOf(file)) {
754: case 0: // USE_URI
755: case 1: // USE_PARENT
756: value = CmsProperty.toMap(getCmsObject()
757: .readPropertyObjects(
758: getRequestContext().getUri(), false));
759: break;
760: case 2: // USE_SEARCH
761: case 3: // USE_SEARCH_URI
762: case 4: // USE_SEARCH_PARENT
763: value = CmsProperty.toMap(getCmsObject()
764: .readPropertyObjects(
765: getRequestContext().getUri(), true));
766: break;
767: case 5: // USE_ELEMENT_URI
768: case 6: // USE_THIS
769: // Read properties of this file
770: value = CmsProperty.toMap(getCmsObject()
771: .readPropertyObjects(
772: getController().getCurrentRequest()
773: .getElementUri(), false));
774: break;
775: case 7: // USE_SEARCH_ELEMENT_URI
776: case 8: // USE_SEARCH_THIS
777: // Try to find property on this file and all parent folders
778: value = CmsProperty.toMap(getCmsObject()
779: .readPropertyObjects(
780: getController().getCurrentRequest()
781: .getElementUri(), true));
782: break;
783: default:
784: // Read properties of the file named in the attribute
785: value = CmsProperty.toMap(getCmsObject()
786: .readPropertyObjects(toAbsolute(file), false));
787: }
788: } catch (Throwable t) {
789: handleException(t);
790: }
791: return value;
792: }
793:
794: /**
795: * Returns a selected file property value, same as using
796: * the <code><cms:property name="***" /></code> tag or
797: * calling {@link #property(String, String, String, boolean)}.<p>
798: *
799: * @param name the name of the property to look for
800: * @return the value of the property found, or null if the property could not be found
801: *
802: * @see #property(String, String, String, boolean)
803: * @see org.opencms.jsp.CmsJspTagProperty
804: */
805: public String property(String name) {
806:
807: return property(name, null, null, false);
808: }
809:
810: /**
811: * Returns a selected file property value, same as using
812: * the <code><cms:property name="***" file="***" /></code> tag or
813: * calling {@link #property(String, String, String, boolean)}.<p>
814: *
815: * @param name the name of the property to look for
816: * @param file the file (or folder) to look at for the property
817: * @return the value of the property found, or null if the property could not be found
818: *
819: * @see #property(String, String, String, boolean)
820: * @see org.opencms.jsp.CmsJspTagProperty
821: */
822: public String property(String name, String file) {
823:
824: return property(name, file, null, false);
825: }
826:
827: /**
828: * Returns a selected file property value, same as using
829: * the <code><cms:property name="***" file="***" default="***" /></code> tag or
830: * calling {@link #property(String, String, String, boolean)}.<p>
831: *
832: * @param name the name of the property to look for
833: * @param file the file (or folder) to look at for the property
834: * @param defaultValue a default value in case the property was not found
835: * @return the value of the property found, or the value of defaultValue
836: * if the property could not be found
837: *
838: * @see #property(String, String, String, boolean)
839: * @see org.opencms.jsp.CmsJspTagProperty
840: */
841: public String property(String name, String file, String defaultValue) {
842:
843: return property(name, file, defaultValue, false);
844: }
845:
846: /**
847: * Returns a selected file property value with optional HTML escaping, same as using
848: * the <code><cms:property name="***" file="***" default="***" /></code> tag.<p>
849: *
850: * Please see the description of the class {@link org.opencms.jsp.CmsJspTagProperty} for
851: * valid options of the <code>file</code> parameter.<p>
852: *
853: * @param name the name of the property to look for
854: * @param file the file (or folder) to look at for the property
855: * @param defaultValue a default value in case the property was not found
856: * @param escapeHtml if <code>true</code>, special HTML characters in the return value
857: * are escaped with their number representations (e.g. & becomes &#38;)
858: * @return the value of the property found, or the value of defaultValue
859: * if the property could not be found
860: *
861: * @see org.opencms.jsp.CmsJspTagProperty
862: */
863: public String property(String name, String file,
864: String defaultValue, boolean escapeHtml) {
865:
866: if (isNotInitialized()) {
867: return getMessage(NOT_INITIALIZED);
868: }
869: try {
870: if (file == null) {
871: file = getController().getCmsObject()
872: .getRequestContext().getUri();
873: }
874: return CmsJspTagProperty.propertyTagAction(name, file,
875: defaultValue, escapeHtml, getRequest());
876: } catch (CmsSecurityException e) {
877: if (defaultValue == null) {
878: handleException(e);
879: }
880: } catch (Throwable t) {
881: handleException(t);
882: }
883: if (defaultValue == null) {
884: CmsMessageContainer msgContainer = Messages.get()
885: .container(Messages.GUI_ERR_FILE_PROP_MISSING_2,
886: name, file);
887: return getMessage(msgContainer);
888: } else {
889: return defaultValue;
890: }
891: }
892:
893: /**
894: * Checks if a template part should be used or not, same as using
895: * the <code><cms:template element="***" /></code> tag.<p>
896: *
897: * @param element the template element to check
898: * @return <code>true</code> if the element is active, <code>false</code> otherwise
899: *
900: * @see org.opencms.jsp.CmsJspTagUser
901: */
902: public boolean template(String element) {
903:
904: return template(element, null, false);
905: }
906:
907: /**
908: * Checks if a template part should be used or not, same as using
909: * the <code><cms:template ifexists="***" /></code> tag.<p>
910: *
911: * @param elementlist the list of elements to check
912: * @param checkall <code>true</code> if all elements in the list should be checked
913: * @return <code>true</code> if the elements available, <code>false</code> otherwise
914: *
915: * @see org.opencms.jsp.CmsJspTagUser
916: */
917: public boolean template(String elementlist, boolean checkall) {
918:
919: return template(null, elementlist, checkall);
920: }
921:
922: /**
923: * Checks if a template part should be used or not, same as using
924: * the <code><cms:template element="***" ifexists="***" /></code> tag.<p>
925: *
926: * @param element the template element to check
927: * @param elementlist the list of elements to check
928: * @param checkall <code>true</code> if all elements in the list should be checked
929: * @return <code>true</code> if the element is active, <code>false</code> otherwise
930: *
931: * @see org.opencms.jsp.CmsJspTagUser
932: */
933: public boolean template(String element, String elementlist,
934: boolean checkall) {
935:
936: if (isNotInitialized()) {
937: return true;
938: }
939: try {
940: return CmsJspTagTemplate.templateTagAction(element,
941: elementlist, checkall, false, getRequest());
942: } catch (Throwable t) {
943: handleException(t);
944: }
945: return true;
946: }
947:
948: /**
949: * Converts a relative URI in the OpenCms VFS to an absolute one based on
950: * the location of the currently processed OpenCms URI.<p>
951: *
952: * @param target the relative URI to convert
953: * @return the target URI converted to an absolute one
954: */
955: public String toAbsolute(String target) {
956:
957: if (isNotInitialized()) {
958: return getMessage(NOT_INITIALIZED);
959: }
960: return CmsLinkManager.getAbsoluteUri(target, getController()
961: .getCurrentRequest().getElementUri());
962: }
963:
964: /**
965: * Returns a selected user property, i.e. information about the currently
966: * logged in user, same as using
967: * the <code><cms:user property="***" /></code> tag.<p>
968: *
969: * @param property the user property to display, please see the tag documentation for valid options
970: * @return the value of the selected user property
971: *
972: * @see org.opencms.jsp.CmsJspTagUser
973: */
974: public String user(String property) {
975:
976: if (isNotInitialized()) {
977: return getMessage(NOT_INITIALIZED);
978: }
979: try {
980: return CmsJspTagUser.userTagAction(property, getRequest());
981: } catch (Throwable t) {
982: handleException(t);
983: }
984: CmsMessageContainer msgContainer = Messages.get().container(
985: Messages.GUI_ERR_USER_PROP_READ_1, property);
986: return getMessage(msgContainer);
987: }
988: }
|