1 31 32 package org.opencms.jsp; 33 34 import org.opencms.i18n.CmsMessageContainer; 35 import org.opencms.main.CmsLog; 36 import org.opencms.util.CmsMacroResolver; 37 import org.opencms.util.CmsStringUtil; 38 import org.opencms.util.I_CmsMacroResolver; 39 40 import java.io.IOException ; 41 import java.util.Arrays ; 42 import java.util.Collections ; 43 import java.util.List ; 44 45 import javax.servlet.jsp.JspException ; 46 import javax.servlet.jsp.JspTagException ; 47 import javax.servlet.jsp.PageContext ; 48 import javax.servlet.jsp.tagext.Tag ; 49 import javax.servlet.jsp.tagext.TagSupport ; 50 51 import org.apache.commons.logging.Log; 52 53 62 public class CmsJspTagContentInfo extends TagSupport implements I_CmsMacroResolver { 63 64 65 private static final String [] KEYS = { 66 "resultSize", 67 "resultIndex", 68 "pageCount", 69 "pageIndex", 70 "pageSize", 71 "pageNavStartIndex", 72 "pageNavEndIndex", 73 "pageNavLength"}; 74 75 76 private static final List KEYS_LIST = Collections.unmodifiableList(Arrays.asList(KEYS)); 77 78 79 private static final Log LOG = CmsLog.getLog(CmsJspTagContentInfo.class); 80 81 82 private static final String [] SCOPES = {"application", "session", "request", "page"}; 83 84 85 private static final List SCOPES_LIST = Collections.unmodifiableList(Arrays.asList(SCOPES)); 86 87 88 private static final long serialVersionUID = -1955531050687258685L; 89 90 91 private String m_scope; 92 93 94 private String m_value; 95 96 97 private String m_variable; 98 99 102 public int doEndTag() { 103 104 release(); 106 return EVAL_PAGE; 107 } 108 109 112 public int doStartTag() throws JspException { 113 114 Tag ancestor = findAncestorWithClass(this, I_CmsXmlContentContainer.class); 116 if (ancestor == null) { 117 CmsMessageContainer container = Messages.get().container(Messages.ERR_PARENTLESS_TAG_1, "contentinfo"); 119 String msg = Messages.getLocalizedMessage(container, pageContext); 120 throw new JspTagException (msg); 121 } 122 123 I_CmsXmlContentContainer contentContainer = (I_CmsXmlContentContainer)ancestor; 124 125 String tagContent = ""; 126 127 if (CmsStringUtil.isNotEmpty(m_variable)) { 128 int scope = getScopeAsInt(m_scope); 129 storeContentInfoBean((CmsJspTagContentLoad)contentContainer, m_variable, scope); 130 } 131 132 if (CmsStringUtil.isNotEmpty(m_value)) { 133 tagContent = resolveMacros(m_value); 135 } 136 137 try { 138 pageContext.getOut().print(tagContent); 139 } catch (IOException e) { 140 CmsMessageContainer message = Messages.get().container(Messages.ERR_PROCESS_TAG_1, "contentinfo"); 141 LOG.error(message.key(), e); 142 throw new JspException (message.key(pageContext.getRequest().getLocale())); 143 } 144 145 return SKIP_BODY; 146 } 147 148 151 public String getMacroValue(String macro) { 152 153 int dotIndex = macro.indexOf('.'); 154 String beanName = null; 155 156 if ((dotIndex > 1) && (dotIndex < (macro.length() - 1))) { 157 beanName = macro.substring(0, dotIndex); 158 } else { 159 return null; 160 } 161 162 String variableName = macro.substring(dotIndex + 1, macro.length()); 163 164 if (CmsStringUtil.isEmpty(beanName) || CmsStringUtil.isEmpty(variableName)) { 165 return null; 166 } 167 168 CmsContentInfoBean bean; 170 int scope = pageContext.getAttributesScope(beanName); 171 try { 172 bean = (CmsContentInfoBean)pageContext.getAttribute(beanName, scope); 173 } catch (ClassCastException e) { 174 return null; 176 } 177 178 if (bean == null) { 179 return null; 180 } 181 182 switch (KEYS_LIST.indexOf(variableName)) { 183 case 0: 184 return Integer.toString(bean.getResultSize()); 186 case 1: 187 return Integer.toString(bean.getResultIndex()); 189 case 2: 190 return Integer.toString(bean.getPageCount()); 192 case 3: 193 return Integer.toString(bean.getPageIndex()); 195 case 4: 196 return Integer.toString(bean.getPageSize()); 198 case 5: 199 return Integer.toString(bean.getPageNavStartIndex()); 201 case 6: 202 return Integer.toString(bean.getPageNavEndIndex()); 204 case 7: 205 return Integer.toString(bean.getPageNavLength()); 207 default: 208 return null; 210 } 211 } 212 213 218 public String getScope() { 219 220 return m_scope; 221 } 222 223 228 public String getValue() { 229 230 return m_value; 231 } 232 233 238 public String getVar() { 239 240 return m_variable; 241 } 242 243 246 public boolean isKeepEmptyMacros() { 247 248 return true; 249 } 250 251 254 public void release() { 255 256 m_scope = null; 257 m_value = null; 258 m_variable = null; 259 super.release(); 260 } 261 262 265 public String resolveMacros(String input) { 266 267 return CmsMacroResolver.resolveMacros(input, this); 268 } 269 270 275 public void setScope(String scope) { 276 277 m_scope = scope; 278 } 279 280 285 public void setValue(String value) { 286 287 m_value = value; 288 } 289 290 295 public void setVar(String var) { 296 297 m_variable = var; 298 } 299 300 308 protected int getScopeAsInt(String scope) { 309 310 int scopeValue; 311 switch (SCOPES_LIST.indexOf(scope)) { 312 case 0: 313 scopeValue = PageContext.APPLICATION_SCOPE; 315 break; 316 case 1: 317 scopeValue = PageContext.SESSION_SCOPE; 319 break; 320 case 2: 321 scopeValue = PageContext.REQUEST_SCOPE; 323 break; 324 default: 325 scopeValue = PageContext.PAGE_SCOPE; 327 break; 328 } 329 330 return scopeValue; 331 332 } 333 334 341 protected void storeContentInfoBean(CmsJspTagContentLoad container, String variable, int scope) { 342 343 CmsContentInfoBean contentInfoBean = container.getContentInfoBean(); 344 345 contentInfoBean.setPageSize(container.getContentInfoBean().getPageSize()); 346 contentInfoBean.setPageIndex(container.getContentInfoBean().getPageIndex()); 347 contentInfoBean.setResultSize(container.getContentInfoBean().getResultSize()); 348 349 pageContext.setAttribute(variable, contentInfoBean, scope); 350 } 351 } | Popular Tags |