KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > dyade > aaa > agent > conf > A3CMLKXmlWrapper


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

19 package fr.dyade.aaa.agent.conf;
20
21 import java.io.*;
22 import java.util.*;
23
24 import fr.dyade.aaa.agent.*;
25
26 import org.objectweb.util.monolog.api.BasicLevel;
27 import org.objectweb.util.monolog.api.Logger;
28
29 import org.kxml.parser.*;
30 import org.kxml.*;
31
32 /**
33  * A3CML XML parser using kxml.
34  */

35 public class A3CMLKXmlWrapper implements A3CMLWrapper {
36   protected A3CMLConfig a3cmlconfig = null;
37
38   public A3CMLKXmlWrapper() { }
39
40   public static final String JavaDoc getValue(Vector atts, String JavaDoc qName) {
41     if(atts != null){
42       for (int i=0; i<atts.size(); i++) {
43         if (((Attribute) atts.elementAt(i)).getName().equals(qName))
44           return ((Attribute) atts.elementAt(i)).getValue();
45       }
46     }
47     return null;
48   }
49
50   /**
51    * Name of configuration to get from the file.
52    */

53   String JavaDoc configName = "default";
54   /** Working attribute used during configuration's */
55   String JavaDoc conf = null;
56   /**
57    * Working attribute used during domain's definition between start and
58    * end element.
59    */

60   A3CMLDomain domain = null;
61   /**
62    * Working attribute used during server's definition between start and
63    * end element.
64    */

65   A3CMLServer server = null;
66   /**
67    * Working attribute used during network's definition between start and
68    * end element.
69    */

70   A3CMLNetwork network = null;
71   /**
72    * Working attribute used during service's definition between start and
73    * end element.
74    */

75   A3CMLService service = null;
76   /**
77    * Working attribute used during service's definition between start and
78    * end element.
79    */

80   A3CMLProperty property = null;
81   /**
82    * Working attribute used during jvmArgs' definition between start and
83    * end element.
84    */

85   String JavaDoc jvmArgs = null;
86   /**
87    * Working attribute used during nat' definition between start and
88    * end element.
89    */

90   A3CMLNat nat = null;
91
92   /**
93    * Parses the xml file named <code>cfgFileName</code>.
94    *
95    * @param cfgFileName the name of the xml file
96    * @param configName the name of the configuration
97    *
98    * @exception Exception unspecialized error
99    */

100   public A3CMLConfig parse(Reader reader,
101                             String JavaDoc configName)
102     throws Exception JavaDoc {
103     this.configName = configName;
104
105     a3cmlconfig = new A3CMLConfig();
106
107     // instantiation du parser
108
XmlParser parser = new XmlParser(reader);
109
110     ParseEvent event = parser.read();
111     while (event.getType() != Xml.END_DOCUMENT) {
112       switch (event.getType()) {
113       case Xml.START_DOCUMENT:
114         break;
115       case Xml.START_TAG: {
116         String JavaDoc name = event.getName();
117
118         if (name.equals(A3CML.ELT_CONFIG)) {
119           conf = getValue(event.getAttributes(),
120                           A3CML.ATT_NAME);
121           if (conf == null)
122             conf = configName;
123         } else if (configName.equals(conf)) {
124           Vector atts = event.getAttributes();
125
126           if (name.equals(A3CML.ELT_DOMAIN)) {
127             domain = new A3CMLDomain(
128               getValue(atts, A3CML.ATT_NAME),
129               getValue(atts, A3CML.ATT_NETWORK));
130           } else if (name.equals(A3CML.ELT_SERVER)) {
131             short sid;
132             try {
133               sid = Short.parseShort(getValue(atts, A3CML.ATT_ID));
134             } catch (NumberFormatException JavaDoc exc) {
135               throw new Exception JavaDoc("bad value for server id: " +
136                                   getValue(atts, A3CML.ATT_ID));
137             }
138             server = new A3CMLServer(
139               sid,
140               getValue(atts, A3CML.ATT_NAME),
141               getValue(atts, A3CML.ATT_HOSTNAME));
142           } else if (name.equals(A3CML.ELT_NETWORK)) {
143             int port;
144             try {
145               port = Integer.parseInt(getValue(atts, A3CML.ATT_PORT));
146             } catch (NumberFormatException JavaDoc exc) {
147               throw new Exception JavaDoc("bad value for network port: " +
148                                   getValue(atts, A3CML.ATT_PORT));
149             }
150             network = new A3CMLNetwork(
151               getValue(atts, A3CML.ATT_DOMAIN),
152               port);
153           } else if (name.equals(A3CML.ELT_SERVICE)) {
154             service = new A3CMLService(
155               getValue(atts, A3CML.ATT_CLASS),
156               getValue(atts, A3CML.ATT_ARGS));
157           } else if (name.equals(A3CML.ELT_PROPERTY)) {
158             property = new A3CMLProperty(
159               getValue(atts, A3CML.ATT_NAME),
160               getValue(atts, A3CML.ATT_VALUE));
161           } else if (name.equals(A3CML.ELT_NAT)) {
162             nat = new A3CMLNat(Short.parseShort(getValue(atts, A3CML.ATT_SID)),
163                                getValue(atts, A3CML.ATT_HOSTNAME),
164                                Integer.parseInt(getValue(atts, A3CML.ATT_PORT)));
165           } else if (name.equals(A3CML.ELT_JVM_ARGS)) {
166             jvmArgs = getValue(atts, A3CML.ATT_VALUE);
167           } else {
168             throw new Exception JavaDoc("unknown element \"" + name + "\"");
169           }
170         }
171         break;
172       }
173       case Xml.END_TAG: {
174         String JavaDoc name = event.getName();
175
176         if (name.equals(A3CML.ELT_CONFIG)) {
177           conf = null;
178         } else if (configName.equals(conf)) {
179           if (name.equals(A3CML.ELT_DOMAIN)) {
180             a3cmlconfig.addDomain(domain);
181             domain = null;
182           } else if (name.equals(A3CML.ELT_SERVER)) {
183             a3cmlconfig.addServer(server);
184             server = null;
185           } else if (name.equals(A3CML.ELT_NETWORK)) {
186             if (server != null) {
187               server.addNetwork(network);
188               // Add the server to the corresponding domains
189
// AF: This step should be done at the end of parsing, in order
190
// to avoid to declare domains first.
191
a3cmlconfig.getDomain(network.domain).addServer(server);
192             } else {
193               // Can never happen (see DTD).
194
}
195             network = null;
196           } else if (name.equals(A3CML.ELT_SERVICE)) {
197             if (server != null) {
198               server.addService(service);
199             } else {
200               // Can never happen (see DTD).
201
}
202             service = null;
203           } else if (name.equals(A3CML.ELT_PROPERTY)) {
204             if (server == null)
205               a3cmlconfig.addProperty(property); // Global property
206
else
207               server.addProperty(property); // Server property
208
property = null;
209           } else if (name.equals(A3CML.ELT_NAT)) {
210             if (server != null)
211               server.addNat(nat);
212             nat = null;
213           } else if (name.equals(A3CML.ELT_JVM_ARGS)) {
214             if (server != null && jvmArgs != null)
215               server.jvmArgs = jvmArgs;
216             jvmArgs = null;
217           } else {
218             throw new Exception JavaDoc("unknown element \"" + name + "\"");
219           }
220         }
221         break;
222       }
223       }
224       event = parser.read();
225     }
226
227     return a3cmlconfig;
228   }
229 }
230
Popular Tags