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(tagN