1 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 ; 42 import java.util.List ; 43 44 import javax.servlet.ServletRequest ; 45 import javax.servlet.jsp.JspException ; 46 import javax.servlet.jsp.tagext.TagSupport ; 47 48 import org.apache.commons.logging.Log; 49 50 101 public class CmsJspTagProperty extends TagSupport { 102 103 104 private static final long serialVersionUID = -4040833541258687977L; 105 106 107 public static final String USE_ELEMENT_URI = "element.uri"; 108 109 110 public static final String USE_PARENT = "parent"; 111 112 113 public static final String USE_SEARCH = "search"; 114 115 116 public static final String USE_SEARCH_ELEMENT_URI = "search.element.uri"; 117 118 119 public static final String USE_SEARCH_PARENT = "search-parent"; 120 121 122 public static final String USE_SEARCH_THIS = "search-this"; 123 124 125 public static final String USE_SEARCH_URI = "search.uri"; 126 127 128 public static final String USE_THIS = "this"; 129 130 131 public static final String USE_URI = "uri"; 132 133 134 public static final String [] 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 147 public static final List ACTION_VALUES_LIST = Arrays.asList(ACTION_VALUES); 149 150 151 private static final Log LOG = CmsLog.getLog(CmsJspTagProperty.class); 152 153 154 private String m_defaultValue; 155 156 157 private boolean m_escapeHtml; 158 159 160 private String m_propertyFile; 161 162 163 private String m_propertyName; 164 165 177 public static String propertyTagAction( 178 String property, 179 String action, 180 String defaultValue, 181 boolean escape, 182 ServletRequest req) throws CmsException { 183 184 CmsFlexController controller = CmsFlexController.getController(req); 185 186 if (action == null) { 188 action = ACTION_VALUES[0]; 189 } 190 191 String value; 192 String vfsUri; 193 boolean search; 194 switch (ACTION_VALUES_LIST.indexOf(action)) { 195 case 0: case 1: vfsUri = controller.getCmsObject().getRequestContext().getUri(); 199 search = false; 200 break; 201 case 2: case 3: case 4: vfsUri = controller.getCmsObject().getRequestContext().getUri(); 206 search = true; 207 break; 208 case 5: case 6: vfsUri = controller.getCurrentRequest().getElementUri(); 212 search = false; 213 break; 214 case 7: case 8: vfsUri = controller.getCurrentRequest().getElementUri(); 218 search = true; 219 break; 220 default: 221 vfsUri = CmsLinkManager.getAbsoluteUri(action, controller.getCurrentRequest().getElementUri()); 223 search = false; 224 } 225 value = controller.getCmsObject().readPropertyObject(vfsUri, property, search).getValue(defaultValue); 227 if (escape) { 228 value = CmsEncoder.escapeHtml(value); 230 } 231 return value; 232 } 233 234 239 public int doStartTag() throws JspException { 240 241 ServletRequest req = pageContext.getRequest(); 242 243 if (CmsFlexController.isCmsRequest(req)) { 245 246 try { 247 String prop = propertyTagAction(getName(), getFile(), m_defaultValue, m_escapeHtml, req); 248 if (prop == null) { 250 prop = ""; 251 } 252 pageContext.getOut().print(prop); 253 254 } catch (Exception 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 (ex); 259 } 260 } 261 return SKIP_BODY; 262 } 263 264 269 public String getDefault() { 270 271 return m_defaultValue != null ? m_defaultValue : ""; 272 } 273 274 279 public String getEscapeHtml() { 280 281 return "" + m_escapeHtml; 282 } 283 284 289 public String getFile() { 290 291 return m_propertyFile != null ? m_propertyFile : "parent"; 292 } 293 294 299 public String getName() { 300 301 return m_propertyName != null ? m_propertyName : ""; 302 } 303 304 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 323 public void setDefault(String def) { 324 325 if (def != null) { 326 m_defaultValue = def; 327 } 328 } 329 330 336 public void setEscapeHtml(String value) { 337 338 if (value != null) { 339 m_escapeHtml = Boolean.valueOf(value.trim()).booleanValue(); 340 } 341 } 342 343 348 public void setFile(String file) { 349 350 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(file)) { 351 m_propertyFile = file; 352 } 353 } 354 355 360 public void setName(String name) { 361 362 if (name != null) { 363 m_propertyName = name; 364 } 365 } 366 367 } 368 | Popular Tags |