KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > client > ClientConfig


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package org.lucane.client;
20
21 import javax.xml.parsers.*;
22
23 import org.lucane.common.Logging;
24 import org.w3c.dom.*;
25
26 /**
27  * Client configuration
28  */

29 public class ClientConfig
30 {
31     //-- attributes
32
private String JavaDoc login = "";
33     private String JavaDoc serverHost = null;
34     private int listenerPort = 0;
35     private int serverPort = 9115;
36     private String JavaDoc language = "en";
37     private String JavaDoc font = null;
38     private String JavaDoc looknfeel = null;
39     private String JavaDoc proxyHost = null;
40     private int proxyPort = 5119;
41     private String JavaDoc publicIpAddress = null;
42     private String JavaDoc publicIpInterface = null;
43
44     /**
45      * Constructor
46      *
47      * @param filename the xml file
48      */

49     public ClientConfig(String JavaDoc filename)
50     throws Exception JavaDoc
51     {
52           DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
53           Document document = builder.parse(filename);
54                                                                   
55           //-- root element
56
Node node = document.getFirstChild();
57           while(node != null && node.getNodeType() != Node.ELEMENT_NODE)
58             node = node.getNextSibling();
59           
60           if(node == null || ! node.getNodeName().equals("lucane-client"))
61             throw new Exception JavaDoc("root element is different from 'lucane-client'");
62                   
63           node = node.getFirstChild();
64           while(node != null)
65           {
66             if(node.getNodeType() == Node.ELEMENT_NODE)
67             {
68                 if(node.getNodeName().equals("login"))
69                     handleLogin(node);
70                 else if(node.getNodeName().equals("server"))
71                     handleServer(node);
72                 else if(node.getNodeName().equals("language"))
73                     handleLanguage(node);
74                 else if(node.getNodeName().equals("looknfeel"))
75                     handleLooknfeel(node);
76                 else if(node.getNodeName().equals("proxy"))
77                     handleProxy(node);
78                 else if(node.getNodeName().equals("public-ip"))
79                     handlePublicIp(node);
80                 else if(node.getNodeName().equals("listener"))
81                     handleListener(node);
82                 else
83                   Logging.getLogger().warning("unexepected node : " + node.getNodeName());
84             }
85             node = node.getNextSibling();
86           }
87     }
88     
89     /**
90      * Parse login node
91      *
92      * @param node the login node
93      */

94     private void handleLogin(Node node)
95     {
96         this.login = node.getAttributes().getNamedItem("value").getNodeValue();
97     }
98     
99     /**
100      * Parse server node
101      *
102      * @param node the server node
103      */

104     private void handleServer(Node node)
105     {
106         this.serverHost = node.getAttributes().getNamedItem("host").getNodeValue();
107         this.serverPort = Integer.parseInt(node.getAttributes().getNamedItem("port").getNodeValue());
108     }
109
110     /**
111      * Parse language node
112      *
113      * @param node the language node
114      */

115     private void handleLanguage(Node node)
116     {
117         this.language = node.getAttributes().getNamedItem("value").getNodeValue();
118         try {
119             this.font = node.getAttributes().getNamedItem("font").getNodeValue();
120         } catch(Exception JavaDoc e) {
121             this.font = null;
122         }
123     }
124     
125     /**
126      * Parse looknfeel node
127      *
128      * @param node the looknfeel node
129      */

130     private void handleLooknfeel(Node node)
131     {
132         this.looknfeel = node.getAttributes().getNamedItem("class").getNodeValue();
133     }
134
135     /**
136      * Parse proxy node
137      *
138      * @param node the proxy node
139      */

140     private void handleProxy(Node node)
141     {
142         this.proxyHost = node.getAttributes().getNamedItem("host").getNodeValue();
143         this.proxyPort = Integer.parseInt(node.getAttributes().getNamedItem("port").getNodeValue());
144     }
145
146     /**
147      * Parse public-ip node
148      *
149      * @param node the public-ip node
150      */

151     private void handlePublicIp(Node node)
152     {
153         if(node.getAttributes().getNamedItem("address") != null)
154             this.publicIpAddress = node.getAttributes().getNamedItem("address").getNodeValue();
155
156         if(node.getAttributes().getNamedItem("interface") != null)
157             this.publicIpInterface = node.getAttributes().getNamedItem("interface").getNodeValue();
158     }
159     
160     /**
161      * Parse listener node
162      *
163      * @param node the listener node
164      */

165     private void handleListener(Node node)
166     {
167         this.listenerPort = Integer.parseInt(node.getAttributes().getNamedItem("port").getNodeValue());
168     }
169
170     //-- getters
171

172     /**
173      * Get the default login
174      *
175      * @return the default login
176      */

177     public String JavaDoc getLogin()
178     {
179         return login;
180     }
181
182     /**
183      * Get the server hostname
184      *
185      * @return the server hostname
186      */

187     public String JavaDoc getServerHost()
188     {
189         return serverHost;
190     }
191
192     /**
193      * Get the server port
194      *
195      * @return the server port
196      */

197     public int getServerPort()
198     {
199         return serverPort;
200     }
201
202     /**
203      * Get the language used
204      *
205      * @return the language
206      */

207     public String JavaDoc getLanguage()
208     {
209         return language;
210     }
211     
212     /**
213      * Get the font if a special one is needed
214      *
215      * @return the font
216      */

217     public String JavaDoc getFont()
218     {
219         return font;
220     }
221
222     /**
223      * Get the looknfeel used
224      *
225      * @return the looknfeel
226      */

227     public String JavaDoc getLooknfeel()
228     {
229         return looknfeel;
230     }
231     
232     /**
233      * Get the proxy host
234      *
235      * @return the proxy host or null if no proxy
236      */

237     public String JavaDoc getProxyHost()
238     {
239         return proxyHost;
240     }
241
242     /**
243      * Get the proxy port
244      *
245      * @return the proxy port
246      */

247     public int getProxyPort()
248     {
249         return proxyPort;
250     }
251
252     /**
253      * Get the public ip
254      *
255      * @return the public ip or none if useless
256      */

257     public String JavaDoc getPublicIpAddress()
258     {
259         return publicIpAddress;
260     }
261
262     /**
263      * Get the public ip interface
264      *
265      * @return the public ip or none if useless
266      */

267     public String JavaDoc getPublicIpInterface()
268     {
269         return publicIpInterface;
270     }
271
272     /**
273      * Get the listener port
274      *
275      * @return the listener port
276      */

277     public int getListenerPort()
278     {
279         return listenerPort;
280     }
281 }
Popular Tags