KickJava   Java API By Example, From Geeks To Geeks.

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


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
24 package org.objectweb.clif.scenario.util.isac.util.tree;
25
26 import java.io.PrintStream JavaDoc;
27 import java.util.Enumeration JavaDoc;
28 import java.util.Hashtable JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Set JavaDoc;
31 import java.util.Vector JavaDoc;
32
33 import org.apache.log4j.Category;
34 import org.objectweb.clif.scenario.util.isac.loadprofile.GroupDescription;
35 import org.objectweb.clif.scenario.util.isac.loadprofile.LoadProfileManager;
36 import org.objectweb.clif.scenario.util.isac.loadprofile.RampDescription;
37 import org.objectweb.clif.scenario.util.isac.util.xml.Tools;
38
39 /**
40  * @author JC Meillaud
41  * @author A Peyrard
42  *
43  *
44  */

45 public class SaveScenario {
46     static Category cat = Category.getInstance(TreeManager.class.getName());
47
48     /**
49      * Method to print the value of a certain attribut (attribut) or add a
50      * warning in the errors vector
51      *
52      * @param node
53      * a scenario node
54      * @param attribut
55      * the name of the attribut we are looking for
56      * @param errors
57      * the errors vector
58      * @return The value of the attibut of this node
59      */

60     public static String JavaDoc printAttribut(ScenarioNode node, String JavaDoc attribut,
61             Vector JavaDoc errors) {
62         NodeDescription nodeDescription = TreeManager.getTreeManager(null)
63                 .getNodeDescription(node);
64         if (nodeDescription.getParams().get(attribut) != null
65                 && nodeDescription.getParams().get(attribut) != "") {
66             return (String JavaDoc) nodeDescription.getParams().get(attribut);
67         } else {
68             errors.add("WARNING : the parameter " + attribut
69                     + " is undefined for the node "
70                     + TreeManager.getTreeManager(null).getNodeType(node));
71             return "";
72
73         }
74     }
75
76     /**
77      * Print the plugin name or add a warning in the errors vector
78      *
79      * @param node
80      * a scenario node
81      * @param errors
82      * the errors vector
83      * @return The value of the plugin of this node
84      */

85     public static String JavaDoc printPlugin(ScenarioNode node, Vector JavaDoc errors) {
86         NodeDescription nodeDescription = TreeManager.getTreeManager(null)
87                 .getNodeDescription(node);
88         if (nodeDescription.getPlugin() == null) {
89             errors.add("WARNING : the plugin name is undefined for the node "
90                     + TreeManager.getTreeManager(null).getNodeType(node));
91             return "";
92         } else {
93             return nodeDescription.getPlugin();
94
95         }
96     }
97
98     /**
99      * Print the action name or add a warning in the errors vector
100      *
101      * @param node
102      * @param errors
103      * @return The string to be printed
104      */

105     public static String JavaDoc printActionName(ScenarioNode node, Vector JavaDoc errors) {
106         NodeDescription nodeDescription = TreeManager.getTreeManager(null)
107                 .getNodeDescription(node);
108         if (nodeDescription.getActionName() == null) {
109             errors.add("WARNING : the action name is undefined for the node "
110                     + TreeManager.getTreeManager(null).getNodeType(node));
111             return "";
112         } else {
113             return nodeDescription.getActionName();
114
115         }
116     }
117
118     /**
119      * Print the parameters of a node
120      *
121      * @param node
122      * a scenario node
123      * @param out
124      * the stream where we print
125      * @param ind
126      * Number of indentation
127      */

128     public static void printParams(ScenarioNode node, PrintStream JavaDoc out, int ind,
129             Vector JavaDoc errors) {
130         NodeDescription nodeDescription = TreeManager.getTreeManager(null)
131                 .getNodeDescription(node);
132         Hashtable JavaDoc params = nodeDescription.getParams();
133         if (params != null) {
134             if (params.size() > 1) {
135                 Tools.printXMLLine(out, "<" + Node.PARAMS + ">", ind);
136                 Set JavaDoc parametersNames = params.keySet();
137                 Iterator JavaDoc iterator = parametersNames.iterator();
138                 while (iterator.hasNext()) {
139                     Object JavaDoc temp = iterator.next();
140                     if (!temp.equals("id")) {
141                         if (params.get(temp) == null
142                                 || params.get(temp).equals("")) {
143                             errors.add("WARNING:the value for the parameter "
144                                     + temp
145                                     + " is undefined for the node "
146                                     + TreeManager.getTreeManager(null)
147                                             .getNodeType(node));
148                         }
149                         Tools.printXMLLine(out, "<" + Node.PARAM + " name=\""
150                                 + temp + "\" value=\""
151                                 + (String JavaDoc) params.get(temp) + "\" />", ind + 2);
152                     }
153                 }
154                 Tools.printXMLLine(out, "</" + Node.PARAMS + ">", ind);
155             }
156         }
157     }
158
159     /**
160      * Print a node
161      *
162      * @param node
163      * a scenario node
164      * @param out
165      * the stream where we print
166      * @param ind
167      * Number of indentation
168      */

169     public static void printNode(ScenarioNode node, PrintStream JavaDoc out, int ind,
170             Vector JavaDoc errors) {
171         String JavaDoc nodeType = TreeManager.getTreeManager(null).getNodeType(node);
172         NodeDescription nodeDescription = TreeManager.getTreeManager(null)
173                 .getNodeDescription(node);
174         //if (!node.getChildren().isEmpty()) {
175
if (nodeType.equals(Node.BEHAVIORS)) {
176             Tools.printXMLLine(out, "<" + Node.BEHAVIORS + ">", ind);
177             if (!(node.getChildren()).isEmpty()) {
178                 printNodeChildren(node, out, ind + 2, errors);
179             }
180             Tools.printXMLLine(out, "</" + Node.BEHAVIORS + ">", ind);
181         } else if (nodeType.equals(Node.BEHAVIOR)) {
182             Tools.printXMLLine(out, "<" + Node.BEHAVIOR + " id=\""
183                     + printAttribut(node, "id", errors) + "\">", ind);
184             if (!(node.getChildren()).isEmpty()) {
185                 printNodeChildren(node, out, ind + 2, errors);
186             }
187             Tools.printXMLLine(out, "</" + Node.BEHAVIOR + ">", ind);
188         } else if (nodeType.equals(Node.PLUGINS)) {
189             Tools.printXMLLine(out, "<" + Node.PLUGINS + ">", ind);
190             if (!(node.getChildren()).isEmpty()) {
191                 printNodeChildren(node, out, ind + 2, errors);
192             }
193             Tools.printXMLLine(out, "</" + Node.PLUGINS + ">", ind);
194         } else if (nodeType.equals(Node.USE)) {
195             //if (node.getChildren() != null) {
196
if (nodeDescription.getParams().size() > 1) {
197                 Tools.printXMLLine(out, "<" + Node.USE + " id=\""
198                         + printAttribut(node, "id", errors) + "\" name=\""
199                         + printPlugin(node, errors) + "\">", ind);
200                 printParams(node, out, ind + 2, errors);
201                 printNodeChildren(node, out, ind + 2, errors);
202                 Tools.printXMLLine(out, "</" + Node.USE + ">", ind);
203             } else {
204                 Tools.printXMLLine(out, "<" + Node.USE + " id=\""
205                         + printAttribut(node, "id", errors) + "\" name=\""
206                         + printPlugin(node, errors) + "\"/>", ind);
207
208             }
209         } else if (nodeType.equals(Node.TIMER)
210                 || (nodeType.equals(Node.SAMPLE))) {
211             if (nodeDescription.getParams().size() > 0) {
212                 Tools.printXMLLine(out, "<" + nodeType + " use=\""
213                         + printAttribut(node, "id", errors) + "\" name=\""
214                         + nodeDescription.getActionName() + "\" >", ind);
215                 printParams(node, out, ind + 2, errors);
216                 printNodeChildren(node, out, ind + 2, errors);
217                 Tools.printXMLLine(out, "</" + nodeType + ">", ind);
218             } else {
219                 Tools.printXMLLine(out, "<" + nodeType + " use=\""
220                         + printAttribut(node, "id", errors) + "\" name=\""
221                         + nodeDescription.getActionName() + "\" />", ind);
222             }
223         } else if (nodeType.equals(Node.THEN)) {
224             Tools.printXMLLine(out, "<" + Node.THEN + ">", ind);
225             if (!(node.getChildren()).isEmpty()) {
226
227                 printNodeChildren(node, out, ind + 2, errors);
228             }
229             Tools.printXMLLine(out, "</" + Node.THEN + ">", ind);
230
231         } else if (nodeType.equals(Node.ELSE)) {
232             Tools.printXMLLine(out, "<" + Node.ELSE + ">", ind);
233             if (!(node.getChildren()).isEmpty()) {
234                 printNodeChildren(node, out, ind + 2, errors);
235             }
236             Tools.printXMLLine(out, "</" + Node.ELSE + ">", ind);
237
238         } else if (nodeType.equals(Node.CHOICE)) {
239             Tools.printXMLLine(out, "<" + Node.CHOICE + " proba=\""
240                     + printAttribut(node, "proba", errors) + "\">", ind);
241             if (!(node.getChildren()).isEmpty()) {
242                 printNodeChildren(node, out, ind + 2, errors);
243             }
244             Tools.printXMLLine(out, "</" + Node.CHOICE + ">", ind);
245         } else if (nodeType.equals(Node.NCHOICE)) {
246             if (!(node.getChildren()).isEmpty()) {
247                 Tools.printXMLLine(out, "<" + Node.NCHOICE + " n=\""
248                         + node.getChildren().size() + "\">", ind);
249                 printNodeChildren(node, out, ind + 2, errors);
250                 Tools.printXMLLine(out, "</" + Node.NCHOICE + ">", ind);
251             } else {
252                 Tools.printXMLLine(out, "<" + Node.NCHOICE + " n=\""
253                         + node.getChildren().size() + "\"/>", ind);
254             }
255         } else if (Node.isControllerNode(nodeType)) {
256             Tools.printXMLLine(out, "<" + nodeType + ">", ind);
257             if (!nodeDescription.getParams().isEmpty()) {
258                 if (nodeDescription.getParams().size() > 1) {
259                     Tools.printXMLLine(out, "<" + Node.CONDITION + " use=\""
260                             + printAttribut(node, "id", errors) + "\" name=\""
261                             + printActionName(node, errors) + "\" >", ind + 2);
262                     printParams(node, out, ind + 4, errors);
263                     Tools.printXMLLine(out, "</" + Node.CONDITION + ">",
264                             ind + 2);
265                 } else {
266                     Tools.printXMLLine(out, "<" + Node.CONDITION + " use=\""
267                             + printAttribut(node, "id", errors) + "\" name=\""
268                             + printActionName(node, errors) + "\" />", ind + 2);
269                 }
270
271             } else {
272                 Tools.printXMLLine(out, "<" + Node.CONDITION
273                         + " use=\"\" name=\"\" />", ind + 2);
274             }
275             if (!(node.getChildren()).isEmpty()) {
276                 printNodeChildren(node, out, ind + 2, errors);
277             }
278             Tools.printXMLLine(out, "</" + nodeType + ">", ind);
279         } else if (nodeType.equals(Node.GROUP)) {
280             if (!(node.getChildren()).isEmpty()) {
281                 Tools.printXMLLine(out, "<" + Node.GROUP + " n=\""
282                         + node.getChildren().size() + "\">", ind);
283                 printNodeChildren(node, out, ind + 2, errors);
284                 Tools.printXMLLine(out, "</" + Node.GROUP + ">", ind);
285             } else {
286                 Tools.printXMLLine(out, "<" + Node.GROUP + "/>", ind);
287             }
288         } else if (nodeType.equals(Node.RAMP)) {
289             if (!(node.getChildren()).isEmpty()) {
290                 Tools.printXMLLine(out, "<" + Node.RAMP + " n=\""
291                         + node.getChildren().size() + "\">", ind);
292                 printNodeChildren(node, out, ind + 2, errors);
293                 Tools.printXMLLine(out, "</" + Node.RAMP + ">", ind);
294             } else {
295                 Tools.printXMLLine(out, "<" + Node.RAMP + " n=\""
296                         + node.getChildren().size() + "\"/>", ind);
297             }
298         }
299
300     }
301
302     /**
303      * Print a node and his children
304      *
305      * @param node
306      * a scenario node
307      * @param out
308      * the stream where we print
309      * @param ind
310      * Number of indentation
311      */

312     public static void printNodeChildren(ScenarioNode node, PrintStream JavaDoc out,
313             int ind, Vector JavaDoc errors) {
314         NodeDescription nodeDescription = TreeManager.getTreeManager(null)
315                 .getNodeDescription(node);
316
317         Vector JavaDoc children = node.getChildren();
318         for (int i = 0; i < children.size(); i++) {
319             printNode((ScenarioNode) children.elementAt(i), out, ind, errors);
320             // cat.debug("node : " + nodeDescription.getType());
321
// cat.debug("sousnode : "
322
// + TreeManager.getTreeManager(null).getNodeDescription(
323
// (ScenarioNode) children.elementAt(i)).getType());
324

325         }
326
327     }
328
329     /**
330      * Print the behaviors section
331      *
332      * @param node
333      * a scenario node
334      * @param out
335      * the stream where we print
336      * @param ind
337      * Number of indentation
338      *
339      */

340     public static void saveBehaviors(ScenarioNode node, PrintStream JavaDoc out,
341             int ind, Vector JavaDoc errors) {
342         printNodeChildren(node, out, ind, errors);
343     }
344
345     /**
346      *
347      * @param rampDescription
348      * @param out
349      * @param ind
350      */

351     public static void printPoints(RampDescription rampDescription,
352             PrintStream JavaDoc out, int ind) {
353         //if ()
354
Tools.printXMLLine(out, "<" + Node.POINTS + ">", ind);
355         if (!rampDescription.getStart().equals(RampDescription.UNKNOWNPOINT)) {
356             Tools.printXMLLine(out, "<" + Node.POINT + " name=\"start\" x=\""
357                     + rampDescription.getStart().x + "\" y=\""
358                     + rampDescription.getStart().y + "\"/>", ind + 2);
359         }
360         if (!rampDescription.getEnd().equals(RampDescription.UNKNOWNPOINT)) {
361             Tools.printXMLLine(out, "<" + Node.POINT + " name=\"end\" x=\""
362                     + rampDescription.getEnd().x + "\" y=\""
363                     + rampDescription.getEnd().y + "\"/>", ind + 2);
364         }
365         if (!rampDescription.getAngle().equals(RampDescription.UNKNOWNPOINT)) {
366             Tools.printXMLLine(out, "<" + Node.POINT + " name=\"angle\" x=\""
367                     + rampDescription.getAngle().x + "\" y=\""
368                     + rampDescription.getAngle().y + "\"/>", ind + 2);
369         }
370         Tools.printXMLLine(out, "</" + Node.POINTS + ">", ind);
371
372     }
373
374     /**
375      * Print the behaviors section
376      *
377      * @param node
378      * a scenario node
379      * @param out
380      * the stream where we print
381      * @param ind
382      * Number of indentation
383      *
384      */

385     public static void saveLoadProfile(ScenarioNode node, PrintStream JavaDoc out,
386             int ind, Vector JavaDoc errors) {
387         LoadProfileManager loadProfileManager = LoadProfileManager
388                 .getInstance();
389         Enumeration JavaDoc groups = loadProfileManager.getElements();
390         while (groups.hasMoreElements()) {
391             GroupDescription groupDescription = ((GroupDescription) groups
392                     .nextElement());
393             Enumeration JavaDoc ramps = groupDescription.getElements();
394             Tools.printXMLLine(out, "<" + Node.GROUP + " behavior=\""
395                     + groupDescription.getBehaviorId() + "\" forceStop=\""
396                     + groupDescription.isForceStop() + "\">", ind);
397             while (ramps.hasMoreElements()) {
398                 RampDescription rampDescription = (RampDescription) ramps
399                         .nextElement();
400                 switch (rampDescription.getType()) {
401                 case RampDescription.LINE:
402
403                     Tools.printXMLLine(out, "<" + Node.RAMP
404                             + " style=\"line\">", ind + 2);
405                     printPoints(rampDescription, out, ind + 4);
406                     Tools.printXMLLine(out, "</" + Node.RAMP + ">", ind + 2);
407                     break;
408                 case RampDescription.ARC:
409                     Tools.printXMLLine(out,
410                             "<" + Node.RAMP + " style=\"arc\">", ind + 2);
411                     printPoints(rampDescription, out, ind + 4);
412                     Tools.printXMLLine(out, "</" + Node.RAMP + ">", ind + 2);
413                     break;
414                 case RampDescription.CRENEL_HV:
415                     Tools
416                             .printXMLLine(out, "<" + Node.RAMP + " style=\""
417                                     + RampDescription.CRENEL_HV_STRING + "\">",
418                                     ind + 2);
419                     printPoints(rampDescription, out, ind + 4);
420                     Tools.printXMLLine(out, "</" + Node.RAMP + ">", ind + 2);
421                     break;
422                 case RampDescription.CRENEL_VH:
423                     Tools
424                             .printXMLLine(out, "<" + Node.RAMP + " style=\""
425                                     + RampDescription.CRENEL_VH_STRING + "\">",
426                                     ind + 2);
427                     printPoints(rampDescription, out, ind + 4);
428                     Tools.printXMLLine(out, "</" + Node.RAMP + ">", ind + 2);
429                     break;
430                 default:
431                     cat.debug("Unknown RampDescription Style");
432                     break;
433                 }
434             }
435             Tools.printXMLLine(out, "</" + Node.GROUP + ">", ind);
436         }
437     }
438 }
Popular Tags