Beispiel #1
0
  /**
   * Description of the Method
   *
   * @param th Description of the Parameter
   * @param lex Description of the Parameter
   * @return Description of the Return Value
   * @exception Exception Description of the Exception
   */
  @Override
  public synchronized E parseXML(XMLTagHandler th, XMLLex lex) throws Exception {
    TagPeer tagPeer;
    TagPeer currentTagPeer = null;
    Tag tag;
    E object = null;
    XMLStack stack = new XMLStack();

    // show the contents of the tag handler
    while ((tag = lex.getNextTag()) != null) {
      if (DEBUG) {
        System.out.println(this + " XMLCParser: Tag=" + tag);
      }
      /*
       *  Process this tag, also process text in between tags
       */
      switch (tag.type) {
        case STAG:
          tagPeer = th.findTagPeer(tag.name);
          if (DEBUG) {
            System.out.println("Tag Peer: " + tagPeer);
          }
          if (tagPeer != null) {
            currentTagPeer = tagPeer;
            tagPeer.assignSlots(stack, tag);
            if (tag.hasImplicitEndTag) {
              object = (E) tagPeer.validateEntity(stack);
              // need to reset otherwise possible PCDATA is assigned wrong
              currentTagPeer = null;
            }
          }
          break;
        case ETAG:
          tagPeer = th.findTagPeer(tag.name);
          if (DEBUG) {
            System.out.println("Tag Peer/End tag: " + tagPeer);
          }
          if (tagPeer != null) {
            object = (E) tagPeer.validateEntity(stack);
          }
          currentTagPeer = null;
          break;
        default:
          if (currentTagPeer != null && currentTagPeer instanceof StringPeer) {
            StringPeer stringPeer = (StringPeer) currentTagPeer;
            if (DEBUG) {
              System.out.println("Applying string to " + stringPeer);
            }
            stringPeer.applyString(stack, tag);
            currentTagPeer = null;
          }
          break;
      }
    }
    return object;
  }
Beispiel #2
0
    /**
     * Handle the end of an element.
     *
     * <p>The default implementation does nothing.
     *
     * @param elementName Description of the Parameter
     * @exception Exception Derived methods may throw exceptions.
     * @see dtdc_rt.parser.XMLHandler#endElement
     */
    @Override
    public void endElement(String elementName) throws Exception {
      tag.clearAttributes();
      tag.type = Tag.ETAG;

      tagPeer = th.findTagPeer(elementName);
      if (tagPeer != null) {
        object = (E) tagPeer.validateEntity(stack);
      }
      currentTagPeer = null;
    }
Beispiel #3
0
    /**
     * Handle the start of an element.
     *
     * <p>The default implementation does nothing.
     *
     * @param elementName Description of the Parameter
     * @exception Exception Derived methods may throw exceptions.
     * @see dtdc_rt.parser.XMLHandler#startElement
     */
    @Override
    public void startElement(String elementName) throws Exception {
      tag.name = elementName;
      tag.type = Tag.STAG;

      if (null == th) {
        throw new IllegalStateException("Tag Handler is null!");
      }
      tagPeer = th.findTagPeer(tag.name);
      if (tagPeer != null) {
        currentTagPeer = tagPeer;
        tagPeer.assignSlots(stack, tag);
      }
    }
Beispiel #4
0
    /**
     * Handle a document type declaration.
     *
     * <p>The default implementation does nothing.
     *
     * @param name Description of the Parameter
     * @param publicId Description of the Parameter
     * @param systemId Description of the Parameter
     * @exception Exception Derived methods may throw exceptions.
     * @see dtdc_rt.parser.XMLHandler#doctypeDecl
     */
    @Override
    public void doctypeDecl(String name, String publicId, String systemId) throws Exception {
      if (DEBUG) {
        System.out.println("DOCTYPE: name=" + name + " pid=" + publicId + " sid=" + systemId);
      }

      if (haveCompiledParser) {
        if (DEBUG) {
          System.out.println("*** have compiled parser already.");
        }
        return;
      }

      XMLTagHandler proposed;
      proposed = XMLTagHandlerFactory.findXMLTagHandler(name);
      if (DEBUG) {
        System.out.println("Current th=" + proposed);
      }
      if (proposed == null || proposed instanceof XMLTreeTagHandler) {
        // if we are a DTDC, get the compiled stuff
        th = getCompiledTagHandler(publicId);
        if (DEBUG) {
          System.out.println("Got the compiled shtuff: " + th);
        }
      } else {
        th = proposed;
      }

      if (th != null) {
        haveCompiledParser = true;
      }

      if (DEBUG) {
        System.out.println("---> TH = " + th.getClass());
      }
    }
Beispiel #5
0
  /**
   * Description of the Method
   *
   * @param node Description of the Parameter
   * @param args Description of the Parameter
   * @return Description of the Return Value
   * @exception Exception Description of the Exception
   */
  private Object[] walkTree(Node node, Object[] args) throws Exception {
    XMLStack stack = (XMLStack) args[1];
    XMLTagHandler th = (XMLTagHandler) args[2];
    TagPeer currentTagPeer = (TagPeer) args[3];
    TagPeer tagPeer;
    Tag tag;
    Object object = null;

    int type = node.getNodeType();
    switch (type) {
      case Node.ELEMENT_NODE:
        /*
         *  Create a tag for this element
         */
        tag = new Tag(Tag.STAG, node.getNodeName());
        Attr attrs[] = sortAttributes(node.getAttributes());
        for (int i = 0; i < attrs.length; i++) {
          Attr attr = attrs[i];
          tag.addAttribute(attr.getNodeName(), attr.getNodeValue());
        }

        tagPeer = th.findTagPeer(tag.name);
        if (DEBUG) {
          System.out.println("Tag Peer: " + tagPeer);
        }
        if (tagPeer != null) {
          currentTagPeer = tagPeer;
          tagPeer.assignSlots(stack, tag);
          if (tag.hasImplicitEndTag) {
            object = tagPeer.validateEntity(stack);
          }
        }

        // recurse through all the children
        NodeList children = node.getChildNodes();
        if (children != null) {
          int len = children.getLength();
          for (int i = 0; i < len; i++) {
            args[3] = currentTagPeer;
            args = walkTree(children.item(i), args);
          }
        }
        break;
      case Node.CDATA_SECTION_NODE:
      case Node.TEXT_NODE:
        tag = new Tag(Tag.STRING, node.getNodeValue());
        if (currentTagPeer != null && currentTagPeer instanceof StringPeer) {
          StringPeer stringPeer = (StringPeer) currentTagPeer;
          if (DEBUG) {
            System.out.println("Applying string to " + stringPeer);
          }
          stringPeer.applyString(stack, tag);
          currentTagPeer = null;
        }

        break;
    }

    if (type == Node.ELEMENT_NODE) {
      tag = new Tag(Tag.ETAG, node.getNodeName());
      tagPeer = th.findTagPeer(tag.name);
      if (DEBUG) {
        System.out.println("Tag Peer/End tag: " + tagPeer);
      }
      if (tagPeer != null) {
        object = tagPeer.validateEntity(stack);
      }
      currentTagPeer = null;
    }

    args[0] = object;
    args[3] = currentTagPeer;
    return args;
  }