1 23 24 package org.objectweb.clif.scenario.util.isac.plugin.gui; 25 26 import java.util.Enumeration ; 27 import java.util.Hashtable ; 28 import java.util.Vector ; 29 30 import javax.xml.parsers.DocumentBuilder ; 31 import javax.xml.parsers.DocumentBuilderFactory ; 32 33 import org.apache.log4j.Category; 34 import org.objectweb.clif.scenario.util.isac.plugin.ObjectDescription; 35 import org.objectweb.clif.scenario.util.isac.plugin.ParameterDescription; 36 import org.objectweb.clif.scenario.util.isac.plugin.PluginDescription; 37 import org.objectweb.clif.scenario.util.isac.plugin.SampleDescription; 38 import org.objectweb.clif.scenario.util.isac.plugin.TestDescription; 39 import org.objectweb.clif.scenario.util.isac.plugin.TimerDescription; 40 import org.w3c.dom.Document ; 41 import org.w3c.dom.Node ; 42 import org.w3c.dom.NodeList ; 43 44 48 public class GUIDescriptionParser { 49 static Category cat = Category.getInstance(GUIDescriptionParser.class 50 .getName()); 51 52 56 private static final String OBJECT = "object"; 57 private static final String TEST = org.objectweb.clif.scenario.util.isac.util.tree.Node.TEST; 58 private static final String SAMPLE = org.objectweb.clif.scenario.util.isac.util.tree.Node.SAMPLE; 59 private static final String TIMER = org.objectweb.clif.scenario.util.isac.util.tree.Node.TIMER; 60 private static final String GROUP = "group"; 61 private static final String NAME = "name"; 62 private static final String PARAM = "param"; 63 private static final String CHOICE = "choice"; 64 private static final String SELECTED = "selected"; 65 private static final String ID = "id"; 66 private static final String CHOICES = "choices"; 67 private static final String TEXT = "text"; 68 private static final String RADIOBUTTON = "radiobutton"; 69 private static final String CHECKBOX = "checkbox"; 70 private static final String COMBO = "combo"; 71 private static final String VALUE = "value"; 72 private static final String DEFAULT = "default"; 73 private static final String FIELD = "field"; 74 private static final String NFIELD = "nfield"; 75 private static final String TABLE = "table"; 76 private static final String COLS = "cols"; 77 private static final String TRUE = "true"; 78 private static final String SIZE = "size"; 79 private static final String PLEASE_EDIT = "please edit this field..."; 80 private static final String LABEL = "label"; 81 82 96 public static Vector loadGUIDescriptionFile(String fileName, 97 PluginDescription plugin, Hashtable panels) { 98 Vector result = null; 99 Document document = null; 100 DocumentBuilderFactory factory = null; 101 Vector parametersNames = new Vector (); 102 int intKey = 0; 103 cat.debug("Load a gui.xml file -> " + fileName); 104 105 try { 106 factory = DocumentBuilderFactory.newInstance(); 107 DocumentBuilder builder = factory.newDocumentBuilder(); 108 document = builder.parse(fileName); 109 } catch (Exception e) { 110 e.printStackTrace(); 111 } 112 Vector nodesName = new Vector (); 113 nodesName.add(OBJECT); 114 nodesName.add(SAMPLE); 115 nodesName.add(TEST); 116 nodesName.add(TIMER); 117 for (int i = 0; i < nodesName.size(); i++) { 118 cat.debug("Load a " + nodesName.get(i) + " gui description"); 119 ParametersWidgetsNode parametersWidgetsNode = null; 120 if (nodesName.get(i).equals(OBJECT)) { 121 ObjectDescription obj = plugin.getObject(); 122 Vector descriptionParams = obj.getParams(); 123 for (int j = 0; j < descriptionParams.size(); ++j) { 124 parametersNames 125 .addElement(((ParameterDescription) descriptionParams 126 .get(j)).getName()); 127 } 128 parametersWidgetsNode = visitDOM(document, nodesName.get(i) 129 .toString(), parametersNames); 130 if (parametersWidgetsNode != null) { 131 intKey = parametersWidgetsNode.hashCode(); 132 while (panels.containsKey(new String ((new Integer (intKey)) 133 .toString()))) 134 intKey++; 135 panels.put((new Integer (intKey)).toString(), 136 parametersWidgetsNode); 137 obj.setGUIKey(new String ((Integer.toString(intKey)))); 138 } else { 139 parametersWidgetsNode = ParametersWidgetsNode 140 .createParametersWidgetsNode( 141 descriptionParams.elements(), 142 org.objectweb.clif.scenario.util.isac.util.tree.Node.USE, 143 plugin.getName()); 144 intKey = parametersWidgetsNode.hashCode(); 145 while (panels.containsKey(new String ((new Integer (intKey)) 146 .toString()))) 147 intKey++; 148 panels.put((new Integer (intKey)).toString(), 149 parametersWidgetsNode); 150 obj.setGUIKey(new String ((Integer.toString(intKey)))); 151 } 152 } else if (nodesName.get(i).equals(TIMER)) { 153 Hashtable timers = plugin.getTimers(); 154 Hashtable remaindingTimers = (Hashtable ) timers.clone(); 155 Hashtable hashtablePWN = new Hashtable (); 156 visitDOM2(hashtablePWN, document, nodesName.get(i).toString(), 157 timers); 158 if (hashtablePWN.size() != 0) { 159 Enumeration keys = hashtablePWN.keys(); 160 cat.debug("Printing of the Timers :" + hashtablePWN.size() 161 + " timers"); 162 while (keys.hasMoreElements()) { 163 String key = (String ) keys.nextElement(); 164 cat.debug("Timer : " + key); 165 cat 166 .debug((ParametersWidgetsNode) hashtablePWN 167 .get(key)); 168 if (timers.containsKey(key)) { 169 TimerDescription temp = (TimerDescription) timers 170 .get(key); 171 intKey = hashtablePWN.get(key).hashCode(); 172 while (panels.containsKey(new String ((new Integer ( 173 intKey)).toString()))) 174 intKey++; 175 temp.setGUIKey(new Integer (intKey).toString()); 176 panels.put((new Integer (intKey)).toString(), 177 hashtablePWN.get(key)); 178 remaindingTimers.remove(key); 179 } 180 } 181 } 182 if (remaindingTimers.size() != 0) { 183 Enumeration timersKeys = remaindingTimers.keys(); 184 while (timersKeys.hasMoreElements()) { 185 String timerKey = (String ) timersKeys.nextElement(); 186 TimerDescription timer = (TimerDescription) remaindingTimers 187 .get(timerKey); 188 Vector params = timer.getParams(); 189 if (params != null) { 190 ParametersWidgetsNode tree = ParametersWidgetsNode 191 .createParametersWidgetsNode(params 192 .elements(), TIMER, plugin 193 .getName()); 194 intKey = tree.hashCode(); 195 while (panels.containsKey(new String ((new Integer ( 196 intKey)).toString()))) 197 intKey++; 198 panels.put(new String ((new Integer (intKey)) 199 .toString()), tree); 200 timer.setGUIKey(new String ((new Integer (intKey)) 201 .toString())); 202 } else { 203 ParametersWidgetsNode tree = ParametersWidgetsNode 204 .createParametersWidgetsNode(null, TIMER, 205 plugin.getName()); 206 intKey = tree.hashCode(); 207 while (panels.containsKey(new String ((new Integer ( 208 intKey)).toString()))) 209 intKey++; 210 panels.put(new String ((new Integer (intKey)) 211 .toString()), tree); 212 timer.setGUIKey(new String ((new Integer (intKey)) 213 .toString())); 214 } 215 } 216 } 217 218 } else if (nodesName.get(i).equals(TEST)) { 219 Hashtable tests = plugin.getTests(); 220 Hashtable remaindingTests = (Hashtable ) tests.clone(); 221 Hashtable hashtablePWN = new Hashtable (); 222 visitDOM2(hashtablePWN, document, nodesName.get(i).toString(), 223 tests); 224 if (hashtablePWN.size() != 0) { 225 Enumeration keys = hashtablePWN.keys(); 226 cat.debug("Printing of the tests :" + hashtablePWN.size() 227 + " tests"); 228 while (keys.hasMoreElements()) { 229 String key = (String ) keys.nextElement(); 230 cat.debug("Test : " + key); 231 cat 232 .debug((ParametersWidgetsNode) hashtablePWN 233 .get(key)); 234 if (tests.containsKey(key)) { 235 TestDescription temp = (TestDescription) tests 236 .get(key); 237 intKey = hashtablePWN.get(key).hashCode(); 238 while (panels.containsKey(new String ((new Integer ( 239 intKey)).toString()))) 240 intKey++; 241 temp.setGUIKey(new Integer (intKey).toString()); 242 panels.put((new Integer (intKey)).toString(), 243 hashtablePWN.get(key)); 244 remaindingTests.remove(key); 245 } 246 } 247 } 248 if (remaindingTests.size() != 0) { 249 Enumeration testsKeys = remaindingTests.keys(); 250 while (testsKeys.hasMoreElements()) { 251 String testKey = (String ) testsKeys.nextElement(); 252 TestDescription test = (TestDescription) remaindingTests 253 .get(testKey); 254 Vector params = test.getParams(); 255 if (params != null) { 256 ParametersWidgetsNode tree = ParametersWidgetsNode 257 .createParametersWidgetsNode(params 258 .elements(), TEST, plugin.getName()); 259 intKey = tree.hashCode(); 260 while (panels.containsKey(new String ((new Integer ( 261 intKey)).toString()))) 262 intKey++; 263 panels.put(new String ((new Integer (intKey)) 264 .toString()), tree); 265 test.setGUIKey(new String ((new Integer (intKey)) 266 .toString())); 267 } else { 268 ParametersWidgetsNode tree = ParametersWidgetsNode 269 .createParametersWidgetsNode(null, TEST, 270 plugin.getName()); 271 intKey = tree.hashCode(); 272 while (panels.containsKey(new String ((new Integer ( 273 intKey)).toString()))) 274 intKey++; 275 panels.put(new String ((new Integer (intKey)) 276 .toString()), tree); 277 test.setGUIKey(new String ((new Integer (intKey)) 278 .toString())); 279 } 280 } 281 } 282 283 } else if (nodesName.get(i).equals(SAMPLE)) { 284 Hashtable samples = plugin.getSamples(); 285 Hashtable remaindingSamples = (Hashtable ) samples.clone(); 286 Hashtable hashtablePWN = new Hashtable (); 287 visitDOM2(hashtablePWN, document, nodesName.get(i).toString(), 288 samples); 289 if (hashtablePWN.size() != 0) { 290 Enumeration keys = hashtablePWN.keys(); 291 cat.debug("Printing of the Samples : " 292 + hashtablePWN.size() + " samples"); 293 while (keys.hasMoreElements()) { 294 String key = (String ) keys.nextElement(); 295 cat.debug("Sample : " + key); 296 if (samples.containsKey(key)) { 297 SampleDescription temp = (SampleDescription) samples 298 .get(key); 299 intKey = hashtablePWN.get(key).hashCode(); 300 while (panels.containsKey(new String ((new Integer ( 301 intKey)).toString()))) 302 intKey++; 303 temp.setGUIKey(new Integer (intKey).toString()); 304 panels.put((new Integer (intKey)).toString(), 305 hashtablePWN.get(key)); 306 remaindingSamples.remove(key); 307 } 308 } 309 310 } 311 if (remaindingSamples.size() != 0) { 312 Enumeration samplesKeys = remaindingSamples.keys(); 313 while (samplesKeys.hasMoreElements()) { 314 String sampleKey = (String ) samplesKeys.nextElement(); 315 SampleDescription sample = (SampleDescription) remaindingSamples 316 .get(sampleKey); 317 Vector params = sample.getParams(); 318 if (params != null) { 319 ParametersWidgetsNode tree = ParametersWidgetsNode 320 .createParametersWidgetsNode(params 321 .elements(), SAMPLE, plugin 322 .getName()); 323 intKey = tree.hashCode(); 324 while (panels.containsKey(new String ((new Integer ( 325 intKey)).toString()))) 326 intKey++; 327 panels.put(new String ((new Integer (intKey)) 328 .toString()), tree); 329 sample.setGUIKey(new String ((new Integer (intKey)) 330 .toString())); 331 } else { 332 ParametersWidgetsNode tree = ParametersWidgetsNode 333 .createParametersWidgetsNode(null, SAMPLE, 334 plugin.getName()); 335 intKey = tree.hashCode(); 336 while (panels.containsKey(new String ((new Integer ( 337 intKey)).toString()))) 338 intKey++; 339 panels.put(new String ((new Integer (intKey)) 340 .toString()), tree); 341 sample.setGUIKey(new String ((new Integer (intKey)) 342 .toString())); 343 } 344 } 345 } 346 } 347 } 348 return result; 349 } 350 359 private static Object returnAttributValue(Node node, String attributName) { 360 String result = null; 361 if (node.getAttributes() != null) 362 for (int i = 0; i < node.getAttributes().getLength(); i++) { 363 if (node.getAttributes().item(i).getNodeName().equals( 364 attributName)) { 365 result = node.getAttributes().item(i).getNodeValue(); 366 } 367 } 368 return result; 369 370 } 371 381 private static Vector returnParameters(Node node, String tagName, 382 String attributName) { 383 cat.info("[BEGIN] returnParameters : "); 384 Vector parametersNames = new Vector (); 385 if (node.hasChildNodes()) { 386 NodeList fils = node.getChildNodes(); 387 for (int i = 0; i < fils.getLength(); i++) { 388 if (fils.item(i).getNodeName().equals(tagName)) { 389 if (returnAttributValue(fils.item(i), attributName) != null) { 390 parametersNames.add(returnAttributValue(fils.item(i), 391 attributName)); 392 } 393 } 394 } 395 } 396 cat.info("[END] returnParameters for (" + tagName + "): " 397 + parametersNames); 398 return parametersNames; 399 } 400 409 private static Vector returnSelectedParameters(Node node, String tagName, 410 String attributName) { 411 cat.info("[BEGIN] returnSelectedParameters (" + attributName + "): "); 412 Vector parametersNames = new Vector (); 413 if (node.hasChildNodes()) { 414 NodeList fils = node.getChildNodes(); 415 for (int i = 0; i < fils.getLength(); i++) { 416 if (fils.item(i).getNodeName().equals(tagName)) { 419 String attribut = (String ) returnAttributValue( 420 fils.item(i), attributName); 421 if (attribut != null && attribut.equals(TRUE)) { 422 parametersNames.add(returnAttributValue(fils.item(i), 423 VALUE)); 424 } 425 } 426 } 427 } 428 cat.info("[END] returnSelectedParameters for (" + tagName + "): " 429 + parametersNames); 430 return parametersNames; 431 } 432 433 443 public static void completeParametersWidgetDescription( 444 ParametersWidgetsNode parent, Vector parametersNames) { 445 cat.debug("taille :" + parametersNames); 446 for (int i = 0; i < parametersNames.size(); i++) { 447 if (!parametersNames.get(i).equals(ID)) { 448 ParametersWidgetsNode result = new ParametersWidgetsNode( 449 new WidgetDescription(WidgetDescription.TEXT_FIELD, 450 parametersNames.get(i).toString(), null, null)); 451 parent.addChild(result); 453 } 454 } 455 cat.debug("taille :" + parametersNames.size()); 456 457 } 458 459 468 private static void returnParametersWidgetDescription( 469 ParametersWidgetsNode parent, Node node, String parentName, 470 String labelValue, Vector parametersNames) { 471 ParametersWidgetsNode result = null; 472 String label = labelValue; 473 boolean isGroup = false; 475 boolean isFinalNode = false; 478 Hashtable params = new Hashtable (); 479 String tagName = node.getNodeName(); 481 482 if (tagName.equals(GROUP)) { 484 parentName = (String ) returnAttributValue(node, NAME); 485 isGroup = true; 486 result = new ParametersWidgetsNode(new WidgetDescription( 487 WidgetDescription.GROUP, parentName, label, params)); 488 result.setParent(parent); 489 parent.addChild(result); 490 if (node.hasChildNodes()) { 491 NodeList fils = node.getChildNodes(); 492 for (int i = 0; i < fils.getLength(); i++) { 493 returnParametersWidgetDescription(result, fils.item(i), 494 parentName, label, parametersNames); 495 } 496 } 497 } else if (tagName.equals(PARAM)) { 498 parentName = (String ) returnAttributValue(node, NAME); 499 String labelTemp = (String ) returnAttributValue(node, LABEL); 500 if (labelTemp != null) { 502 label = labelTemp; 503 } 504 if (parametersNames.contains(parentName)) { 505 parametersNames.remove(parentName); 506 cat.debug("GUI description for the node " + parentName); 507 } else { 508 cat 509 .debug("We dont have to analyse description for this parameter :" 510 + parentName); 511 return; 512 } 513 515 cat.debug("node " + tagName + " : " + parentName); 518 } else if (tagName.equals(RADIOBUTTON)) { 519 params.put(CHOICES, returnParameters(node, CHOICE, VALUE)); 520 params.put(SELECTED, 521 returnSelectedParameters(node, CHOICE, DEFAULT)); 522 cat.debug("node " + tagName + " : " + params.toString()); 523 result = new ParametersWidgetsNode(new WidgetDescription( 524 WidgetDescription.RADIO_GROUP, parentName, label, params)); 525 parentName = ""; 526 parent.addChild(result); 527 isFinalNode = true; 528 } else if (tagName.equals(CHECKBOX)) { 529 params.put(CHOICES, returnParameters(node, CHOICE, VALUE)); 530 params.put(SELECTED, 531 returnSelectedParameters(node, CHOICE, DEFAULT)); 532 cat.debug("node " + tagName + " : " + params.toString()); 533 result = new ParametersWidgetsNode(new WidgetDescription( 534 WidgetDescription.CHECK_BOX, parentName, label, params)); 535 parentName = ""; 536 parent.addChild(result); 537 isFinalNode = true; 538 } else if (tagName.equals(COMBO)) { 539 params.put(CHOICES, returnParameters(node, CHOICE, VALUE)); 540 params.put(SELECTED, 541 returnSelectedParameters(node, CHOICE, DEFAULT)); 542 cat.debug("node " + tagName + " : " + params.toString()); 543 result = new ParametersWidgetsNode(new WidgetDescription( 544 WidgetDescription.COMBO, parentName, label, params)); 545 parentName = ""; 546 parent.addChild(result); 547 isFinalNode = true; 548 } else if (tagName.equals(FIELD)) { 549 params.put(SIZE, returnAttributValue(node, SIZE) != null 550 ? returnAttributValue(node, SIZE) 551 : "0"); 552 params.put(TEXT, returnAttributValue(node, TEXT) != null 553 ? returnAttributValue(node, TEXT) 554 : PLEASE_EDIT); 555 cat.debug("node " + tagName + "(" + parentName + ") : " 556 + params.toString()); 557 result = new ParametersWidgetsNode(new WidgetDescription( 558 WidgetDescription.TEXT_FIELD, parentName, label, params)); 559 parentName = ""; 560 parent.addChild(result); 561 isFinalNode = true; 562 } else if (tagName.equals(NFIELD)) { 563 cat.debug("node " + tagName + "(" + parentName + ") : " 564 + params.toString()); 565 result = new ParametersWidgetsNode(new WidgetDescription( 566 WidgetDescription.NFIELD, parentName, label, params)); 567 parentName = ""; 568 parent.addChild(result); 569 isFinalNode = true; 570 } else if (tagName.equals(TABLE)) { 571 cat.debug("node " + tagName + "(" + parentName + ") : " 572 + params.toString()); 573 params.put(COLS, returnAttributValue(node, COLS) != null 574 ? returnAttributValue(node, COLS) 575 : ""); 576 result = new ParametersWidgetsNode(new WidgetDescription( 577 WidgetDescription.TABLE, parentName, label, params)); 578 parentName = ""; 579 parent.addChild(result); 580 isFinalNode = true; 581 } 582 if (node.hasChildNodes() && !isGroup && !isFinalNode) { 583 NodeList fils = node.getChildNodes(); 584 for (int i = 0; i < fils.getLength(); i++) { 585 returnParametersWidgetDescription(parent, fils.item(i), 586 parentName, label, parametersNames); 587 } 588 } 589 } 590 591 599 private static ParametersWidgetsNode visitDOM(Node node, String tagName, 600 Vector parametersNames) { 601 boolean EndNode = false; 604 switch (node.getNodeType()) { 605 case Node.ELEMENT_NODE : 606 cat.info("Node : " + tagName); 607 if (node.getNodeName().equals(tagName)) { 608 609 ParametersWidgetsNode result = new ParametersWidgetsNode( 610 null); 611 EndNode = true; 612 if (tagName.equals(OBJECT)) { 613 result.addChild(new ParametersWidgetsNode( 614 new WidgetDescription( 615 WidgetDescription.TEXT_FIELD, ID, null, 616 null))); 617 } else { 618 result.addChild(new ParametersWidgetsNode( 619 new WidgetDescription(WidgetDescription.COMBO, 620 ID, null, null))); 621 } 622 623 returnParametersWidgetDescription(result, node, "", "", 624 parametersNames); 625 completeParametersWidgetDescription(result, parametersNames); 626 627 if (result.getChildren().size() != 1) { 628 cat.debug("We have a Winner"); 629 cat.debug(result); 630 return result; 631 } 632 633 } 634 break; 635 default : 636 } 637 if (node.hasChildNodes() && !EndNode) { 638 NodeList fils = node.getChildNodes(); 639 for (int i = 0; i < fils.getLength(); i++) { 640 ParametersWidgetsNode temp = visitDOM(fils.item(i), tagName, 641 parametersNames); 642 if (temp != null) 643 return temp; 644 } 645 646 } 647 return null; 648 } 649 657 private static void visitDOM2(Hashtable hashtable, Node node, 658 String tagName, Hashtable plugins) { 659 boolean EndNode = false; 660 Vector descriptionParams = null; 662 Vector parametersNames = new Vector (); 663 664 switch (node.getNodeType()) { 665 case Node.ELEMENT_NODE : 666 if (node.getNodeName().equals(tagName)) { 667 Object name = returnAttributValue(node, NAME); 668 cat.debug("Node : " + tagName + " (" 669 + name + ")"); 670 ParametersWidgetsNode result = null; 671 EndNode = true; 672 673 if (plugins.containsKey(name)) { 674 if (tagName.equals(TIMER)) { 675 descriptionParams = ((TimerDescription) plugins 676 .get(name)) 677 .getParams(); 678 } else if (tagName.equals(TEST)) { 679 descriptionParams = ((TestDescription) plugins 680 .get(name)) 681 .getParams(); 682 } else if (tagName.equals(SAMPLE)) { 683 descriptionParams = ((SampleDescription) plugins 684 .get(name)) 685 .getParams(); 686 } else { 687 cat.debug("Error"); 688 } 689 if (descriptionParams != null) { 690 for (int i = 0; i < descriptionParams.size(); ++i) { 691 parametersNames 692 .addElement(((ParameterDescription) descriptionParams 693 .get(i)).getName()); 694 } 695 } 696 result = visitDOM(node, tagName, parametersNames); 697 if (result != null) { 698 hashtable.put(name, 701 result); 702 } 703 } 704 705 } 706 break; 707 default : 708 } 709 710 if (node.hasChildNodes() && !EndNode) { 711 NodeList fils = node.getChildNodes(); 712 for (int i = 0; i < fils.getLength(); ++i) { 713 visitDOM2(hashtable, fils.item(i), tagName, plugins); 714 } 715 716 } 717 719 } 720 721 } | Popular Tags |