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_config = config; 079 } 080 081 /** 082 * Prepares the macro resolver to use for formatter info / setting info beans. 083 * 084 * @param cms the CMS context to use 085 * @param locale the locale to use 086 * @param formatter the formatter bean 087 * @return the macro resolver to sue 088 */ 089 public static CmsMacroResolver getMacroResolverForFormatter( 090 CmsObject cms, 091 Locale locale, 092 I_CmsFormatterBean formatter) { 093 094 final CmsMacroResolver resolver = new CmsMacroResolver(); 095 resolver.setCmsObject(cms); 096 CmsMultiMessages messages = new CmsMultiMessages(locale); 097 messages.addMessages(OpenCms.getWorkplaceManager().getMessages(locale)); 098 for (String type : formatter.getResourceTypeNames()) { 099 try { 100 I_CmsResourceType typeObj = OpenCms.getResourceManager().getResourceType(type); 101 String schema = typeObj.getConfiguration().getString( 102 CmsResourceTypeXmlContent.CONFIGURATION_SCHEMA, 103 null); 104 if (schema != null) { 105 CmsXmlContentDefinition contentDef = CmsXmlContentDefinition.unmarshal(cms, schema); 106 CmsMessages schemaMessages = contentDef.getContentHandler().getMessages(locale); 107 messages.addMessages(schemaMessages); 108 109 } 110 } catch (Exception e) { 111 CmsJspStandardContextBean.LOG.warn(e.getLocalizedMessage(), e); 112 } 113 } 114 resolver.setCmsObject(cms); 115 resolver.setKeepEmptyMacros(true); 116 resolver.setMessages(messages); 117 return resolver; 118 } 119 120 /** 121 * Gets the matching container types. 122 * 123 * @return the container types the formatter fits into 124 */ 125 public List<String> getContainerTypes() { 126 127 return new ArrayList<>(m_formatter.getContainerTypes()); 128 } 129 130 /** 131 * Gets the path of the formatter definition XML file. 132 * 133 * @return the path of the formatter definition XML file 134 */ 135 public String getDefinition() { 136 137 return m_formatter.getLocation(); 138 } 139 140 /** 141 * Gets the localized description. 142 * 143 * @return the description 144 */ 145 public String getDescription() { 146 147 Locale locale = m_cms.getRequestContext().getLocale(); 148 return getDescription(locale); 149 } 150 151 /** 152 * @see org.opencms.jsp.util.I_CmsInfoWrapper#getDescription(java.util.Locale) 153 */ 154 public String getDescription(Locale locale) { 155 156 String result = m_formatter.getDescription(locale); 157 return result; 158 } 159 160 /** 161 * @see org.opencms.jsp.util.I_CmsFormatterInfo#getDescriptionKey() 162 */ 163 public String getDescriptionKey() { 164 165 return CmsKeyDummyMacroResolver.getKey(m_formatter.getDescription(null), m_macroResolver); 166 } 167 168 /** 169 * Gets the localization keys from which the description is read. 170 * 171 * @return the list of localization keys 172 */ 173 public List<String> getDescriptionKeys() { 174 175 return CmsKeyDummyMacroResolver.getKeys(m_formatter.getDescription(null), m_macroResolver); 176 177 } 178 179 /** 180 * Gets the raw description, without resolving any macros. 181 * 182 * @return the raw description 183 */ 184 public String getDescriptionRaw() { 185 186 return m_formatter.getDescription(null); 187 188 } 189 190 /** 191 * Gets the display type of the formatter. 192 * 193 * @return the display type of the formatter 194 */ 195 public String getDisplay() { 196 197 return m_formatter.getDisplayType(); 198 } 199 200 /** 201 * @see org.opencms.jsp.util.I_CmsFormatterInfo#getIsActive() 202 */ 203 public boolean getIsActive() { 204 205 return true; 206 } 207 208 /** 209 * Checks if the formatter is a detail formatter. 210 * 211 * @return true if the formatter is a detail formatter 212 */ 213 public boolean getIsDetailFormatter() { 214 215 return m_formatter.isDetailFormatter(); 216 } 217 218 /** 219 * Checks if the formatter is a display formatter. 220 * 221 * @return true if the formatter is a display formatter 222 */ 223 public boolean getIsDisplayFormatter() { 224 225 return m_formatter.isDisplayFormatter(); 226 } 227 228 /** 229 * @see org.opencms.jsp.util.I_CmsFormatterInfo#getIsFormatter() 230 */ 231 public boolean getIsFormatter() { 232 233 return m_formatter instanceof CmsFormatterBean; 234 } 235 236 /** 237 * @see org.opencms.jsp.util.I_CmsFormatterInfo#getIsFunction() 238 */ 239 public boolean getIsFunction() { 240 241 return m_formatter instanceof CmsFunctionFormatterBean; 242 } 243 244 /** 245 * @see org.opencms.jsp.util.I_CmsFormatterInfo#getIsResourceType() 246 */ 247 public boolean getIsResourceType() { 248 249 return false; 250 } 251 252 /** 253 * Gets the JSP path. 254 * 255 * @return the JSP path 256 */ 257 public String getJsp() { 258 259 if (m_formatter instanceof CmsFunctionFormatterBean) { 260 return ((CmsFunctionFormatterBean)m_formatter).getRealJspRootPath(); 261 } else { 262 return m_formatter.getJspRootPath(); 263 } 264 } 265 266 /** 267 * Gets the formatter key. 268 * 269 * @return the formatter key 270 */ 271 public String getKey() { 272 273 return m_formatter.getKey(); 274 } 275 276 /** 277 * Gets the maximum container width. 278 * 279 * @return the maximum container width 280 */ 281 public int getMaxWidth() { 282 283 return m_formatter.getMaxWidth(); 284 } 285 286 /** 287 * Gets the minimum container width. 288 * 289 * @return the minimum container width 290 */ 291 public int getMinWidth() { 292 293 return m_formatter.getMinWidth(); 294 } 295 296 /** 297 * @see org.opencms.jsp.util.I_CmsFormatterInfo#getName() 298 */ 299 public String getName() { 300 301 return getKey(); 302 } 303 304 /** 305 * Gets the user-readable formatter name. 306 * 307 * @return the user-readable name 308 */ 309 public String getNiceName() { 310 311 return m_formatter.getNiceName(m_cms.getRequestContext().getLocale()); 312 } 313 314 /** 315 * Gets the localization key for the nice name, if one was used, or null otherwise. 316 * 317 * @return the localization key 318 */ 319 public String getNiceNameKey() { 320 321 return CmsKeyDummyMacroResolver.getKey(m_formatter.getNiceName(null), m_macroResolver); 322 } 323 324 /** 325 * Gets the raw nice name, without resolving any macros. 326 * 327 * @return the raw nice name 328 */ 329 public String getNiceNameRaw() { 330 331 return m_formatter.getNiceName(null); 332 } 333 334 /** 335 * Returns the rank of the formatter. 336 * 337 * @return the rank 338 */ 339 public int getRank() { 340 341 return m_formatter.getRank(); 342 } 343 344 /** 345 * Gets a list of wrapper beans for the element setting definitions. 346 * 347 * @return the element setting definition wrappers 348 */ 349 public List<CmsSettingDefinitionWrapper> getSettings() { 350 351 Map<String, CmsXmlContentProperty> settingDefs = m_formatter.getSettings(m_config); 352 List<CmsSettingDefinitionWrapper> result = new ArrayList<>(); 353 for (Map.Entry<String, CmsXmlContentProperty> entry : settingDefs.entrySet()) { 354 CmsSettingDefinitionWrapper setting = new CmsSettingDefinitionWrapper( 355 m_cms, 356 entry.getValue(), 357 locale -> getMacroResolverForFormatter(m_cms, locale, m_formatter)); 358 result.add(setting); 359 } 360 return result; 361 362 } 363 364 /** 365 * Gets the resource types. 366 * 367 * @return the resource types 368 */ 369 public List<String> getTypes() { 370 371 List<String> result = new ArrayList<>(m_formatter.getResourceTypeNames()); 372 Collections.sort(result); 373 return result; 374 } 375 376 /** 377 * Gets the nice name of the formatter in the given locale. 378 * 379 * @param locale the locale to use 380 * @return the nice name of the formatter 381 */ 382 public String niceName(Locale locale) { 383 384 return m_formatter.getNiceName(locale); 385 } 386 387}