Source code: com/opencms/flex/jsp/CmsJspTagProperty.java
1 /*
2 * File : $Source: /usr/local/cvs/opencms/src/com/opencms/flex/jsp/Attic/CmsJspTagProperty.java,v $
3 * Date : $Date: 2003/05/13 13:18:20 $
4 * Version: $Revision: 1.9.2.1 $
5 *
6 * This library is part of OpenCms -
7 * the Open Source Content Mananagement System
8 *
9 * Copyright (C) 2002 - 2003 Alkacon Software (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, 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 com.opencms.flex.jsp;
33
34 import com.opencms.core.CmsException;
35 import com.opencms.flex.cache.CmsFlexController;
36 import com.opencms.util.Encoder;
37
38 import javax.servlet.ServletRequest;
39
40 /**
41 * Provides access to the properties of a resource in the OpenCms VFS .<p>
42 *
43 * Of particular importance is the setting of the <code>file</code> attribute,
44 * which can take the following values.<p>
45 *
46 * This attribute allows you to specify where to search for the property.<BR>
47 * The following values are supported:
48 * </P>
49 * <DL>
50 * <DT><b>uri</b> (default)
51 * <DD> Look up the property on the file with the
52 * uri requested by the user.
53 * <DT><b>search.uri</b> or <b>search</b>
54 * <DD>Look up the property by also checking all parent folders for the property,
55 * starting with the file with uri requested by the user and
56 * going "upward" if the property was not found there.
57 * <DT><b>element.uri</b>
58 * <DD>Look up the property on the currently
59 * processed sub - element. This is useful in templates or other pages that
60 * consist of many elements.
61 * <DT><b>search.element.uri</b>
62 * <DD>Look up the property by also checking all parent folders for the
63 * property, starting with the file with the currently processed sub -
64 * element and going "upward" if the property was not found there.
65 * <DT><B>{some-file-uri}</B>
66 * <DD>Look up the property on that exact file
67 * uri in the OpenCms VFS,<EM> fallback if no other valid option is
68 * selected for the file attribute.</EM>
69 * </DD>
70 * </DL>
71 *
72 * <P>There are also some deprecated options for the "file" value that are
73 * still supported but should not longer be used:</P>
74 * <DL>
75 * <DT>parent
76 * <DD>same as <STRONG>uri</STRONG>
77 * <DT>search-parent
78 * <DD>same as <STRONG>search.uri</STRONG>
79 * <DT>this
80 * <DD>same as <STRONG>element.uri</STRONG>
81 * <DT>search-this
82 * <DD>same as <STRONG>search.element.uri</STRONG></DD>
83 * </DL>
84 *
85 * @author Alexander Kandzior (a.kandzior@alkacon.com)
86 * @version $Revision: 1.9.2.1 $
87 */
88 public class CmsJspTagProperty extends javax.servlet.jsp.tagext.TagSupport {
89
90 // internal member variables
91 private String m_propertyName = null;
92 private String m_propertyFile = null;
93 private String m_defaultValue = null;
94 private boolean m_escapeHtml = false;
95
96 // public accessor constants
97 public static final String USE_URI = "uri";
98 public static final String USE_PARENT = "parent";
99 public static final String USE_SEARCH = "search";
100 public static final String USE_SEARCH_URI = "search.uri";
101 public static final String USE_SEARCH_PARENT = "search-parent";
102 public static final String USE_ELEMENT_URI = "element.uri";
103 public static final String USE_THIS = "this";
104 public static final String USE_SEARCH_ELEMENT_URI = "search.element.uri";
105 public static final String USE_SEARCH_THIS = "search-this";
106
107 // DEBUG flag
108 private final static int DEBUG = 0;
109
110 /** static array of the possible "file" properties */
111 public static final String[] m_actionValues = {
112 USE_URI,
113 USE_PARENT,
114 USE_SEARCH,
115 USE_SEARCH_URI,
116 USE_SEARCH_PARENT,
117 USE_ELEMENT_URI,
118 USE_THIS,
119 USE_SEARCH_ELEMENT_URI,
120 USE_SEARCH_THIS
121 };
122
123 /** array list for fast lookup */
124 public static final java.util.List m_actionValue =
125 java.util.Arrays.asList(m_actionValues);
126
127 /**
128 * Sets the property name.<p>
129 *
130 * @param name the property name to set
131 */
132 public void setName(String name) {
133 if (name != null) {
134 m_propertyName = name;
135 }
136 }
137
138 /**
139 * Returns the property name.<p>
140 *
141 * @return String the property name
142 */
143 public String getName() {
144 return m_propertyName!=null?m_propertyName:"";
145 }
146
147 /**
148 * Sets the default value.<p>
149 *
150 * This is used if a selected property is not found.<p>
151 *
152 * @param def the default value
153 */
154 public void setDefault(String def) {
155 if (def != null) {
156 m_defaultValue = def;
157 }
158 }
159
160 /**
161 * Returns the default value.<p>
162 *
163 * @return the default value
164 */
165 public String getDefault() {
166 return m_defaultValue!=null?m_defaultValue:"";
167 }
168
169 /**
170 * Sets the file name.<p>
171 *
172 * @param file the file name
173 */
174 public void setFile(String file) {
175 if (file != null) {
176 m_propertyFile = file.toLowerCase();
177 }
178 }
179
180 /**
181 * Returns the file name.<p>
182 *
183 * @return the file name
184 */
185 public String getFile() {
186 return m_propertyFile!=null?m_propertyFile:"parent";
187 }
188
189 /**
190 * Set the escape html flag.<p>
191 *
192 * @param value should be "true" or "false" (all values other then "true" are
193 * considered to be false)
194 */
195 public void setEscapeHtml(String value) {
196 if (value != null) {
197 m_escapeHtml = "true".equalsIgnoreCase(value.trim());
198 }
199 }
200
201 /**
202 * The value of the escape html flag.<p>
203 *
204 * @return the value of the escape html flag
205 */
206 public String getEscapeHtml() {
207 return "" + m_escapeHtml;
208 }
209
210 /**
211 * @see javax.servlet.jsp.tagext.Tag#release()
212 */
213 public void release() {
214 super.release();
215 m_propertyFile = null;
216 m_propertyName = null;
217 m_defaultValue = null;
218 m_escapeHtml = false;
219 }
220
221 /**
222 * @return SKIP_BODY
223 * @see javax.servlet.jsp.tagext.Tag#doStartTag()
224 */
225 public int doStartTag() throws javax.servlet.jsp.JspException {
226
227 ServletRequest req = pageContext.getRequest();
228
229 // This will always be true if the page is called through OpenCms
230 if (CmsFlexController.isCmsRequest(req)) {
231
232 try {
233 String prop = propertyTagAction(getName(), getFile(), m_defaultValue, m_escapeHtml, req);
234 // Make sure that no null String is returned
235 if (prop == null) prop = "";
236 pageContext.getOut().print(prop);
237
238 } catch (Exception ex) {
239 System.err.println("Error in Jsp 'property' tag processing: " + ex);
240 System.err.println(com.opencms.util.Utils.getStackTrace(ex));
241 throw new javax.servlet.jsp.JspException(ex);
242 }
243 }
244 return SKIP_BODY;
245 }
246
247 /**
248 * Internal action method.<p>
249 *
250 * @param property the property to look up
251 * @param action the search action
252 * @param defaultValue the default value
253 * @param escape if the result html should be escaped or not
254 * @param req the current request
255 * @return String the value of the property or <code>null</code> if not found (and no
256 * defaultValue provided)
257 * @throws CmsException if something goes wrong
258 */
259 public static String propertyTagAction(
260 String property,
261 String action,
262 String defaultValue,
263 boolean escape,
264 ServletRequest req
265 ) throws CmsException {
266 CmsFlexController controller = (CmsFlexController)req.getAttribute(CmsFlexController.ATTRIBUTE_NAME);
267 if (DEBUG > 0) {
268 System.err.println("propertyTagAction() called!\nproperty=" + property
269 + "\naction=" + action
270 + "\ndefaultValue=" + defaultValue
271 + "\nescape=" + escape);
272 System.err.println("propertyTagAction() request URI=" + controller.getCmsObject().getRequestContext().getUri());
273 }
274 String value;
275
276 // if action is not set use default
277 if (action == null) action = m_actionValues[0];
278
279 switch (m_actionValue.indexOf(action)) {
280 case 0: // USE_URI
281 case 1: // USE_PARENT
282 // Read properties of parent (i.e. top requested) file
283 value = controller.getCmsObject().readProperty(controller.getCmsObject().getRequestContext().getUri(), property, false, defaultValue);
284 break;
285 case 2: // USE_SEARCH
286 case 3: // USE_SEARCH_URI
287 case 4: // USE_SEARCH_PARENT
288 // Try to find property on parent file and all parent folders
289 value = controller.getCmsObject().readProperty(controller.getCmsObject().getRequestContext().getUri(), property, true, defaultValue);
290 break;
291 case 5: // USE_ELEMENT_URI
292 case 6: // USE_THIS
293 // Read properties of this file
294 value = controller.getCmsObject().readProperty(controller.getCurrentRequest().getElementUri(), property, false, defaultValue);
295 break;
296 case 7: // USE_SEARCH_ELEMENT_URI
297 case 8: // USE_SEARCH_THIS
298 // Try to find property on this file and all parent folders
299 value = controller.getCmsObject().readProperty(controller.getCurrentRequest().getElementUri(), property, true, defaultValue);
300 break;
301 default:
302 // Read properties of the file named in the attribute
303 value = controller.getCmsObject().readProperty(controller.getCurrentRequest().toAbsolute(action), property, false, defaultValue);
304 }
305 if (escape) value = Encoder.escapeHtml(value);
306 if (DEBUG > 0) {
307 System.err.println("propertyTagAction(): result=" + value );
308 }
309 return value;
310 }
311
312 }