KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > server > ConfigParser


1 /*- ConfigParser.java ---------------------------------------------+
2  | |
3  | Copyright (C) 2002-2003 Joseph Monti, LlamaChat |
4  | countjoe@users.sourceforge.net |
5  | http://www.42llamas.com/LlamaChat/ |
6  | |
7  | This program is free software; you can redistribute it and/or |
8  | modify it under the terms of the GNU General Public License |
9  | as published by the Free Software Foundation; either version 2 |
10  | of the License, or (at your option) any later version |
11  | |
12  | This program is distributed in the hope that it will be useful, |
13  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15  | GNU General Public License for more details. |
16  | |
17  | A copy of the GNU General Public License may be found in the |
18  | installation directory named "GNUGPL.txt" |
19  | |
20  +-----------------------------------------------------------------+
21  */

22
23 package server;
24
25 import org.xml.sax.helpers.DefaultHandler JavaDoc;
26 import org.xml.sax.Attributes JavaDoc;
27
28 /* -------------------- JavaDoc Information ----------------------*/
29 /**
30  * a default handler for config file parsing in XML
31  * @author Joseph Monti <a HREF="mailto:countjoe@users.sourceforge.net">countjoe@users.sourceforge.net</a>
32  * @version 0.8
33  */

34 public class ConfigParser extends DefaultHandler JavaDoc {
35     /**
36      * a reference back to the server
37      */

38     private LlamaChatServer server;
39     
40     /**
41      * status indicator for telling if in message tag
42      */

43     private boolean inMessage;
44     
45     /**
46      * buffer to use while parsing welcome message
47      */

48     private String JavaDoc messageBuffer;
49     
50     /**
51      * constructor to initialize data
52      * @param a reference back to the calling server
53      */

54     ConfigParser(LlamaChatServer s) {
55         server = s;
56         inMessage = false;
57         messageBuffer = "";
58     }
59     
60     /**
61      * used for gathering welcome message (ignores all other tags)
62      */

63     public void characters(char[] ch, int start, int length) {
64         String JavaDoc s = new String JavaDoc(ch, start, length);
65         if (s != null && inMessage) {
66             s = s.replaceAll("[\\t\\n]", " ");
67             s = s.replaceAll("[ ]{2,}", " ");
68             s = s.trim();
69             messageBuffer+= s;
70         }
71     }
72     
73     /**
74      * used for gathering welcome message (ignores all other tags)
75      */

76     public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName) {
77         String JavaDoc name = localName;
78         if ("".equals(name)) name = qName;
79         if ("WelcomeMessage".equals(name)) {
80             inMessage = false;
81             server.welcomeMessage = messageBuffer;
82         }
83     }
84     
85     /**
86      * parses the data within the atributes for the configuration settings
87      */

88     public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName,
89                                                     Attributes JavaDoc attributes) {
90         String JavaDoc name = localName;
91         if ("".equals(name)) name = qName;
92         if ("Port".equals(name)) {
93             String JavaDoc val = attributes.getValue("value");
94             try {
95                 if (val != null) server.PORT = Integer.parseInt(val);
96             } catch (NumberFormatException JavaDoc e) { }
97         } else if ("SysLogFile".equals(name)) {
98             String JavaDoc val = attributes.getValue("value");
99             if (val != null) server.sysLogFile = val;
100         } else if ("ChatLogPath".equals(name)) {
101             String JavaDoc val = attributes.getValue("value");
102             if (val != null) {
103                 if ("".equals(val)) {
104                     val = ".";
105                 }
106                 server.chatLogPath = val;
107             }
108         } else if ("AllowAdmin".equals(name)) {
109             String JavaDoc val = attributes.getValue("value");
110             if (val != null) {
111                 if (val.equals("yes")) {
112                     server.allowAdmin = true;
113                 } else if (val.equals("no")) {
114                     server.allowAdmin = false;
115                 }
116             }
117         } else if ("PassPhrase".equals(name)) {
118             String JavaDoc val = attributes.getValue("value");
119             if (val != null) server.adminPass = val;
120         } else if ("UserExportFile".equals(name)) {
121             String JavaDoc val = attributes.getValue("value");
122             if (val != null) server.userExportFile = val;
123         } else if ("AllowUserChannels".equals(name)) {
124             String JavaDoc val = attributes.getValue("value");
125             if (val != null) {
126                 if (val.equals("yes")) {
127                     server.channels.allowUserChannels = 'y';
128                 } else if (val.equals("admin")) {
129                     server.channels.allowUserChannels = 'a';
130                 } if (val.equals("no")) {
131                     server.channels.allowUserChannels = 'n';
132                 }
133             }
134         } else if ("DefaultChannel".equals(name)) {
135             String JavaDoc val = attributes.getValue("value");
136             if (val != null) server.channels.setDefaultChannel(val);
137         } else if ("Channel".equals(name)) {
138             String JavaDoc val = attributes.getValue("value");
139             String JavaDoc pass = null;
140             if (attributes.getLength() > 1) {
141                 pass = attributes.getValue("pass");
142             }
143             if (val != null) server.channels.addSystemChannel(val, pass);
144         } else if ("WelcomeMessage".equals(name)) {
145             inMessage = true;
146         } else if ("br".equals(name)) {
147             messageBuffer+= "\n";
148         }
149     }
150 }
151
Popular Tags