001: /*- ConfigParser.java ---------------------------------------------+
002: | |
003: | Copyright (C) 2002-2003 Joseph Monti, LlamaChat |
004: | countjoe@users.sourceforge.net |
005: | http://www.42llamas.com/LlamaChat/ |
006: | |
007: | This program is free software; you can redistribute it and/or |
008: | modify it under the terms of the GNU General Public License |
009: | as published by the Free Software Foundation; either version 2 |
010: | of the License, or (at your option) any later version |
011: | |
012: | This program is distributed in the hope that it will be useful, |
013: | but WITHOUT ANY WARRANTY; without even the implied warranty of |
014: | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
015: | GNU General Public License for more details. |
016: | |
017: | A copy of the GNU General Public License may be found in the |
018: | installation directory named "GNUGPL.txt" |
019: | |
020: +-----------------------------------------------------------------+
021: */
022:
023: package server;
024:
025: import org.xml.sax.helpers.DefaultHandler;
026: import org.xml.sax.Attributes;
027:
028: /* -------------------- JavaDoc Information ----------------------*/
029: /**
030: * a default handler for config file parsing in XML
031: * @author Joseph Monti <a href="mailto:countjoe@users.sourceforge.net">countjoe@users.sourceforge.net</a>
032: * @version 0.8
033: */
034: public class ConfigParser extends DefaultHandler {
035: /**
036: * a reference back to the server
037: */
038: private LlamaChatServer server;
039:
040: /**
041: * status indicator for telling if in message tag
042: */
043: private boolean inMessage;
044:
045: /**
046: * buffer to use while parsing welcome message
047: */
048: private String messageBuffer;
049:
050: /**
051: * constructor to initialize data
052: * @param a reference back to the calling server
053: */
054: ConfigParser(LlamaChatServer s) {
055: server = s;
056: inMessage = false;
057: messageBuffer = "";
058: }
059:
060: /**
061: * used for gathering welcome message (ignores all other tags)
062: */
063: public void characters(char[] ch, int start, int length) {
064: String s = new String(ch, start, length);
065: if (s != null && inMessage) {
066: s = s.replaceAll("[\\t\\n]", " ");
067: s = s.replaceAll("[ ]{2,}", " ");
068: s = s.trim();
069: messageBuffer += s;
070: }
071: }
072:
073: /**
074: * used for gathering welcome message (ignores all other tags)
075: */
076: public void endElement(String uri, String localName, String qName) {
077: String name = localName;
078: if ("".equals(name))
079: name = qName;
080: if ("WelcomeMessage".equals(name)) {
081: inMessage = false;
082: server.welcomeMessage = messageBuffer;
083: }
084: }
085:
086: /**
087: * parses the data within the atributes for the configuration settings
088: */
089: public void startElement(String uri, String localName,
090: String qName, Attributes attributes) {
091: String name = localName;
092: if ("".equals(name))
093: name = qName;
094: if ("Port".equals(name)) {
095: String val = attributes.getValue("value");
096: try {
097: if (val != null)
098: server.PORT = Integer.parseInt(val);
099: } catch (NumberFormatException e) {
100: }
101: } else if ("SysLogFile".equals(name)) {
102: String val = attributes.getValue("value");
103: if (val != null)
104: server.sysLogFile = val;
105: } else if ("ChatLogPath".equals(name)) {
106: String val = attributes.getValue("value");
107: if (val != null) {
108: if ("".equals(val)) {
109: val = ".";
110: }
111: server.chatLogPath = val;
112: }
113: } else if ("AllowAdmin".equals(name)) {
114: String val = attributes.getValue("value");
115: if (val != null) {
116: if (val.equals("yes")) {
117: server.allowAdmin = true;
118: } else if (val.equals("no")) {
119: server.allowAdmin = false;
120: }
121: }
122: } else if ("PassPhrase".equals(name)) {
123: String val = attributes.getValue("value");
124: if (val != null)
125: server.adminPass = val;
126: } else if ("UserExportFile".equals(name)) {
127: String val = attributes.getValue("value");
128: if (val != null)
129: server.userExportFile = val;
130: } else if ("AllowUserChannels".equals(name)) {
131: String val = attributes.getValue("value");
132: if (val != null) {
133: if (val.equals("yes")) {
134: server.channels.allowUserChannels = 'y';
135: } else if (val.equals("admin")) {
136: server.channels.allowUserChannels = 'a';
137: }
138: if (val.equals("no")) {
139: server.channels.allowUserChannels = 'n';
140: }
141: }
142: } else if ("DefaultChannel".equals(name)) {
143: String val = attributes.getValue("value");
144: if (val != null)
145: server.channels.setDefaultChannel(val);
146: } else if ("Channel".equals(name)) {
147: String val = attributes.getValue("value");
148: String pass = null;
149: if (attributes.getLength() > 1) {
150: pass = attributes.getValue("pass");
151: }
152: if (val != null)
153: server.channels.addSystemChannel(val, pass);
154: } else if ("WelcomeMessage".equals(name)) {
155: inMessage = true;
156: } else if ("br".equals(name)) {
157: messageBuffer += "\n";
158: }
159: }
160: }
|