1 23 package org.objectweb.clif.scenario.util.isac.util; 24 25 import java.io.FileInputStream ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.util.Enumeration ; 29 import java.util.Hashtable ; 30 import java.util.Properties ; 31 import java.util.Vector ; 32 33 import javax.xml.parsers.DocumentBuilder ; 34 import javax.xml.parsers.DocumentBuilderFactory ; 35 import javax.xml.parsers.SAXParser ; 36 import javax.xml.parsers.SAXParserFactory ; 37 38 import org.objectweb.clif.scenario.util.isac.FileName; 39 import org.objectweb.clif.scenario.util.isac.loadprofile.Point; 40 import org.objectweb.clif.scenario.util.isac.loadprofile.RampDescription; 41 import org.objectweb.clif.scenario.util.isac.plugin.ObjectDescription; 42 import org.objectweb.clif.scenario.util.isac.plugin.ParameterDescription; 43 import org.objectweb.clif.scenario.util.isac.plugin.PluginDescription; 44 import org.objectweb.clif.scenario.util.isac.plugin.SampleDescription; 45 import org.objectweb.clif.scenario.util.isac.plugin.TestDescription; 46 import org.objectweb.clif.scenario.util.isac.plugin.TimerDescription; 47 import org.objectweb.clif.scenario.util.isac.plugin.parser.AnalyseSaxPlugin; 48 import org.objectweb.clif.scenario.util.isac.util.xml.IsacEntityResolver; 49 import org.objectweb.util.monolog.api.BasicLevel; 50 import org.objectweb.util.monolog.api.Logger; 51 import org.objectweb.util.monolog.api.LoggerFactory; 52 import org.w3c.dom.Document ; 53 import org.w3c.dom.Element ; 54 import org.w3c.dom.NamedNodeMap ; 55 import org.w3c.dom.Node ; 56 import org.w3c.dom.NodeList ; 57 import org.xml.sax.InputSource ; 58 import org.xml.sax.XMLReader ; 59 60 66 public class ScenarioSyntaxChecker { 67 private static Logger cat; 68 69 private static Hashtable plugins; 72 73 private static Vector behaviorIds; 74 75 private static int tagNumber; 77 78 private static String tagName; 79 80 private static Hashtable sessionObjectPluginName; 82 83 private static String lastActionTag; 84 85 private static String lastActionName; 86 87 private static String lastActionSOId; 88 89 private static int lastActionNumberOfParamsDefined; 90 91 private static String lastControllerTag; 92 93 private static Point lastEnd; 94 95 private static Point currentEnd; 96 97 private static Point currentStart; 98 99 113 private static boolean attributesChecker(NamedNodeMap nodeAttributes, 114 Hashtable attributesDesc, Vector errors, Vector warnings) { 115 if (nodeAttributes.getLength() < attributesDesc.size()) { 117 errors 118 .add(tagNumber 119 + "(" 120 + tagName 121 + "): The attributes are not correctly defined, this tag must define : " 122 + attributesDesc); 123 return false; 124 } 125 if (nodeAttributes.getLength() < attributesDesc.size()) { 127 warnings.add(tagNumber + "(" + tagName 128 + "): We have too much attributes, this tag must define : " 129 + attributesDesc); 130 } 131 Enumeration names = attributesDesc.keys(); 133 while (names.hasMoreElements()) { 134 String name = (String ) names.nextElement(); 135 Node nodeValue = nodeAttributes.getNamedItem(name); 136 if (nodeValue == null) { 137 errors.add(tagNumber + "(" + tagName 138 + "): An attribute is not defined : " + name); 139 return false; 140 } 141 String value = nodeValue.getNodeValue(); 142 if (value.equals("")) { 144 if (((AttributeDescription) attributesDesc.get(name)).mustBeInit) { 145 errors 146 .add(tagNumber 147 + "(" 148 + tagName 149 + "): The value of this attribute must be initialized : " 150 + name); 151 return false; 152 } else { 153 warnings.add(tagNumber + "(" + tagName 154 + "): The value of this attribute is empty : " 155 + name); 156 } 157 } 158 ((AttributeDescription) attributesDesc.get(name)).value = value; 160 } 161 return true; 163 } 164 165 180 private static boolean visit(Node node, Vector errors, Vector warnings, 181 ClassLoader cl) { 182 boolean analyzeChildren = true; 184 switch (node.getNodeType()) { 186 case Node.ELEMENT_NODE: 188 tagNumber++; 190 tagName = ((Element ) node).getTagName(); 192 193 cat.log(BasicLevel.DEBUG, " * NODE ANALYZED : " + tagName); 194 198 if (tagName 200 .equals(org.objectweb.clif.scenario.util.isac.util.tree.Node.BEHAVIORS)) { 201 } else if (org.objectweb.clif.scenario.util.isac.util.tree.Node.USE 204 .equals(tagName)) { 205 NamedNodeMap attributes = node.getAttributes(); 206 Hashtable attNames = new Hashtable (); 209 attNames.put("id", new AttributeDescription(true, true)); 210 attNames.put("name", new AttributeDescription(true, true)); 211 attributesChecker(attributes, attNames, errors, warnings); 213 String idValue = ((AttributeDescription) attNames.get("id")).value; 215 String nameValue = ((AttributeDescription) attNames.get("name")).value; 217 sessionObjectPluginName.put(idValue, nameValue); 220 if (!plugins.containsKey(nameValue)) { 222 PluginDescription pd = loadPluginDescription(nameValue 223 + "/", cl, errors); 224 if (pd == null) { 226 return false; 227 } else { 228 plugins.put(idValue, pd); 229 } 230 } 231 lastActionTag = tagName; 232 lastActionNumberOfParamsDefined = 0; 233 lastActionSOId = idValue; 234 if (node.hasChildNodes()) { 235 NodeList children = node.getChildNodes(); 236 for (int i = 0; i < children.getLength(); i++) { 237 Node tempNode = children.item(i); 238 if (tempNode.getNodeType() == Node.ELEMENT_NODE) { 239 boolean continueProcess = true; 240 continueProcess = visit(tempNode, errors, warnings, 241 cl); 242 if (!continueProcess) 244 return false; 245 } 246 } 247 } 248 int numberOfParamsRequired = ((PluginDescription) plugins 250 .get(idValue)).getObject().getParams().size(); 251 if ((numberOfParamsRequired-1) > lastActionNumberOfParamsDefined) { 253 errors 255 .add(tagNumber 256 + "(" 257 + tagName 258 + "): A parameter has not have been defined, nb required=" 259 + numberOfParamsRequired + ", nb found=" 260 + lastActionNumberOfParamsDefined); 261 return false ; 262 } 263 analyzeChildren = false; 264 } else if (tagName 266 .equals(org.objectweb.clif.scenario.util.isac.util.tree.Node.BEHAVIOR)) { 267 NamedNodeMap attributes = node.getAttributes(); 269 Hashtable attNames = new Hashtable (); 272 attNames.put("id", new AttributeDescription(true, true)); 273 attributesChecker(attributes, attNames, errors, warnings); 275 String idValue = ((AttributeDescription) attNames.get("id")).value; 276 behaviorIds.add(idValue); 278 } else if (org.objectweb.clif.scenario.util.isac.util.tree.Node 283 .isPluginNode(tagName) 284 || org.objectweb.clif.scenario.util.isac.util.tree.Node.CONDITION 285 .equals(tagName)) { 286 NamedNodeMap attributes = node.getAttributes(); 287 Hashtable attNames = new Hashtable (); 290 attNames.put("use", new AttributeDescription(true, true)); 291 attNames.put("name", new AttributeDescription(true, true)); 292 attributesChecker(attributes, attNames, errors, warnings); 294 String idPluginUsed = ((AttributeDescription) attNames 295 .get("use")).value; 296 String actionName = ((AttributeDescription) attNames 297 .get("name")).value; 298 if (!sessionObjectPluginName.containsKey(idPluginUsed)) { 299 errors 300 .add(tagNumber 301 + "(" 302 + tagName 303 + "): This session object id, have not been defined : " 304 + idPluginUsed); 305 return false; 306 } 307 lastActionTag = tagName; 308 lastActionName = actionName; 309 lastActionSOId = idPluginUsed; 310 lastActionNumberOfParamsDefined = 0; 311 if (node.hasChildNodes()) { 313 NodeList children = node.getChildNodes(); 314 for (int i = 0; i < children.getLength(); i++) { 315 Node tempNode = children.item(i); 316 if (tempNode.getNodeType() == Node.ELEMENT_NODE) { 317 boolean continueProcess = true; 318 continueProcess = visit(tempNode, errors, warnings, 319 cl); 320 if (!continueProcess) 322 return false; 323 } 324 } 325 } 326 int numberOfParamsRequired = 0; 328 if (tagName 329 .equals(org.objectweb.clif.scenario.util.isac.util.tree.Node.CONDITION)) 330 numberOfParamsRequired = ((TestDescription) ((PluginDescription) plugins 331 .get(idPluginUsed)).getTests().get(actionName)) 332 .getParams().size(); 333 else if (tagName 334 .equals(org.objectweb.clif.scenario.util.isac.util.tree.Node.SAMPLE)) 335 numberOfParamsRequired = ((SampleDescription) ((PluginDescription) plugins 336 .get(idPluginUsed)).getSamples().get(actionName)) 337 .getParams().size(); 338 else if (tagName 339 .equals(org.objectweb.clif.scenario.util.isac.util.tree.Node.TIMER)) 340 numberOfParamsRequired = ((TimerDescription) ((PluginDescription) plugins 341 .get(idPluginUsed)).getTimers().get(actionName)) 342 .getParams().size(); 343 if ((numberOfParamsRequired-1) > lastActionNumberOfParamsDefined) { 345 errors 347 .add(tagNumber 348 + "(" 349 + tagName 350 + "): A parameter has not have been defined, nb required=" 351 + numberOfParamsRequired + ", nb found=" 352 + lastActionNumberOfParamsDefined); 353 return false ; 354 } 355 analyzeChildren = false; 356 } else if (tagName 357 .equals(org.objectweb.clif.scenario.util.isac.util.tree.Node.PARAMS)) { 358 } else if (tagName 360 .equals(org.objectweb.clif.scenario.util.isac.util.tree.Node.PARAM)) { 361 NamedNodeMap attributes = node.getAttributes(); 362 Hashtable attNames = new Hashtable (); 365 attNames.put("value", new AttributeDescription(true, false)); 366 attNames.put("name", new AttributeDescription(true, true)); 367 attributesChecker(attributes, attNames, errors, warnings); 369 String name = ((AttributeDescription) attNames.get("name")).value; 370 PluginDescription pd = (PluginDescription) plugins 372 .get(lastActionSOId); 373 Vector params = null; 375 if (lastActionTag 377 .equals(org.objectweb.clif.scenario.util.isac.util.tree.Node.SAMPLE)) { 378 SampleDescription sample = (SampleDescription) pd 379 .getSamples().get(lastActionName); 380 params = sample.getParams(); 381 } 382 else if (lastActionTag 384 .equals(org.objectweb.clif.scenario.util.isac.util.tree.Node.TIMER)) { 385 TimerDescription timer = (TimerDescription) pd.getTimers() 386 .get(lastActionName); 387 params = timer.getParams(); 388 } 389 else if (lastActionTag 391 .equals(org.objectweb.clif.scenario.util.isac.util.tree.Node.CONDITION)) { 392 TestDescription test = (TestDescription) pd.getTests().get( 393 lastActionName); 394 params = test.getParams(); 395 } 396 else if (lastActionTag 398 .equals(org.objectweb.clif.scenario.util.isac.util.tree.Node.USE)) { 399 ObjectDescription object = (ObjectDescription) pd 400 .getObject(); 401 params = object.getParams(); 402 } 403 404 boolean belongs = false; 406 for (int i = 0; i < params.size(); i++) { 407 ParameterDescription param = (ParameterDescription) params 408 .elementAt(i); 409 if (param.getName().equals(name)) { 410 lastActionNumberOfParamsDefined++; 411 belongs = true; 412 break; 413 } 414 } 415 if (!belongs) { 417 warnings.add(tagNumber + "(" + tagName + "): This action (" 418 + lastActionTag 419 + ") does not required a parameter " + name); 420 } 421 } else if (org.objectweb.clif.scenario.util.isac.util.tree.Node 423 .isControllerNode(tagName)) { 424 NamedNodeMap attributes = node.getAttributes(); 425 Hashtable attNames = new Hashtable (); 428 attributesChecker(attributes, attNames, errors, warnings); 430 lastControllerTag = tagName; 432 } else if (org.objectweb.clif.scenario.util.isac.util.tree.Node.CHOICE 434 .equals(tagName)) { 435 NamedNodeMap attributes = node.getAttributes(); 436 Hashtable attNames = new Hashtable (); 439 attNames.put("proba", new AttributeDescription(true, true)); 440 attributesChecker(attributes, attNames, errors, warnings); 442 } 443 else if (org.objectweb.clif.scenario.util.isac.util.tree.Node.LOAD_PROFILE 445 .equals(tagName)) { 446 } else if (org.objectweb.clif.scenario.util.isac.util.tree.Node.GROUP 449 .equals(tagName)) { 450 NamedNodeMap attributes = node.getAttributes(); 451 Hashtable attNames = new Hashtable (); 454 attNames.put("behavior", new AttributeDescription(true, true)); 455 attNames.put("forceStop", 456 new AttributeDescription(false, false)); 457 attributesChecker(attributes, attNames, errors, warnings); 459 String behaviorId = ((AttributeDescription) attNames 461 .get("behavior")).value; 462 if (!behaviorIds.contains(behaviorId)) { 464 warnings 465 .add(tagNumber 466 + "(" 467 + tagName 468 + "): This behavior id is not defined, so this group won't be executed" 469 + behaviorId); 470 } 471 lastEnd = null; 473 currentEnd = null; 474 currentStart = null; 475 } else if (org.objectweb.clif.scenario.util.isac.util.tree.Node.RAMP 476 .equals(tagName)) { 477 NamedNodeMap attributes = node.getAttributes(); 479 Hashtable attNames = new Hashtable (); 482 attNames.put("style", new AttributeDescription(true, true)); 483 attributesChecker(attributes, attNames, errors, warnings); 485 String style = ((AttributeDescription) attNames.get("style")).value; 486 RampDescription.isDefinedStyle(style); 487 } else if (org.objectweb.clif.scenario.util.isac.util.tree.Node.POINTS 488 .equals(tagName)) { 489 lastEnd = currentEnd; 492 currentEnd = null; 493 currentStart = null; 494 if (node.hasChildNodes()) { 496 NodeList children = node.getChildNodes(); 497 for (int i = 0; i < children.getLength(); i++) { 498 Node tempNode = children.item(i); 499 if (tempNode.getNodeType() == Node.ELEMENT_NODE) { 500 boolean continueProcess = true; 501 continueProcess = visit(tempNode, errors, warnings, 502 cl); 503 if (!continueProcess) 505 return false; 506 } 507 } 508 } 509 if (currentStart == null || currentEnd == null) { 511 errors.add(tagNumber 512 + "(" 513 + tagName 514 + "): This ramp has not defined the two required point : start="+currentStart+" and stop=" 515 + currentEnd); 516 return false ; 517 } 518 analyzeChildren = false ; 519 } else if (org.objectweb.clif.scenario.util.isac.util.tree.Node.POINT 520 .equals(tagName)) { 521 NamedNodeMap attributes = node.getAttributes(); 523 Hashtable attNames = new Hashtable (); 526 attNames.put("name", new AttributeDescription(true, true)); 527 attNames.put("x", new AttributeDescription(true, true)); 528 attNames.put("y", new AttributeDescription(true, true)); 529 attributesChecker(attributes, attNames, errors, warnings); 531 String name = ((AttributeDescription) attNames.get("name")).value; 532 String xAxis = ((AttributeDescription) attNames.get("x")).value; 533 String yAxis = ((AttributeDescription) attNames.get("y")).value; 534 if (xAxis != null && yAxis != null && name != null) { 535 Integer xAxisInteger = new Integer (xAxis); 536 Integer yAxisInteger = new Integer (yAxis); 537 if (name.equals(RampDescription.POINT_START)) { 538 currentStart = new Point(xAxisInteger.intValue(), 542 yAxisInteger.intValue()); 543 if (lastEnd != null) { 545 if (!lastEnd.equals(currentStart)) { 546 errors 547 .add(tagNumber 548 + "(" 549 + tagName 550 + "): A ramp have a starting point which is not the same than the previous ramp ending point ! le=" 551 + lastEnd + ", cs=" 552 + currentStart); 553 return false; 554 } 555 } 556 if (currentEnd != null) { 557 if (currentStart.x >= currentEnd.x) { 560 errors 561 .add(tagNumber 562 + "(" 563 + tagName 564 + "): A ramp have a starting point which is after the ending point in the time scale !"); 565 return false; 566 } 567 } 568 } else if (name.equals(RampDescription.POINT_END)) { 569 currentEnd = new Point(xAxisInteger.intValue(), yAxisInteger 571 .intValue()); 572 if (currentStart != null) { 573 if (currentStart.x >= currentEnd.x) { 576 errors 577 .add(tagNumber 578 + "(" 579 + tagName 580 + "): The starting point is after the ending point in the time scale, ignore the ramp !"); 581 return false; 582 } 583 } 584 } else { 585 warnings.add(tagNumber + "(" + tagName 586 + "): This kind of point is not defined : " 587 + name); 588 } 589 } 590 } 591 592 else { 596 } 598 break; 599 default: 600 } 602 if (analyzeChildren) { 603 if (node.hasChildNodes()) { 605 NodeList children = node.getChildNodes(); 606 for (int i = 0; i < children.getLength(); i++) { 607 Node tempNode = children.item(i); 608 if (tempNode.getNodeType() == Node.ELEMENT_NODE) { 609 boolean continueProcess = true; 610 continueProcess = visit(tempNode, errors, warnings, cl); 611 if (!continueProcess) 613 return false; 614 } 615 } 616 } 617 } 618 return true; 619 } 620 621 632 public static PluginDescription loadPluginDescription(String dirName, 633 ClassLoader cl, Vector errors) { 634 cat.log(BasicLevel.DEBUG, "-> loadPluginDescription"); 635 Properties properties = new Properties (); 637 try { 638 InputStream is = null; 640 if (cl != null) { 641 is = cl.getResourceAsStream(dirName 642 + FileName.PLUGIN_PROPERTIES_FILE); 643 } 644 if (is == null) { 646 is = new FileInputStream (dirName 647 + FileName.PLUGIN_PROPERTIES_FILE); 648 } 649 if (is == null) { 651 errors 652 .add("Unable to find the properties file of the plugin : " 653 + dirName + FileName.PLUGIN_PROPERTIES_FILE); 654 return null; 655 } 656 properties.load(is); 657 is.close(); 658 } catch (IOException ioe) { 659 cat.log(BasicLevel.WARN, "Unable to analyse property file : " 660 + dirName + "plugin.properties"); 661 } 662 String pluginXMLFile = properties.getProperty("plugin.xmlFile"); 664 String guiXMLFile = properties.getProperty("plugin.guiFile"); 665 666 AnalyseSaxPlugin handler = new AnalyseSaxPlugin(); 668 try { 669 InputStream istream = null; 671 if (cl != null) { 672 istream = cl.getResourceAsStream(dirName + pluginXMLFile); 673 } 674 if (istream == null) { 675 istream = new FileInputStream (dirName + pluginXMLFile); 676 } 677 if (istream == null) { 678 errors.add("Unable to load the plugin description file : " 679 + dirName + pluginXMLFile); 680 return null; 681 } 682 InputSource is = new InputSource (istream); 684 SAXParserFactory factory = SAXParserFactory.newInstance(); 685 factory.setValidating(true); 686 SAXParser saxParser = factory.newSAXParser(); 687 XMLReader reader = saxParser.getXMLReader(); 688 reader.setContentHandler(handler); 689 reader.setErrorHandler(handler); 690 reader.setEntityResolver(new IsacEntityResolver(cl)); 691 reader.parse(is); 692 } catch (Exception e) { 693 cat.log(BasicLevel.WARN, " ---> Parser error : " + dirName 694 + pluginXMLFile + " -> " + e); 695 errors.add(" ---> Parser error : " + dirName + pluginXMLFile 696 + " -> " + e); 697 return null; 698 } 699 PluginDescription tempDesc = handler.getPluginDescription(); 700 701 return tempDesc; 703 } 704 705 724 public static boolean loadScenario(String fileName, Vector errors, 725 Vector warnings, ClassLoader cl, LoggerFactory loggerFactory) { 726 cat = loggerFactory.getLogger(ScenarioSyntaxChecker.class.getName()); 728 Document document = null; 729 try { 730 InputStream istream = null; 732 if (cl != null) { 733 istream = cl.getResourceAsStream(fileName); 734 } 735 if (istream == null) { 737 istream = new FileInputStream (fileName); 738 } 739 if (istream == null) { 741 errors.add("Unable to find the scenario file \"" + fileName 742 + "\" in path or classpath"); 743 return false; 744 } 745 InputSource is = new InputSource (istream); 747 DocumentBuilderFactory factory = DocumentBuilderFactory 749 .newInstance(); 750 DocumentBuilder builder = factory.newDocumentBuilder(); 751 document = builder.parse(is); 752 } catch (Exception e) { 753 errors.add(e.toString()); 756 return false; 757 } 758 plugins = new Hashtable (); 760 behaviorIds = new Vector (); 761 sessionObjectPluginName = new Hashtable (); 762 tagNumber = 1; 764 boolean result = visit(document, errors, warnings, cl); 766 cat = null; 768 plugins = null; 769 behaviorIds = null; 770 sessionObjectPluginName = null; 771 return result; 773 } 774 775 private static class AttributeDescription { 780 public boolean required; 781 782 public boolean mustBeInit; 783 784 public String value; 785 786 public AttributeDescription() { 787 } 788 789 public AttributeDescription(boolean r, boolean m) { 790 this.required = r; 791 this.mustBeInit = m; 792 } 793 794 public String toString() { 795 return "[" + required + "," + mustBeInit + "," + value + "]"; 796 } 797 } 798 } | Popular Tags |