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 GmbH & Co. KG, 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.decorator; 029 030import org.opencms.file.CmsFile; 031import org.opencms.file.CmsObject; 032import org.opencms.file.CmsResource; 033import org.opencms.i18n.CmsLocaleManager; 034import org.opencms.main.CmsException; 035import org.opencms.main.CmsLog; 036import org.opencms.util.CmsStringUtil; 037 038import java.util.HashMap; 039import java.util.Iterator; 040import java.util.List; 041import java.util.Locale; 042import java.util.Map; 043 044import org.apache.commons.logging.Log; 045 046/** 047 * The CmsDecorationMap is the object representation of a single decoartion file.<p> 048 * 049 * The semicolon seperated elements of the decoartion file are stored in a map. <p> 050 * The map uses the decoration as keys and CmsDecorationObjects as values.<p> 051 * Multiple CmsDecorationMaps form a CmsDecorationBundle. 052 * 053 * @since 6.1.3 054 */ 055public class CmsDecorationMap implements Comparable<CmsDecorationMap> { 056 057 /** The seperator for the CSV file. */ 058 public static final String CSV_SEPERATOR = "|"; 059 060 /** The log object for this class. */ 061 private static final Log LOG = CmsLog.getLog(CmsDecorationMap.class); 062 063 /** The map to store all elements in. */ 064 private Map<String, CmsDecorationObject> m_decorationMap; 065 066 /** The decorator defintion to be used for this decoration map. */ 067 private CmsDecorationDefintion m_decoratorDefinition; 068 069 /** The locale of this decoration map. */ 070 private Locale m_locale; 071 072 /** The name of the decoration map. */ 073 private String m_name; 074 075 /** 076 * Constructor, creates a new, empty CmsDecorationMap.<p> 077 * 078 * @param decDef the CmsDecorationDefintion to be used in this decoration map 079 * @param name The name of the decoration map 080 * @param locale the locale for this decoration map 081 */ 082 public CmsDecorationMap(CmsDecorationDefintion decDef, String name, Locale locale) { 083 084 m_decoratorDefinition = decDef; 085 m_name = name; 086 m_locale = locale; 087 m_decorationMap = new HashMap<String, CmsDecorationObject>(); 088 } 089 090 /** 091 * Constructor, creates a new CmsDecorationMap.<p> 092 * 093 * @param cms the CmsObject 094 * @param res the resource to extrace the decorations from 095 * @param decDef the CmsDecorationDefintion to be used in this decoration map 096 * @throws CmsException if something goes wrong 097 */ 098 public CmsDecorationMap(CmsObject cms, CmsResource res, CmsDecorationDefintion decDef) 099 throws CmsException { 100 101 m_decoratorDefinition = decDef; 102 m_name = res.getName(); 103 m_locale = extractLocale(); 104 m_decorationMap = fillMap(cms, res); 105 106 } 107 108 /** 109 * @see java.lang.Comparable#compareTo(java.lang.Object) 110 */ 111 public int compareTo(CmsDecorationMap o) { 112 113 return m_name.compareTo(o.getName()); 114 } 115 116 /** 117 * @see java.lang.Object#equals(java.lang.Object) 118 */ 119 @Override 120 public boolean equals(Object obj) { 121 122 if (obj == this) { 123 return true; 124 } 125 if (obj instanceof CmsDecorationMap) { 126 return ((CmsDecorationMap)obj).m_name.equals(m_name); 127 } 128 return false; 129 } 130 131 /** 132 * Returns the decorationMap.<p> 133 * 134 * @return the decorationMap 135 */ 136 public Map<String, CmsDecorationObject> getDecorationMap() { 137 138 return m_decorationMap; 139 } 140 141 /** 142 * Returns the locale.<p> 143 * 144 * @return the locale 145 */ 146 public Locale getLocale() { 147 148 return m_locale; 149 } 150 151 /** 152 * Returns the name.<p> 153 * 154 * @return the name 155 */ 156 public String getName() { 157 158 return m_name; 159 } 160 161 /** 162 * @see java.lang.Object#hashCode() 163 */ 164 @Override 165 public int hashCode() { 166 167 return m_name.hashCode(); 168 } 169 170 /** 171 * Sets the decorationMap.<p> 172 * 173 * @param decorationMap the decorationMap to set 174 */ 175 public void setDecorationMap(Map<String, CmsDecorationObject> decorationMap) { 176 177 m_decorationMap = decorationMap; 178 } 179 180 /** 181 * @see java.lang.Object#toString() 182 */ 183 @Override 184 public String toString() { 185 186 StringBuffer buf = new StringBuffer(); 187 buf.append(this.getClass().getName()); 188 buf.append(" [name = '"); 189 buf.append(m_name); 190 buf.append("' locale="); 191 buf.append(m_locale); 192 buf.append("' mapsize="); 193 buf.append(m_decorationMap.size()); 194 buf.append("]"); 195 return buf.toString(); 196 } 197 198 /** 199 * Extracts the locale from the decoration filename.<p> 200 * 201 *@return locale extraced form filename or null 202 */ 203 private Locale extractLocale() { 204 205 Locale loc = null; 206 int underscore = m_name.lastIndexOf("_"); 207 if (underscore > -1) { 208 String localeName = m_name.substring(underscore + 1); 209 if (localeName.lastIndexOf(".") > -1) { 210 localeName = localeName.substring(0, localeName.lastIndexOf(".")); 211 } 212 loc = CmsLocaleManager.getLocale(localeName); 213 } 214 215 return loc; 216 } 217 218 /** 219 * Fills the decoration map with values from the decoation file.<p> 220 * 221 * @param cms the CmsObject 222 * @param res the decoration file 223 * @return decoration map, using decoration as key and decoration description as value 224 * @throws CmsException if something goes wrong 225 */ 226 private Map<String, CmsDecorationObject> fillMap(CmsObject cms, CmsResource res) throws CmsException { 227 228 if (LOG.isDebugEnabled()) { 229 LOG.debug( 230 Messages.get().getBundle().key(Messages.LOG_DECORATION_MAP_FILL_MAP_2, m_name, m_decoratorDefinition)); 231 } 232 233 Map<String, CmsDecorationObject> decMap = new HashMap<String, CmsDecorationObject>(); 234 // upgrade the resource to get the file content 235 CmsFile file = cms.readFile(res); 236 // get all the entries 237 String unparsedContent = new String(file.getContents()); 238 239 String delimiter = "\r\n"; 240 if (unparsedContent.indexOf(delimiter) == -1) { 241 // there was no \r\n delimiter in the csv file, so check if the lines are seperated by 242 // \n only 243 if (unparsedContent.indexOf("\n") > -1) { 244 delimiter = "\n"; 245 } 246 } 247 248 if (LOG.isDebugEnabled()) { 249 LOG.debug( 250 Messages.get().getBundle().key( 251 Messages.LOG_DECORATION_MAP_FILL_MAP_DELIMITER_2, 252 res.getName(), 253 CmsStringUtil.escapeJavaScript(delimiter))); 254 } 255 256 List<String> entries = CmsStringUtil.splitAsList(unparsedContent, delimiter); 257 258 if (LOG.isDebugEnabled()) { 259 LOG.debug( 260 Messages.get().getBundle().key( 261 Messages.LOG_DECORATION_MAP_FILL_MAP_SPLIT_LIST_2, 262 res.getName(), 263 entries)); 264 } 265 Iterator<String> i = entries.iterator(); 266 while (i.hasNext()) { 267 try { 268 String entry = i.next(); 269 // extract key and value 270 if (CmsStringUtil.isNotEmpty(entry)) { 271 int speratator = entry.indexOf(CSV_SEPERATOR); 272 if (speratator > -1) { 273 String key = entry.substring(0, speratator).trim(); 274 String value = entry.substring(speratator + 1).trim(); 275 if (CmsStringUtil.isNotEmpty(key) && CmsStringUtil.isNotEmpty(value)) { 276 CmsDecorationObject decObj = new CmsDecorationObject( 277 key, 278 value, 279 m_decoratorDefinition, 280 m_locale); 281 decMap.put(key, decObj); 282 if (LOG.isDebugEnabled()) { 283 LOG.debug( 284 Messages.get().getBundle().key( 285 Messages.LOG_DECORATION_MAP_ADD_DECORATION_OBJECT_2, 286 decObj, 287 key)); 288 } 289 } 290 } 291 } 292 } catch (Exception e) { 293 if (LOG.isErrorEnabled()) { 294 LOG.error(Messages.get().getBundle().key(Messages.LOG_DECORATION_MAP_FILL_2, m_name, e)); 295 } 296 } 297 } 298 299 return decMap; 300 } 301}