KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > clif > scenario > util > isac > util > ScenarioSyntaxChecker


1 /*
2  * CLIF is a Load Injection Framework
3  * Copyright (C) 2004 France Telecom R&D
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 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  * CLIF
20  *
21  * Contact: clif@objectweb.org
22  */

23 package org.objectweb.clif.scenario.util.isac.util;
24
25 import java.io.FileInputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.Hashtable JavaDoc;
30 import java.util.Properties JavaDoc;
31 import java.util.Vector JavaDoc;
32
33 import javax.xml.parsers.DocumentBuilder JavaDoc;
34 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
35 import javax.xml.parsers.SAXParser JavaDoc;
36 import javax.xml.parsers.SAXParserFactory JavaDoc;
37
38 import org.objectweb.clif.scenario.util.isac.FileName;
39 import org.objectweb.clif.scenario.util.isac.loadprofile.Point;
40 import org.objectweb.clif.scenario.util.isac.loadprofile.RampDescription;
41 import org.objectweb.clif.scenario.util.isac.plugin.ObjectDescription;
42 import org.objectweb.clif.scenario.util.isac.plugin.ParameterDescription;
43 import org.objectweb.clif.scenario.util.isac.plugin.PluginDescription;
44 import org.objectweb.clif.scenario.util.isac.plugin.SampleDescription;
45 import org.objectweb.clif.scenario.util.isac.plugin.TestDescription;
46 import org.objectweb.clif.scenario.util.isac.plugin.TimerDescription;
47 import org.objectweb.clif.scenario.util.isac.plugin.parser.AnalyseSaxPlugin;
48 import org.objectweb.clif.scenario.util.isac.util.xml.IsacEntityResolver;
49 import org.objectweb.util.monolog.api.BasicLevel;
50 import org.objectweb.util.monolog.api.Logger;
51 import org.objectweb.util.monolog.api.LoggerFactory;
52 import org.w3c.dom.Document JavaDoc;
53 import org.w3c.dom.Element JavaDoc;
54 import org.w3c.dom.NamedNodeMap JavaDoc;
55 import org.w3c.dom.Node JavaDoc;
56 import org.w3c.dom.NodeList JavaDoc;
57 import org.xml.sax.InputSource JavaDoc;
58 import org.xml.sax.XMLReader JavaDoc;
59
60 /**
61  * This class will be used to check if the syntax of a scenario is correct
62  *
63  * @author JC Meillaud
64  * @author A Peyrard
65  */

