001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (C) Alkacon Software (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.xml.containerpage; 029 030import org.opencms.file.CmsObject; 031import org.opencms.file.CmsResource; 032import org.opencms.file.types.CmsResourceTypeFunctionConfig; 033import org.opencms.util.CmsUUID; 034import org.opencms.xml.content.CmsXmlContentProperty; 035 036import java.util.ArrayList; 037import java.util.HashMap; 038import java.util.IdentityHashMap; 039import java.util.List; 040import java.util.Map; 041 042/** 043 * A class which contains the data parsed from a dynamic function XML content.<p> 044 */ 045public class CmsDynamicFunctionBean { 046 047 /** 048 * A bean which contains a single format for a dynamic function, which contains of the 049 * function JSP, the container settings and the parameters. 050 */ 051 public static class Format { 052 053 /** The structure id of the function jsp. */ 054 private CmsUUID m_jspStructureId; 055 056 /** The max width string of the container settings. */ 057 private String m_maxWidth; 058 059 /** The min width string of the container settings. */ 060 private String m_minWidth; 061 062 /** Flag to indicate whether there are no container settings. */ 063 private boolean m_noContainerSettings; 064 065 /** The parameters for the JSP. */ 066 private Map<String, String> m_parameters = new HashMap<String, String>(); 067 068 /** Container type of the container settings. */ 069 private String m_type; 070 071 /** 072 * Creates a new format instance.<p> 073 * 074 * @param structureId the structure id of the JSP 075 * @param type the container type 076 * @param minWidth the minimum width 077 * @param maxWidth the maximum width 078 * 079 * @param parameters the JSP parameters 080 */ 081 public Format( 082 CmsUUID structureId, 083 String type, 084 String minWidth, 085 String maxWidth, 086 Map<String, String> parameters) { 087 088 m_jspStructureId = structureId; 089 m_type = type; 090 m_minWidth = minWidth; 091 m_maxWidth = maxWidth; 092 m_parameters = parameters; 093 } 094 095 /** 096 * Returns the structure id of the JSP.<p> 097 * 098 * @return the structure id of the JSP 099 */ 100 public CmsUUID getJspStructureId() { 101 102 return m_jspStructureId; 103 } 104 105 /** 106 * Returns the maximum width.<p> 107 * 108 * @return the maximum width 109 */ 110 public String getMaxWidth() { 111 112 return m_maxWidth; 113 } 114 115 /** 116 * Returns the minimum width.<p> 117 * 118 * @return the minimum width 119 */ 120 public String getMinWidth() { 121 122 return m_minWidth; 123 } 124 125 /** 126 * Returns the map of parameters for the JSP.<p> 127 * 128 * @return the map of parameters for the JSP 129 **/ 130 public Map<String, String> getParameters() { 131 132 return m_parameters; 133 } 134 135 /** 136 * Gets the container type.<p> 137 * 138 * @return the container type 139 */ 140 public String getType() { 141 142 return m_type; 143 } 144 145 /** 146 * Returns true if this format has no container settings.<p> 147 * 148 * @return true if this format has no container settings 149 */ 150 public boolean hasNoContainerSettings() { 151 152 return m_noContainerSettings; 153 } 154 155 /** 156 * Sets the parameters which should be used if the format has no parameters of its own.<p> 157 * 158 * @param parameters the default parameters 159 */ 160 protected void setDefaultParameters(Map<String, String> parameters) { 161 162 if (m_parameters.isEmpty()) { 163 m_parameters = parameters; 164 } 165 } 166 167 /** 168 * Sets the flag to indicate that this format has no container settings.<p> 169 * 170 * @param noContainerSettings the new value for the noContainerSettings flag 171 */ 172 protected void setNoContainerSettings(boolean noContainerSettings) { 173 174 m_noContainerSettings = true; 175 } 176 } 177 178 /** The function formatter resource. */ 179 private CmsResource m_functionFormatter; 180 181 /** The primary format of the dynamic function. */ 182 private Format m_mainFormat; 183 184 /** The other formats of the dynamic function.*/ 185 private List<Format> m_otherFormats; 186 187 /** The resource from which the dynamic function bean has been read. */ 188 private CmsResource m_resource; 189 190 /** The setting configuration for the dynamic function. */ 191 private Map<String, CmsXmlContentProperty> m_settingConfig; 192 193 /** 194 * Creates a new dynamic function bean.<p> 195 * 196 * @param mainFormat the primary format 197 * @param otherFormats the list of other formats 198 * @param settingConfig the setting configuration 199 * @param resource the resource from which the dynamic function bean has been read 200 * @param functionFormatter the generic formatter for dynamic functions 201 */ 202 public CmsDynamicFunctionBean( 203 Format mainFormat, 204 List<Format> otherFormats, 205 Map<String, CmsXmlContentProperty> settingConfig, 206 CmsResource resource, 207 CmsResource functionFormatter) { 208 209 m_mainFormat = mainFormat; 210 m_otherFormats = otherFormats; 211 m_settingConfig = settingConfig; 212 for (Format format : otherFormats) { 213 format.setDefaultParameters(mainFormat.getParameters()); 214 } 215 m_resource = resource; 216 m_functionFormatter = functionFormatter; 217 } 218 219 /** 220 * Finds the correct format for a given container type and width.<p> 221 * 222 * @param cms the current CMS context 223 * @param type the container type 224 * @param width the container width 225 * 226 * @return the format for the given container type and width 227 */ 228 public Format getFormatForContainer(CmsObject cms, String type, int width) { 229 230 IdentityHashMap<CmsFormatterBean, Format> formatsByFormatter = new IdentityHashMap<CmsFormatterBean, Format>(); 231 // relate formatters to formats so we can pick the corresponding format after a formatter has been selected 232 CmsFormatterBean mainFormatter = createFormatterBean(m_mainFormat, true); 233 formatsByFormatter.put(mainFormatter, m_mainFormat); 234 List<I_CmsFormatterBean> formatters = new ArrayList<I_CmsFormatterBean>(); 235 for (Format format : m_otherFormats) { 236 CmsFormatterBean formatter = createFormatterBean(format, false); 237 formatsByFormatter.put(formatter, format); 238 formatters.add(formatter); 239 } 240 formatters.add(0, mainFormatter); 241 CmsFormatterConfiguration formatterConfiguration = CmsFormatterConfiguration.create(cms, formatters); 242 I_CmsFormatterBean matchingFormatter = formatterConfiguration.getDefaultFormatter(type, width); 243 if (matchingFormatter == null) { 244 return null; 245 } 246 return formatsByFormatter.get(matchingFormatter); 247 } 248 249 /** 250 * Creates the formatter list for this dynamic function.<p> 251 * 252 * @return the formatter list for this dynamic function 253 */ 254 public List<CmsFormatterBean> getFormatters() { 255 256 CmsFormatterBean mainFormatter = createFormatterBean(m_mainFormat, true); 257 List<CmsFormatterBean> formatters = new ArrayList<CmsFormatterBean>(); 258 formatters.add(mainFormatter); 259 for (Format format : m_otherFormats) { 260 formatters.add(createFormatterBean(format, false)); 261 } 262 return formatters; 263 } 264 265 /** 266 * Gets the generic function formatter resource.<p> 267 * 268 * @return the generic function formatter resource 269 */ 270 public CmsResource getFunctionFormatter() { 271 272 return m_functionFormatter; 273 } 274 275 /** 276 * Gets the main format.<p> 277 * 278 * @return the main format 279 */ 280 public Format getMainFormat() { 281 282 return m_mainFormat; 283 } 284 285 /** 286 * Returns the setting configuration for this dynamic function.<p> 287 * 288 * @return the setting configuration for this dynamic function 289 */ 290 public Map<String, CmsXmlContentProperty> getSettings() { 291 292 return m_settingConfig; 293 } 294 295 /** 296 * Helper method to create a formatter bean from a format.<p> 297 * 298 * @param format the format bean 299 * @param isPreview if true, the formatter returned will be marked as a preview formatter 300 * 301 * @return the formatter corresponding to the format 302 */ 303 protected CmsFormatterBean createFormatterBean(Format format, boolean isPreview) { 304 305 if (format.hasNoContainerSettings()) { 306 return new CmsFormatterBean( 307 CmsResourceTypeFunctionConfig.FORMATTER_PATH, 308 m_functionFormatter.getStructureId(), 309 m_resource.getRootPath(), 310 isPreview); 311 } else { 312 CmsFormatterBean result = new CmsFormatterBean( 313 format.getType(), 314 CmsResourceTypeFunctionConfig.FORMATTER_PATH, 315 format.getMinWidth(), 316 format.getMaxWidth(), 317 "" + isPreview, 318 "false", 319 m_resource.getRootPath()); 320 result.setJspStructureId(m_functionFormatter.getStructureId()); 321 return result; 322 } 323 } 324 325}