コード例 #1
0
ファイル: XmlParser.java プロジェクト: cah-rfelsburg/hyperic
  /** General parsing used by both parse methods above */
  private static void generalParse(XmlTagHandler tag, Document doc) throws XmlParseException {

    Element root = doc.getRootElement();
    if (!root.getName().equalsIgnoreCase(tag.getName())) {
      throw new XmlParseException(
          "Incorrect root tag.  Expected <" + tag.getName() + "> but got <" + root.getName() + ">");
    }
    XmlParser.processNode(root, tag, new DummyFilter());
  }
コード例 #2
0
ファイル: XmlParser.java プロジェクト: cah-rfelsburg/hyperic
  private static void checkSubNodes(Element elem, XmlTagHandler tag, XmlFilterHandler filter)
      throws XmlAttrException, XmlTagException {
    XmlTagInfo[] subTags = tag.getSubTags();
    Map hash;

    hash = new HashMap();

    // First, count how many times each sub-tag is referenced
    for (Iterator i = elem.getChildren().iterator(); i.hasNext(); ) {
      Element e = (Element) i.next();
      String name;
      Integer val;

      name = e.getName().toLowerCase();
      if ((val = (Integer) hash.get(name)) == null) {
        val = new Integer(0);
      }

      val = new Integer(val.intValue() + 1);
      hash.put(name, val);
    }

    for (int i = 0; i < subTags.length; i++) {
      String name = subTags[i].getTag().getName().toLowerCase();
      Integer iVal = (Integer) hash.get(name);
      int threshold = 0, val;

      val = iVal == null ? 0 : iVal.intValue();

      switch (subTags[i].getType()) {
        case XmlTagInfo.REQUIRED:
          if (val == 0) {
            throw new XmlMissingTagException(elem, name);
          } else if (val != 1) {
            throw new XmlTooManyTagException(elem, name);
          }
          break;
        case XmlTagInfo.OPTIONAL:
          if (val > 1) {
            throw new XmlTooManyTagException(elem, name);
          }
          break;
        case XmlTagInfo.ONE_OR_MORE:
          threshold++;
        case XmlTagInfo.ZERO_OR_MORE:
          if (val < threshold) {
            throw new XmlMissingTagException(elem, name);
          }
          break;
      }

      hash.remove(name);
    }

    // Now check for excess sub-tags
    if (hash.size() != 0) {
      Set keys = hash.keySet();

      throw new XmlTooManyTagException(elem, (String) keys.iterator().next());
    }

    // Recurse to all sub-tags
    for (Iterator i = elem.getChildren().iterator(); i.hasNext(); ) {
      Element child = (Element) i.next();

      for (int j = 0; j < subTags.length; j++) {
        XmlTagHandler subTag = subTags[j].getTag();
        String subName = subTag.getName();

        if (child.getName().equalsIgnoreCase(subName)) {
          XmlParser.processNode(child, subTag, filter);
          break;
        }
      }
    }
  }