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