~ubuntu-branches/ubuntu/precise/jcsp/precise

« back to all changes in this revision

Viewing changes to src/org/jcsp/net/settings/ConfigReader.java

  • Committer: Bazaar Package Importer
  • Author(s): Miguel Landaeta
  • Date: 2010-06-20 18:12:26 UTC
  • Revision ID: james.westby@ubuntu.com-20100620181226-8yg8d9rjjjiuy7oz
Tags: upstream-1.1-rc4
ImportĀ upstreamĀ versionĀ 1.1-rc4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//////////////////////////////////////////////////////////////////////
 
2
//                                                                  //
 
3
//  JCSP ("CSP for Java") Libraries                                 //
 
4
//  Copyright (C) 1996-2008 Peter Welch and Paul Austin.            //
 
5
//                2001-2004 Quickstone Technologies Limited.        //
 
6
//                                                                  //
 
7
//  This library is free software; you can redistribute it and/or   //
 
8
//  modify it under the terms of the GNU Lesser General Public      //
 
9
//  License as published by the Free Software Foundation; either    //
 
10
//  version 2.1 of the License, or (at your option) any later       //
 
11
//  version.                                                        //
 
12
//                                                                  //
 
13
//  This library is distributed in the hope that it will be         //
 
14
//  useful, but WITHOUT ANY WARRANTY; without even the implied      //
 
15
//  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR         //
 
16
//  PURPOSE. See the GNU Lesser General Public License for more     //
 
17
//  details.                                                        //
 
18
//                                                                  //
 
19
//  You should have received a copy of the GNU Lesser General       //
 
20
//  Public License along with this library; if not, write to the    //
 
21
//  Free Software Foundation, Inc., 59 Temple Place, Suite 330,     //
 
22
//  Boston, MA 02111-1307, USA.                                     //
 
23
//                                                                  //
 
24
//  Author contact: P.H.Welch@kent.ac.uk                             //
 
25
//                                                                  //
 
26
//                                                                  //
 
27
//////////////////////////////////////////////////////////////////////
 
28
 
 
29
package org.jcsp.net.settings;
 
30
 
 
31
import java.io.*;
 
32
 
 
33
/**
 
34
 * <p>Used internally by the JCSP network infrastructure to load a configuration from an XML file.</p>
 
35
 *
 
36
 * <p>This is not a full XML reader, and is capable of reading only a subset of XML.</p>
 
37
 *
 
38
 * @author Quickstone Technologies Limited
 
39
 */
 
40
public class ConfigReader implements XMLConfigConstants
 
