KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > net > admin > ThemeHelper


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

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 JavaDoc;
37 import java.io.FileReader JavaDoc;
38 import java.io.FilenameFilter JavaDoc;
39 import java.util.HashMap JavaDoc;
40 import java.util.Iterator JavaDoc;
41 import java.util.Map JavaDoc;
42
43 public class ThemeHelper {
44   public final static String JavaDoc 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 JavaDoc getInstalledThemes() {
50     SnipSpace space = (SnipSpace) Components.getComponent(SnipSpace.class);
51     Snip[] themeSnips = space.match(THEME_PREFIX);
52     Map JavaDoc themes = new HashMap JavaDoc();
53     for (int t = 0; t < themeSnips.length; t++) {
54       String JavaDoc 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 JavaDoc getThemeDocuments(Configuration config, int valueType) {
63     // find theme files in filesystem
64
File JavaDoc themeDir = new File JavaDoc(config.getWebInfDir(), "themes");
65     File JavaDoc[] files = themeDir.listFiles(new FilenameFilter JavaDoc() {
66       public boolean accept(File JavaDoc file, String JavaDoc s) {
67         return s.endsWith(".snip");
68       }
69     });
70
71     Map JavaDoc themeDocs = new HashMap JavaDoc();
72     SAXReader saxReader = new SAXReader();
73     for (int f = 0; f < files.length; f++) {
74       try {
75         Document themeDoc = saxReader.read(new FileReader JavaDoc(files[f]));
76         Iterator JavaDoc it = themeDoc.getRootElement().elementIterator("snip");
77         while (it.hasNext()) {
78           Element snipEl = (Element) it.next();
79           String JavaDoc tagName = snipEl.element("name").getText();
80           if (tagName.startsWith(THEME_PREFIX) &&
81             tagName.indexOf('/', THEME_PREFIX.length()) == -1) {
82             String JavaDoc 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 JavaDoc e) {
97         Logger.warn("Error reading potential theme file", e);
98       }
99     }
100     return themeDocs;
101   }
102 }
103
Popular Tags