1 25 package org.snipsnap.net.admin; 26 27 import org.dom4j.Document; 28 import org.dom4j.Element; 29 import org.dom4j.io.SAXReader; 30 import org.radeox.util.logging.Logger; 31 import org.snipsnap.config.Configuration; 32 import org.snipsnap.container.Components; 33 import org.snipsnap.snip.Snip; 34 import org.snipsnap.snip.SnipSpace; 35 36 import java.io.File ; 37 import java.io.FileReader ; 38 import java.io.FilenameFilter ; 39 import java.util.HashMap ; 40 import java.util.Iterator ; 41 import java.util.Map ; 42 43 public class ThemeHelper { 44 public final static String THEME_PREFIX = "SnipSnap/themes/"; 45 public final static int FILES = 0; 46 public final static int DOCUMENTS = 1; 47 public final static int CONTENT = 2; 48 49 public static Map getInstalledThemes() { 50 SnipSpace space = (SnipSpace) Components.getComponent(SnipSpace.class); 51 Snip[] themeSnips = space.match(THEME_PREFIX); 52 Map themes = new HashMap (); 53 for (int t = 0; t < themeSnips.length; t++) { 54 String name = themeSnips[t].getName(); 55 if(name.indexOf('/', THEME_PREFIX.length()) == -1) { 56 themes.put(name.substring(THEME_PREFIX.length()), themeSnips[t]); 57 } 58 } 59 return themes; 60 } 61 62 public static Map getThemeDocuments(Configuration config, int valueType) { 63 File themeDir = new File (config.getWebInfDir(), "themes"); 65 File [] files = themeDir.listFiles(new FilenameFilter () { 66 public boolean accept(File file, String s) { 67 return s.endsWith(".snip"); 68 } 69 }); 70 71 Map themeDocs = new HashMap (); 72 SAXReader saxReader = new SAXReader(); 73 for (int f = 0; f < files.length; f++) { 74 try { 75 Document themeDoc = saxReader.read(new FileReader (files[f])); 76 Iterator it = themeDoc.getRootElement().elementIterator("snip"); 77 while (it.hasNext()) { 78 Element snipEl = (Element) it.next(); 79 String tagName = snipEl.element("name").getText(); 80 if (tagName.startsWith(THEME_PREFIX) && 81 tagName.indexOf('/', THEME_PREFIX.length()) == -1) { 82 String themeName = tagName.substring(tagName.lastIndexOf('/') + 1); 83 switch(valueType) { 84 case FILES: 85 themeDocs.put(themeName, files[f]); 86 break; 87 case DOCUMENTS: 88 themeDocs.put(themeName, themeDoc); 89 break; 90 case CONTENT: 91 themeDocs.put(themeName, snipEl.elementText("content")); 92 break; 93 } 94 } 95 } 96 } catch (Exception e) { 97 Logger.warn("Error reading potential theme file", e); 98 } 99 } 100 return themeDocs; 101 } 102 } 103 | Popular Tags |