41
{
 
42
   /**
 
43
    * Diagnostic routine. This can load an XML configuration file and then display the configuration
 
44
    * structure constructed. Specify the name of the file as the first command line parameter.
 
45
    */
 
46
   static public void main(String[] args)
 
47
   {
 
48
      // Parse arguments
 
49
      if (args.length != 1)
 
50
         System.err.println("Usage: XMLReader <filename>");
 
51
      else
 
52
      {
 
53
         String filename = args[0];
 
54
         try
 
55
         {
 
56
            ConfigReader cr = new ConfigReader(new FileInputStream(filename));
 
57
            JCSPConfig config = cr.getConfig();
 
58
            System.out.println(config);
 
59
         }
 
60
         catch (Exception e)
 
61
         {
 
62
            System.err.println("Error while reading config.");
 
63
            e.printStackTrace();
 
64
         }
 
65
      }
 
66
   }
 
67
   
 
68
   /** The config built up. */
 
69
   private JCSPConfig config = new JCSPConfig();
 
70
   
 
71
   /**
 
72
    * Constructs a new configuration from the given source stream. This will attempt to parse the file
 
73
    * using recursive-descent approach.
 
74
    *
 
75
    * @param instream source of the XML configuration.
 
76
    * @throws IOException if there is a problem with the stream or the file is improperly formatted.
 
77
    * @throws XMLValidationException if there is a symantic problem with the configuration.
 
78
    */
 
79
   public ConfigReader(InputStream instream) throws IOException
 
80
   {
 
81
      BufferedReader in = new BufferedReader(new InputStreamReader(instream));
 
82
      Tag t;
 
83
      while ((t = nextTag(in)) != null)
 
84
      {
 
85
         if (t.name.equals("JCSP-CONFIG"))
 
86
         {
 
87
            doJCSP_Config(in);
 
88
            continue;
 
89
         }
 
90
         if (t.name.equals("?xml"))
 
91
            continue;
 
92
         t.bad();
 
93
      }
 
94
   }
 
95
   
 
96
   private void do_template(Reader in) throws IOException
 
97
   {
 
98
      Tag t;
 
99
      while (((t = nextTag(in)) != null) && (!t.name.equals("/" + "")))
 
100
         // put clauses here
 
101
         t.bad();
 
102
   }
 
103
   
 
104
   private void doJCSP_Config(Reader in) throws IOException
 
105
   {
 
106
      Tag t;
 
107
      while (((t = nextTag(in)) != null) && (!t.name.equals("/JCSP-CONFIG")))
 
108
      {
 
109
         if (t.name.equals(ELEMENT_SETTINGS))
 
110
         {
 
111
            if (!t.terminated) 
 
112
               doSettings(in);
 
113
            continue;
 
114
         }
 
115
         if (t.name.equals(ELEMENT_SERVICES))
 
116
         {
 
117
            if (!t.terminated) 
 
118
               doServices(in);
 
119
            continue;
 
120
         }
 
121
         if (t.name.equals(ELEMENT_PLUGINS))
 
122
         {
 
123
            if (!t.terminated) 
 
124
               doPlugins(in);
 
125
            continue;
 
126
         }
 
127
         if (t.name.equals(ELEMENT_PROTOCOLS))
 
128
         {
 
129
            if (!t.terminated) 
 
130
               doProtocols(in);
 
131
            continue;
 
132
         }
 
133
         if (t.name.equals(ELEMENT_ADDRESSES))
 
134
         {
 
135
            if (!t.terminated) 
 
136
               doAddresses(in);
 
137
            continue;
 
138
         }
 
139
         if (t.name.equals(ELEMENT_NODE_SPECS))
 
140
         {
 
141
            if (!t.terminated) 
 
142
               doNodeSpecs(in);
 
143
            continue;
 
144
         }
 
145
         if (t.name.equals(ELEMENT_LINK_PROFILES))
 
146
         {
 
147
            if (!t.terminated) 
 
148
               doLinkProfiles(in);
 
149
            continue;
 
150
         }
 
151
         if (t.name.equals(ELEMENT_NODE_PROFILES))
 
152
         {
 
153
            if (!t.terminated) 
 
154
               doNodeProfiles(in);
 
155
            continue;
 
156
         }
 
157
         t.bad();
 
158
      }
 
159
   }
 
160
   
 
161
   private void doSettings(Reader in) throws IOException
 
162
   {
 
163
      Tag t;
 
164
      while (((t = nextTag(in)) != null) && (!t.name.equals("/" + ELEMENT_SETTINGS)))
 
165
      {
 
166
         if (t.name.equals(ELEMENT_SETTING))
 
167
         {
 
168
            String name = t.getAttrib("name");
 
169
            String value = t.getAttrib("value");
 
170
            Setting setting = config.getSettings().getSetting(name);
 
171
            if (setting == null)
 
172
            {
 
173
               setting = new Setting(name, value);
 
174
               config.getSettings().addSetting(setting);
 
175
            }
 
176
            else
 
177
               throw new XMLValidationException("Setting \"" + name + "\" already exists with value " + setting.getValue());
 
178
            continue;
 
179
         }
 
180
         t.bad();
 
181
      }
 
182
   }
 
183
   
 
184
   private int servicePos = 0;
 
185
   private int protocolPos = 0;
 
186
   
 
187
   private void doServices(Reader in) throws IOException
 
188
   {
 
189
      Tag t;
 
190
      while (((t = nextTag(in)) != null) && (!t.name.equals("/" + ELEMENT_SERVICES)))
 
191
      {
 
192
         if (t.name.equals(ELEMENT_SERVICE))
 
193
         {
 
194
            String name = t.getAttrib("name");
 
195
            String className = t.getAttrib("class");
 
196
            boolean run = Tag.getBooleanValue(t.getAttrib("run"));
 
197
            try
 
198
            {
 
199
               Class serviceClass = Class.forName(className);
 
200
               config.getServices().addService(new Service(name, serviceClass, run, servicePos++));
 
201
            }
 
202
            catch (ClassNotFoundException e)
 
203
            {
 
204
               throw new XMLValidationException("Unable to load class " + className);
 
205
            }
 
206
            if (!t.terminated) 
 
207
               doService(in);
 
208
            continue;
 
209
         }
 
210
         t.bad();
 
211
      }
 
212
   }
 
213
   
 
214
   private void doService(Reader in) throws IOException
 
215
   {
 
216
      Tag t;
 
217
      while (((t = nextTag(in)) != null) && (!t.name.equals("/" + ELEMENT_SERVICE)))
 
218
      {
 
219
         if (t.name.equals(ELEMENT_ADDRESS_SETTING))
 
220
         {
 
221
            String name = t.getAttrib("name");
 
222
            String value = t.getAttrib("value");
 
223
            String pID = t.getAttrib("protocolid");
 
224
            Service current = config.getServices().getLastService();
 
225
            AddressSetting addrSetting = (AddressSetting) current.getAddressSetting(name);
 
226
            if (addrSetting == null)
 
227
            {
 
228
               addrSetting = new AddressSetting(name, value, pID);
 
229
               current.addAddressSetting(addrSetting);
 
230
            }
 
231
            else
 
232
               addrSetting.addAlternate(new AddressSetting(name, value, pID));
 
233
            continue;
 
234
         }
 
235
         if (t.name.equals(ELEMENT_SETTING))
 
236
         {
 
237
            String name = t.getAttrib("name");
 
238
            String value = t.getAttrib("value");
 
239
            Setting setting = config.getServices().getLastService().getSetting(name);
 
240
            if (setting == null)
 
241
            {
 
242
               setting = new Setting(name, value);
 
243
               config.getServices().getLastService().addSetting(setting);
 
244
            }
 
245
            else
 
246
               throw new XMLValidationException("Setting \"" + name + "\" already exists with value " + setting.getValue());
 
247
            continue;
 
248
         }
 
249
         t.bad();
 
250
      }
 
251
   }
 
252
   
 
253
   private void doPlugins(Reader in) throws IOException
 
254
   {
 
255
      Tag t;
 
256
      while (((t = nextTag(in)) != null) && (!t.name.equals("/" + ELEMENT_PLUGINS)))
 
257
      {
 
258
         if (t.name.equals(ELEMENT_PLUGIN))
 
259
         {
 
260
            String name = t.getAttrib("name");
 
261
            String className = t.getAttrib("classname");
 
262
            try
 
263
            {
 
264
               Class classToUse = Class.forName(className);
 
265
               Plugin plugin = new Plugin(name, classToUse);
 
266
               config.getPlugins().addPlugin(plugin);
 
267
            }
 
268
            catch (Exception e)
 
269
            {
 
270
               throw new XMLValidationException("Unable to load class " + className);
 
271
            }
 
272
            if (!t.terminated) 
 
273
               doPlugin(in);
 
274
            continue;
 
275
         }
 
276
         t.bad();
 
277
      }
 
278
   }
 
279
   
 
280
   private void doPlugin(Reader in) throws IOException
 
281
   {
 
282
      Tag t;
 
283
      while (((t = nextTag(in)) != null) && (!t.name.equals("/" + ELEMENT_PLUGIN)))
 
284
         // put clauses here
 
285
         t.bad();
 
286
   }
 
287
   
 
288
   private void doProtocols(Reader in) throws IOException
 
289
   {
 
290
      Tag t;
 
291
      while (((t = nextTag(in)) != null) && (!t.name.equals("/" + ELEMENT_PROTOCOLS)))
 
292
      {
 
293
         if (t.name.equals(ELEMENT_PROTOCOL))
 
294
         {
 
295
            String id = t.getAttrib("id");
 
296
            String name = t.getAttrib("name");
 
297
            String idClass = t.getAttrib("idclass");
 
298
            try
 
299
            {
 
300
               Class classToUse = Class.forName(idClass);
 
301
               Protocol protocol = new Protocol(id, name, classToUse, protocolPos++);
 
302
               config.getProtocols().addProtocol(protocol);
 
303
            }
 
304
            catch (ClassNotFoundException e)
 
305
            {
 
306
               throw new XMLValidationException("Unable to load class " + idClass);
 
307
            }
 
308
            if (!t.terminated) 
 
309
               doProtocol(in);
 
310
            continue;
 
311
         }
 
312
         t.bad();
 
313
      }
 
314
   }
 
315
   
 
316
   private void doProtocol(Reader in) throws IOException
 
317
   {
 
318
      Tag t;
 
319
      while (((t = nextTag(in)) != null) && (!t.name.equals("/" + ELEMENT_PROTOCOL)))
 
320
      {
 
321
         if (t.name.equals(ELEMENT_SPECS))
 
322
         {
 
323
            if (!t.terminated) 
 
324
               doProtocolSpecs(in);
 
325
            continue;
 
326
         }
 
327
         if (t.name.equals(ELEMENT_PROTOCOL_SETTINGS))
 
328
         {
 
329
            if (!t.terminated) 
 
330
               doProtocolSettings(in);
 
331
            continue;
 
332
         }
 
333
         t.bad();
 
334
      }
 
335
   }
 
336
   
 
337
   private void doProtocolSpecs(Reader in) throws IOException
 
338
   {
 
339
      Tag t;
 
340
      while (((t = nextTag(in)) != null)
 
341
      && (!t.name.equals("/" + ELEMENT_SPECS)))
 
342
      {
 
343
         if (t.name.equals(ELEMENT_MAXSPEED))
 
344
         {
 
345
            int value = Tag.getIntValue(t.getAttrib("value"));
 
346
            MaxSpeed m = new MaxSpeed(value);
 
347
            Protocol p = config.getProtocols().getLastProtocol();
 
348
            p.addSpec(m);
 
349
            continue;
 
350
         }
 
351
         if (t.name.equals(ELEMENT_WIRELESS))
 
352
         {
 
353
            String value = t.getAttrib("value");
 
354
            Wireless w = null;
 
355
            
 
356
            if (value.equals(XML_TRISTATE_TRUE))
 
357
               w = new Wireless(1, false);
 
358
            else if (value.equals(XML_TRISTATE_FALSE))
 
359
               w = new Wireless(-1, false);
 
360
            else if (value.equals(XML_TRISTATE_CANBE))
 
361
               w = new Wireless(0, false);
 
362
            else
 
363
               //this validation should be done by the DTD
 
364
               throw new XMLValidationException("Invalid Wireless value " + "\"" + value + "\"" + "\nUse either \"" + 
 
365
                                                XML_TRISTATE_TRUE + "\", \"" + XML_TRISTATE_FALSE + "\" or \"" + 
 
366
                                                XML_TRISTATE_CANBE + "\".");
 
367
            Protocol p = config.getProtocols().getLastProtocol();
 
368
            p.addSpec(w);
 
369
            continue;
 
370
         }
 
371
         if (t.name.equals(ELEMENT_RELIABLE))
 
372
         {
 
373
            boolean value = Tag.getBooleanValue(t.getAttrib("value"));
 
374
            Reliable r = new Reliable(value, false);
 
375
            Protocol p = config.getProtocols().getLastProtocol();
 
376
            p.addSpec(r);
 
377
            continue;
 
378
         }
 
379
         if (t.name.equals(ELEMENT_CONNECTION_ORIENTED))
 
380
         {
 
381
            boolean value = Tag.getBooleanValue(t.getAttrib("value"));
 
382
            ConnectionOriented co = new ConnectionOriented(value, false);
 
383
            Protocol p = config.getProtocols().getLastProtocol();
 
384
            p.addSpec(co);
 
385
            continue;
 
386
         }
 
387
         if (t.name.equals(ELEMENT_OTHERSPEC))
 
388
         {
 
389
            String name = t.getAttrib("name");
 
390
            String value = t.getAttrib("value");
 
391
            String type = t.getAttrib("type");
 
392
            OtherSpec os = null;
 
393
            if (type.equals(DATA_TYPE_INDICATOR_INT))
 
394
            {
 
395
               int intVal = Tag.getIntValue(value);
 
396
               os = new OtherSpec(name, intVal, false);
 
397
            }
 
398
            else if (type.equals(DATA_TYPE_INDICATOR_DOUBLE))
 
399
            {
 
400
               double dblVal = Tag.getDoubleValue(value);
 
401
               os = new OtherSpec(name, dblVal, false);
 
402
            }
 
403
            else if (type.equals(DATA_TYPE_INDICATOR_STRING))
 
404
               os = new OtherSpec(name, value, false);
 
405
            else if (type.equals(DATA_TYPE_INDICATOR_BOOLEAN))
 
406
            {
 
407
               boolean booVal = Tag.getBooleanValue(value);
 
408
               os = new OtherSpec(name, booVal, false);
 
409
            }
 
410
            Protocol p = config.getProtocols().getLastProtocol();
 
411
            p.addSpec(os);
 
412
            continue;
 
413
         }
 
414
         t.bad();
 
415
      }
 
416
   }
 
417
   
 
418
   private void doProtocolSettings(Reader in) throws IOException
 
419
   {
 
420
      Tag t;
 
421
      while (((t = nextTag(in)) != null) && (!t.name.equals("/" + ELEMENT_PROTOCOL_SETTINGS)))
 
422
      {
 
423
         if (t.name.equals(ELEMENT_PROTOCOL_SETTING))
 
424
         {
 
425
            Protocol p = config.getProtocols().getLastProtocol();
 
426
            String name = t.getAttrib("name");
 
427
            String value = t.getAttrib("value");
 
428
            Setting setting = p.getSetting(name);
 
429
            if (setting == null)
 
430
            {
 
431
               setting = new Setting(name, value);
 
432
               p.addSetting(setting);
 
433
            }
 
434
            else
 
435
               throw new XMLValidationException("Protocol Setting \"" + name + "\" already exists with value " + 
 
436
                                                setting.getValue());
 
437
            continue;
 
438
         }
 
439
         t.bad();
 
440
      }
 
441
   }
 
442
   
 
443
   private void doAddresses(Reader in) throws IOException
 
444
   {
 
445
      Tag t;
 
446
      while (((t = nextTag(in)) != null) && (!t.name.equals("/" + ELEMENT_ADDRESSES)))
 
447
      {
 
448
         if (t.name.equals(ELEMENT_ADDRESS))
 
449
         {
 
450
            String protocolid = t.getAttrib("protocolid");
 
451
            String value = t.getAttrib("value");
 
452
            boolean unique = Tag.getBooleanValue(t.getAttrib("unique"));
 
453
            Address add = new Address(protocolid, value, unique);
 
454
            config.getAddresses().addAddress(add);
 
455
            if (!t.terminated) 
 
456
               doAddress(in);
 
457
            continue;
 
458
         }
 
459
         t.bad();
 
460
      }
 
461
   }
 
462
   
 
463
   private void doAddress(Reader in) throws IOException
 
464
   {
 
465
      Tag t;
 
466
      while (((t = nextTag(in)) != null) && (!t.name.equals("/" + ELEMENT_ADDRESS)))
 
467
      {
 
468
         if (t.name.equals(ELEMENT_SPECS))
 
469
         {
 
470
            if (!t.terminated) doAddressSpecs(in);
 
471
            continue;
 
472
         }
 
473
         t.bad();
 
474
      }
 
475
   }
 
476
   
 
477
   private void doAddressSpecs(Reader in) throws IOException
 
478
   {
 
479
      Tag t;
 
480
      while (((t = nextTag(in)) != null) && (!t.name.equals("/" + ELEMENT_SPECS)))
 
481
      {
 
482
         if (t.name.equals(ELEMENT_MAXSPEED))
 
483
         {
 
484
            int value = Tag.getIntValue(t.getAttrib("value"));
 
485
            MaxSpeed m = new MaxSpeed(value);
 
486
            Address a = config.getAddresses().getLastAddress();
 
487
            a.addSpec(m);
 
488
            continue;
 
489
         }
 
490
         if (t.name.equals(ELEMENT_WIRELESS))
 
491
         {
 
492
            String value = t.getAttrib("value");
 
493
            Wireless w = null;
 
494
            if (value.equals(XML_TRISTATE_TRUE))
 
495
               w = new Wireless(1, false);
 
496
            else if (value.equals(XML_TRISTATE_FALSE))
 
497
               w = new Wireless(-1, false);
 
498
            else if (value.equals(XML_TRISTATE_CANBE))
 
499
               w = new Wireless(0, false);
 
500
            else
 
501
               //this validation should be done by the DTD
 
502
               throw new XMLValidationException("Invalid Wireless value \"" + value + "\"" + "\nUse either \"" + 
 
503
                                                XML_TRISTATE_TRUE + "\", \"" + XML_TRISTATE_FALSE + "\" or \"" + 
 
504
                                                XML_TRISTATE_CANBE + "\".");
 
505
            Address a = config.getAddresses().getLastAddress();
 
506
            a.addSpec(w);
 
507
            continue;
 
508
         }
 
509
         if (t.name.equals(ELEMENT_RELIABLE))
 
510
         {
 
511
            boolean value = Tag.getBooleanValue(t.getAttrib("value"));
 
512
            Reliable r = new Reliable(value, false);
 
513
            Address a = config.getAddresses().getLastAddress();
 
514
            a.addSpec(r);
 
515
            continue;
 
516
         }
 
517
         if (t.name.equals(ELEMENT_CONNECTION_ORIENTED))
 
518
         {
 
519
            boolean value = Tag.getBooleanValue(t.getAttrib("value"));
 
520
            ConnectionOriented co = new ConnectionOriented(value, false);
 
521
            Address a = config.getAddresses().getLastAddress();
 
522
            a.addSpec(co);
 
523
            continue;
 
524
         }
 
525
         if (t.name.equals(ELEMENT_OTHERSPEC))
 
526
         {
 
527
            String name = t.getAttrib("name");
 
528
            String value = t.getAttrib("value");
 
529
            String type = t.getAttrib("type");
 
530
            OtherSpec os = null;
 
531
            if (type.equals(DATA_TYPE_INDICATOR_INT))
 
532
            {
 
533
               int intVal = Tag.getIntValue(value);
 
534
               os = new OtherSpec(name, intVal, false);
 
535
            }
 
536
            else if (type.equals(DATA_TYPE_INDICATOR_DOUBLE))
 
537
            {
 
538
               double dblVal = Tag.getDoubleValue(value);
 
539
               os = new OtherSpec(name, dblVal, false);
 
540
            }
 
541
            else if (type.equals(DATA_TYPE_INDICATOR_STRING))
 
542
               os = new OtherSpec(name, value, false);
 
543
            else if (type.equals(DATA_TYPE_INDICATOR_BOOLEAN))
 
544
            {
 
545
               boolean booVal = Tag.getBooleanValue(value);
 
546
               os = new OtherSpec(name, booVal, false);
 
547
            }
 
548
            Address a = config.getAddresses().getLastAddress();
 
549
            a.addSpec(os);
 
550
            continue;
 
551
         }
 
552
         t.bad();
 
553
      }
 
554
   }
 
555
   
 
556
   private void doNodeSpecs(Reader in) throws IOException
 
557
   {
 
558
      Tag t;
 
559
      while (((t = nextTag(in)) != null) && (!t.name.equals("/" + ELEMENT_NODE_SPECS)))
 
560
      {
 
561
         if (t.name.equals(ELEMENT_MAXSPEED))
 
562
         {
 
563
            int value = Tag.getIntValue(t.getAttrib("value"));
 
564
            MaxSpeed m = new MaxSpeed(value);
 
565
            config.getNodeSpecs().addSpec(m);
 
566
            continue;
 
567
         }
 
568
         if (t.name.equals(ELEMENT_MEMORY))
 
569
         {
 
570
            int value = Tag.getIntValue(t.getAttrib("value"));
 
571
            Memory m = new Memory(value);
 
572
            config.getNodeSpecs().addSpec(m);
 
573
            continue;
 
574
         }
 
575
         if (t.name.equals(ELEMENT_OTHERSPEC))
 
576
         {
 
577
            String name = t.getAttrib("name");
 
578
            String value = t.getAttrib("value");
 
579
            String type = t.getAttrib("type");
 
580
            OtherSpec os = null;
 
581
            if (type.equals(DATA_TYPE_INDICATOR_INT))
 
582
            {
 
583
               int intVal = Tag.getIntValue(value);
 
584
               os = new OtherSpec(name, intVal, false);
 
585
            }
 
586
            else if (type.equals(DATA_TYPE_INDICATOR_DOUBLE))
 
587
            {
 
588
               double dblVal = Tag.getDoubleValue(value);
 
589
               os = new OtherSpec(name, dblVal, false);
 
590
            }
 
591
            else if (type.equals(DATA_TYPE_INDICATOR_STRING))
 
592
               os = new OtherSpec(name, value, false);
 
593
            else if (type.equals(DATA_TYPE_INDICATOR_BOOLEAN))
 
594
            {
 
595
               boolean booVal = Tag.getBooleanValue(value);
 
596
               os = new OtherSpec(name, booVal, false);
 
597
            }
 
598
            config.getNodeSpecs().addSpec(os);
 
599
            continue;
 
600
         }
 
601
         t.bad();
 
602
      }
 
603
   }
 
604
   
 
605
   private void doLinkProfiles(Reader in) throws IOException
 
606
   {
 
607
      Tag t;
 
608
      while (((t = nextTag(in)) != null) && (!t.name.equals("/" + ELEMENT_LINK_PROFILES)))
 
609
      {
 
610
         if (t.name.equals(ELEMENT_LINK_PROFILE))
 
611
         {
 
612
            String name = t.getAttrib("name");
 
613
            boolean exactMatchRequired = Tag.getBooleanValue(t.getAttrib("requireExactMatch"));
 
614
            LinkProfile lp = new LinkProfile(name, exactMatchRequired);
 
615
            config.getLinkProfiles().addProfile(lp);
 
616
            if (!t.terminated) 
 
617
               doLinkProfile(in);
 
618
            continue;
 
619
         }
 
620
         t.bad();
 
621
      }
 
622
   }
 
623
   
 
624
   private void doLinkProfile(Reader in) throws IOException
 
625
   {
 
626
      Tag t;
 
627
      while (((t = nextTag(in)) != null) && (!t.name.equals("/" + ELEMENT_LINK_PROFILE)))
 
628
      {
 
629
         if (t.name.equals(ELEMENT_LINK_REQS))
 
630
         {
 
631
            if (!t.terminated) 
 
632
               doLinkReqs(in);
 
633
            continue;
 
634
         }
 
635
         t.bad();
 
636
      }
 
637
   }
 
638
   
 
639
   private void doLinkReqs(Reader in) throws IOException
 
640
   {
 
641
      Tag t;
 
642
      while (((t = nextTag(in)) != null) && (!t.name.equals("/" + ELEMENT_LINK_REQS)))
 
643
      {
 
644
         if (t.name.equals(ELEMENT_LINK_REQ_MINSPEED))
 
645
         {
 
646
            int value = Tag.getIntValue(t.getAttrib("value"));
 
647
            MinSpeed ms = new MinSpeed(value);
 
648
            config.getLinkProfiles().getLastProfile().addReq(ms);
 
649
            continue;
 
650
         }
 
651
         if (t.name.equals(ELEMENT_LINK_REQ_PROTOCOL))
 
652
         {
 
653
            String protocolid = t.getAttrib("protocolid");
 
654
            ReqProtocol rp = new ReqProtocol(protocolid);
 
655
            config.getLinkProfiles().getLastProfile().addReq(rp);
 
656
            continue;
 
657
         }
 
658
         if (t.name.equals(ELEMENT_WIRELESS))
 
659
         {
 
660
            String value = t.getAttrib("value");
 
661
            Wireless w = null;
 
662
            if (value.equals(XML_TRISTATE_TRUE))
 
663
               w = new Wireless(1, true);
 
664
            else if (value.equals(XML_TRISTATE_FALSE))
 
665
               w = new Wireless(-1, true);
 
666
            else if (value.equals(XML_TRISTATE_CANBE))
 
667
               w = new Wireless(0, true);
 
668
            else
 
669
               //this validation should be done by the DTD
 
670
               throw new XMLValidationException("Invalid Wireless value \"" + value + "\"\nUse either \"" + XML_TRISTATE_TRUE + 
 
671
                                                "\", \"" + XML_TRISTATE_FALSE + "\" or \"" + XML_TRISTATE_CANBE + "\".");
 
672
            config.getLinkProfiles().getLastProfile().addReq(w);
 
673
            continue;
 
674
         }
 
675
         if (t.name.equals(ELEMENT_RELIABLE))
 
676
         {
 
677
            boolean value = Tag.getBooleanValue(t.getAttrib("value"));
 
678
            Reliable r = new Reliable(value, true);
 
679
            config.getLinkProfiles().getLastProfile().addReq(r);
 
680
            continue;
 
681
         }
 
682
         if (t.name.equals(ELEMENT_CONNECTION_ORIENTED))
 
683
         {
 
684
            boolean value = Tag.getBooleanValue(t.getAttrib("value"));
 
685
            ConnectionOriented co = new ConnectionOriented(value, true);
 
686
            config.getLinkProfiles().getLastProfile().addReq(co);
 
687
            continue;
 
688
         }
 
689
         if (t.name.equals(ELEMENT_LINK_REQ_MAXPING))
 
690
         {
 
691
            int value = Tag.getIntValue(t.getAttrib("value"));
 
692
            MaxPing mp = new MaxPing(value);
 
693
            config.getLinkProfiles().getLastProfile().addReq(mp);
 
694
            continue;
 
695
         }
 
696
         if (t.name.equals(ELEMENT_LINK_REQ_OTHER))
 
697
         {
 
698
            String name = t.getAttrib("name");
 
699
            String comparator = t.getAttrib("comparator");
 
700
            String type = t.getAttrib("type");
 
701
            String value = t.getAttrib("value");
 
702
            OtherReq or = null;
 
703
            if (type.equals(DATA_TYPE_INDICATOR_INT))
 
704
            {
 
705
               int intVal = Tag.getIntValue(value);
 
706
               or = new OtherReq(name, intVal, comparator);
 
707
            }
 
708
            else if (type.equals(DATA_TYPE_INDICATOR_DOUBLE))
 
709
            {
 
710
               double dblVal = Tag.getDoubleValue(value);
 
711
               or = new OtherReq(name, dblVal, comparator);
 
712
            }
 
713
            else if (type.equals(DATA_TYPE_INDICATOR_STRING))
 
714
               or = new OtherReq(name, value, comparator);
 
715
            else if (type.equals(DATA_TYPE_INDICATOR_BOOLEAN))
 
716
            {
 
717
               boolean booVal = Tag.getBooleanValue(value);
 
718
               or = new OtherReq(name, booVal, comparator);
 
719
            }
 
720
            config.getLinkProfiles().getLastProfile().addReq(or);
 
721
            continue;
 
722
         }
 
723
         t.bad();
 
724
      }
 
725
   }
 
726
   
 
727
   private void doNodeProfiles(Reader in) throws IOException
 
728
   {
 
729
      Tag t;
 
730
      while (((t = nextTag(in)) != null) && (!t.name.equals("/" + ELEMENT_NODE_PROFILES)))
 
731
      {
 
732
         if (t.name.equals(ELEMENT_NODE_PROFILE))
 
733
         {
 
734
            String name = t.getAttrib("name");
 
735
            boolean exactMatchRequired = Tag.getBooleanValue(t.getAttrib("requireExactMatch"));
 
736
            NodeProfile np = new NodeProfile(name, exactMatchRequired);
 
737
            config.getNodeProfiles().addProfile(np);
 
738
            if (!t.terminated) doNodeProfile(in);
 
739
            continue;
 
740
         }
 
741
         t.bad();
 
742
      }
 
743
   }
 
744
   
 
745
   private void doNodeProfile(Reader in) throws IOException
 
746
   {
 
747
      Tag t;
 
748
      while (((t = nextTag(in)) != null) && (!t.name.equals("/" + ELEMENT_NODE_PROFILE)))
 
749
      {
 
750
         if (t.name.equals(ELEMENT_NODE_REQS))
 
751
         {
 
752
            if (!t.terminated) 
 
753
               doNodeReqs(in);
 
754
            continue;
 
755
         }
 
756
         t.bad();
 
757
      }
 
758
   }
 
759
   
 
760
   private void doNodeReqs(Reader in) throws IOException
 
761
   {
 
762
      Tag t;
 
763
      while (((t = nextTag(in)) != null) && (!t.name.equals("/" + ELEMENT_NODE_REQS)))
 
764
      {
 
765
         if (t.name.equals(ELEMENT_NODE_REQ_MINSPEED))
 
766
         {
 
767
            int value = Tag.getIntValue(t.getAttrib("value"));
 
768
            MinSpeed ms = new MinSpeed(value);
 
769
            config.getNodeProfiles().getLastProfile().addReq(ms);
 
770
            continue;
 
771
         }
 
772
         if (t.name.equals(ELEMENT_NODE_REQ_MINMEMORY))
 
773
         {
 
774
            int value = Tag.getIntValue(t.getAttrib("value"));
 
775
            MinMemory mm = new MinMemory(value);
 
776
            config.getNodeProfiles().getLastProfile().addReq(mm);
 
777
            continue;
 
778
         }
 
779
         if (t.name.equals(ELEMENT_NODE_REQ_OTHER))
 
780
         {
 
781
            String name = t.getAttrib("name");
 
782
            String comparator = t.getAttrib("comparator");
 
783
            String type = t.getAttrib("type");
 
784
            String value = t.getAttrib("value");
 
785
            OtherReq or = null;
 
786
            if(type.equals(DATA_TYPE_INDICATOR_INT))
 
787
            {
 
788
               int intVal = Tag.getIntValue(value);
 
789
               or = new OtherReq(name, intVal, comparator);
 
790
            }
 
791
            else if(type.equals(DATA_TYPE_INDICATOR_DOUBLE))
 
792
            {
 
793
               double dblVal = Tag.getDoubleValue(value);
 
794
               or = new OtherReq(name, dblVal, comparator);
 
795
            }
 
796
            else if(type.equals(DATA_TYPE_INDICATOR_STRING))
 
797
               or = new OtherReq(name, value, comparator);
 
798
            else if(type.equals(DATA_TYPE_INDICATOR_BOOLEAN))
 
799
            {
 
800
               boolean booVal = Tag.getBooleanValue(value);
 
801
               or = new OtherReq(name, booVal, comparator);
 
802
            }
 
803
            config.getNodeProfiles().getLastProfile().addReq(or);
 
804
            continue;
 
805
         }
 
806
         t.bad();
 
807
      }
 
808
   }
 
809
   
 
810
   public JCSPConfig getConfig()
 
811
   {
 
812
      return config;
 
813
   }
 
814
   
 
815
   private static final int MAX_ATTRIBS = 20;
 
816
   private final String[] attribs = new String[MAX_ATTRIBS];
 
817
   private final String[] values = new String[MAX_ATTRIBS];
 
818
   
 
819
   private Tag nextTag(Reader in) throws IOException
 
820
   {
 
821
      int i;
 
822
      char c;
 
823
      String name;
 
824
      boolean terminated = false; //put in for J# compat - is always set
 
825
      do
 
826
      {
 
827
         // skip whitespace
 
828
         do
 
829
         {
 
830
            i = in.read();
 
831
            if (i < 0)
 
832
               return null;
 
833
            c = (char) i;
 
834
         } while (isSpace(c));
 
835
         // expect to find a '<' character
 
836
         if (c != '<')
 
837
            throw new IOException("Expected '<'");
 
838
         // get the tag name (up to a space)
 
839
         c = nextChar(in);
 
840
         if (c != '!') 
 
841
            break;
 
842
         // comment - skip until '>'
 
843
         do
 
844
         {
 
845
            c = nextChar(in);
 
846
         } while (c != '>');
 
847
      } while (true);
 
848
      name = "";
 
849
      do
 
850
      {
 
851
         name = name + c;
 
852
         c = nextChar(in);
 
853
      } while ((!isSpace(c)) && (c != '/') && (c != '>'));
 
854
      // get attributes
 
855
      i = 0;
 
856
      do
 
857
      {
 
858
         // skip whitespace
 
859
         while (isSpace(c))
 
860
            c = nextChar(in);
 
861
         if (c == '>')
 
862
         {
 
863
            terminated = false;
 
864
            break;
 
865
         }
 
866
         if (c == '/')
 
867
         {
 
868
            terminated = true;
 
869
            while (c != '>')
 
870
               c = nextChar(in);
 
871
            break;
 
872
         }
 
873
         // read the attribute name
 
874
         attribs[i] = "";
 
875
         while ((c != '=') && (!isSpace(c)) && (c != '>') && (c != '/'))
 
876
         {
 
877
            attribs[i] = attribs[i] + c;
 
878
            c = nextChar(in);
 
879
         }
 
880
         // read the attribute value
 
881
         c = nextChar(in); // a quote
 
882
         if (c == '\"')
 
883
         {
 
884
            c = nextChar(in); // skip the quote
 
885
            values[i] = "";
 
886
            do
 
887
            {
 
888
               values[i] = values[i] + c;
 
889
               c = nextChar(in);
 
890
            } while (c != '\"');
 
891
            c = nextChar(in);
 
892
            i++;
 
893
         }
 
894
         else
 
895
         {
 
896
            // skip to the '>' or '/' characters
 
897
            while ((c != '>') && (c != '/'))
 
898
               c = nextChar(in);
 
899
         }
 
900
      } while (true);
 
901
      // return the tag
 
902
      String[] a = new String[i], v = new String[i];
 
903
      for (int j = 0; j < i; j++)
 
904
      {
 
905
         a[j] = attribs[j];
 
906
         v[j] = values[j];
 
907
      }
 
908
      return new Tag(name, a, v, terminated);
 
909
   }
 
910
   
 
911
   private boolean isSpace(char c)
 
912
   {
 
913
      return (c == ' ') || (c == '\t') || (c == '\r') || (c == '\n');
 
914
   }
 
915
   
 
916
   private char nextChar(Reader in) throws IOException
 
917
   {
 
918
      int i = in.read();
 
919
      if (i < 0)
 
920
         throw new IOException("Unexpected end of file");
 
921
      return (char) i;
 
922
   }
 
923
   
 
924
   private static class Tag
 
925
   {
 
926
      public final String name;
 
927
      public final String[] attrib;
 
928
      public final String[] value;
 
929
      public final boolean terminated;
 
930
      public Tag(String name, String[] attrib, String[] value, boolean terminated)
 
931
      {
 
932
         this.name = name;
 
933
         this.attrib = attrib;
 
934
         this.value = value;
 
935
         this.terminated = terminated;
 
936
      }
 
937
      public String getAttrib(String attr, String def)
 
938
      {
 
939
         for (int i = 0; i < attrib.length; i++)
 
940
         {
 
941
            if (attr.equals(attrib[i]))
 
942
               return value[i];
 
943
         }
 
944
         return def;
 
945
      }
 
946
      public String getAttrib(String attr)
 
947
      {
 
948
         return getAttrib(attr, null);
 
949
      }
 
950
      public void bad() throws IOException
 
951
      {
 
952
         throw new IOException("Unexpected tag - " + name);
 
953
      }
 
954
      public static boolean getBooleanValue(String val)
 
955
      {
 
956
         if (val.equals(XML_BOOLEAN_TRUE))
 
957
            return true;
 
958
         if (val.equals(XML_BOOLEAN_FALSE))
 
959
            return false;
 
960
         throw new XMLValidationException("expected boolean tag");
 
961
      }
 
962
      public static int getIntValue(String val)
 
963
      {
 
964
         return Integer.parseInt(val);
 
965
      }
 
966
      public static double getDoubleValue(String val)
 
967
      {
 
968
         return Double.parseDouble(val);
 
969
      }
 
970
   }
 
971
   
 
972
   /**
 
973
    * Thrown in the event of a semantic error in the parsed XML file.
 
974
    *
 
975
    * @author Quickstone Technologies Limited
 
976
    */
 
977
   public static class XMLValidationException extends RuntimeException
 
978
   {
 
979
      /**
 
980
       * Creates a new exception without a detail message.
 
981
       */
 
982
      public XMLValidationException()
 
983
      {
 
984
         super();
 
985
      }
 
986
      
 
987
      /**
 
988
       * Creates a new exception with a detail message.
 
989
       *
 
990
       * @param msg the detail message.
 
991
       */
 
992
      public XMLValidationException(String msg)
 
993
      {
 
994
         super(msg);
 
995
      }
 
996
   }
 
997
}
 
 
b'\\ No newline at end of file'