Example #1
0
  /** 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());
  }
Example #2
0
  private static void dumpNode(XmlTagHandler tag, PrintStream out, int indent)
      throws XmlTagException {
    XmlTagInfo[] subTags = tag.getSubTags();

    out.println(StringUtil.repeatChars(' ', indent) + "Tag <" + tag.getName() + ">:");
    if (tag instanceof XmlAttrHandler) {
      XmlAttr[] attrs;

      attrs = ((XmlAttrHandler) tag).getAttributes();
      if (attrs.length == 0)
        out.println(
            StringUtil.repeatChars(' ', indent) + "- has no required or optional attributes");

      XmlParser.dumpAttrs(attrs, "REQUIRED", XmlAttr.REQUIRED, out, indent);
      XmlParser.dumpAttrs(attrs, "OPTIONAL", XmlAttr.OPTIONAL, out, indent);
    } else {
      out.println(StringUtil.repeatChars(' ', indent) + "- has no required or optional attributes");
    }

    if (tag instanceof XmlUnAttrHandler)
      out.println(StringUtil.repeatChars(' ', indent) + "- handles arbitrary attributes");

    subTags = tag.getSubTags();
    if (subTags.length == 0) {
      out.println(StringUtil.repeatChars(' ', indent) + "- has no subtags");
    } else {
      for (int i = 0; i < subTags.length; i++) {
        String name = subTags[i].getTag().getName();
        int type = subTags[i].getType();

        out.print(StringUtil.repeatChars(' ', indent) + "- has subtag <" + name + ">, which ");
        switch (type) {
          case XmlTagInfo.REQUIRED:
            out.println("is REQUIRED");
            break;
          case XmlTagInfo.OPTIONAL:
            out.println("is OPTIONAL");
            break;
          case XmlTagInfo.ONE_OR_MORE:
            out.println("is REQUIRED at least ONCE");
            break;
          case XmlTagInfo.ZERO_OR_MORE:
            out.println("can be specified any # of times");
            break;
        }

        XmlParser.dumpNode(subTags[i].getTag(), out, indent + 4);
      }
    }
  }
Example #3
0
  private static void dumpNodeWiki(XmlTagHandler tag, PrintStream out, int indent)
      throws XmlTagException {
    XmlTagInfo[] subTags = tag.getSubTags();

    if (indent == 1) {
      out.println(StringUtil.repeatChars('*', indent) + " Tag *<" + tag.getName() + ">*: ");
    }

    if (tag instanceof XmlAttrHandler) {
      XmlAttr[] attrs;

      attrs = ((XmlAttrHandler) tag).getAttributes();

      dumpAttrsWiki(attrs, "*REQUIRED*", XmlAttr.REQUIRED, out, indent + 1);
      dumpAttrsWiki(attrs, "*OPTIONAL*", XmlAttr.OPTIONAL, out, indent + 1);
    }

    subTags = tag.getSubTags();
    if (subTags.length != 0) {

      for (int i = 0; i < subTags.length; i++) {
        String name = subTags[i].getTag().getName();
        int type = subTags[i].getType();
        String desc = "";

        switch (type) {
          case XmlTagInfo.REQUIRED:
            desc = "REQUIRED";
            break;
          case XmlTagInfo.OPTIONAL:
            desc = "OPTIONAL";
            break;
          case XmlTagInfo.ONE_OR_MORE:
            desc = "REQUIRED at least ONCE";
            break;
          case XmlTagInfo.ZERO_OR_MORE:
            desc = "can be specified any # of times";
            break;
        }

        out.println(StringUtil.repeatChars('*', indent + 1) + " Sub Tag *<" + name + ">* " + desc);
        dumpNodeWiki(subTags[i].getTag(), out, indent + 1);
      }
    }
  }
Example #4
0
  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;
        }
      }
    }
  }