001: /* CVS ID: $Id: XMLGenericModel.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: import java.io.*;
006:
007: import javax.xml.parsers.*;
008:
009: import org.w3c.dom.*;
010:
011: import net.wastl.webmail.server.*;
012:
013: /*
014: * XMLGenericModel.java
015: *
016: * Created: Thu May 4 15:53:12 2000
017: *
018: * Copyright (C) 2000 Sebastian Schaffert
019: *
020: * This program is free software; you can redistribute it and/or
021: * modify it under the terms of the GNU General Public License
022: * as published by the Free Software Foundation; either version 2
023: * of the License, or (at your option) any later version.
024: *
025: * This program is distributed in the hope that it will be useful,
026: * but WITHOUT ANY WARRANTY; without even the implied warranty of
027: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
028: * GNU General Public License for more details.
029: *
030: * You should have received a copy of the GNU General Public License
031: * along with this program; if not, write to the Free Software
032: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
033: *
034: */
035:
036: /**
037: * A generic representation of WebMail data. Contains mainly state information
038: * and the system configuration
039: *
040: * @author Sebastian Schaffert
041: * @version
042: */
043:
044: public class XMLGenericModel {
045:
046: protected Document root;
047:
048: protected Element sysdata;
049:
050: protected Element statedata;
051:
052: protected WebMailServer parent;
053:
054: protected long current_id = 0;
055:
056: protected DocumentBuilder parser;
057:
058: public XMLGenericModel(WebMailServer parent, Element rsysdata)
059: throws ParserConfigurationException {
060:
061: this .parent = parent;
062:
063: parser = DocumentBuilderFactory.newInstance()
064: .newDocumentBuilder();
065:
066: initRoot();
067:
068: statedata = root.createElement("STATEDATA");
069:
070: NodeList nl = root.getDocumentElement().getElementsByTagName(
071: "STATEDATA");
072: if (nl.getLength() > 0) {
073: System.err
074: .println("*** Warning: Webmail usermodel template contained a STATEDATA tag, although this should only be created at runtime!");
075: root.getDocumentElement().replaceChild(statedata,
076: nl.item(0));
077: } else {
078: root.getDocumentElement().appendChild(statedata);
079: }
080:
081: this .sysdata = rsysdata;
082:
083: }
084:
085: protected void initRoot() {
086: // Create a new usermodel from the template file
087: try {
088: root = parser.parse("file://"
089: + parent.getProperty("webmail.xml.path")
090: + System.getProperty("file.separator")
091: + "generic_template.xml");
092: } catch (Exception ex) {
093: System.err
094: .println("Error parsing WebMail UserModel template "
095: + ex.getMessage());
096: ex.printStackTrace();
097: }
098: }
099:
100: public Document getRoot() {
101: return root;
102: }
103:
104: public Element getStateData() {
105: return statedata;
106: }
107:
108: public void init() {
109: setStateVar("base uri", parent.getBasePath());
110: setStateVar("img base uri", parent.getImageBasePath() + "/"
111: + parent.getDefaultLocale().getLanguage() + "/"
112: + parent.getDefaultTheme());
113: setStateVar("webmail version", parent.getVersion());
114: setStateVar("operating system", System.getProperty("os.name")
115: + " " + System.getProperty("os.version") + "/"
116: + System.getProperty("os.arch"));
117: setStateVar("java virtual machine", System
118: .getProperty("java.vendor")
119: + " "
120: + System.getProperty("java.vm.name")
121: + " "
122: + System.getProperty("java.version"));
123: }
124:
125: public void update() {
126: // Insert the sysdata and userdata objects into the usermodel tree
127: try {
128: NodeList nl = root.getElementsByTagName("SYSDATA");
129: root.getDocumentElement().replaceChild(
130: root.importNode(sysdata, true), nl.item(0));
131: } catch (ArrayIndexOutOfBoundsException ex) {
132: System.err
133: .println("The WebMail GenericModel template file didn't contain a SYSDATA tag.");
134: } catch (DOMException ex) {
135: System.err
136: .println("Something went wrong with the XML generic model.");
137: }
138: }
139:
140: /**
141: * Create a unique ID.
142: * Important: This returns a new Unique ID within this session.
143: * It should be used to generate IDs for Folders and messages so that they can be easily referenced
144: */
145: public String getNextID() {
146: return Long.toHexString(++current_id).toUpperCase();
147: }
148:
149: public synchronized void setException(Exception ex) {
150: Element exception = root.createElement("EXCEPTION");
151: Element ex_message = root.createElement("EX_MESSAGE");
152: Element ex_stacktrace = root.createElement("EX_STACKTRACE");
153: exception.appendChild(ex_message);
154: exception.appendChild(ex_stacktrace);
155:
156: Text msg = root.createTextNode(ex.getMessage());
157: ex_message.appendChild(msg);
158:
159: String my_stack = "";
160: CharArrayWriter cstream = new CharArrayWriter();
161: ex.printStackTrace(new PrintWriter(cstream));
162: my_stack = cstream.toString();
163: CDATASection stack = root.createCDATASection(my_stack);
164: ex_stacktrace.appendChild(stack);
165:
166: NodeList nl = statedata.getElementsByTagName("EXCEPTION");
167: if (nl.getLength() > 0) {
168: statedata.replaceChild(exception, nl.item(0));
169: } else {
170: statedata.appendChild(exception);
171: }
172:
173: //XMLCommon.debugXML(root);
174: }
175:
176: /**
177: * We need to synchronize that to avoid problems, but this should be fast anyway
178: */
179: public synchronized void setStateVar(String name, String value) {
180: Element var = XMLCommon.getElementByAttribute(statedata, "VAR",
181: "name", name);
182: if (var == null) {
183: var = root.createElement("VAR");
184: var.setAttribute("name", name);
185: statedata.appendChild(var);
186: }
187: var.setAttribute("value", value);
188: }
189:
190: public Element createStateVar(String name, String value) {
191: Element var = root.createElement("VAR");
192: var.setAttribute("name", name);
193: var.setAttribute("value", value);
194: return var;
195: }
196:
197: public void addStateVar(String name, String value) {
198: Element var = root.createElement("VAR");
199: var.setAttribute("name", name);
200: statedata.appendChild(var);
201: var.setAttribute("value", value);
202: }
203:
204: /**
205: * We need to synchronize that because it can cause problems with multiple threads
206: */
207: public synchronized void removeAllStateVars(String name) {
208: NodeList nl=statedata.getElementsByTagName("VAR");
209:
210: /* This suxx: NodeList Object is changed when removing children !!!
211: I will store all nodes that should be deleted in a Vector and delete them afterwards */
212: int length=nl.getLength();
213: Vector v=new Vector(nl.getLength());
214: for(int i=0;i<length;i++) {
215: if(((Element)nl.item(i)).getAttribute("name").equals(name)) {
216: v.addElement(nl.item(i));
217: }
218: }
219: Enumeration enum=v.elements();
220: while(enum.hasMoreElements()) {
221: Node n=(Node)enum.nextElement();
222: statedata.removeChild(n);
223: }
224: }
225:
226: public String getStateVar(String name) {
227: Element var = XMLCommon.getElementByAttribute(statedata, "VAR",
228: "name", name);
229: if (var == null) {
230: return "";
231: } else {
232: return var.getAttribute("value");
233: }
234: }
235:
236: } // XMLGenericModel
|