Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/jabbee/util/ConfigReader.java


1   /*
2    * ConfigReader.java
3    *
4    * Created on November 1, 2002, 8:07 AM
5    *
6    *
7    * License:
8    *
9    * The contents of this file are subject to the Jabber Open Source
10   * License Version 1.0 (the "License"). You may not copy or use this
11   * file, in either source code or executable form, except in compliance
12   * with the License. You may obtain a copy of the License at
13   * http://www.jabber.com/license/ or at http://www.opensource.org/.
14   * Software distributed under the License is distributed on an "AS IS" 
15   * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
16   * the License for the specific language governing rights and limitations
17   * under the License.
18   *
19   * Copyrights:
20   *
21   * Portions created by or assigned to Jabber.com, Inc. are Copyright
22   * (c) 1999-2000 Jabber.com, Inc. All Rights Reserved. Contact information
23   * for Jabber.com, Inc. is available at http://www.jabber.com/. Portions
24   * Copyright (c) 1998-1999 Jeremie Miller.
25   *
26   * Acknowledgements:
27   *
28   * Special thanks to the Jabber Open Source Contributors for their
29   * suggestions and support of Jabber.
30   *
31   */
32  
33  package org.jabbee.util;
34  
35  import java.util.ArrayList;
36  import org.jabbee.util.ConfigHandler;
37  import org.w3c.dom.Document;
38  import org.w3c.dom.Element;
39  import org.w3c.dom.NamedNodeMap;
40  import org.w3c.dom.Node;
41  import org.w3c.dom.NodeList;
42  
43  /**
44   * Reads the given configuration file and places information in the
45   * appropriate panels.
46   *
47   * @author  Gregory Kaczmarczyk
48   *
49   * @version 0.02
50   */
51  public final class ConfigReader extends ConfigHandler {
52      
53      /** Creates a new instance of ConfigReader */
54      public ConfigReader(Document d) {
55          configDoc = d;
56      }
57      
58      public void run() {
59          try {
60              sleep(5);
61              populatePanels();
62          } catch (InterruptedException ex) {
63          }
64      }
65  
66      /** Validates the root element of an XML document
67       *
68       * @param rootElement the name of the root element
69       *
70       * @return boolean True if valid, false if not
71       */
72      public boolean validateRoot(String rootElement) {
73          if (configDoc.getDocumentElement().getTagName().equals(rootElement))
74              return true;
75          else
76              return false;
77      }
78  
79      /** Reads the Document and transfers appropriate values to the various panels
80       */
81      public void populatePanels() {
82          Element root = configDoc.getDocumentElement();
83          NodeList firstChildren = root.getChildNodes();
84  
85          String[] GeneralValues     = new String[10];
86          ArrayList[] AdminIDs       = new ArrayList[2];
87          String maxSize = "";
88          Node browseNode;
89          boolean[] FiltersValues    = new boolean[16];
90          String[] LoggingValues     = new String[4];
91          String[] FilesystemValues  = new String[3];
92          String[] ConnectionsValues = new String[15];
93          ArrayList[] ClientPorts    = new ArrayList[3];
94          ArrayList[] ServerPorts    = new ArrayList[2];
95          String[] InputOutputValues = new String[9];
96          ArrayList[] SSLInfo        = new ArrayList[3];
97          ArrayList[] NetworkInfo    = new ArrayList[3];
98          String[] ComponentsValues  = new String[18];
99          ArrayList[] ConferenceRoom = new ArrayList[7];
100         boolean[] miscValues       = new boolean[5];
101 
102         String tagName = "";
103         NamedNodeMap attList;
104         Node attr;
105         String idAttr = "";
106         // We're avoiding a null pointer exception
107         ComponentsValues[7] = "";
108 
109         for (int i = 0; i < firstChildren.getLength(); i++) {
110             Node child = firstChildren.item(i);
111             if (child instanceof Element) {
112                 // Manage first children
113                 tagName = child.getNodeName();
114                 if (tagName.equals("service")) {
115                     attList = child.getAttributes();
116                     for (int j = 0; j < attList.getLength(); j++) {
117                         attr = attList.item(j);
118                         if (!(attr.getNodeName().equals("id")))
119                             continue;
120                         idAttr = attr.getNodeValue();
121 
122                         if (idAttr.equals("sessions")) {
123                             GeneralValues[0] = simulateXPathForValue(child, "/host/jabberd:cmdline");
124                             GeneralValues[1] = simulateXPathForValue(child, "/jsm/vCard/FN");
125                             GeneralValues[2] = simulateXPathForValue(child, "/jsm/vCard/DESC");
126                             GeneralValues[3] = simulateXPathForValue(child, "/jsm/vCard/URL");
127                             GeneralValues[4] = simulateXPathForValue(child, "/jsm/update/jabberd:cmdline");
128                             AdminIDs = getChildValues(simulateXPathGetNode(child, "/jsm/admin"), new String[] {"read", "write"}, 2);
129                             GeneralValues[5] = simulateXPathForValue(child, "/jsm/admin/reply/subject");
130                             GeneralValues[6] = simulateXPathForValue(child, "/jsm/admin/reply/body");
131                             miscValues[0] = (simulateXPathForAttribute(child, "/jsm/register", "notify").equals("yes")) ? true : false;
132                             GeneralValues[7] = simulateXPathForValue(child, "/jsm/register/instructions");
133                             GeneralValues[8] = simulateXPathForValue(child, "/jsm/welcome/subject");
134                             GeneralValues[9] = simulateXPathForValue(child, "/jsm/welcome/body");
135                             maxSize = simulateXPathForValue(child, "/jsm/filter/max_size");
136                             FiltersValues[0] = simulateXPathPathExists(child, "/jsm/filter/allow/conditions/ns");
137                             FiltersValues[1] = simulateXPathPathExists(child, "/jsm/filter/allow/conditions/unavailable");
138                             FiltersValues[2] = simulateXPathPathExists(child, "/jsm/filter/allow/conditions/from");
139                             FiltersValues[3] = simulateXPathPathExists(child, "/jsm/filter/allow/conditions/resource");
140                             FiltersValues[4] = simulateXPathPathExists(child, "/jsm/filter/allow/conditions/subject");
141                             FiltersValues[5] = simulateXPathPathExists(child, "/jsm/filter/allow/conditions/body");
142                             FiltersValues[6] = simulateXPathPathExists(child, "/jsm/filter/allow/conditions/show");
143                             FiltersValues[7] = simulateXPathPathExists(child, "/jsm/filter/allow/conditions/type");
144                             FiltersValues[8] = simulateXPathPathExists(child, "/jsm/filter/allow/conditions/roster");
145                             FiltersValues[9] = simulateXPathPathExists(child, "/jsm/filter/allow/conditions/group");
146                             FiltersValues[10] = simulateXPathPathExists(child, "/jsm/filter/allow/actions/error");
147                             FiltersValues[11] = simulateXPathPathExists(child, "/jsm/filter/allow/actions/offline");
148                             FiltersValues[12] = simulateXPathPathExists(child, "/jsm/filter/allow/actions/forward");
149                             FiltersValues[13] = simulateXPathPathExists(child, "/jsm/filter/allow/actions/reply");
150                             FiltersValues[14] = simulateXPathPathExists(child, "/jsm/filter/allow/actions/continue");
151                             FiltersValues[15] = simulateXPathPathExists(child, "/jsm/filter/allow/actions/settype");
152                             miscValues[3] = simulateXPathPathExists(child, "/jsm/vcard2jud");
153                             browseNode = simulateXPathGetNode(child, "/jsm/browse");
154                             NodeList browseChildren = browseNode.getChildNodes();
155                             // use only the first instance of jud
156                             boolean firstJud = true;
157                             for (int k = 0; k < browseChildren.getLength(); k++) {
158                                 if (browseChildren.item(k).getNodeName().equals("conference")) {
159                                     ComponentsValues[8] = getAttributeValue(browseChildren.item(k), "type");
160                                     ComponentsValues[7] = getAttributeValue(browseChildren.item(k), "jid");
161                                     ComponentsValues[6] = getAttributeValue(browseChildren.item(k), "name");
162                                     continue;
163                                 // if there are elements that aren't service, ignore them
164                                 } else if (!browseChildren.item(k).getNodeName().equals("service"))
165                                     continue;
166                                 if (firstJud && getAttributeValue(browseChildren.item(k), "type").equals("jud")) {
167                                     ComponentsValues[0] = getAttributeValue(browseChildren.item(k), "name");
168                                     ComponentsValues[1] = getAttributeValue(browseChildren.item(k), "jid");
169                                     firstJud = false;
170                                 }
171                             }
172                         } else if (idAttr.equals("c2s")) {
173                             ConnectionsValues[0] = simulateXPathForValue(child, "/pthcsock/authtime");
174                             ConnectionsValues[1] = simulateXPathForValue(child, "/load/pthsock_client");
175                             if (simulateXPathPathExists(child, "/pthcsock/karma")) {
176                                 ConnectionsValues[2] = simulateXPathForValue(child, "/pthcsock/karma/init");
177                                 ConnectionsValues[3] = simulateXPathForValue(child, "/pthcsock/karma/max");
178                                 ConnectionsValues[4] = simulateXPathForValue(child, "/pthcsock/karma/inc");
179                                 ConnectionsValues[5] = simulateXPathForValue(child, "/pthcsock/karma/dec");
180                                 ConnectionsValues[6] = simulateXPathForValue(child, "/pthcsock/karma/penalty");
181                                 ConnectionsValues[7] = simulateXPathForValue(child, "/pthcsock/karma/restore");
182                             } else {
183                                 for (int k = 2; k < 8; k++) ConnectionsValues[k] = "";
184                             }
185                             ClientPorts = getChildValues(simulateXPathGetNode(child, "/pthcsock"), new String[] {"ip", "ssl"}, 3);
186                         } else if (idAttr.equals("dnsrv")) {
187                             // to be implemented later
188                         } else if (idAttr.equals("s2s")) {
189                             miscValues[1] = simulateXPathPathExists(child, "/dialback/legacy");
190                             ConnectionsValues[8] = simulateXPathForValue(child, "/load/dialback");
191                             if (simulateXPathPathExists(child, "/dialback/karma")) {
192                                 ConnectionsValues[9] = simulateXPathForValue(child, "/dialback/karma/init");
193                                 ConnectionsValues[10] = simulateXPathForValue(child, "/dialback/karma/max");
194                                 ConnectionsValues[11] = simulateXPathForValue(child, "/dialback/karma/inc");
195                                 ConnectionsValues[12] = simulateXPathForValue(child, "/dialback/karma/dec");
196                                 ConnectionsValues[13] = simulateXPathForValue(child, "/dialback/karma/penalty");
197                                 ConnectionsValues[14] = simulateXPathForValue(child, "/dialback/karma/restore");
198                             } else {
199                                 for (int k = 9; k < 15; k++) ConnectionsValues[k] = "";
200                             }
201                             ServerPorts = getChildValues(simulateXPathGetNode(child, "/dialback"), new String[] {"ip"}, 3);
202                         } else if (idAttr.equals("jud")) {
203                             ComponentsValues[2] = simulateXPathForValue(child, "/load/jud");
204                             ComponentsValues[3] = simulateXPathForValue(child, "/jud/vCard/FN");
205                             ComponentsValues[4] = simulateXPathForValue(child, "/jud/vCard/DESC");
206                             ComponentsValues[5] = simulateXPathForValue(child, "/jud/vCard/URL");
207                         } else if (idAttr.equals(ComponentsValues[7])) {
208                             ComponentsValues[9] = simulateXPathForValue(child, "/load/conference");
209                             // don't think the following (#10) is necessary for configuration gui
210                             ComponentsValues[10] = simulateXPathPathExists(child, "/conference/private") ? "private" : "public";
211                             ComponentsValues[11] = simulateXPathForValue(child, "/conference/history");
212                             ComponentsValues[12] = simulateXPathForValue(child, "/conference/vCard/FN");
213                             ComponentsValues[13] = simulateXPathForValue(child, "/conference/vCard/DESC");
214                             ComponentsValues[14] = simulateXPathForValue(child, "/conference/vCard/URL");
215                             ComponentsValues[15] = simulateXPathForValue(child, "/conference/notice/join");
216                             ComponentsValues[16] = simulateXPathForValue(child, "/conference/notice/leave");
217                             ComponentsValues[17] = simulateXPathForValue(child, "/conference/notice/rename");
218                             if (simulateXPathPathExists(child, "/conference/room")) {
219                                 NodeList confRoomList = simulateXPathGetNode(child, "/conference").getChildNodes();
220                                 for (int k = 0; k < 7; k++)
221                                     ConferenceRoom[k] = new ArrayList(0);
222                                 for (int k = 0; k < confRoomList.getLength(); k++) {
223                                     if (confRoomList.item(k).getNodeName().equals("room")) {
224                                         ConferenceRoom[0].add(getAttributeValue(confRoomList.item(k), "jid"));
225                                         ConferenceRoom[1].add(simulateXPathForValue(confRoomList.item(k), "/name"));
226                                         ConferenceRoom[2].add((simulateXPathPathExists(confRoomList.item(k), "/privacy") ? "privacy" : ""));
227                                         if (simulateXPathPathExists(confRoomList.item(k), "/secret"))
228                                             ConferenceRoom[3].add(simulateXPathForValue(confRoomList.item(k), "/secret"));
229                                         else
230                                             ConferenceRoom[3].add("");
231                                         if (simulateXPathPathExists(confRoomList.item(k), "/secret")) {
232                                             ConferenceRoom[4].add(simulateXPathForValue(confRoomList.item(k), "/notice/join"));
233                                             ConferenceRoom[5].add(simulateXPathForValue(confRoomList.item(k), "/notice/leave"));
234                                             ConferenceRoom[6].add(simulateXPathForValue(confRoomList.item(k), "/notice/rename"));
235                                         } else {
236                                             ConferenceRoom[4].add("");
237                                             ConferenceRoom[5].add("");
238                                             ConferenceRoom[6].add("");
239                                         }
240                                     } else
241                                         continue;
242                                 }
243                                 miscValues[4] = true;
244                             }
245                         }
246                     }
247 
248                 } else if (tagName.equals("xdb")) {
249                     FilesystemValues[1] = simulateXPathForValue(child, "/load/xdb_file");
250                     FilesystemValues[2] = simulateXPathForValue(child, "/xdb_file/spool/jabberd:cmdline");
251 
252                 } else if (tagName.equals("log")) {
253                     attList = child.getAttributes();
254                     for (int j = 0; j < attList.getLength(); j++) {
255                         attr = attList.item(j);
256                         if (!(attr.getNodeName().equals("id")))
257                             continue;
258                         idAttr = attr.getNodeValue();
259 
260                         if (idAttr.equals("elogger")) {
261                             LoggingValues[0] = getChildTextValue(child, "format");
262                             LoggingValues[1] = getChildTextValue(child, "file");
263                             miscValues[2] = simulateXPathPathExists(child, "stderr");
264                         } else if (idAttr.equals("rlogger")) {
265                             LoggingValues[2] = getChildTextValue(child, "format");
266                             LoggingValues[3] = getChildTextValue(child, "file");
267                         }
268                     }
269 
270                 } else if (tagName.equals("io")) {
271                     if (simulateXPathPathExists(child, "/karma")) {
272                         InputOutputValues[0] = simulateXPathForValue(child, "/karma/heartbea");
273                         InputOutputValues[1] = simulateXPathForValue(child, "/karma/init");
274                         InputOutputValues[2] = simulateXPathForValue(child, "/karma/max");
275                         InputOutputValues[3] = simulateXPathForValue(child, "/karma/inc");
276                         InputOutputValues[4] = simulateXPathForValue(child, "/karma/dec");
277                         InputOutputValues[5] = simulateXPathForValue(child, "/karma/penalty");
278                         InputOutputValues[6] = simulateXPathForValue(child, "/karma/restore");
279                     } else {
280                         for (int k=0; k < 7; k++) InputOutputValues[k] = "";
281                     }
282                     InputOutputValues[7] = simulateXPathForAttribute(child, "/rate", "points");
283                     InputOutputValues[8] = simulateXPathForAttribute(child, "/rate", "time");
284                     SSLInfo = getChildValues(simulateXPathGetNode(child, "/ssl"), new String[] {"key"}, 3);
285                     NetworkInfo = getChildValues(child, new String[] {"allow", "deny"}, new String[] {"ip", "mask"}, 3);
286 
287                 } else if (tagName.equals("pidfile")) {
288                     FilesystemValues[0] = child.getFirstChild().getNodeValue().trim();
289                 }
290             }
291         }
292 
293         general.populateFields(GeneralValues);
294         general.setAdminIDs(AdminIDs);
295         general.setRegNotify(miscValues[0]);
296         filters.setMaxSize(maxSize);
297         filters.setCheckBoxValues(FiltersValues);
298         logging.populateFields(LoggingValues);
299         logging.setStdErr(miscValues[2]);
300         filesystem.populateFields(FilesystemValues);
301         security.setSSL(SSLInfo);
302         security.setNetworks(NetworkInfo);
303         connections.populateFields(ConnectionsValues);
304         connections.setClientPorts(ClientPorts);
305         connections.setServerPorts(ServerPorts);
306         connections.setLegacy(miscValues[1]);
307         inputOutput.populateFields(InputOutputValues);
308         components.setUpdate(miscValues[3]);
309         components.populateFields(ComponentsValues);
310         if (miscValues[4])
311             components.addRooms(ConferenceRoom);
312     }
313 
314     private Document configDoc;
315 }