KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > clif > scenario > util > isac > util > tree > LoadScenario


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.tree;
24
25 import java.io.FileInputStream JavaDoc;
26 import java.util.Hashtable JavaDoc;
27 import java.util.Vector JavaDoc;
28
29 import javax.xml.parsers.DocumentBuilder JavaDoc;
30 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
31
32 import org.apache.log4j.Category;
33 import org.objectweb.clif.scenario.util.isac.loadprofile.GroupDescription;
34 import org.objectweb.clif.scenario.util.isac.loadprofile.LoadProfileManager;
35 import org.objectweb.clif.scenario.util.isac.loadprofile.Point;
36 import org.objectweb.clif.scenario.util.isac.loadprofile.RampDescription;
37 import org.objectweb.clif.scenario.util.isac.loadprofile.gui.Size;
38 import org.objectweb.clif.scenario.util.isac.plugin.PluginManager;
39 import org.objectweb.clif.scenario.util.isac.util.tree.nodes.ChoiceNode;
40 import org.objectweb.clif.scenario.util.isac.util.tree.nodes.ControlerNode;
41 import org.w3c.dom.Document JavaDoc;
42 import org.w3c.dom.Element JavaDoc;
43 import org.w3c.dom.NamedNodeMap JavaDoc;
44 import org.w3c.dom.Node JavaDoc;
45 import org.w3c.dom.NodeList JavaDoc;
46 import org.xml.sax.InputSource JavaDoc;
47
48 /**
49  * This class implements some methods which will be used to load behaviors xml
50  * file
51  *
52  * @author JC Meillaud
53  * @author A Peyrard
54  */

