1 31 32 package org.opencms.jsp.decorator; 33 34 import org.opencms.file.CmsFile; 35 import org.opencms.file.CmsObject; 36 import org.opencms.file.CmsResource; 37 import org.opencms.main.CmsException; 38 import org.opencms.main.CmsLog; 39 import org.opencms.util.CmsStringUtil; 40 41 import java.util.HashMap ; 42 import java.util.Iterator ; 43 import java.util.List ; 44 import java.util.Locale ; 45 import java.util.Map ; 46 47 import org.apache.commons.logging.Log; 48 49 62 public class CmsDecorationMap implements Comparable { 63 64 65 public static final String CSV_SEPERATOR = "|"; 66 67 68 private static final Log LOG = CmsLog.getLog(CmsDecorationMap.class); 69 70 71 private Map m_decorationMap; 72 73 74 private CmsDecorationDefintion m_decoratorDefinition; 75 76 77 private Locale m_locale; 78 79 80 private String m_name; 81 82 90 public CmsDecorationMap(CmsObject cms, CmsResource res, CmsDecorationDefintion decDef) 91 throws CmsException { 92 93 m_decoratorDefinition = decDef; 94 m_name = res.getName(); 95 m_locale = extractLocale(); 96 m_decorationMap = fillMap(cms, res); 97 98 } 99 100 103 public int compareTo(Object o) { 104 105 int compValue = 0; 106 if (o instanceof CmsDecorationMap) { 107 compValue = m_name.compareTo(((CmsDecorationMap)o).getName()); 108 } 109 return compValue; 110 } 111 112 117 public Map getDecorationMap() { 118 119 return m_decorationMap; 120 } 121 122 127 public Locale getLocale() { 128 129 return m_locale; 130 } 131 132 137 public String getName() { 138 139 return m_name; 140 } 141 142 145 public String toString() { 146 147 StringBuffer buf = new StringBuffer (); 148 buf.append(this.getClass().getName()); 149 buf.append(" [name = '"); 150 buf.append(m_name); 151 buf.append("' locale="); 152 buf.append(m_locale); 153 buf.append("' mapsize="); 154 buf.append(m_decorationMap.size()); 155 buf.append("]"); 156 return buf.toString(); 157 } 158 159 164 private Locale extractLocale() { 165 166 Locale loc = null; 167 int underscore = m_name.indexOf("_"); 168 if (underscore > -1) { 169 String localeName = m_name.substring(underscore + 1); 170 if (localeName.indexOf(".") > -1) { 171 localeName = localeName.substring(0, localeName.indexOf(".")); 172 } 173 loc = new Locale (localeName); 174 } 175 176 return loc; 177 } 178 179 187 private Map fillMap(CmsObject cms, CmsResource res) throws CmsException { 188 189 if (LOG.isDebugEnabled()) { 190 LOG.debug(Messages.get().getBundle().key( 191 Messages.LOG_DECORATION_MAP_FILL_MAP_2, 192 m_name, 193 m_decoratorDefinition)); 194 } 195 196 Map decMap = new HashMap (); 197 CmsFile file = CmsFile.upgrade(res, cms); 199 String unparsedContent = new String (file.getContents()); 201 202 String delimiter = "\r\n"; 203 if (unparsedContent.indexOf(delimiter) == -1) { 204 if (unparsedContent.indexOf("\n") > -1) { 207 delimiter = "\n"; 208 } 209 } 210 211 if (LOG.isDebugEnabled()) { 212 LOG.debug(Messages.get().getBundle().key( 213 Messages.LOG_DECORATION_MAP_FILL_MAP_DELIMITER_2, 214 res.getName(), 215 CmsStringUtil.escapeJavaScript(delimiter))); 216 } 217 218 List entries = CmsStringUtil.splitAsList(unparsedContent, delimiter); 219 220 if (LOG.isDebugEnabled()) { 221 LOG.debug(Messages.get().getBundle().key( 222 Messages.LOG_DECORATION_MAP_FILL_MAP_SPLIT_LIST_2, 223 res.getName(), 224 entries)); 225 } 226 Iterator i = entries.iterator(); 227 while (i.hasNext()) { 228 try { 229 String entry = (String )i.next(); 230 if (CmsStringUtil.isNotEmpty(entry)) { 232 int speratator = entry.indexOf(CSV_SEPERATOR); 233 if (speratator > -1) { 234 String key = entry.substring(0, speratator).trim(); 235 String value = entry.substring(speratator + 1).trim(); 236 if (CmsStringUtil.isNotEmpty(key) && CmsStringUtil.isNotEmpty(value)) { 237 CmsDecorationObject decObj = new CmsDecorationObject( 238 key, 239 value, 240 m_decoratorDefinition, 241 m_locale); 242 decMap.put(key, decObj); 243 if (LOG.isDebugEnabled()) { 244 LOG.debug(Messages.get().getBundle().key( 245 Messages.LOG_DECORATION_MAP_ADD_DECORATION_OBJECT_2, 246 decObj, 247 key)); 248 } 249 } 250 } 251 } 252 } catch (Exception e) { 253 if (LOG.isErrorEnabled()) { 254 LOG.error(Messages.get().getBundle().key(Messages.LOG_DECORATION_MAP_FILL_2, m_name, e)); 255 } 256 } 257 } 258 259 return decMap; 260 } 261 262 } 263 | Popular Tags |