Beispiel #1
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);
      }
    }
  }
Beispiel #2
0
 public static void dump(XmlTagHandler root, PrintStream out) {
   try {
     XmlParser.dumpNode(root, out, 0);
   } catch (XmlTagException exc) {
     out.println("Error traversing tags: " + exc.getMessage());
   }
 }
Beispiel #3
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());
  }
Beispiel #4
0
  private static void processNode(Element elem, XmlTagHandler tag, XmlFilterHandler filter)
      throws XmlAttrException, XmlTagException {
    if (tag instanceof XmlTagEntryHandler) {
      ((XmlTagEntryHandler) tag).enter();
    }

    if (tag instanceof XmlFilterHandler) {
      filter = (XmlFilterHandler) tag;
    }

    XmlParser.checkAttributes(elem, tag, filter);
    if (tag instanceof XmlTextHandler) {
      ((XmlTextHandler) tag).handleText(elem.getText());
    }
    XmlParser.checkSubNodes(elem, tag, filter);

    if (tag instanceof XmlTagExitHandler) {
      ((XmlTagExitHandler) tag).exit();
    }
  }
Beispiel #5
0
  public Document newDocument() throws SAXException {
    Document docResult = null;

    try {
      DocumentBuilderFactory factory = XmlParser.createDocumentBuilderFactory();
      DocumentBuilder builder = factory.newDocumentBuilder();
      builder.setErrorHandler(new ErrorHandler());

      docResult = builder.newDocument();
    } catch (ParserConfigurationException e) {
      throw new SAXException(e);
    }

    return docResult;
  }
Beispiel #6
0
  public void writeDocument(Document doc, String file) throws IOException, SAXException {
    try {

      TransformerFactory factory = XmlParser.createTransformerFactory();
      Transformer trans = factory.newTransformer();

      trans.setOutputProperty("indent", "yes");
      trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

      DOMSource src = new DOMSource(doc);
      StreamResult result = new StreamResult(new File(file));

      trans.transform(src, result);
    } catch (TransformerException e) {
      throw new SAXException(e);
    }
  }
Beispiel #7
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;
        }
      }
    }
  }
Beispiel #8
0
  public Document readDocument(Object source, Node after) throws IOException, SAXException {
    Document docResult = null;

    try {
      DocumentBuilderFactory factory = XmlParser.createDocumentBuilderFactory();
      DocumentBuilder builder = factory.newDocumentBuilder();
      builder.setErrorHandler(new ErrorHandler());

      File f = null;
      if (source instanceof String) {
        f = new File((String) source);
        docResult = builder.parse(f);
      } else if (source instanceof InputStream) {
        docResult = builder.parse((InputStream) source);
      }
      Node nodeRoot = docResult.getFirstChild();

      // //////////////////////////////////////////////////////
      // Make sure we have a DBSetup XML file.

      // This is dumb, because there could be an XML comment before
      // the root element. The root node is not the same thing as the
      // root element.
      if (nodeRoot.getNodeName().equalsIgnoreCase("Covalent.DBSetup") == false) {
        if (source instanceof String) {
          throw new IOException(source.toString() + " is not a DBSetup XML file.");
        } else {
          throw new IOException("Source is not a DBSetup XML file.");
        }
      }

      // ///////////////////////////////////////////////////////
      // Look for include tags

      NodeList listNodes = nodeRoot.getChildNodes();

      for (int iNode = 0; iNode < listNodes.getLength(); iNode++) {
        Node node = listNodes.item(iNode);

        if (node.getNodeName().equalsIgnoreCase("include") == true) {
          NamedNodeMap map = node.getAttributes();

          for (int iAttr = 0; iAttr < map.getLength(); iAttr++) {
            Node nodeMap = map.item(iAttr);

            if (nodeMap.getNodeName().equalsIgnoreCase("file") == true) {
              File fileInclude = new File(nodeMap.getNodeValue());

              if (fileInclude.isAbsolute() == false) {
                if (!(source instanceof String)) {
                  throw new IOException("Cannot resolve paths" + " relative to a " + "stream.");
                }
                String strPath = f.getAbsolutePath();
                int iIndex = strPath.lastIndexOf(File.separatorChar);
                strPath = strPath.substring(0, iIndex);
                fileInclude = new File(strPath + File.separatorChar + fileInclude.getPath());
              }

              this.readDocument(fileInclude.getAbsolutePath(), node);
              nodeRoot.removeChild(node);
            }
          }
        } else if (after != null) DBSetup.importNodeAfter(after, node, true);
      }
    } catch (ParserConfigurationException e) {
      throw new SAXException(e);
    }

    return docResult;
  }