66 public class ScenarioSyntaxChecker {
67     private static Logger cat;
68
69     // hashtable to store the plugin description which will be analyzed
70
// during the parsing
71
private static Hashtable JavaDoc plugins;
72
73     private static Vector JavaDoc behaviorIds;
74
75     // store the current tag number
76
private static int tagNumber;
77
78     private static String JavaDoc tagName;
79
80     // store the name of plugins per session object
81
private static Hashtable JavaDoc sessionObjectPluginName;
82
83     private static String JavaDoc lastActionTag;
84
85     private static String JavaDoc lastActionName;
86
87     private static String JavaDoc lastActionSOId;
88
89     private static int lastActionNumberOfParamsDefined;
90
91     private static String JavaDoc lastControllerTag;
92
93     private static Point lastEnd;
94
95     private static Point currentEnd;
96
97     private static Point currentStart;
98
99     /**
100      * This method analyze if the attributes are defined
101      *
102      * @param The
103      * parent node of the attributes
104      * @param attributesDesc
105      * The hashtable which store each attributes name, and if the
106      * value must be defined
107      * @param errors
108      * The errors vector
109      * @param warnings
110      * The warnings vector
111      * @return true if the analyze succeed else false
112      */

113     private static boolean attributesChecker(NamedNodeMap JavaDoc nodeAttributes,
114             Hashtable JavaDoc attributesDesc, Vector JavaDoc errors, Vector JavaDoc warnings) {
115         // if we have not enough attributes
116
if (nodeAttributes.getLength() < attributesDesc.size()) {
117             errors
118                     .add(tagNumber
119                             + "("
120                             + tagName
121                             + "): The attributes are not correctly defined, this tag must define : "
122                             + attributesDesc);
123             return false;
124         }
125         // if we have more attributes than we need
126
if (nodeAttributes.getLength() < attributesDesc.size()) {
127             warnings.add(tagNumber + "(" + tagName
128                     + "): We have too much attributes, this tag must define : "
129                     + attributesDesc);
130         }
131         // check if the attributes are all defined
132
Enumeration JavaDoc names = attributesDesc.keys();
133         while (names.hasMoreElements()) {
134             String JavaDoc name = (String JavaDoc) names.nextElement();
135             Node JavaDoc nodeValue = nodeAttributes.getNamedItem(name);
136             if (nodeValue == null) {
137                 errors.add(tagNumber + "(" + tagName
138                         + "): An attribute is not defined : " + name);
139                 return false;
140             }
141             String JavaDoc value = nodeValue.getNodeValue();
142             // if value required add an error else just a warning
143
if (value.equals("")) {
144                 if (((AttributeDescription) attributesDesc.get(name)).mustBeInit) {
145                     errors
146                             .add(tagNumber
147                                     + "("
148                                     + tagName
149                                     + "): The value of this attribute must be initialized : "
150                                     + name);
151                     return false;
152                 } else {
153                     warnings.add(tagNumber + "(" + tagName
154                             + "): The value of this attribute is empty : "
155                             + name);
156                 }
157             }
158             // put the value in the description of this attribute
159
((AttributeDescription) attributesDesc.get(name)).value = value;
160         }
161         // all checking have been done, so return true
162
return true;
163     }
164
165     /**
166      * fonction which explore the xml file
167      *
168      * @param node
169      * the node to be explored
170      * @param errors
171      * The errors in the parsing process
172      * @param warnings
173      * The warnings in the parsing process
174      * @param cl
175      * The class loader which will be use to load plugin description
176      * files
177      * @return True if we could continue the process, false if we found some
178      * errors
179      */

180     private static boolean visit(Node JavaDoc node, Vector JavaDoc errors, Vector JavaDoc warnings,
181             ClassLoader JavaDoc cl) {
182         // init boolean which store if we need to analyze children
183
boolean analyzeChildren = true;
184         // string to store the tag name
185
switch (node.getNodeType()) {
186         // analyze only if the node is an element_node
187
case Node.ELEMENT_NODE:
188             // increments the tag number
189
tagNumber++;
190             // get the tag name
191
tagName = ((Element JavaDoc) node).getTagName();
192
193             cat.log(BasicLevel.DEBUG, " * NODE ANALYZED : " + tagName);
194             ///////////////////////////////////////////////////////////////////
195
// analyse all type of tag name which can be found on a scenario //
196
///////////////////////////////////////////////////////////////////
197

198             // first the 'behaviors' tag but we have nothing to check
199
if (tagName
200                     .equals(org.objectweb.clif.scenario.util.isac.util.tree.Node.BEHAVIORS)) {
201                 // do nothing
202
} // 'use' tag
203
else if (org.objectweb.clif.scenario.util.isac.util.tree.Node.USE
204                     .equals(tagName)) {
205                 NamedNodeMap JavaDoc attributes = node.getAttributes();
206                 // init a new hashtable which store the attributes names for a
207
// 'plugin' tag and if they are required
208
Hashtable JavaDoc attNames = new Hashtable JavaDoc();
209                 attNames.put("id", new AttributeDescription(true, true));
210                 attNames.put("name", new AttributeDescription(true, true));
211                 // verify the attributs
212
attributesChecker(attributes, attNames, errors, warnings);
213                 // get the id
214
String JavaDoc idValue = ((AttributeDescription) attNames.get("id")).value;
215                 // get the name
216
String JavaDoc nameValue = ((AttributeDescription) attNames.get("name")).value;
217                 // store the name of this plugin which will be used for this
218
// session object
219
sessionObjectPluginName.put(idValue, nameValue);
220                 // check if this plugin have been already initialized
221
if (!plugins.containsKey(nameValue)) {
222                     PluginDescription pd = loadPluginDescription(nameValue
223                             + "/", cl, errors);
224                     // if the description could not be analized
225
if (pd == null) {
226                         return false;
227                     } else {
228                         plugins.put(idValue, pd);
229                     }
230                 }
231                 lastActionTag = tagName;
232                 lastActionNumberOfParamsDefined = 0;
233                 lastActionSOId = idValue;
234                 if (node.hasChildNodes()) {
235                     NodeList JavaDoc children = node.getChildNodes();
236                     for (int i = 0; i < children.getLength(); i++) {
237                         Node JavaDoc tempNode = children.item(i);
238                         if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
239                             boolean continueProcess = true;
240                             continueProcess = visit(tempNode, errors, warnings,
241                                     cl);
242                             // if we find some errors, stop the process
243
if (!continueProcess)
244                                 return false;
245                         }
246                     }
247                 }
248                 // analyze the number of params required
249
int numberOfParamsRequired = ((PluginDescription) plugins
250                         .get(idValue)).getObject().getParams().size();
251                 // we decrease the number required, because during the plugin analyze we add a 'id' parameter
252
if ((numberOfParamsRequired-1) > lastActionNumberOfParamsDefined) {
253                     // we don't manage to get all the parameters
254
errors
255                             .add(tagNumber
256                                     + "("
257                                     + tagName
258                                     + "): A parameter has not have been defined, nb required="
259                                     + numberOfParamsRequired + ", nb found="
260                                     + lastActionNumberOfParamsDefined);
261                     return false ;
262                 }
263                 analyzeChildren = false;
264             } // 'behavior' tag
265
else if (tagName
266                     .equals(org.objectweb.clif.scenario.util.isac.util.tree.Node.BEHAVIOR)) {
267                 // check attributes
268
NamedNodeMap JavaDoc attributes = node.getAttributes();
269                 // init a new hashtable which store the attributes names for a
270
// 'behavior' tag and if they are required
271
Hashtable JavaDoc attNames = new Hashtable JavaDoc();
272                 attNames.put("id", new AttributeDescription(true, true));
273                 // verify the attributes
274
attributesChecker(attributes, attNames, errors, warnings);
275                 String JavaDoc idValue = ((AttributeDescription) attNames.get("id")).value;
276                 // add the id of this behavior to the vecor which store them
277
behaviorIds.add(idValue);
278             } // 'sample', 'timer', 'plugin', 'use' tags will be managed if this
279
// part, but
280
// 'plugin' has been previously analysed so they won't execute this
281
// part
282
else if (org.objectweb.clif.scenario.util.isac.util.tree.Node
283                     .isPluginNode(tagName)
284                     || org.objectweb.clif.scenario.util.isac.util.tree.Node.CONDITION
285                             .equals(tagName)) {
286                 NamedNodeMap JavaDoc attributes = node.getAttributes();
287                 // init a new hashtable which store the attributes names for a
288
// 'behavior' tag and if they are required
289
Hashtable JavaDoc attNames = new Hashtable JavaDoc();
290                 attNames.put("use", new AttributeDescription(true, true));
291                 attNames.put("name", new AttributeDescription(true, true));
292                 // verify the attributes
293
attributesChecker(attributes, attNames, errors, warnings);
294                 String JavaDoc idPluginUsed = ((AttributeDescription) attNames
295                         .get("use")).value;
296                 String JavaDoc actionName = ((AttributeDescription) attNames
297                         .get("name")).value;
298                 if (!sessionObjectPluginName.containsKey(idPluginUsed)) {
299                     errors
300                             .add(tagNumber
301                                     + "("
302                                     + tagName
303                                     + "): This session object id, have not been defined : "
304                                     + idPluginUsed);
305                     return false;
306                 }
307                 lastActionTag = tagName;
308                 lastActionName = actionName;
309                 lastActionSOId = idPluginUsed;
310                 lastActionNumberOfParamsDefined = 0;
311                 // analyze the children of this action node
312
if (node.hasChildNodes()) {
313                     NodeList JavaDoc children = node.getChildNodes();
314                     for (int i = 0; i < children.getLength(); i++) {
315                         Node JavaDoc tempNode = children.item(i);
316                         if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
317                             boolean continueProcess = true;
318                             continueProcess = visit(tempNode, errors, warnings,
319                                     cl);
320                             // if we find some errors, stop the process
321
if (!continueProcess)
322                                 return false;
323                         }
324                     }
325                 }
326                 // analyze the number of params required
327
int numberOfParamsRequired = 0;
328                 if (tagName
329                         .equals(org.objectweb.clif.scenario.util.isac.util.tree.Node.CONDITION))
330                     numberOfParamsRequired = ((TestDescription) ((PluginDescription) plugins
331                             .get(idPluginUsed)).getTests().get(actionName))
332                             .getParams().size();
333                 else if (tagName
334                         .equals(org.objectweb.clif.scenario.util.isac.util.tree.Node.SAMPLE))
335                     numberOfParamsRequired = ((SampleDescription) ((PluginDescription) plugins
336                             .get(idPluginUsed)).getSamples().get(actionName))
337                             .getParams().size();
338                 else if (tagName
339                         .equals(org.objectweb.clif.scenario.util.isac.util.tree.Node.TIMER))
340                     numberOfParamsRequired = ((TimerDescription) ((PluginDescription) plugins
341                             .get(idPluginUsed)).getTimers().get(actionName))
342                             .getParams().size();
343                 // we decrease the number required, because during the plugin analyze we add a 'id' parameter
344
if ((numberOfParamsRequired-1) > lastActionNumberOfParamsDefined) {
345                     // we don't manage to get all the parameters
346
errors
347                             .add(tagNumber
348                                     + "("
349                                     + tagName
350                                     + "): A parameter has not have been defined, nb required="
351                                     + numberOfParamsRequired + ", nb found="
352                                     + lastActionNumberOfParamsDefined);
353                     return false ;
354                 }
355                 analyzeChildren = false;
356             } else if (tagName
357                     .equals(org.objectweb.clif.scenario.util.isac.util.tree.Node.PARAMS)) {
358                 // do nothing
359
} else if (tagName
360                     .equals(org.objectweb.clif.scenario.util.isac.util.tree.Node.PARAM)) {
361                 NamedNodeMap JavaDoc attributes = node.getAttributes();
362                 // init a new hashtable which store the attributes names for a
363
// 'behavior' tag and if they are required
364
Hashtable JavaDoc attNames = new Hashtable JavaDoc();
365                 attNames.put("value", new AttributeDescription(true, false));
366                 attNames.put("name", new AttributeDescription(true, true));
367                 // verify the attributes
368
attributesChecker(attributes, attNames, errors, warnings);
369                 String JavaDoc name = ((AttributeDescription) attNames.get("name")).value;
370                 // get the plugin description of this action
371
PluginDescription pd = (PluginDescription) plugins
372                         .get(lastActionSOId);
373                 // get the current params of the description
374
Vector JavaDoc params = null;
375                 // either it's a 'sample'
376
if (lastActionTag
377                         .equals(org.objectweb.clif.scenario.util.isac.util.tree.Node.SAMPLE)) {
378                     SampleDescription sample = (SampleDescription) pd
379                             .getSamples().get(lastActionName);
380                     params = sample.getParams();
381                 }
382                 // or a 'timer'
383
else if (lastActionTag
384                         .equals(org.objectweb.clif.scenario.util.isac.util.tree.Node.TIMER)) {
385                     TimerDescription timer = (TimerDescription) pd.getTimers()
386                             .get(lastActionName);
387                     params = timer.getParams();
388                 }
389                 // or a 'condition'
390
else if (lastActionTag
391                         .equals(org.objectweb.clif.scenario.util.isac.util.tree.Node.CONDITION)) {
392                     TestDescription test = (TestDescription) pd.getTests().get(
393                             lastActionName);
394                     params = test.getParams();
395                 }
396                 // or a 'use'
397
else if (lastActionTag
398                         .equals(org.objectweb.clif.scenario.util.isac.util.tree.Node.USE)) {
399                     ObjectDescription object = (ObjectDescription) pd
400                             .getObject();
401                     params = object.getParams();
402                 }
403
404                 // check if this param belongs to the current action
405
boolean belongs = false;
406                 for (int i = 0; i < params.size(); i++) {
407                     ParameterDescription param = (ParameterDescription) params
408                             .elementAt(i);
409                     if (param.getName().equals(name)) {
410                         lastActionNumberOfParamsDefined++;
411                         belongs = true;
412                         break;
413                     }
414                 }
415                 // if this parameters is not needed
416
if (!belongs) {
417                     warnings.add(tagNumber + "(" + tagName + "): This action ("
418                             + lastActionTag
419                             + ") does not required a parameter " + name);
420                 }
421             } // 'if', 'while', 'preemptive' tag doesn't need any attributes
422
else if (org.objectweb.clif.scenario.util.isac.util.tree.Node
423                     .isControllerNode(tagName)) {
424                 NamedNodeMap JavaDoc attributes = node.getAttributes();
425                 // init a new hashtable which store the attributes names for a
426
// controler tag and if they are required
427
Hashtable JavaDoc attNames = new Hashtable JavaDoc();
428                 // verify the attributes
429
attributesChecker(attributes, attNames, errors, warnings);
430                 // set the current controller tag
431
lastControllerTag = tagName;
432             } // 'choice' tag
433
else if (org.objectweb.clif.scenario.util.isac.util.tree.Node.CHOICE
434                     .equals(tagName)) {
435                 NamedNodeMap JavaDoc attributes = node.getAttributes();
436                 // init a new hashtable which store the attributes names for a
437
// 'choice' tag and if they are required
438
Hashtable JavaDoc attNames = new Hashtable JavaDoc();
439                 attNames.put("proba", new AttributeDescription(true, true));
440                 // verify the attributes
441
attributesChecker(attributes, attNames, errors, warnings);
442             }
443             // 'loadprofile' tag, nothing to check
444
else if (org.objectweb.clif.scenario.util.isac.util.tree.Node.LOAD_PROFILE
445                     .equals(tagName)) {
446                 // do nothing
447
} // 'group' tag
448
else if (org.objectweb.clif.scenario.util.isac.util.tree.Node.GROUP
449                     .equals(tagName)) {
450                 NamedNodeMap JavaDoc attributes = node.getAttributes();
451                 // init a new hashtable which store the attributes names for a
452
// 'group' tag and if they are required
453
Hashtable JavaDoc attNames = new Hashtable JavaDoc();
454                 attNames.put("behavior", new AttributeDescription(true, true));
455                 attNames.put("forceStop",
456                         new AttributeDescription(false, false));
457                 // verify the attributes
458
attributesChecker(attributes, attNames, errors, warnings);
459                 // get the behavior id value
460
String JavaDoc behaviorId = ((AttributeDescription) attNames
461                         .get("behavior")).value;
462                 // check if the behavior id exists
463
if (!behaviorIds.contains(behaviorId)) {
464                     warnings
465                             .add(tagNumber
466                                     + "("
467                                     + tagName
468                                     + "): This behavior id is not defined, so this group won't be executed"
469                                     + behaviorId);
470                 }
471                 // init last end point
472
lastEnd = null;
473                 currentEnd = null;
474                 currentStart = null;
475             } else if (org.objectweb.clif.scenario.util.isac.util.tree.Node.RAMP
476                     .equals(tagName)) {
477                 // check if the style has been defined
478
NamedNodeMap JavaDoc attributes = node.getAttributes();
479                 // init a new hashtable which store the attributes names for a
480
// 'ramp' tag and if they are required
481
Hashtable JavaDoc attNames = new Hashtable JavaDoc();
482                 attNames.put("style", new AttributeDescription(true, true));
483                 // verify the attributes
484
attributesChecker(attributes, attNames, errors, warnings);
485                 String JavaDoc style = ((AttributeDescription) attNames.get("style")).value;
486                 RampDescription.isDefinedStyle(style);
487             } else if (org.objectweb.clif.scenario.util.isac.util.tree.Node.POINTS
488                     .equals(tagName)) {
489                 // set the current end point as the last end point because
490
// we are describing a new ramp
491
lastEnd = currentEnd;
492                 currentEnd = null;
493                 currentStart = null;
494                 // analyze the children of this action node
495
if (node.hasChildNodes()) {
496                     NodeList JavaDoc children = node.getChildNodes();
497                     for (int i = 0; i < children.getLength(); i++) {
498                         Node JavaDoc tempNode = children.item(i);
499                         if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
500                             boolean continueProcess = true;
501                             continueProcess = visit(tempNode, errors, warnings,
502                                     cl);
503                             // if we find some errors, stop the process
504
if (!continueProcess)
505                                 return false;
506                         }
507                     }
508                 }
509                 // check if start and end point have been defined
510
if (currentStart == null || currentEnd == null) {
511                     errors.add(tagNumber
512                                     + "("
513                                     + tagName
514                                     + "): This ramp has not defined the two required point : start="+currentStart+" and stop="
515                                     + currentEnd);
516                     return false ;
517                 }
518                 analyzeChildren = false ;
519             } else if (org.objectweb.clif.scenario.util.isac.util.tree.Node.POINT
520                     .equals(tagName)) {
521                 // get the point
522
NamedNodeMap JavaDoc attributes = node.getAttributes();
523                 // init a new hashtable which store the attributes names for a
524
// 'ramp' tag and if they are required
525
Hashtable JavaDoc attNames = new Hashtable JavaDoc();
526                 attNames.put("name", new AttributeDescription(true, true));
527                 attNames.put("x", new AttributeDescription(true, true));
528                 attNames.put("y", new AttributeDescription(true, true));
529                 // verify the attributes
530
attributesChecker(attributes, attNames, errors, warnings);
531                 String JavaDoc name = ((AttributeDescription) attNames.get("name")).value;
532                 String JavaDoc xAxis = ((AttributeDescription) attNames.get("x")).value;
533                 String JavaDoc yAxis = ((AttributeDescription) attNames.get("y")).value;
534                 if (xAxis != null && yAxis != null && name != null) {
535                     Integer JavaDoc xAxisInteger = new Integer JavaDoc(xAxis);
536                     Integer JavaDoc yAxisInteger = new Integer JavaDoc(yAxis);
537                     if (name.equals(RampDescription.POINT_START)) {
538                         // start point
539
// verify that the start point of this ramp is
540
// the same than the last end ramp point
541
currentStart = new Point(xAxisInteger.intValue(),
542                                 yAxisInteger.intValue());
543                         // if we are not the starting ramp of the group
544
if (lastEnd != null) {
545                             if (!lastEnd.equals(currentStart)) {
546                                 errors
547                                         .add(tagNumber
548                                                 + "("
549                                                 + tagName
550                                                 + "): A ramp have a starting point which is not the same than the previous ramp ending point ! le="
551                                                 + lastEnd + ", cs="
552                                                 + currentStart);
553                                 return false;
554                             }
555                         }
556                         if (currentEnd != null) {
557                             // test if the starting point is before the
558
// ending point in the time scale
559
if (currentStart.x >= currentEnd.x) {
560                                 errors
561                                         .add(tagNumber
562                                                 + "("
563                                                 + tagName
564                                                 + "): A ramp have a starting point which is after the ending point in the time scale !");
565                                 return false;
566                             }
567                         }
568                     } else if (name.equals(RampDescription.POINT_END)) {
569                         // end point
570
currentEnd = new Point(xAxisInteger.intValue(), yAxisInteger
571                                 .intValue());
572                         if (currentStart != null) {
573                             // test if the starting point is before the
574
// ending point in the time scale
575
if (currentStart.x >= currentEnd.x) {
576                                 errors
577                                         .add(tagNumber
578                                                 + "("
579                                                 + tagName
580                                                 + "): The starting point is after the ending point in the time scale, ignore the ramp !");
581                                 return false;
582                             }
583                         }
584                     } else {
585                         warnings.add(tagNumber + "(" + tagName
586                                 + "): This kind of point is not defined : "
587                                 + name);
588                     }
589                 }
590             }
591
592             // in this case the node is necessarily a simple node, 'then' or
593
// 'nchoice'
594
// this kind of nodes doesn't need any analyze
595
else {
596                 // do nothing
597
}
598             break;
599         default:
600         // do nothing
601
}
602         if (analyzeChildren) {
603             // analyze the children of the current node
604
if (node.hasChildNodes()) {
605                 NodeList JavaDoc children = node.getChildNodes();
606                 for (int i = 0; i < children.getLength(); i++) {
607                     Node JavaDoc tempNode = children.item(i);
608                     if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
609                         boolean continueProcess = true;
610                         continueProcess = visit(tempNode, errors, warnings, cl);
611                         // if we find some errors, stop the process
612
if (!continueProcess)
613                             return false;
614                     }
615                 }
616             }
617         }
618         return true;
619     }
620
621     /**
622      * Load a plugin description from a properties file which has it path
623      * specified in parameter
624      *
625      * @param dirName
626      * The directory name of the plugin
627      * @param cl
628      * The classLoader to load the file
629      * @return A PluginDescription object, null if the description could not be
630      * load
631      */

