KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > jsp > CmsJspTagProperty


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/jsp/CmsJspTagProperty.java,v $
3  * Date : $Date: 2006/03/27 14:52:19 $
4  * Version: $Revision: 1.21 $
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.jsp;
33
34 import org.opencms.flex.CmsFlexController;
35 import org.opencms.i18n.CmsEncoder;
36 import org.opencms.main.CmsException;
37 import org.opencms.main.CmsLog;
38 import org.opencms.staticexport.CmsLinkManager;
39 import org.opencms.util.CmsStringUtil;
40
41 import java.util.Arrays JavaDoc;
42 import java.util.List JavaDoc;
43
44 import javax.servlet.ServletRequest JavaDoc;
45 import javax.servlet.jsp.JspException JavaDoc;
46 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
47
48 import org.apache.commons.logging.Log;
49
50 /**
51  * Provides access to the properties of a resource in the OpenCms VFS .<p>
52  *
53  * Of particular importance is the setting of the <code>file</code> attribute,
54  * which can take the following values.<p>
55  *
56  * This attribute allows you to specify where to search for the property.<BR>
57  * The following values are supported:
58  * </P>
59  * <DL>
60  * <DT><b>uri</b> (default)
61  * <DD> Look up the property on the file with the
62  * uri requested by the user.
63  * <DT><b>search.uri</b> or <b>search</b>
64  * <DD>Look up the property by also checking all parent folders for the property,
65  * starting with the file with uri requested by the user and
66  * going "upward" if the property was not found there.
67  * <DT><b>element.uri</b>
68  * <DD>Look up the property on the currently
69  * processed sub - element. This is useful in templates or other pages that
70  * consist of many elements.
71  * <DT><b>search.element.uri</b>
72  * <DD>Look up the property by also checking all parent folders for the
73  * property, starting with the file with the currently processed sub -
74  * element and going "upward" if the property was not found there.
75  * <DT><B>{some-file-uri}</B>
76  * <DD>Look up the property on that exact file
77  * uri in the OpenCms VFS,<EM> fallback if no other valid option is
78  * selected for the file attribute.</EM>
79  * </DD>
80  * </DL>
81  *
82  * <P>There are also some deprecated options for the "file" value that are
83  * still supported but should not longer be used:</P>
84  * <DL>
85  * <DT>parent
86  * <DD>same as <STRONG>uri</STRONG>
87  * <DT>search-parent
88  * <DD>same as <STRONG>search.uri</STRONG>
89  * <DT>this
90  * <DD>same as <STRONG>element.uri</STRONG>
91  * <DT>search-this
92  * <DD>same as <STRONG>search.element.uri</STRONG></DD>
93  * </DL>
94  *
95  * @author Alexander Kandzior
96  *
97  * @version $Revision: 1.21 $
98  *
99  * @since 6.0.0
100  */

