| 1 23 24 package org.objectweb.clif.scenario.util.isac.util.tree; 25 26 import java.io.PrintStream ; 27 import java.util.Enumeration ; 28 import java.util.Hashtable ; 29 import java.util.Iterator ; 30 import java.util.Set ; 31 import java.util.Vector ; 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 45 public class SaveScenario { 46 static Category cat = Category.getInstance(TreeManager.class.getName()); 47 48 60 public static String printAttribut(ScenarioNode node, String attribut, 61 Vector 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 ) 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 85 public static String printPlugin(ScenarioNode node, Vector 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 105 public static String printActionName(ScenarioNode node, Vector 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 128 public static void printParams(ScenarioNode node, PrintStream out, int ind, 129 Vector errors) { 130 NodeDescription nodeDescription = TreeManager.getTreeManager(null) 131 .getNodeDescription(node); 132 Hashtable params = nodeDescription.getParams(); 133 if (params != null) { 134 if (params.size() > 1) { 135 Tools.printXMLLine(out, "<" + Node.PARAMS + ">", ind); 136 Set parametersNames = params.keySet(); 137 Iterator iterator = parametersNames.iterator(); 138 while (iterator.hasNext()) { 139 Object 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 ) params.get(temp) + "\" />", ind + 2); 152 } 153 } 154 Tools.printXMLLine(out, "</" + Node.PARAMS + ">", ind); 155 } 156 } 157 } 158 159 169 public static void printNode(ScenarioNode node, PrintStream out, int ind, 170 Vector errors) { 171 String nodeType = TreeManager.getTreeManager(null).getNodeType(node); 172 NodeDescription nodeDescription = TreeManager.getTreeManager(null) 173 .getNodeDescription(node); 174 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 (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 312 public static void printNodeChildren(ScenarioNode node, PrintStream out, 313 int ind, Vector errors) { 314 NodeDescription nodeDescription = TreeManager.getTreeManager(null) 315 .getNodeDescription(node); 316 317 Vector children = node.getChildren(); 318 for (int i = 0; i < children.size(); i++) { 319 printNode((ScenarioNode) children.elementAt(i), out, ind, errors); 320 325 } 326 327 } 328 329 340 public static void saveBehaviors(ScenarioNode node, PrintStream out, 341 int ind, Vector errors) { 342 printNodeChildren(node, out, ind, errors); 343 } 344 345 351 public static void printPoints(RampDescription rampDescription, 352 PrintStream out, int ind) { 353 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 385 public static void saveLoadProfile(ScenarioNode node, PrintStream out, 386 int ind, Vector errors) { 387 LoadProfileManager loadProfileManager = LoadProfileManager 388 .getInstance(); 389 Enumeration groups = loadProfileManager.getElements(); 390 while (groups.hasMoreElements()) { 391 GroupDescription groupDescription = ((GroupDescription) groups 392 .nextElement()); 393 Enumeration 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 |