001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.jsp.util; 029 030import org.opencms.ade.configuration.CmsADEConfigData; 031import org.opencms.file.CmsObject; 032import org.opencms.file.types.CmsResourceTypeXmlContent; 033import org.opencms.file.types.I_CmsResourceType; 034import org.opencms.i18n.CmsMessages; 035import org.opencms.i18n.CmsMultiMessages; 036import org.opencms.main.OpenCms; 037import org.opencms.util.CmsMacroResolver; 038import org.opencms.xml.CmsXmlContentDefinition; 039import org.opencms.xml.containerpage.CmsFormatterBean; 040import org.opencms.xml.containerpage.CmsFunctionFormatterBean; 041import org.opencms.xml.containerpage.I_CmsFormatterBean; 042import org.opencms.xml.content.CmsXmlContentProperty; 043 044import java.util.ArrayList; 045import java.util.Collections; 046import java.util.List; 047import java.util.Locale; 048import java.util.Map; 049 050/** 051 * Wrapper class for accessing formatter information from JSPs. 052 */ 053public class CmsFormatterInfoWrapper implements I_CmsFormatterInfo { 054 055 /** The CMS context to use. */ 056 private CmsObject m_cms; 057 058 /** The sitemap configuration for the current context. */ 059 private CmsADEConfigData m_config; 060 061 /** The wrapped formatter. */ 062 private I_CmsFormatterBean m_formatter; 063 064 /** The macro resolver to use. */ 065 private CmsMacroResolver m_macroResolver; 066 067 /** 068 * Creates a new instance. 069 * 070 * @param cms the CMS context 071 * @param config the sitemap configuration 072 * @param formatter the formatter bean to wrap 073 */ 074 public CmsFormatterInfoWrapper(CmsObject cms, CmsADEConfigData config, I_CmsFormatterBean formatter) { 075 076 m_cms = cms; 077 m_formatter = formatter; 078 m_macroResolver = getMacroResolverForFormatter(cms, formatter); 079 m_config = config; 080 } 081 082 /** 083 * Prepares the macro resolver to use for formatter info / setting info beans. 084 * 085 * @param cms the CMS context to use 086 * @param formatter the formatter bean 087 * @return the macro resolver to sue 088 */ 089 public static CmsMacroResolver getMacroResolverForFormatter(CmsObject cms, I_CmsFormatterBean formatter) { 090 091 final CmsMacroResolver resolver = new CmsMacroResolver(); 092 resolver.setCmsObject(cms); 093 Locale locale = cms.getRequestContext().getLocale(); 094 CmsMultiMessages messages = new CmsMultiMessages(locale); 095 messages.addMessages(OpenCms.getWorkplaceManager().getMessages(locale)); 096 for (String type : formatter.getResourceTypeNames()) { 097 try { 098 I_CmsResourceType typeObj = OpenCms.getResourceManager().getResourceType(type); 099 String schema = typeObj.getConfiguration().getString( 100 CmsResourceTypeXmlContent.CONFIGURATION_SCHEMA, 101 null); 102 if (schema != null) { 103 CmsXmlContentDefinition contentDef = CmsXmlContentDefinition.unmarshal(cms, schema); 104 CmsMessages schemaMessages = contentDef.getContentHandler().getMessages(locale); 105 messages.addMessages(schemaMessages); 106 107 } 108 } catch (Exception e) { 109 CmsJspStandardContextBean.LOG.warn(e.getLocalizedMessage(), e); 110 } 111 } 112 resolver.setCmsObject(cms); 113 resolver.setKeepEmptyMacros(true); 114 resolver.setMessages(messages); 115 return resolver; 116 } 117 118 /** 119 * Gets the description of the formatter in the given locale. 120 * 121 * @param locale the locale to use 122 * @return the description 123 */ 124 public String description(Locale locale) { 125 126 String result = m_formatter.getDescription(locale); 127 result = m_macroResolver.resolveMacros(result); 128 return result; 129 } 130 131 /** 132 * Gets the matching container types. 133 * 134 * @return the container types the formatter fits into 135 */ 136 public List<String> getContainerTypes() { 137 138 return new ArrayList<>(m_formatter.getContainerTypes()); 139 } 140 141 /** 142 * Gets the path of the formatter definition XML file. 143 * 144 * @return the path of the formatter definition XML file 145 */ 146 public String getDefinition() { 147 148 return m_formatter.getLocation(); 149 } 150 151 /** 152 * Gets the localized description. 153 * 154 * @return the description 155 */ 156 public String getDescription() { 157 158 Locale locale = m_cms.getRequestContext().getLocale(); 159 String result = m_formatter.getDescription(locale); 160 result = m_macroResolver.resolveMacros(result); 161 return result; 162 } 163 164 /** 165 * @see org.opencms.jsp.util.I_CmsFormatterInfo#getDescriptionKey() 166 */ 167 public String getDescriptionKey() { 168 169 return CmsKeyDummyMacroResolver.getKey(m_formatter.getDescription(null), m_macroResolver); 170 } 171 172 /** 173 * Gets the raw description, without resolving any macros. 174 * 175 * @return the raw description 176 */ 177 public String getDescriptionRaw() { 178 179 return m_formatter.getDescription(null); 180 181 } 182 183 /** 184 * Gets the display type of the formatter. 185 * 186 * @return the display type of the formatter 187 */ 188 public String getDisplay() { 189 190 return m_formatter.getDisplayType(); 191 } 192 193 /** 194 * @see org.opencms.jsp.util.I_CmsFormatterInfo#getIsActive() 195 */ 196 public boolean getIsActive() { 197 198 return true; 199 } 200 201 /** 202 * Checks if the formatter is a detail formatter. 203 * 204 * @return true if the formatter is a detail formatter 205 */ 206 public boolean getIsDetailFormatter() { 207 208 return m_formatter.isDetailFormatter(); 209 } 210 211 /** 212 * Checks if the formatter is a display formatter. 213 * 214 * @return true if the formatter is a display formatter 215 */ 216 public boolean getIsDisplayFormatter() { 217 218 return m_formatter.isDisplayFormatter(); 219 } 220 221 /** 222 * @see org.opencms.jsp.util.I_CmsFormatterInfo#getIsFormatter() 223 */ 224 public boolean getIsFormatter() { 225 226 return m_formatter instanceof CmsFormatterBean; 227 } 228 229 /** 230 * @see org.opencms.jsp.util.I_CmsFormatterInfo#getIsFunction() 231 */ 232 public boolean getIsFunction() { 233 234 return m_formatter instanceof CmsFunctionFormatterBean; 235 } 236 237 /** 238 * @see org.opencms.jsp.util.I_CmsFormatterInfo#getIsResourceType() 239 */ 240 public boolean getIsResourceType() { 241 242 return false; 243 } 244 245 /** 246 * Gets the JSP path. 247 * 248 * @return the JSP path 249 */ 250 public String getJsp() { 251 252 if (m_formatter instanceof CmsFunctionFormatterBean) { 253 return ((CmsFunctionFormatterBean)m_formatter).getRealJspRootPath(); 254 } else { 255 return m_formatter.getJspRootPath(); 256 } 257 } 258 259 /** 260 * Gets the formatter key. 261 * 262 * @return the formatter key 263 */ 264 public String getKey() { 265 266 return m_formatter.getKey(); 267 } 268 269 /** 270 * Gets the maximum container width. 271 * 272 * @return the maximum container width 273 */ 274 public int getMaxWidth() { 275 276 return m_formatter.getMaxWidth(); 277 } 278 279 /** 280 * Gets the minimum container width. 281 * 282 * @return the minimum container width 283 */ 284 public int getMinWidth() { 285 286 return m_formatter.getMinWidth(); 287 } 288 289 /** 290 * @see org.opencms.jsp.util.I_CmsFormatterInfo#getName() 291 */ 292 public String getName() { 293 294 return getKey(); 295 } 296 297 /** 298 * Gets the user-readable formatter name. 299 * 300 * @return the user-readable name 301 */ 302 public String getNiceName() { 303 304 return m_formatter.getNiceName(m_cms.getRequestContext().getLocale()); 305 } 306 307 /** 308 * Gets the localization key for the nice name, if one was used, or null otherwise. 309 * 310 * @return the localization key 311 */ 312 public String getNiceNameKey() { 313 314 return CmsKeyDummyMacroResolver.getKey(m_formatter.getNiceName(null), m_macroResolver); 315 } 316 317 /** 318 * Gets the raw nice name, without resolving any macros. 319 * 320 * @return the raw nice name 321 */ 322 public String getNiceNameRaw() { 323 324 return m_formatter.getNiceName(null); 325 } 326 327 /** 328 * Returns the rank of the formatter. 329 * 330 * @return the rank 331 */ 332 public int getRank() { 333 334 return m_formatter.getRank(); 335 } 336 337 /** 338 * Gets a list of wrapper beans for the element setting definitions. 339 * 340 * @return the element setting definition wrappers 341 */ 342 public List<CmsSettingDefinitionWrapper> getSettings() { 343 344 Map<String, CmsXmlContentProperty> settingDefs = m_formatter.getSettings(m_config); 345 List<CmsSettingDefinitionWrapper> result = new ArrayList<>(); 346 for (Map.Entry<String, CmsXmlContentProperty> entry : settingDefs.entrySet()) { 347 CmsSettingDefinitionWrapper setting = new CmsSettingDefinitionWrapper( 348 m_cms, 349 entry.getValue(), 350 m_macroResolver); 351 result.add(setting); 352 } 353 return result; 354 355 } 356 357 /** 358 * Gets the resource types. 359 * 360 * @return the resource types 361 */ 362 public List<String> getTypes() { 363 364 List<String> result = new ArrayList<>(m_formatter.getResourceTypeNames()); 365 Collections.sort(result); 366 return result; 367 } 368 369 /** 370 * Gets the nice name of the formatter in the given locale. 371 * 372 * @param locale the locale to use 373 * @return the nice name of the formatter 374 */ 375 public String niceName(Locale locale) { 376 377 return m_formatter.getNiceName(locale); 378 } 379 380}