1 31 32 package org.opencms.jsp.decorator; 33 34 import org.opencms.cache.CmsVfsMemoryObjectCache; 35 import org.opencms.file.CmsObject; 36 import org.opencms.file.CmsResource; 37 import org.opencms.file.CmsResourceFilter; 38 import org.opencms.file.types.CmsResourceTypePlain; 39 import org.opencms.main.CmsException; 40 import org.opencms.main.CmsLog; 41 42 import java.util.ArrayList ; 43 import java.util.Collections ; 44 import java.util.Iterator ; 45 import java.util.List ; 46 import java.util.Locale ; 47 48 import org.apache.commons.logging.Log; 49 50 59 public class CmsDecorationDefintion { 60 61 62 private static final Log LOG = CmsLog.getLog(CmsDecorationDefintion.class); 63 64 65 private String m_configurationFile; 66 67 68 private boolean m_markFirst; 69 70 71 private String m_name; 72 73 74 private String m_postText; 75 76 77 private String m_postTextFirst; 78 79 80 private String m_preText; 81 82 83 private String m_preTextFirst; 84 85 88 public CmsDecorationDefintion() { 89 90 m_configurationFile = null; 91 m_markFirst = false; 92 m_name = null; 93 m_postText = null; 94 m_postTextFirst = null; 95 m_preText = null; 96 m_preTextFirst = null; 97 } 98 99 110 public CmsDecorationDefintion( 111 String name, 112 String preText, 113 String postText, 114 String preTextFirst, 115 String postTextFirst, 116 boolean markFrist, 117 String configurationFile) { 118 119 m_configurationFile = configurationFile; 120 m_markFirst = markFrist; 121 m_name = name; 122 m_postText = postText; 123 m_postTextFirst = postTextFirst; 124 m_preText = preText; 125 m_preTextFirst = preTextFirst; 126 } 127 128 136 public CmsDecorationBundle createDecorationBundle(CmsObject cms, Locale locale) throws CmsException { 137 138 List decorationMapFiles = getDecorationMapFiles(cms); 140 if (LOG.isDebugEnabled()) { 141 LOG.debug(Messages.get().getBundle().key( 142 Messages.LOG_DECORATION_DEFINITION_MAP_FILES_2, 143 decorationMapFiles, 144 locale)); 145 } 146 147 List decorationMaps = getDecorationMaps(cms, decorationMapFiles); 149 if (LOG.isDebugEnabled()) { 150 LOG.debug(Messages.get().getBundle().key(Messages.LOG_DECORATION_DEFINITION_MAPS_2, decorationMaps, locale)); 151 } 152 153 return createDecorationBundle(decorationMaps, locale); 158 } 159 160 166 public String getConfigurationFile() { 167 168 return m_configurationFile; 169 } 170 171 176 public String getName() { 177 178 return m_name; 179 } 180 181 186 public String getPostText() { 187 188 return m_postText; 189 } 190 191 196 public String getPostTextFirst() { 197 198 return m_postTextFirst; 199 } 200 201 206 public String getPreText() { 207 208 return m_preText; 209 } 210 211 216 public String getPreTextFirst() { 217 218 return m_preTextFirst; 219 } 220 221 226 public boolean isMarkFirst() { 227 228 return m_markFirst; 229 } 230 231 236 public void setConfigurationFile(String configurationFile) { 237 238 m_configurationFile = configurationFile; 239 } 240 241 246 public void setMarkFirst(boolean markFirst) { 247 248 m_markFirst = markFirst; 249 } 250 251 256 public void setName(String name) { 257 258 m_name = name; 259 } 260 261 266 public void setPostText(String postText) { 267 268 m_postText = postText; 269 } 270 271 276 public void setPostTextFirst(String postTextFirst) { 277 278 m_postTextFirst = postTextFirst; 279 } 280 281 286 public void setPreText(String preText) { 287 288 m_preText = preText; 289 } 290 291 296 public void setPreTextFirst(String preTextFirst) { 297 298 m_preTextFirst = preTextFirst; 299 } 300 301 304 public String toString() { 305 306 StringBuffer buf = new StringBuffer (); 307 buf.append(this.getClass().getName()); 308 buf.append(" [name = '"); 309 buf.append(m_name); 310 buf.append("', markFirst = '"); 311 buf.append(m_markFirst); 312 buf.append("', preText = '"); 313 buf.append(m_preText); 314 buf.append("', postText = '"); 315 buf.append(m_postText); 316 buf.append("', preTextFirst = '"); 317 buf.append(m_preTextFirst); 318 buf.append("', postTextFirst = '"); 319 buf.append(m_postTextFirst); 320 buf.append("', configFile = "); 321 buf.append(m_configurationFile); 322 buf.append("]"); 323 return buf.toString(); 324 } 325 326 333 private CmsDecorationBundle createDecorationBundle(List decorationMaps, Locale locale) { 334 335 CmsDecorationBundle decorationBundle = new CmsDecorationBundle(locale); 336 Collections.sort(decorationMaps); 338 Iterator i = decorationMaps.iterator(); 340 while (i.hasNext()) { 341 CmsDecorationMap decMap = (CmsDecorationMap)i.next(); 342 if ((locale == null) 347 || (locale != null && decMap.getLocale() != null && locale.getDisplayLanguage().equals( 348 decMap.getLocale().getDisplayLanguage())) 349 || (locale != null && decMap.getLocale() == null)) { 350 decorationBundle.putAll(decMap.getDecorationMap()); 351 if (LOG.isDebugEnabled()) { 352 LOG.debug(Messages.get().getBundle().key( 353 Messages.LOG_DECORATION_DEFINITION_CREATE_BUNDLE_2, 354 decMap.getName(), 355 locale)); 356 } 357 } 358 } 359 return decorationBundle; 360 } 361 362 369 private List getDecorationMapFiles(CmsObject cms) throws CmsException { 370 371 List files = new ArrayList (); 372 373 StringBuffer baseFilename = new StringBuffer (); 377 baseFilename.append(CmsResource.getParentFolder(m_configurationFile)); 378 String filename = cms.readResource(m_configurationFile).getName(); 379 if (filename.indexOf(".") > -1) { 381 filename = filename.substring(0, filename.indexOf(".")); 382 } 383 if (filename.indexOf("_") > -1) { 385 filename = filename.substring(0, filename.indexOf("_")); 386 } 387 baseFilename.append(filename); 388 String basename = baseFilename.toString(); 389 390 392 List resources = cms.readResources(CmsResource.getParentFolder(m_configurationFile), CmsResourceFilter.DEFAULT); 393 Iterator i = resources.iterator(); 394 while (i.hasNext()) { 395 CmsResource res = (CmsResource)i.next(); 396 if (cms.getSitePath(res).startsWith(basename) && res.getTypeId() == CmsResourceTypePlain.getStaticTypeId()) { 397 files.add(res); 398 } 399 } 400 401 return files; 402 } 403 404 411 private List getDecorationMaps(CmsObject cms, List decorationListFiles) { 412 413 List decorationMaps = new ArrayList (); 414 Iterator i = decorationListFiles.iterator(); 415 while (i.hasNext()) { 416 CmsResource res = (CmsResource)i.next(); 417 try { 418 CmsDecorationMap decMap = (CmsDecorationMap)CmsVfsMemoryObjectCache.getVfsMemoryObjectCache().getCachedObject( 419 cms, 420 res.getRootPath()); 421 if (decMap == null) { 422 decMap = new CmsDecorationMap(cms, res, this); 423 CmsVfsMemoryObjectCache.getVfsMemoryObjectCache().putCachedObject(cms, res.getRootPath(), decMap); 424 } 425 426 decorationMaps.add(decMap); 427 } catch (CmsException e) { 428 if (LOG.isErrorEnabled()) { 429 LOG.error(Messages.get().getBundle().key( 430 Messages.LOG_DECORATION_DEFINITION_CREATE_MAP_2, 431 res.getName(), 432 e)); 433 } 434 } 435 } 436 return decorationMaps; 437 } 438 439 } 440 | Popular Tags |