632     public static PluginDescription loadPluginDescription(String JavaDoc dirName,
633             ClassLoader JavaDoc cl, Vector JavaDoc errors) {
634         cat.log(BasicLevel.DEBUG, "-> loadPluginDescription");
635         // load the plugin properties
636
Properties JavaDoc properties = new Properties JavaDoc();
637         try {
638             // if class loader is defined try to load the file into classpath
639
InputStream JavaDoc is = null;
640             if (cl != null) {
641                 is = cl.getResourceAsStream(dirName
642                         + FileName.PLUGIN_PROPERTIES_FILE);
643             }
644             // if is still null
645
if (is == null) {
646                 is = new FileInputStream JavaDoc(dirName
647                         + FileName.PLUGIN_PROPERTIES_FILE);
648             }
649             // if is still null
650
if (is == null) {
651                 errors
652                         .add("Unable to find the properties file of the plugin : "
653                                 + dirName + FileName.PLUGIN_PROPERTIES_FILE);
654                 return null;
655             }
656             properties.load(is);
657             is.close();
658         } catch (IOException JavaDoc ioe) {
659             cat.log(BasicLevel.WARN, "Unable to analyse property file : "
660                     + dirName + "plugin.properties");
661         }
662         // load the properties defined in the file
663
String JavaDoc pluginXMLFile = properties.getProperty("plugin.xmlFile");
664         String JavaDoc guiXMLFile = properties.getProperty("plugin.guiFile");
665
666         // open the xml definiton file
667
AnalyseSaxPlugin handler = new AnalyseSaxPlugin();
668         try {
669             // if class loader is define try to load thanks to it
670
InputStream JavaDoc istream = null;
671             if (cl != null) {
672                 istream = cl.getResourceAsStream(dirName + pluginXMLFile);
673             }
674             if (istream == null) {
675                 istream = new FileInputStream JavaDoc(dirName + pluginXMLFile);
676             }
677             if (istream == null) {
678                 errors.add("Unable to load the plugin description file : "
679                         + dirName + pluginXMLFile);
680                 return null;
681             }
682             // get the input source from the stream
683
InputSource JavaDoc is = new InputSource JavaDoc(istream);
684             SAXParserFactory JavaDoc factory = SAXParserFactory.newInstance();
685             factory.setValidating(true);
686             SAXParser JavaDoc saxParser = factory.newSAXParser();
687             XMLReader JavaDoc reader = saxParser.getXMLReader();
688             reader.setContentHandler(handler);
689             reader.setErrorHandler(handler);
690             reader.setEntityResolver(new IsacEntityResolver(cl));
691             reader.parse(is);
692         } catch (Exception JavaDoc e) {
693             cat.log(BasicLevel.WARN, " ---> Parser error : " + dirName
694                     + pluginXMLFile + " -> " + e);
695             errors.add(" ---> Parser error : " + dirName + pluginXMLFile
696                     + " -> " + e);
697             return null;
698         }
699         PluginDescription tempDesc = handler.getPluginDescription();
700
701         // return the description analyzed
702
return tempDesc;
703     }
704
705     /**
706      * Load a behavior file
707      *
708      * @param fileName
709      * The name of the file to be loaded
710      * @param errors
711      * A vector to store the errors which occurs, we exit as soon as
712      * an error append, therefore this vector will be empty or
713      * containing an unique element
714      * @param warnings
715      * A vector to store the warnings which could occurs during
716      * parsing
717      * @param cl
718      * the classLoader to load the scenario if this params is null,
719      * try to load the file in the curent path
720      * @param loggerFactory
721      * If you want to trace the parsing process
722      * @return true if the analyze succeed, else false
723      */

