001: /* CVS ID: $Id: XMLResourceBundle.java,v 1.1.1.1 2002/10/02 18:42:54 wastl Exp $ */
002: package net.wastl.webmail.xml;
003:
004: import java.util.*;
005:
006: import javax.xml.parsers.*;
007:
008: import org.w3c.dom.*;
009:
010: import net.wastl.webmail.server.WebMailServer;
011:
012: /*
013: * XMLResourceBundle.java
014: *
015: * Created: Sun Mar 5 17:59:33 2000
016: *
017: * Copyright (C) 2000 Sebastian Schaffert
018: *
019: * This program is free software; you can redistribute it and/or
020: * modify it under the terms of the GNU General Public License
021: * as published by the Free Software Foundation; either version 2
022: * of the License, or (at your option) any later version.
023: *
024: * This program is distributed in the hope that it will be useful,
025: * but WITHOUT ANY WARRANTY; without even the implied warranty of
026: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
027: * GNU General Public License for more details.
028: *
029: * You should have received a copy of the GNU General Public License
030: * along with this program; if not, write to the Free Software
031: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
032: */
033: /**
034: * A ResourceBundle implementation that uses a XML file to store the resources.
035: *
036: * @author Sebastian Schaffert
037: * @version
038: */
039:
040: public class XMLResourceBundle extends ResourceBundle {
041:
042: protected boolean debug = false;
043:
044: protected Document root;
045: protected String language;
046:
047: protected Element elem_locale;
048: protected Element elem_common;
049: protected Element elem_default;
050:
051: public XMLResourceBundle(String resourcefile, String lang)
052: throws Exception {
053: DocumentBuilder parser = DocumentBuilderFactory.newInstance()
054: .newDocumentBuilder();
055: root = parser.parse("file://" + resourcefile);
056: language = lang;
057: NodeList nl = root.getElementsByTagName("COMMON");
058: if (nl.getLength() > 0) {
059: elem_common = (Element) nl.item(0);
060: } else {
061: elem_common = null;
062: }
063:
064: elem_locale = null;
065: elem_default = null;
066: /* Now the locale specific stuff; fallback to default if not possbile */
067: String default_lang = root.getDocumentElement().getAttribute(
068: "default");
069: if (debug)
070: System.err.println("XMLResourceBundle (" + resourcefile
071: + "): Default language '" + default_lang + "'.");
072: nl = root.getElementsByTagName("LOCALE");
073: for (int i = 0; i < nl.getLength(); i++) {
074: Element e = (Element) nl.item(i);
075: if (e.getAttribute("lang").equals(lang)) {
076: elem_locale = e;
077: }
078: if (e.getAttribute("lang").equals(default_lang)) {
079: elem_default = e;
080: }
081: }
082: }
083:
084: protected String getResult(Element element, String key) {
085: NodeList nl = element.getElementsByTagName("RESOURCE");
086: for (int i = 0; i < nl.getLength(); i++) {
087: Element e = (Element) nl.item(i);
088: if (e.getAttribute("name").equals(key)) {
089: String s = "";
090: NodeList textl = e.getChildNodes();
091: for (int j = 0; j < textl.getLength(); j++) {
092: if (debug)
093: System.err.println("XMLResourceBundle (" + key
094: + "): Type "
095: + textl.item(j).getNodeName());
096: if (textl.item(j).getNodeName().equals("#text")
097: || textl.item(j).getNodeName().equals(
098: "#cdata-section")) {
099: s += textl.item(j).getNodeValue();
100: }
101: }
102: return s;
103: }
104: }
105: return null;
106: }
107:
108: public Object handleGetObject(String key) {
109: String retval = null;
110: if (elem_locale != null) {
111: retval = getResult(elem_locale, key);
112: }
113: if (retval == null && elem_default != null) {
114: retval = getResult(elem_default, key);
115: }
116: if (retval == null && elem_common != null) {
117: retval = getResult(elem_common, key);
118: }
119: if (debug)
120: System.err.println("XMLResourceBundle: " + key + " = "
121: + retval);
122: return retval;
123: }
124:
125: protected void getKeys(Element element, Hashtable hash) {
126: NodeList nl = element.getElementsByTagName("RESOURCE");
127: for (int i = 0; i < nl.getLength(); i++) {
128: hash.put(((Element) nl.item(i)).getAttribute("name"), "");
129: }
130: }
131:
132: public Enumeration getKeys() {
133:
134: Hashtable prop = new Hashtable();
135:
136: if (elem_common != null) {
137: getKeys(elem_common, prop);
138: }
139: if (elem_default != null) {
140: getKeys(elem_default, prop);
141: }
142: if (elem_locale != null) {
143: getKeys(elem_locale, prop);
144: }
145: return prop.keys();
146: }
147:
148: public static synchronized ResourceBundle getBundle(String name,
149: Locale locale, ClassLoader cl)
150: throws MissingResourceException {
151: String lang = locale.getLanguage();
152:
153: ResourceBundle ret = null;
154:
155: try {
156:
157: ret = new XMLResourceBundle(WebMailServer.getServer()
158: .getProperty("webmail.template.path")
159: + System.getProperty("file.separator")
160: + name
161: + ".xml", lang);
162: } catch (Exception ex) {
163: ex.printStackTrace();
164: throw new MissingResourceException("Resource not found",
165: name, "");
166: }
167:
168: return ret;
169: }
170:
171: } // XMLResourceBundle
|