101 public class CmsJspTagProperty extends TagSupport JavaDoc {
102
103     /** Serial version UID required for safe serialization. */
104     private static final long serialVersionUID = -4040833541258687977L;
105
106     /** Accessor constant: Use element uri. */
107     public static final String JavaDoc USE_ELEMENT_URI = "element.uri";
108
109     /** Accessor constant: Use parent (same as USE_URI). */
110     public static final String JavaDoc USE_PARENT = "parent";
111
112     /** Accessor constant: Use search (same as USE_SEARCH_URI). */
113     public static final String JavaDoc USE_SEARCH = "search";
114
115     /** Accessor constant: Use search element uri. */
116     public static final String JavaDoc USE_SEARCH_ELEMENT_URI = "search.element.uri";
117
118     /** Accessor constant: Search parent (same as USE_SEARCH_URI). */
119     public static final String JavaDoc USE_SEARCH_PARENT = "search-parent";
120
121     /** Accessor constant: Use seach this (same as USE_SEARCH_ELEMENT_URI). */
122     public static final String JavaDoc USE_SEARCH_THIS = "search-this";
123
124     /** Accessor constant: Search uri. */
125     public static final String JavaDoc USE_SEARCH_URI = "search.uri";
126
127     /** Accessor constant: Use this (same as USE_ELEMENT_URI). */
128     public static final String JavaDoc USE_THIS = "this";
129
130     /** Accessor constant: Use uri. */
131     public static final String JavaDoc USE_URI = "uri";
132
133     /** Static array of the possible "file" properties. */
134     // automatic member sorting will cause compilation error (static order) due to the naming convention.
135
public static final String JavaDoc[] ACTION_VALUES = {
136         USE_URI,
137         USE_PARENT,
138         USE_SEARCH,
139         USE_SEARCH_URI,
140         USE_SEARCH_PARENT,
141         USE_ELEMENT_URI,
142         USE_THIS,
143         USE_SEARCH_ELEMENT_URI,
144         USE_SEARCH_THIS};
145
146     /** Array list for fast lookup. */
147     // automatic member sorting will cause compilation error.
148
public static final List JavaDoc ACTION_VALUES_LIST = Arrays.asList(ACTION_VALUES);
149
150     /** The log object for this class. */
151     private static final Log LOG = CmsLog.getLog(CmsJspTagProperty.class);
152
153     /** The default value. */
154     private String JavaDoc m_defaultValue;
155
156     /** Indicates if HTML should be escaped. */
157     private boolean m_escapeHtml;
158
159     /** The file to read the property from. */
160     private String JavaDoc m_propertyFile;
161
162     /** The name of the property to read. */
163     private String JavaDoc m_propertyName;
164
165     /**
166      * Internal action method.<p>
167      *
168      * @param property the property to look up
169      * @param action the search action
170      * @param defaultValue the default value
171      * @param escape if the result html should be escaped or not
172      * @param req the current request
173      * @return String the value of the property or <code>null</code> if not found (and no
174      * defaultValue provided)
175      * @throws CmsException if something goes wrong
176      */

177     public static String JavaDoc propertyTagAction(
178         String JavaDoc property,
179         String JavaDoc action,
180         String JavaDoc defaultValue,
181         boolean escape,
182         ServletRequest JavaDoc req) throws CmsException {
183
184         CmsFlexController controller = CmsFlexController.getController(req);
185
186         // if action is not set use default
187
if (action == null) {
188             action = ACTION_VALUES[0];
189         }
190
191         String JavaDoc value;
192         String JavaDoc vfsUri;
193         boolean search;
194         switch (ACTION_VALUES_LIST.indexOf(action)) {
195             case 0: // USE_URI
196
case 1: // USE_PARENT
197
// read properties of parent (i.e. top requested) file
198
vfsUri = controller.getCmsObject().getRequestContext().getUri();
199                 search = false;
200                 break;
201             case 2: // USE_SEARCH
202
case 3: // USE_SEARCH_URI
203
case 4: // USE_SEARCH_PARENT
204
// try to find property on parent file and all parent folders
205
vfsUri = controller.getCmsObject().getRequestContext().getUri();
206                 search = true;
207                 break;
208             case 5: // USE_ELEMENT_URI
209
case 6: // USE_THIS
210
// read properties of this file
211
vfsUri = controller.getCurrentRequest().getElementUri();
212                 search = false;
213                 break;
214             case 7: // USE_SEARCH_ELEMENT_URI
215
case 8: // USE_SEARCH_THIS
216
// try to find property on this file and all parent folders
217
vfsUri = controller.getCurrentRequest().getElementUri();
218                 search = true;
219                 break;
220             default:
221                 // read properties of the file named in the attribute
222
vfsUri = CmsLinkManager.getAbsoluteUri(action, controller.getCurrentRequest().getElementUri());
223                 search = false;
224         }
225         // now read the property from the VFS
226
value = controller.getCmsObject().readPropertyObject(vfsUri, property, search).getValue(defaultValue);
227         if (escape) {
228             // HTML escape the value
229
value = CmsEncoder.escapeHtml(value);
230         }
231         return value;
232     }
233
234     /**
235      * @return SKIP_BODY
236      * @throws JspException in case somethins goes wrong
237      * @see javax.servlet.jsp.tagext.Tag#doStartTag()
238      */

239     public int doStartTag() throws JspException JavaDoc {
240
241         ServletRequest JavaDoc req = pageContext.getRequest();
242
243         // This will always be true if the page is called through OpenCms
244
if (CmsFlexController.isCmsRequest(req)) {
245
246             try {
247                 String JavaDoc prop = propertyTagAction(getName(), getFile(), m_defaultValue, m_escapeHtml, req);
248                 // Make sure that no null String is returned
249
if (prop == null) {
250                     prop = "";
251                 }
252                 pageContext.getOut().print(prop);
253
254             } catch (Exception JavaDoc ex) {
255                 if (LOG.isErrorEnabled()) {
256                     LOG.error(Messages.get().getBundle().key(Messages.ERR_PROCESS_TAG_1, "property"), ex);
257                 }
258                 throw new javax.servlet.jsp.JspException JavaDoc(ex);
259             }
260         }
261         return SKIP_BODY;
262     }
263
264     /**
265      * Returns the default value.<p>
266      *
267      * @return the default value
268      */

269     public String JavaDoc getDefault() {
270
271         return m_defaultValue != null ? m_defaultValue : "";
272     }
273
274     /**
275      * The value of the escape html flag.<p>
276      *
277      * @return the value of the escape html flag
278      */

279     public String JavaDoc getEscapeHtml() {
280
281         return "" + m_escapeHtml;
282     }
283
284     /**
285      * Returns the file name.<p>
286      *
287      * @return the file name
288      */

289     public String JavaDoc getFile() {
290
291         return m_propertyFile != null ? m_propertyFile : "parent";
292     }
293
294     /**
295      * Returns the property name.<p>
296      *
297      * @return String the property name
298      */

299     public String JavaDoc getName() {
300
301         return m_propertyName != null ? m_propertyName : "";
302     }
303
304     /**
305      * @see javax.servlet.jsp.tagext.Tag#release()
306      */

307     public void release() {
308
309         super.release();
310         m_propertyFile = null;
311         m_propertyName = null;
312         m_defaultValue = null;
313         m_escapeHtml = false;
314     }
315
316     /**
317      * Sets the default value.<p>
318      *
319      * This is used if a selected property is not found.<p>
320      *
321      * @param def the default value
322      */

323     public void setDefault(String JavaDoc def) {
324
325         if (def != null) {
326             m_defaultValue = def;
327         }
328     }
329
330     /**
331      * Set the escape html flag.<p>
332      *
333      * @param value should be <code>"true"</code> or <code>"false"</code> (all values other then <code>"true"</code> are
334      * considered to be false)
335      */

336     public void setEscapeHtml(String JavaDoc value) {
337
338         if (value != null) {
339             m_escapeHtml = Boolean.valueOf(value.trim()).booleanValue();
340         }
341     }
342
343     /**
344      * Sets the file name.<p>
345      *
346      * @param file the file name
347      */

348     public void setFile(String JavaDoc file) {
349
350         if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(file)) {
351             m_propertyFile = file;
352         }
353     }
354
355     /**
356      * Sets the property name.<p>
357      *
358      * @param name the property name to set
359      */

360     public void setName(String JavaDoc name) {
361
362         if (name != null) {
363             m_propertyName = name;
364         }
365     }
366
367 }
368
Popular Tags