724     public static boolean loadScenario(String JavaDoc fileName, Vector JavaDoc errors,
725             Vector JavaDoc warnings, ClassLoader JavaDoc cl, LoggerFactory loggerFactory) {
726         // if logger factory have been specified, get the logger of the class
727
cat = loggerFactory.getLogger(ScenarioSyntaxChecker.class.getName());
728         Document JavaDoc document = null;
729         try {
730             // if the class loader is defined try to load the scenario with it
731
InputStream JavaDoc istream = null;
732             if (cl != null) {
733                 istream = cl.getResourceAsStream(fileName);
734             }
735             // if is still null look for the file into path
736
if (istream == null) {
737                 istream = new FileInputStream JavaDoc(fileName);
738             }
739             // if is still null
740
if (istream == null) {
741                 errors.add("Unable to find the scenario file \"" + fileName
742                         + "\" in path or classpath");
743                 return false;
744             }
745             // create the source from the stream
746
InputSource JavaDoc is = new InputSource JavaDoc(istream);
747             // parse the file with DOM
748
DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory
749                     .newInstance();
750             DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
751             document = builder.parse(is);
752         } catch (Exception JavaDoc e) {
753             // an exception append, so return the exception message in the
754
// errors vector
755
errors.add(e.toString());
756             return false;
757         }
758         // before launching the analyze init the static attributes
759
plugins = new Hashtable JavaDoc();
760         behaviorIds = new Vector JavaDoc();
761         sessionObjectPluginName = new Hashtable JavaDoc();
762         // because we don't analyze the 'scenario' tag
763
tagNumber = 1;
764         // analyse the dom tree
765
boolean result = visit(document, errors, warnings, cl);
766         // remove all the static object references of the class
767
cat = null;
768         plugins = null;
769         behaviorIds = null;
770         sessionObjectPluginName = null;
771         // return the result of the parsing
772
return result;
773     }
774
775     // Define a class which will store an attribute
776
// and if it is required
777
// if it need to be initialized
778
// and the value of this attributes
779
private static class AttributeDescription {
780         public boolean required;
781
782         public boolean mustBeInit;
783
784         public String JavaDoc value;
785
786         public AttributeDescription() {
787         }
788
789         public AttributeDescription(boolean r, boolean m) {
790             this.required = r;
791             this.mustBeInit = m;
792         }
793
794         public String JavaDoc toString() {
795             return "[" + required + "," + mustBeInit + "," + value + "]";
796         }
797     }
798 }
Popular Tags