Example #1
0
  /*
   * SodaXML: Constructor
   *
   * Input:
   * 	sodaTest: A full path to a soda test file.
   *
   * Output:
   * 	None.
   */
  public SodaXML(String sodaTest, SodaReporter reporter) {
    File testFD = null;
    DocumentBuilderFactory dbf = null;
    DocumentBuilder db = null;

    this.reporter = reporter;

    try {
      testFD = new File(sodaTest);
      dbf = DocumentBuilderFactory.newInstance();
      db = dbf.newDocumentBuilder();
      doc = db.parse(testFD);
      sodaTypes = new SodaTypes();
      types = sodaTypes.getTypes();
      events = this.parse(doc.getDocumentElement().getChildNodes());
    } catch (Exception exp) {
      this.events = null;
      if (this.reporter == null) {
        exp.printStackTrace();
      } else {
        this.reporter.ReportException(exp);
      }
    }
  }
Example #2
0
  /*
   * parse:
   * 	This method parses all the xml nodes into a SodaEvents object.
   *
   * Input:
   * 	node:  This is a node list from the soda xml test.
   *
   * Output:
   * 	returns a SodaEvents object.
   */
  private SodaEvents parse(NodeList node) throws Exception {
    SodaHash data = null;
    SodaEvents dataList = null;
    boolean err = false;
    int len = 0;

    dataList = new SodaEvents();

    len = node.getLength();
    for (int i = 0; i <= len - 1; i++) {
      Node child = node.item(i);
      String name = child.getNodeName();

      if (name.startsWith("#")) {
        continue;
      }

      if (!sodaTypes.isValid(name)) {
        if (this.reporter == null) {
          System.err.printf("Error: Invalid Soda Element: '%s'!\n", name);
        } else {
          this.reporter.ReportError(String.format("Error: Invalid Soda Element: '%s'!", name));
        }

        err = true;
        break;
      }

      data = new SodaHash();
      data.put("do", name);
      data.put("type", SodaElements.valueOf(name.toUpperCase()));

      if (child.hasAttributes()) {
        data = processAttributes(data, child);
      }

      if (name.contains("javascript")) {
        String tmp = child.getTextContent();
        if (!tmp.isEmpty()) {
          data.put("content", tmp);
        }
      }

      if (child.hasChildNodes()) {
        if (name.contains("execute")) {
          String[] list = processArgs(child.getChildNodes());
          data.put("args", list);
        } else {
          SodaEvents tmp = parse(child.getChildNodes());
          if (tmp != null) {
            data.put("children", tmp);
          } else {
            err = true;
            break;
          }
        }
      }

      if (!data.isEmpty()) {
        dataList.add(data);
      } else {
        System.out.printf("Note: No data found.\n");
      }
    }

    if (err) {
      dataList = null;
    }

    return dataList;
  }