001: /**
002: * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, version 2.1, dated February 1999.
003: *
004: * This program is free software; you can redistribute it and/or modify
005: * it under the terms of the latest version of the GNU Lesser General
006: * Public License as published by the Free Software Foundation;
007: *
008: * This program is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
011: * GNU Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public License
014: * along with this program (LICENSE.txt); if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
016: */package org.jamwiki;
017:
018: import java.io.File;
019: import java.util.Collection;
020: import java.util.HashMap;
021: import java.util.List;
022: import java.util.Map;
023: import java.util.Vector;
024: import org.jamwiki.utils.Utilities;
025: import org.jamwiki.model.WikiConfigurationObject;
026: import org.jamwiki.utils.WikiLogger;
027: import org.jamwiki.utils.XMLUtil;
028: import org.w3c.dom.Document;
029: import org.w3c.dom.Node;
030: import org.w3c.dom.NodeList;
031:
032: /**
033: * The <code>WikiConfiguration</code> class provides the infrastructure for
034: * retrieving configuration values. Note that with JAMWiki configuration
035: * values differ from site properties by being generally less site-specific
036: * and falling into specific categories, such as pseudo-topics and parser
037: * values.
038: *
039: * @see org.jamwiki.utils.PseudoTopicHandler
040: * @see org.jamwiki.utils.NamespaceHandler
041: */
042: public class WikiConfiguration {
043:
044: /** Standard logger. */
045: private static final WikiLogger logger = WikiLogger
046: .getLogger(WikiConfiguration.class.getName());
047:
048: private static WikiConfiguration instance = null;
049:
050: private List dataHandlers = null;
051: private Map namespaces = null;
052: private List parsers = null;
053: private List pseudotopics = null;
054: private List searchEngines = null;
055: private Map translations = null;
056: private List userHandlers = null;
057:
058: /** Name of the configuration file. */
059: public static final String JAMWIKI_CONFIGURATION_FILE = "jamwiki-configuration.xml";
060: private static final String XML_CONFIGURATION_ROOT = "configuration";
061: private static final String XML_DATA_HANDLER = "data-handler";
062: private static final String XML_DATA_HANDLER_ROOT = "data-handlers";
063: private static final String XML_NAMESPACE = "namespace";
064: private static final String XML_NAMESPACE_COMMENTS = "comments";
065: private static final String XML_NAMESPACE_MAIN = "main";
066: private static final String XML_NAMESPACE_ROOT = "namespaces";
067: private static final String XML_PARAM_CLASS = "class";
068: private static final String XML_PARAM_KEY = "key";
069: private static final String XML_PARAM_NAME = "name";
070: private static final String XML_PARAM_STATE = "state";
071: private static final String XML_PARSER = "parser";
072: private static final String XML_PARSER_ROOT = "parsers";
073: private static final String XML_PSEUDOTOPIC = "pseudotopic";
074: private static final String XML_PSEUDOTOPIC_ROOT = "pseudotopics";
075: private static final String XML_SEARCH_ENGINE = "search-engine";
076: private static final String XML_SEARCH_ENGINE_ROOT = "search-engines";
077: private static final String XML_TRANSLATION = "translation";
078: private static final String XML_TRANSLATION_ROOT = "translations";
079: private static final String XML_USER_HANDLER = "user-handler";
080: private static final String XML_USER_HANDLER_ROOT = "user-handlers";
081:
082: /**
083: *
084: */
085: private WikiConfiguration() {
086: this .initialize();
087: }
088:
089: /**
090: *
091: */
092: public static WikiConfiguration getInstance() {
093: if (WikiConfiguration.instance == null) {
094: WikiConfiguration.instance = new WikiConfiguration();
095: }
096: return WikiConfiguration.instance;
097: }
098:
099: /**
100: *
101: */
102: public Collection getDataHandlers() {
103: return this .dataHandlers;
104: }
105:
106: /**
107: *
108: */
109: public Map getNamespaces() {
110: return this .namespaces;
111: }
112:
113: /**
114: *
115: */
116: public Collection getParsers() {
117: return this .parsers;
118: }
119:
120: /**
121: *
122: */
123: public Collection getPseudotopics() {
124: return this .pseudotopics;
125: }
126:
127: /**
128: *
129: */
130: public Collection getSearchEngines() {
131: return this .searchEngines;
132: }
133:
134: /**
135: *
136: */
137: public Map getTranslations() {
138: return this .translations;
139: }
140:
141: /**
142: *
143: */
144: public Collection getUserHandlers() {
145: return this .userHandlers;
146: }
147:
148: /**
149: *
150: */
151: private void initialize() {
152: try {
153: this .dataHandlers = new Vector();
154: this .namespaces = new HashMap();
155: this .parsers = new Vector();
156: this .pseudotopics = new Vector();
157: this .searchEngines = new Vector();
158: this .translations = new HashMap();
159: this .userHandlers = new Vector();
160: File file = Utilities
161: .getClassLoaderFile(JAMWIKI_CONFIGURATION_FILE);
162: Document document = XMLUtil.parseXML(file, false);
163: Node node = document.getElementsByTagName(
164: XML_CONFIGURATION_ROOT).item(0);
165: NodeList children = node.getChildNodes();
166: Node child = null;
167: for (int i = 0; i < children.getLength(); i++) {
168: child = children.item(i);
169: if (child.getNodeName().equals(XML_PARSER_ROOT)) {
170: this .parsers = this .parseConfigurationObjects(
171: child, XML_PARSER);
172: } else if (child.getNodeName().equals(
173: XML_DATA_HANDLER_ROOT)) {
174: this .dataHandlers = this .parseConfigurationObjects(
175: child, XML_DATA_HANDLER);
176: } else if (child.getNodeName().equals(
177: XML_SEARCH_ENGINE_ROOT)) {
178: this .searchEngines = this
179: .parseConfigurationObjects(child,
180: XML_SEARCH_ENGINE);
181: } else if (child.getNodeName().equals(
182: XML_USER_HANDLER_ROOT)) {
183: this .userHandlers = this .parseConfigurationObjects(
184: child, XML_USER_HANDLER);
185: } else if (child.getNodeName().equals(
186: XML_NAMESPACE_ROOT)) {
187: this .parseNamespaces(child);
188: } else if (child.getNodeName().equals(
189: XML_PSEUDOTOPIC_ROOT)) {
190: this .parsePseudotopics(child);
191: } else if (child.getNodeName().equals(
192: XML_TRANSLATION_ROOT)) {
193: this .parseTranslations(child);
194: } else {
195: logUnknownChild(node, child);
196: }
197: }
198: logger.config("Configuration values loaded from "
199: + file.getPath());
200: } catch (Exception e) {
201: logger.severe("Failure while parsing configuration file "
202: + JAMWIKI_CONFIGURATION_FILE, e);
203: }
204: }
205:
206: /**
207: *
208: */
209: private WikiConfigurationObject parseConfigurationObject(Node node)
210: throws Exception {
211: WikiConfigurationObject configurationObject = new WikiConfigurationObject();
212: NodeList children = node.getChildNodes();
213: for (int j = 0; j < children.getLength(); j++) {
214: Node child = children.item(j);
215: if (child.getNodeName().equals(XML_PARAM_CLASS)) {
216: configurationObject.setClazz(XMLUtil
217: .getTextContent(child));
218: } else if (child.getNodeName().equals(XML_PARAM_KEY)) {
219: configurationObject.setKey(XMLUtil
220: .getTextContent(child));
221: } else if (child.getNodeName().equals(XML_PARAM_NAME)) {
222: configurationObject.setName(XMLUtil
223: .getTextContent(child));
224: } else if (child.getNodeName().equals(XML_PARAM_STATE)) {
225: configurationObject.setState(XMLUtil
226: .getTextContent(child));
227: } else {
228: logUnknownChild(node, child);
229: }
230: }
231: return configurationObject;
232: }
233:
234: /**
235: *
236: */
237: private List parseConfigurationObjects(Node node, String name)
238: throws Exception {
239: List results = new Vector();
240: NodeList children = node.getChildNodes();
241: for (int j = 0; j < children.getLength(); j++) {
242: Node child = children.item(j);
243: if (child.getNodeName().equals(name)) {
244: results.add(this .parseConfigurationObject(child));
245: } else {
246: logUnknownChild(node, child);
247: }
248: }
249: return results;
250: }
251:
252: /**
253: *
254: */
255: private void parseNamespace(Node node) throws Exception {
256: NodeList children = node.getChildNodes();
257: String name = "";
258: String main = "";
259: String comments = "";
260: for (int j = 0; j < children.getLength(); j++) {
261: Node child = children.item(j);
262: if (child.getNodeName().equals(XML_PARAM_NAME)) {
263: name = XMLUtil.getTextContent(child);
264: } else if (child.getNodeName().equals(XML_NAMESPACE_MAIN)) {
265: main = XMLUtil.getTextContent(child);
266: } else if (child.getNodeName().equals(
267: XML_NAMESPACE_COMMENTS)) {
268: comments = XMLUtil.getTextContent(child);
269: } else {
270: logUnknownChild(node, child);
271: }
272: }
273: this .namespaces.put(name, new String[] { main, comments });
274: }
275:
276: /**
277: *
278: */
279: private void parseNamespaces(Node node) throws Exception {
280: NodeList children = node.getChildNodes();
281: for (int j = 0; j < children.getLength(); j++) {
282: Node child = children.item(j);
283: if (child.getNodeName().equals(XML_NAMESPACE)) {
284: this .parseNamespace(child);
285: } else {
286: logUnknownChild(node, child);
287: }
288: }
289: }
290:
291: /**
292: *
293: */
294: private void parsePseudotopic(Node node) throws Exception {
295: NodeList children = node.getChildNodes();
296: for (int j = 0; j < children.getLength(); j++) {
297: Node child = children.item(j);
298: if (child.getNodeName().equals(XML_PARAM_NAME)) {
299: this .pseudotopics.add(XMLUtil.getTextContent(child));
300: } else {
301: logUnknownChild(node, child);
302: }
303: }
304: }
305:
306: /**
307: *
308: */
309: private void parsePseudotopics(Node node) throws Exception {
310: NodeList children = node.getChildNodes();
311: for (int j = 0; j < children.getLength(); j++) {
312: Node child = children.item(j);
313: if (child.getNodeName().equals(XML_PSEUDOTOPIC)) {
314: this .parsePseudotopic(child);
315: } else {
316: logUnknownChild(node, child);
317: }
318: }
319: }
320:
321: /**
322: *
323: */
324: private void parseTranslation(Node node) throws Exception {
325: NodeList children = node.getChildNodes();
326: String name = "";
327: String key = "";
328: for (int j = 0; j < children.getLength(); j++) {
329: Node child = children.item(j);
330: if (child.getNodeName().equals(XML_PARAM_NAME)) {
331: name = XMLUtil.getTextContent(child);
332: } else if (child.getNodeName().equals(XML_PARAM_KEY)) {
333: key = XMLUtil.getTextContent(child);
334: } else {
335: logUnknownChild(node, child);
336: }
337: }
338: this .translations.put(key, name);
339: }
340:
341: /**
342: *
343: */
344: private void parseTranslations(Node node) throws Exception {
345: NodeList children = node.getChildNodes();
346: for (int j = 0; j < children.getLength(); j++) {
347: Node child = children.item(j);
348: if (child.getNodeName().equals(XML_TRANSLATION)) {
349: this .parseTranslation(child);
350: } else {
351: logUnknownChild(node, child);
352: }
353: }
354: }
355:
356: /**
357: * Utility class to log two XML nodes.
358: * @param node
359: * @param child
360: */
361: private void logUnknownChild(Node node, Node child) {
362: logger.finest("Unknown child of " + node.getNodeName()
363: + " tag: " + child.getNodeName() + " / "
364: + child.getNodeValue());
365: }
366: }
|