55 public class LoadScenario {
56     static Category cat = Category.getInstance(LoadScenario.class.getName());
57
58     static PluginManager pluginManager = PluginManager.getPluginManager();
59
60     private static TreeManager treeManager = TreeManager.getTreeManager(null);
61
62     private static LoadProfileManager loadProfileManager = LoadProfileManager
63             .getInstance();
64
65     private static NodeDescription currentDesc = null;
66
67     private static String JavaDoc currentControllerType = null;
68
69     private static ScenarioNode tree = null;
70
71     private static ScenarioNode lastControllerNode = null;
72
73     private static String JavaDoc currentGroupId = null;
74
75     private static RampDescription currentRamp = null;
76
77     private static Point currentEnd = null;
78
79     private static Point currentStart = null;
80
81     private static Point lastEnd = null;
82     
83     private static Size maxScale = new Size(0,0) ;
84
85     /**
86      * fonction which explore the xml file
87      *
88      * @param parent
89      * The parent node
90      * @param isControllerChild
91      * true if the parent is a controller
92      * @param node
93      * the node to be explored
94      * @param nodes
95      * The nodes descriptions
96      * @param errors
97      * The errors in the loading process
98      * @return True if we could continue the process, false if we found some
99      * errors
100      */

101     private static boolean visit(ScenarioNode parent,
102             boolean isControllerChild, Node node, Hashtable JavaDoc nodes, Vector JavaDoc errors) {
103         ScenarioNode result = null;
104
105         // will store if the node is a controller
106
boolean flag = false;
107         String JavaDoc tagName = null;
108         switch (node.getNodeType()) {
109         case Node.ELEMENT_NODE:
110             tagName = ((Element JavaDoc) node).getTagName();
111             if (tagName
112                     .equals(org.objectweb.clif.scenario.util.isac.util.tree.Node.BEHAVIORS)) {
113                 NodeDescription desc = NodeDescription
114                         .createNonePluginNode(org.objectweb.clif.scenario.util.isac.util.tree.Node.BEHAVIORS);
115                 // search a new key
116
int keyInt = desc.hashCode();
117                 // check in the table if the key already exist
118
while (nodes.containsKey(new String JavaDoc((new Integer JavaDoc(keyInt))
119                         .toString())))
120                     keyInt++;
121                 // add the description node in the table
122
nodes.put(new String JavaDoc((new Integer JavaDoc(keyInt)).toString()), desc);
123                 // build the new node, it will be the tree root node
124
result = new ScenarioNode(new String JavaDoc((new Integer JavaDoc(keyInt))
125                         .toString()));
126                 // create the global tree
127
tree = result;
128             } else if (tagName
129                     .equals(org.objectweb.clif.scenario.util.isac.util.tree.Node.BEHAVIOR)) {
130                 // create a new behavior node description
131
NodeDescription desc = NodeDescription
132                         .createNonePluginNode(org.objectweb.clif.scenario.util.isac.util.tree.Node.BEHAVIOR);
133
134                 NamedNodeMap JavaDoc attributs = node.getAttributes();
135                 // verify the attributs
136
if (attributs.getLength() < 1) {
137                     errors.add("WARNING:" + "The node \"" + tagName
138                             + "\" must define an attribute \"id\"");
139
140                 } else {
141                     Node id = attributs.getNamedItem("id");
142                     if (id == null) {
143                         errors
144                                 .add("WARNING:"
145                                         + "The attribute \"id\" is not defined in a node "
146                                         + tagName);
147                     } else {
148                         String JavaDoc idValue = id.getNodeValue();
149                         if (idValue.equals("")) {
150                             errors
151                                     .add("WARNING:"
152                                             + "The attribute \"id\" is not initialised in a node "
153                                             + tagName);
154                         } else {
155                             // check if this id doesn't already exist
156
if (!treeManager.getBehaviorsIds()
157                                     .contains(idValue)) {
158                                 // set the id parameter
159
desc.getParams().remove("id");
160                                 desc.getParams().put("id", idValue);
161                                 cat.debug("ID OF THE BEHAVIOR="+idValue) ;
162                             }
163                             else {
164                                 errors.add("ERROR: The value of this behavior id already exist : " + idValue) ;
165                             }
166                         }
167                     }
168                 }
169                 // search a new key
170
int keyInt = desc.hashCode();
171                 // check in the table if the key already exist
172
while (nodes.containsKey(new String JavaDoc((new Integer JavaDoc(keyInt))
173                         .toString())))
174                     keyInt++;
175                 // add the description node in the table
176
nodes.put(new String JavaDoc((new Integer JavaDoc(keyInt)).toString()), desc);
177                 // build the new node, it will be the tree root node
178
result = new ScenarioNode(new String JavaDoc((new Integer JavaDoc(keyInt))
179                         .toString()));
180             } else if (org.objectweb.clif.scenario.util.isac.util.tree.Node.USE
181                     .equals(tagName)) {
182                 NamedNodeMap JavaDoc attributs = node.getAttributes();
183                 // verify the attributs
184
if (attributs.getLength() < 2) {
185                     errors
186                             .add("ERROR:"
187                                     + "The node \""
188                                     + tagName
189                                     + "\" must define two parameters, \"id\" and \"name\"");
190                     return false;
191                 }
192                 Node id = attributs.getNamedItem("id");
193                 if (id == null) {
194                     errors
195                             .add("ERROR:"
196                                     + "The parameter \"id\" is not defined in the node "
197                                     + tagName);
198                     return false;
199                 }
200                 String JavaDoc idValue = id.getNodeValue();
201                 if (idValue.equals("")) {
202                     errors
203                             .add("ERROR:"
204                                     + "The parameter \"id\" is not initialised in the node "
205                                     + tagName);
206                     return false;
207                 }
208                 Node name = attributs.getNamedItem("name");
209                 if (name == null) {
210                     errors
211                             .add("ERROR:"
212                                     + "The parameter \"name\" is not defined in the node "
213                                     + tagName);
214                     return false;
215                 }
216                 String JavaDoc nameValue = name.getNodeValue();
217                 if (nameValue.equals("")) {
218                     errors
219                             .add("ERROR:"
220                                     + "The parameter \"name\" is not intialised in the node "
221                                     + tagName);
222                     return false;
223                 }
224                 Vector JavaDoc descs = pluginManager.createNodesDescriptions(tagName);
225                 NodeDescription desc = null;
226                 for (int i = 0; i < descs.size(); i++) {
227                     if (((NodeDescription) descs.elementAt(i)).getPlugin()
228                             .equals(nameValue))
229                         desc = (NodeDescription) descs.elementAt(i);
230                 }
231                 if (desc == null) {
232                     errors.add("ERROR:"
233                             + "The plugin used could not be found : "
234                             + nameValue);
235                     return false;
236                 }
237                 // add the id value to the desc
238
Hashtable JavaDoc params = desc.getParams();
239                 params.remove("id");
240                 params.put("id", idValue);
241
242                 // set the current description to be updated when we
243
// will analyse parameters
244
currentDesc = desc;
245                 // search a new key
246
int keyInt = desc.hashCode();
247                 // check in the table if the key already exist
248
while (nodes.containsKey(new String JavaDoc((new Integer JavaDoc(keyInt))
249                         .toString())))
250                     keyInt++;
251                 // add the description node in the table
252
nodes.put(new String JavaDoc((new Integer JavaDoc(keyInt)).toString()), desc);
253                 // build the new node, it will be the tree root node
254
result = new ScenarioNode(new String JavaDoc((new Integer JavaDoc(keyInt))
255                         .toString()));
256             } else if (org.objectweb.clif.scenario.util.isac.util.tree.Node
257                     .isPluginNode(tagName)) {
258                 NamedNodeMap JavaDoc attributs = node.getAttributes();
259                 // verify the attributs
260
if (attributs.getLength() < 2) {
261                     errors
262                             .add("ERROR:"
263                                     + "The node \""
264                                     + tagName
265                                     + "\" must define two parameters, \"use\" and \"name\"");
266                     return false;
267                 }
268                 Node use = attributs.getNamedItem("use");
269                 if (use == null) {
270                     errors
271                             .add("ERROR:"
272                                     + "The parameter \"use\" is not defined in the node "
273                                     + tagName);
274                     return false;
275                 }
276                 String JavaDoc useValue = use.getNodeValue();
277                 if (useValue.equals("")) {
278                     errors
279                             .add("ERROR:"
280                                     + "The parameter \"use\" is not initialised in the node "
281                                     + tagName);
282                     return false;
283                 }
284                 Node name = attributs.getNamedItem("name");
285                 if (name == null) {
286                     errors
287                             .add("ERROR:"
288                                     + "The parameter \"name\" is not defined in the node "
289                                     + tagName);
290                     return false;
291                 }
292                 String JavaDoc nameValue = name.getNodeValue();
293                 if (nameValue.equals("")) {
294                     errors
295                             .add("ERROR:"
296                                     + "The parameter \"name\" is not intialised in the node "
297                                     + tagName);
298                     return false;
299                 }
300                 Hashtable JavaDoc usedPlugins = TreeManager.getUsedPlugins(nodes);
301                 if (!usedPlugins.containsKey(useValue)) {
302                     errors
303                             .add("ERROR:"
304                                     + "The plugin used in this action could not be found : "
305                                     + useValue);
306                     return false;
307                 } else {
308                     NodeDescription descPlugin = (NodeDescription) usedPlugins
309                             .get(useValue);
310                     String JavaDoc pluginName = descPlugin.getPlugin();
311                     NodeDescription desc = pluginManager.createNodeDescription(
312                             pluginName, tagName, nameValue);
313                     // set the current description to be updated when we
314
// will analyse parameters
315
currentDesc = desc;
316                     // set the id of the plugin used for this node
317
Hashtable JavaDoc params = currentDesc.getParams();
318                     if (params != null) {
319                         if (params.containsKey("id")) {
320                             // Change the param value
321
params.remove("id");
322                             params.put("id", useValue);
323                         }
324                     }
325                     // search a new key
326
int keyInt = desc.hashCode();
327                     // check in the table if the key already exist
328
while (nodes.containsKey(new String JavaDoc((new Integer JavaDoc(keyInt))
329                             .toString())))
330                         keyInt++;
331                     // add the description node in the table
332
nodes.put(new String JavaDoc((new Integer JavaDoc(keyInt)).toString()),
333                             desc);
334                     // build the new node, it will be the tree root node
335
result = new ScenarioNode(new String JavaDoc((new Integer JavaDoc(keyInt))
336                             .toString()));
337                 }
338             } else if (tagName
339                     .equals(org.objectweb.clif.scenario.util.isac.util.tree.Node.PARAMS)) {
340                 // do nothing
341
} else if (tagName
342                     .equals(org.objectweb.clif.scenario.util.isac.util.tree.Node.PARAM)) {
343                 NamedNodeMap JavaDoc attributs = node.getAttributes();
344                 // verify the attributs
345
if (attributs.getLength() < 2) {
346                     errors
347                             .add("ERROR:"
348                                     + "The node \""
349                                     + tagName
350                                     + "\" must define two parameters, \"name\" and \"value\"");
351                     return false;
352                 }
353                 Node name = attributs.getNamedItem("name");
354                 if (name == null) {
355                     errors
356                             .add("ERROR:"
357                                     + "The parameter \"name\" is not defined in the node "
358                                     + tagName);
359                     return false;
360                 }
361                 String JavaDoc nameValue = name.getNodeValue();
362                 if (nameValue.equals("")) {
363                     errors
364                             .add("ERROR:"
365                                     + "The parameter \"name\" is not initialised in the node "
366                                     + tagName);
367                     return false;
368                 }
369                 Node value = attributs.getNamedItem("value");
370                 if (value == null) {
371                     errors
372                             .add("ERROR:"
373                                     + "The parameter \"value\" is not defined in the node "
374                                     + tagName);
375                     return false;
376                 }
377                 String JavaDoc valueValue = value.getNodeValue();
378                 if (valueValue.equals("")) {
379                     errors
380                             .add("WARNING:"
381                                     + "The parameter \"value\" is not intialised in the node "
382                                     + tagName);
383                 }
384                 if (currentDesc != null) {
385                     Hashtable JavaDoc params = currentDesc.getParams();
386                     if (params != null) {
387                         if (params.containsKey(nameValue)) {
388                             // Change the param value
389
params.remove(nameValue);
390                             params.put(nameValue, valueValue);
391                         }
392                     }
393                 }
394             } else if (org.objectweb.clif.scenario.util.isac.util.tree.Node
395                     .isControllerNode(tagName)) {
396                 // set the current controller type
397
currentControllerType = tagName;
398                 flag = true;
399             } else if (org.objectweb.clif.scenario.util.isac.util.tree.Node.CONDITION
400                     .equals(tagName)) {
401                 if (currentControllerType != null) {
402                     NamedNodeMap JavaDoc attributs = node.getAttributes();
403                     // verify the attributs
404
if (attributs.getLength() < 2) {
405                         errors
406                                 .add("ERROR:"
407                                         + "The node \""
408                                         + tagName
409                                         + "\" must define two parameters, \"use\" and \"name\"");
410                         return false;
411                     }
412                     Node use = attributs.getNamedItem("use");
413                     if (use == null) {
414                         errors
415                                 .add("ERROR:"
416                                         + "The parameter \"use\" is not defined in the node "
417                                         + tagName);
418                         return false;
419                     }
420                     String JavaDoc useValue = use.getNodeValue();
421                     if (useValue.equals("")) {
422                         errors
423                                 .add("ERROR:"
424                                         + "The parameter \"use\" is not initialised in the node "
425                                         + tagName);
426                         return false;
427                     }
428                     Node name = attributs.getNamedItem("name");
429                     if (name == null) {
430                         errors
431                                 .add("ERROR:"
432                                         + "The parameter \"name\" is not defined in the node "
433                                         + tagName);
434                         return false;
435                     }
436                     String JavaDoc nameValue = name.getNodeValue();
437                     if (nameValue.equals("")) {
438                         errors
439                                 .add("ERROR:"
440                                         + "The parameter \"name\" is not intialised in the node "
441                                         + tagName);
442                         return false;
443                     }
444                     Hashtable JavaDoc usedPlugins = TreeManager.getUsedPlugins(nodes);
445                     if (!usedPlugins.containsKey(useValue)) {
446                         errors
447                                 .add("ERROR: Unable to find the plugin which is used by this action : "
448                                         + useValue);
449                         return false;
450                     } else {
451                         NodeDescription descPlugin = (NodeDescription) usedPlugins
452                                 .get(useValue);
453                         String JavaDoc pluginName = descPlugin.getPlugin();
454                         NodeDescription desc = ControlerNode
455                                 .createNodeDescription(pluginName,
456                                         currentControllerType, nameValue);
457                         // set the current description to be updated when we
458
// will analyse parameters
459
currentDesc = desc;
460                         // set the id of the plugin used for this node
461
Hashtable JavaDoc params = currentDesc.getParams();
462                         if (params != null) {
463                             if (params.containsKey("id")) {
464                                 // Change the param value
465
params.remove("id");
466                                 params.put("id", useValue);
467                             }
468                         }
469                         // search a new key
470
int keyInt = desc.hashCode();
471                         // check in the table if the key already exist
472
while (nodes.containsKey(new String JavaDoc(
473                                 (new Integer JavaDoc(keyInt)).toString())))
474                             keyInt++;
475                         // add the description node in the table
476
nodes.put(new String JavaDoc((new Integer JavaDoc(keyInt)).toString()),
477                                 desc);
478                         // build the new node, it will be the tree root node
479
result = new ScenarioNode(new String JavaDoc((new Integer JavaDoc(
480                                 keyInt)).toString()));
481                         // set the current controller type to null
482
currentControllerType = null;
483                     }
484                 }
485             } else if (org.objectweb.clif.scenario.util.isac.util.tree.Node.CHOICE
486                     .equals(tagName)) {
487                 NamedNodeMap JavaDoc attributs = node.getAttributes();
488                 // verify the attributs
489
if (attributs.getLength() < 1) {
490                     errors
491                             .add("ERROR:"
492                                     + "The node \""
493                                     + tagName
494                                     + "\" must define one parameter, \"proba\"");
495                     return false;
496                 }
497                 Node proba = attributs.getNamedItem("proba");
498                 if (proba == null) {
499                     errors
500                             .add("ERROR:"
501                                     + "The parameter \"proba\" is not defined in the node "
502                                     + tagName);
503                     return false;
504                 }
505                 String JavaDoc probaValue = proba.getNodeValue();
506                 if (probaValue.equals("")) {
507                     errors
508                             .add("WARNING:"
509                                     + "The parameter \"proba\" is not initialised in the node "
510                                     + tagName);
511                 }
512                 
513                 NodeDescription desc = ChoiceNode.createNodeDescription();
514                 currentDesc = desc;
515                 // set the proba parameter
516
Hashtable JavaDoc params = currentDesc.getParams();
517                 if (params != null) {
518                     if (params.containsKey("proba")) {
519                         // Change the param value
520
params.remove("proba");
521                         params.put("proba", probaValue);
522                     }
523                 }
524                 int keyInt = desc.hashCode();
525                 // check in the table if the key already exist
526
while (nodes.containsKey(new String JavaDoc((new Integer JavaDoc(keyInt))
527                         .toString())))
528                     keyInt++;
529                 // add the description node in the table
530
nodes.put(new String JavaDoc((new Integer JavaDoc(keyInt)).toString()), desc);
531                 // build the new node, it will be the tree root node
532
result = new ScenarioNode(new String JavaDoc((new Integer JavaDoc(keyInt))
533                         .toString()));
534             }
535             // LOAD PROFILE LOAD PART
536
else if (org.objectweb.clif.scenario.util.isac.util.tree.Node.LOAD_PROFILE
537                     .equals(tagName)) {
538                 // do nothing
539
} else if (org.objectweb.clif.scenario.util.isac.util.tree.Node.GROUP
540                     .equals(tagName)) {
541                 // get the behaviorID
542
NamedNodeMap JavaDoc attributs = node.getAttributes();
543                 // verify the attributs
544
if (attributs.getLength() < 1) {
545                     errors
546                             .add("ERROR:"
547                                     + "The node \""
548                                     + tagName
549                                     + "\" must define at least one parameter, \"behavior\"");
550                     return false;
551                 }
552                 Node behavior = attributs.getNamedItem("behavior");
553                 if (behavior == null) {
554                     errors
555                             .add("ERROR:"
556                                     + "The parameter \"behavior\" is not defined in the node "
557                                     + tagName);
558                     return false;
559                 }
560                 String JavaDoc behaviorValue = behavior.getNodeValue();
561                 if (behaviorValue.equals("")) {
562                     errors
563                             .add("ERROR:"
564                                     + "The parameter \"behavior\" is not initialised in the node "
565                                     + tagName);
566                     return false;
567                 }
568                 Node forceStop = attributs.getNamedItem("forceStop");
569                 String JavaDoc forceValue = "";
570                 if (forceStop != null) {
571                     forceValue = forceStop.getNodeValue();
572                 }
573                 // check if the behavior id exists
574
if (treeManager.getBehaviorsIds().contains(behaviorValue)) {
575                     // generate a new id for this group
576
currentGroupId = loadProfileManager.groupIdGenerator();
577                     // Create the new group
578
GroupDescription gd = new GroupDescription(currentGroupId);
579                     gd.setBehaviorId(behaviorValue);
580                     // set the stop threads mode
581
if (forceValue.equals("false")) {
582                         gd.setForceStop(false);
583                     }
584                     // set default color
585
gd.setCurveColor(loadProfileManager.getDefaultColor());
586                     // add the group in the loadprofilemanager
587
loadProfileManager.addGroupDescription(gd);
588                 }
589                 else {
590                     errors.add("ERROR: Don't find the behavior used in this group -> " + behaviorValue) ;
591                     return false ;
592                 }
593                 // init last end point
594
lastEnd = null;
595                 currentEnd = null;
596                 currentStart = null;
597             } else if (org.objectweb.clif.scenario.util.isac.util.tree.Node.RAMP
598                     .equals(tagName)) {
599                 // if current groupId is null, we can not define ramp
600
// outside a group description
601
if (currentGroupId == null) {
602                     errors
603                             .add("WARNING : There a ramp defined outside a group description, ignore it...");
604                     return true;
605                 } else {
606                     // get the style of the ramp
607
NamedNodeMap JavaDoc attributs = node.getAttributes();
608                     // verify the attributs
609
if (attributs.getLength() < 1) {
610                         errors.add("ERROR:" + "The node \"" + tagName
611                                 + "\" must define one parameter, \"style\"");
612                         return false;
613                     }
614                     Node style = attributs.getNamedItem("style");
615                     if (style == null) {
616                         errors
617                                 .add("ERROR:"
618                                         + "The parameter \"style\" is not defined in the node "
619                                         + tagName);
620                         return false;
621                     }
622                     String JavaDoc styleValue = style.getNodeValue();
623                     // test with the defined style
624
if (styleValue.equals(RampDescription.LINE_STRING)) {
625                         String JavaDoc rampId = loadProfileManager.rampIdGenerator();
626                         currentRamp = new RampDescription(rampId,
627                                 RampDescription.LINE);
628                     } else if (styleValue.equals(RampDescription.ARC_STRING)) {
629                         String JavaDoc rampId = loadProfileManager.rampIdGenerator();
630                         currentRamp = new RampDescription(rampId,
631                                 RampDescription.ARC);
632                     } else if (styleValue
633                             .equals(RampDescription.CRENEL_VH_STRING)) {
634                         String JavaDoc rampId = loadProfileManager.rampIdGenerator();
635                         currentRamp = new RampDescription(rampId,
636                                 RampDescription.CRENEL_VH);
637                     } else if (styleValue
638                             .equals(RampDescription.CRENEL_HV_STRING)) {
639                         String JavaDoc rampId = loadProfileManager.rampIdGenerator();
640                         currentRamp = new RampDescription(rampId,
641                                 RampDescription.CRENEL_HV);
642                     }
643                     // unknow style
644
else {
645                         errors.add("WARNING : This style : " + styleValue
646                                 + ", is unknow...");
647                         return false;
648                     }
649                 }
650             } else if (org.objectweb.clif.scenario.util.isac.util.tree.Node.POINTS
651                     .equals(tagName)) {
652                 // set the current end point as the last end point because
653
// we are describing a new ramp
654
lastEnd = currentEnd;
655                 currentEnd = null;
656                 currentStart = null;
657             } else if (org.objectweb.clif.scenario.util.isac.util.tree.Node.POINT
658                     .equals(tagName)) {
659                 // check if we are in a ramp description
660
if (currentRamp == null) {
661                     errors
662                             .add("WARNING : There is a point defined outside a ramp description, ignore it...");
663                     return true;
664                 } else {
665                     // get the point
666
NamedNodeMap JavaDoc attributs = node.getAttributes();
667                     // verify the attributs
668
if (attributs.getLength() < 3) {
669                         errors
670                                 .add("ERROR:"
671                                         + "The node \""
672                                         + tagName
673                                         + "\" must define three parameters, \"name\" \"x\" \"y\"");
674                         return false;
675                     }
676                     Node name = attributs.getNamedItem("name");
677                     if (name == null) {
678                         errors
679                                 .add("ERROR:"
680                                         + "The parameter \"name\" is not defined in the node "
681                                         + tagName);
682                         return false;
683                     }
684                     String JavaDoc nameValue = name.getNodeValue();
685                     if (nameValue.equals("")) {
686                         errors
687                                 .add("ERROR:"
688                                         + "The parameter \"name\" is not initialised in the node "
689                                         + tagName);
690                         return false;
691                     }
692                     Node xNode = attributs.getNamedItem("x");
693                     if (xNode == null) {
694                         errors
695                                 .add("ERROR:"
696                                         + "The parameter \"x\" is not defined in the node "
697                                         + tagName);
698                         return false;
699                     }
700                     String JavaDoc xAxis = xNode.getNodeValue();
701                     if (xAxis.equals("")) {
702                         errors
703                                 .add("ERROR:"
704                                         + "The parameter \"x\" is not initialised in the node "
705                                         + tagName);
706                         return false;
707                     }
708                     Node yNode = attributs.getNamedItem("y");
709                     if (yNode == null) {
710                         errors
711                                 .add("ERROR:"
712                                         + "The parameter \"y\" is not defined in the node "
713                                         + tagName);
714                         return false;
715                     }
716                     String JavaDoc yAxis = yNode.getNodeValue();
717                     if (yAxis.equals("")) {
718                         errors
719                                 .add("ERROR:"
720                                         + "The parameter \"y\" is not initialised in the node "
721                                         + tagName);
722                         return false;
723                     }
724
725                     Integer JavaDoc xAxisInteger = new Integer JavaDoc(xAxis);
726                     Integer JavaDoc yAxisInteger = new Integer JavaDoc(yAxis);
727                     // analyse the point name
728
if (nameValue.equals(RampDescription.POINT_START)) {
729                         // start point
730
// verify that the start point of this ramp is
731
// the same than the last end ramp point
732
currentStart = new Point(xAxisInteger.intValue(),
733                                 yAxisInteger.intValue());
734
735                         // if we are not the starting ramp of the group
736
if (lastEnd != null) {
737                             if (!lastEnd.equals(currentStart)) {
738                                 errors
739                                         .add("ERROR : A ramp have a starting point which is not the same than the previous ramp ending point !");
740                                 return false;
741                             }
742                         }
743                         if (currentEnd != null) {
744                             // test if the starting point is before the
745
// ending point in the time scale
746
if (currentStart.x >= currentEnd.x) {
747                                 errors
748                                         .add("ERROR : The starting point is after the ending point in the time scale !");
749                                 return false;
750                             }
751                             // the starting and the ending point
752
// have been defined we can add the ramp
753
currentRamp.setStart(currentStart);
754                             currentRamp.setEnd(currentEnd);
755                             // add the ramp to the load profile
756
// manager
757
loadProfileManager.addRampDescriptionToGroup(
758                                     currentRamp, currentGroupId);
759                         }
760                     } else if (nameValue.equals(RampDescription.POINT_END)) {
761                         // end point
762
currentEnd = new Point(xAxisInteger.intValue(),
763                                 yAxisInteger.intValue());
764                         if (currentStart != null) {
765                             // test if the starting point is before the
766
// ending point in the time scale
767
if (currentStart.x >= currentEnd.x) {
768                                 errors
769                                         .add("ERROR : The starting point is after the ending point in the time scale ! ");
770                                 return true;
771                             }
772                             // the starting and the ending point have
773
// been defined we can add the ramp
774
currentRamp.setStart(currentStart);
775                             currentRamp.setEnd(currentEnd);
776                             // add the ramp to the load profile manager
777
loadProfileManager.addRampDescriptionToGroup(
778                                     currentRamp, currentGroupId);
779                         }
780
781                     } else if (nameValue.equals(RampDescription.POINT_ANGLE)) {
782                         // angle point
783
errors
784                                     .add("WARNING : The ramp is a "+currentRamp.getType()+" and does not need any angle point, so ignore it !");
785                             return true;
786 // currentRamp.setAngle(new Point(xAxisInteger
787
// .intValue(), yAxisInteger.intValue()));
788
}
789                     else {
790                         errors
791                         .add("WARNING : The ramp is a "+currentRamp.getType()+" and does not need any "+nameValue+" point, so ignore it !");
792                         return true;
793                     }
794                     // check if this point have some maximum points
795
if (xAxisInteger.intValue() > maxScale.getWidth()) {
796                         maxScale.setWidth(xAxisInteger.intValue()) ;
797                     }
798                     if (yAxisInteger.intValue() > maxScale.getHeight()) {
799                         maxScale.setHeight(yAxisInteger.intValue()) ;
800                     }
801                 }
802             }
803
804             // in this case the node is necessarily a simple node
805
else {
806                 NodeDescription desc = NodeDescription
807                         .createNonePluginNode(tagName);
808                 // search a new key
809
int keyInt = desc.hashCode();
810                 // check in the table if the key already exist
811
while (nodes.containsKey(new String JavaDoc((new Integer JavaDoc(keyInt))
812                         .toString())))
813                     keyInt++;
814                 // add the description node in the table
815
nodes.put(new String JavaDoc((new Integer JavaDoc(keyInt)).toString()), desc);
816                 // build the new node, it will be the tree root node
817
result = new ScenarioNode(new String JavaDoc((new Integer JavaDoc(keyInt))
818                         .toString()));
819             }
820             break;
821         default:
822         // do nothing
823
}
824         // update the variable which store the last result node
825
if ((result != null) && (parent != null)) {
826             // if the node is a controller child node
827
if (isControllerChild) {
828                 // if the last node controller child have been intialised
829
if (lastControllerNode != null)
830                     lastControllerNode.addChild(result);
831                 else {
832                     parent.addChild(result);
833                     parent.printTest();
834                     lastControllerNode = result;
835                 }
836             } else
837                 parent.addChild(result);
838         }
839
840         if (node.hasChildNodes()) {
841             NodeList JavaDoc fils = node.getChildNodes();
842             for (int i = 0; i < fils.getLength(); i++) {
843                 Node tempNode = fils.item(i);
844                 if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
845                     boolean continueProcess = true;
846                     if (result != null) {
847                         continueProcess = visit(result, flag, tempNode, nodes,
848                                 errors);
849                     } else {
850                         continueProcess = visit(parent, flag, tempNode, nodes,
851                                 errors);
852                     }
853                     // if we find some errors, stop the process
854
if (!continueProcess)
855                         return false;
856                 }
857             }
858         }
859         // if the tagname is initialised
860
if (tagName != null)
861             // and if we was in a controller node
862
if (org.objectweb.clif.scenario.util.isac.util.tree.Node
863                     .isControllerNode(tagName)) {
864                 lastControllerNode = null;
865             }
866
867         return true;
868     }
869
870     /**
871      * Load a behavior file
872      *
873      * @param fileName
874      * The name of the file to be loaded
875      * @param nodes
876      * The hashtable containing the nodes description, will be
877      * updated during the analyse
878      * @return The tree loaded in the file
879      */

880     public static ScenarioNode loadScenario(String JavaDoc fileName, Hashtable JavaDoc nodes,
881             Vector JavaDoc errors) {
882         Document JavaDoc document = null;
883         try {
884             InputSource JavaDoc is = new InputSource JavaDoc(new FileInputStream JavaDoc(fileName));
885             DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory
886                     .newInstance();
887             DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
888             document = builder.parse(is);
889         } catch (Exception JavaDoc e) {
890             errors.add(e.toString());
891             return null;
892         }
893         // analyse the dom tree
894
if (visit(null, false, document, nodes, errors)) {
895             // if the max scale has not been initialized don't set it
896
if (maxScale.getWidth() != 0 && maxScale.getHeight() != 0) {
897                 // change the scale of the drawable part
898
loadProfileManager.setScale(maxScale) ;
899             }
900             return tree;
901         }
902         else
903             return null;
904     }
905 }
Popular Tags