1 18 19 package org.apache.roller.business; 20 21 import java.io.File ; 22 import java.io.FileInputStream ; 23 import java.io.FileNotFoundException ; 24 import java.io.FileReader ; 25 import java.io.FilenameFilter ; 26 import java.io.IOException ; 27 import java.io.InputStreamReader ; 28 import java.util.ArrayList ; 29 import java.util.Collection ; 30 import java.util.Collections ; 31 import java.util.Date ; 32 import java.util.HashMap ; 33 import java.util.Iterator ; 34 import java.util.List ; 35 import java.util.Map ; 36 import org.apache.commons.logging.Log; 37 import org.apache.commons.logging.LogFactory; 38 import org.apache.roller.RollerException; 39 import org.apache.roller.ThemeNotFoundException; 40 import org.apache.roller.config.RollerConfig; 41 import org.apache.roller.model.RollerFactory; 42 import org.apache.roller.model.ThemeManager; 43 import org.apache.roller.model.UserManager; 44 import org.apache.roller.pojos.Theme; 45 import org.apache.roller.pojos.ThemeTemplate; 46 import org.apache.roller.pojos.WeblogTemplate; 47 import org.apache.roller.pojos.WebsiteData; 48 49 50 56 public class ThemeManagerImpl implements ThemeManager { 57 58 private static Log log = LogFactory.getLog(ThemeManagerImpl.class); 59 60 private Map themes = null; 61 62 63 protected ThemeManagerImpl() { 64 65 log.debug("Initializing ThemeManagerImpl"); 68 69 this.themes = this.loadAllThemesFromDisk(); 70 log.info("Loaded "+this.themes.size()+" themes from disk."); 71 } 72 73 74 77 public Theme getTheme(String name) 78 throws ThemeNotFoundException, RollerException { 79 80 Theme theme = (Theme) this.themes.get(name); 81 if(theme == null) 82 throw new ThemeNotFoundException("Couldn't find theme ["+name+"]"); 83 84 return theme; 85 } 86 87 88 91 public Theme getThemeById(String id) 92 throws ThemeNotFoundException, RollerException { 93 94 return this.getTheme(id); 97 } 98 99 100 103 public List getThemesList() { 104 105 List themes = new ArrayList (this.themes.keySet()); 106 107 Collections.sort(themes); 109 110 return themes; 111 } 112 113 114 117 public List getEnabledThemesList() { 118 119 Collection all_themes = this.themes.values(); 120 121 List enabled_themes = new ArrayList (); 123 Iterator it = all_themes.iterator(); 124 Theme theme = null; 125 while(it.hasNext()) { 126 theme = (Theme) it.next(); 127 if(theme.isEnabled()) 128 enabled_themes.add(theme.getName()); 129 } 130 131 Collections.sort(enabled_themes); 133 134 return enabled_themes; 135 } 136 137 138 141 public ThemeTemplate getTemplate(String theme_name, String template_name) 142 throws ThemeNotFoundException, RollerException { 143 144 Theme theme = this.getTheme(theme_name); 146 147 return theme.getTemplate(template_name); 148 } 149 150 151 154 public ThemeTemplate getTemplateById(String id) 155 throws ThemeNotFoundException, RollerException { 156 157 if(id == null) 158 throw new ThemeNotFoundException("Theme id was null"); 159 160 String [] split = id.split(":", 2); 163 if(split.length != 2) 164 throw new ThemeNotFoundException("Invalid theme id ["+id+"]"); 165 166 return this.getTemplate(split[0], split[1]); 167 } 168 169 170 173 public ThemeTemplate getTemplateByLink(String theme_name, String template_link) 174 throws ThemeNotFoundException, RollerException { 175 176 Theme theme = this.getTheme(theme_name); 178 179 return theme.getTemplateByLink(template_link); 180 } 181 182 183 187 private Map loadAllThemesFromDisk() { 188 189 Map themes = new HashMap (); 190 191 String themespath = RollerConfig.getProperty("context.realPath"); 193 if(themespath.endsWith(File.separator)) 194 themespath += "themes"; 195 else 196 themespath += File.separator + "themes"; 197 198 File themesdir = new File (themespath); 200 FilenameFilter filter = new FilenameFilter () { 201 public boolean accept(File dir, String name) { 202 File file = 203 new File (dir.getAbsolutePath() + File.separator + name); 204 return file.isDirectory(); 205 } 206 }; 207 String [] themenames = themesdir.list(filter); 208 209 if(themenames == null) 210 themenames = new String [0]; 211 212 Theme theme = null; 214 for(int i=0; i < themenames.length; i++) { 215 try { 216 theme = this.loadThemeFromDisk(themenames[i], 217 themespath + File.separator + themenames[i]); 218 themes.put(theme.getName(), theme); 219 } catch (Throwable unexpected) { 220 log.error("Problem reading theme " + themenames[i], unexpected); 222 } 223 } 224 225 return themes; 226 } 227 228 232 private Theme loadThemeFromDisk(String theme_name, String themepath) { 233 234 log.info("Loading theme "+theme_name); 235 236 Theme theme = new Theme(); 237 theme.setName(theme_name); 238 theme.setAuthor("Roller"); 239 theme.setLastEditor("Roller"); 240 theme.setEnabled(true); 241 242 File themedir = new File (themepath); 244 FilenameFilter filter = new FilenameFilter () 245 { 246 public boolean accept(File dir, String name) 247 { 248 return name.endsWith(".vm"); 249 } 250 }; 251 String [] templates = themedir.list(filter); 252 253 String template_name = null; 255 ThemeTemplate theme_template = null; 256 for (int i=0; i < templates.length; i++) { 257 template_name = templates[i].substring(0, templates[i].length() - 3); 259 File template_file = new File (themepath + File.separator + templates[i]); 260 261 String msg = "read theme template file ["+template_file+"]"; 263 if(!template_file.exists() && !template_file.canRead()) { 264 log.error("Couldn't " + msg); 265 continue; 266 } 267 char[] chars = null; 268 int length; 269 try { 270 chars = new char[(int) template_file.length()]; 272 FileInputStream stream = new FileInputStream (template_file); 273 InputStreamReader reader = new InputStreamReader (stream, "UTF-8"); 274 length = reader.read(chars); 275 } catch (Exception noprob) { 276 log.error("Exception while attempting to " + msg); 277 if (log.isDebugEnabled()) log.debug(noprob); 278 continue; 279 } 280 281 boolean navbar = true; 283 String template_link = template_name; 284 if (template_name.startsWith("_") && template_name.length() > 1) { 285 navbar = false; 286 template_link = template_link.substring(1); 287 log.debug("--- " + template_link); 288 } 289 290 String decorator = "_decorator"; 291 if("_decorator".equals(template_name)) { 292 decorator = null; 293 } 294 295 theme_template = new ThemeTemplate( 301 theme, 302 theme_name+":"+template_name, 303 template_name, 304 template_name, 305 new String (chars, 0, length), 306 template_link, 307 new Date (template_file.lastModified()), 308 "velocity", 309 false, 310 navbar, 311 decorator); 312 313 theme.setTemplate(template_name, theme_template); 315 } 316 317 theme.setLastModified(theme_template.getLastModified()); 320 321 return theme; 322 } 323 324 332 public void saveThemePages(WebsiteData website, Theme theme) 333 throws RollerException { 334 335 log.debug("Setting custom templates for website: "+website.getName()); 336 337 try { 338 UserManager userMgr = RollerFactory.getRoller().getUserManager(); 339 340 Collection templates = theme.getTemplates(); 341 Iterator iter = templates.iterator(); 342 ThemeTemplate theme_template = null; 343 while ( iter.hasNext() ) { 344 theme_template = (ThemeTemplate) iter.next(); 345 346 WeblogTemplate template = null; 347 348 if(theme_template.getName().equals(WeblogTemplate.DEFAULT_PAGE)) { 349 try { 351 template = userMgr.getPage(website.getDefaultPageId()); 352 } catch(Exception e) { 353 } 355 } else { 356 template = userMgr.getPageByName(website, theme_template.getName()); 358 } 359 360 361 if (template != null) { 362 template.setContents(theme_template.getContents()); 364 template.setLink(theme_template.getLink()); 365 366 } else { 367 template = new WeblogTemplate( 369 null, website, theme_template.getName(), theme_template.getDescription(), theme_template.getLink(), theme_template.getContents(), new Date (), theme_template.getTemplateLanguage(), theme_template.isHidden(), theme_template.isNavbar(), theme_template.getDecoratorName() ); 381 userMgr.savePage( template ); 382 } 383 } 384 385 website.setEditorTheme(Theme.CUSTOM); 387 388 if(website.getDefaultPageId() == null || 391 website.getDefaultPageId().trim().equals("") || 392 website.getDefaultPageId().equals("dummy")) { 393 WeblogTemplate template = userMgr.getPageByName(website, "Weblog"); 395 if(template != null) { 396 log.debug("Setting default page to "+template.getId()); 397 website.setDefaultPageId(template.getId()); 398 } 399 } 400 401 WeblogTemplate dayTemplate = userMgr.getPageByName(website, "_day"); 403 if(dayTemplate != null) { 404 log.debug("Setting default day page to "+dayTemplate.getId()); 405 website.setWeblogDayPageId(dayTemplate.getId()); 406 } 407 408 userMgr.saveWebsite(website); 410 411 } catch (Exception e) { 412 log.error("ERROR in action",e); 413 throw new RollerException( e ); 414 } 415 } 416 } 417 | Popular Tags |