001: /**
002: * @(#)ResourceBase.java 1.0 2001/09/02
003: *
004: * Copyleft 2000 by Steve Excellent Lee
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: */package org.bulbul.webmail.xmlresource;
020:
021: import java.io.IOException;
022: import java.util.*;
023:
024: import org.w3c.dom.*;
025: import org.xml.sax.*;
026:
027: import javax.xml.parsers.*;
028:
029: import net.wastl.webmail.server.WebMailServer;
030:
031: /**
032: * A ResourceBundle implementation that uses a XML file to store the resources.
033: * Modified from Sebastian Schaffert's
034: * net.wastl.webmail.xml.XMLResourceBundle.java
035: *
036: * New scheme:
037: * We separate locale resource to differenet files instead of putting all
038: * different locale resources into single xml file, since the encoding
039: * can't vary. (A single xml file can only use one encoding).
040: *
041: * Subclasses must override <code>getXmlResourceFilename</code> and
042: * provide the filename which contains appropriate locale-specific resources.
043: *
044: * Note:
045: * The resource files must resides in the directory that defined by
046: * `webmail.template.path' property, hence <code>getXmlResourceFilename</code>
047: * must returns only filename without pathname)
048: *
049: * @author Steve Excellent Lee
050: * @version 1.0 2001
051: */
052: public abstract class ResourceBase extends ResourceBundle {
053: protected boolean debug = true;
054:
055: protected Document xmlRoot = null;
056:
057: protected Element elementBundle = null; // The <BUNDLE> element of resource xml file
058: protected Element elem_common = null;
059:
060: /**
061: * Sole constructor. (For invocation by subclass constructors, typically
062: * implicit.)
063: */
064: public ResourceBase() {
065: }
066:
067: public Enumeration getKeys() {
068: Hashtable prop = new Hashtable();
069:
070: if (elem_common != null) {
071: getKeys(elem_common, prop);
072: }
073: if (elementBundle != null) {
074: getKeys(elementBundle, prop);
075: }
076: return prop.keys();
077: }
078:
079: protected Object handleGetObject(String key)
080: throws MissingResourceException {
081: String retval = null;
082:
083: // Lazily load the XML resource file
084: if (xmlRoot == null) {
085: loadXmlResourceFile();
086: }
087:
088: if (elementBundle != null) {
089: retval = getResult(elementBundle, key);
090: }
091: if ((retval == null) && (elem_common != null)) {
092: retval = getResult(elem_common, key);
093: }
094:
095: if (debug)
096: System.err.println("XMLResourceBundle: " + key + " = "
097: + retval);
098:
099: return retval;
100: }
101:
102: /**
103: * See class description.
104: */
105: abstract protected String getXmlResourceFilename();
106:
107: protected void loadXmlResourceFile() {
108: try {
109: DocumentBuilder parser = DocumentBuilderFactory
110: .newInstance().newDocumentBuilder();
111: System.err.println("file://"
112: + WebMailServer.getServer().getProperty(
113: "webmail.template.path")
114: + System.getProperty("file.separator")
115: + getXmlResourceFilename());
116: xmlRoot = parser.parse("file://"
117: + WebMailServer.getServer().getProperty(
118: "webmail.template.path")
119: + System.getProperty("file.separator")
120: + getXmlResourceFilename());
121:
122: NodeList nl = xmlRoot.getElementsByTagName("COMMON");
123: if (nl.getLength() > 0) {
124: elem_common = (Element) nl.item(0);
125: }
126:
127: nl = xmlRoot.getElementsByTagName("LOCALE");
128: if (nl.getLength() > 0) {
129: elementBundle = (Element) nl.item(0);
130: }
131: } catch (IOException e) {
132: System.err.println(e);
133: } catch (SAXException e) {
134: System.err.println(e);
135: } catch (ParserConfigurationException e) {
136: System.err.println(e);
137: }
138: }
139:
140: protected void getKeys(Element element, Hashtable hash) {
141: NodeList nl = element.getElementsByTagName("RESOURCE");
142: for (int i = 0; i < nl.getLength(); i++) {
143: hash.put(((Element) nl.item(i)).getAttribute("name"), "");
144: }
145: }
146:
147: protected String getResult(Element element, String key) {
148: NodeList nl = element.getElementsByTagName("RESOURCE");
149: for (int i = 0; i < nl.getLength(); i++) {
150: Element e = (Element) nl.item(i);
151: if (e.getAttribute("name").equals(key)) {
152: String s = "";
153: NodeList textl = e.getChildNodes();
154: for (int j = 0; j < textl.getLength(); j++) {
155: if (debug)
156: System.err.println("XMLResourceBundle (" + key
157: + "): Type "
158: + textl.item(j).getNodeName());
159: if (textl.item(j).getNodeName().equals("#text")
160: || textl.item(j).getNodeName().equals(
161: "#cdata-section")) {
162: s += textl.item(j).getNodeValue();
163: }
164: }
165: return s;
166: }
167: }
168: return null;
169: }
170: }
|