⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cardparser.java

📁 一个用 java写的wap浏览器 对于浏览器感兴趣起的可以看看咯
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * j2wap is distributed under the terms of the GNU Public License
 *
 *  j2wap was written by:
 *
 *	        Scott Campbell
 *			Michael Nordling
 *			Karl Maftoum
 * 			Julian Bright
 *
 *			This was a final project for Comp.Engineering at the University of Canberra, Australia
 *
 *			Now released as Open Source software. 28 November 2000
 *
 *			Email: k_maftoum@hotmail.com for more info
 *
 *			Use entirely at your own risk.
 */

package wae;

import java.io.IOException;
import java.util.Stack;
import java.util.Vector;
import ui.*;

/**
 * Title:       CardParser
 * Description: This card extends class <code>Parser</code> to provide
 *              specific functionality required to parse a single card.
 * Company:     J2wap.com
 * @author:     Scott campbell
 * @version     1.1
 */
public class CardParser extends Parser
{
  /** Flags if the desired card has been found in the deck. */
  private boolean c_bolFoundDesiredCard       = false;
  /** Indicates whether navigation to this card was via a GO or Prev. */
  private boolean c_bolNavDirectionFlag       = true;
  /** Flags whether an anchot is currenmtly being parsed. */
  private boolean c_bolInAnchorElement        = false;
  /**
   * When parsing an anchor this keeps a count of the number of GO elements
   * in found that anchor.
   */
  private int     c_intNumOfGoTagsInAnchor    = 0;
  /**
   * When parsing an anchor this keeps a count of the number of elements
   * in found that anchor which are not Go elements.
   */
  private int     c_intNumOfNonGoTagsInAnchor = 0;
  /**
   * The vector representation of the card being parsed. This is what the user
   * interface renders.
   */
  private Vector  c_clsVecStack;
  /**
   *  All content of an ANCHOR element is added to this whilst it is being
   *  parsed. This is required so that a decision can be made as to whether the
   *  anchor can be stored on the card vector as an A tag instead of an ANCHOR.
   */
  private Vector  c_clsTempAnchorVec          = new Vector(1,1);
  /** Stores the names of all DO elements that are found in the card. */
  private Vector  c_clsVecDosInCard           = new Vector(1,1);


  /**
   * The method that controls the parsing of a card.
   * It iterates through the bytecode looking for elements and
   * their attributes and content (if any), and adds them to a vector.
   * @param  in_clsCardId       id attribute of the card to be parsed.
   * @param  in_clsVecstack     Vector representation of the card that is built
   *                            by the CardPaser.
   * @param  in_clsVecDosInCard Stores the names of all DO elements that are
   *                            found in the card.
   * @param  in_bolNavDirecFlag Indicates whether navigation to this card was
   *                            via a GO or Prev.
   * @return  Event ID of the intrinsic event if one was invoked.
   */
  public int parse (String   in_clsCardId,
                    Vector   in_clsVecstack,
                    Vector   in_clsVecDosInCard,
                    boolean  in_bolNavDirecFlag)
      throws IOException, NullPointerException, J2WapException
  {
    int out_intEventId    = 0; // EventId if an intrinsic event.
    char chrEntityBuf []  = new char [1];
    c_clsVecStack         = in_clsVecstack;
    c_clsVecDosInCard     = in_clsVecDosInCard;
    c_bolNavDirectionFlag = in_bolNavDirecFlag;
    c_clsDeck.ResetToWmlStartPos ();

    while (!c_bolFinishedParsing)
    {
      byte bytId = nextByte ();

      // If byte is invalid donot parse the rest of the card.
      if (bytId == Wbxml.INVALID_BYTE)
        break;

      switch (bytId)
      {
        case Wbxml.SWITCH_PAGE:
          if (nextByte () != 0)
            //System.out.println("Curr. only CP0 supported");
          break;
        case Wbxml.END:
          if (c_bolFoundDesiredCard)
          {
            String clsStrEndtag = popEndTag ();
            // Check if the end tag is </anchor>.
            if (clsStrEndtag.equals (WmlTags.END_TAG_ANCHOR))
              addAnchorToVector ();
            else
              conditionalAddToVector (clsStrEndtag);
            closeElement ();
          } // if
          break;
        case Wbxml.ENTITY:
          char charArr[] =  {(char) readMultiByteInteger ()};
          conditionalAddToVector (charArr);
          break;
        case Wbxml.STR_I:
          conditionalAddToVector(readInlineString ());
          break;
        case Wbxml.EXT_I_0: case Wbxml.EXT_I_1: case Wbxml.EXT_I_2:
        case Wbxml.EXT_T_0: case Wbxml.EXT_T_1: case Wbxml.EXT_T_2:
        case Wbxml.EXT_0:   case Wbxml.EXT_1:   case Wbxml.EXT_2:
        case Wbxml.OPAQUE:
          conditionalAddToVector (handleExtensions (bytId));
          break;
        case Wbxml.PI:
          ; // We don't handle processing instructions.
          break;
        case Wbxml.STR_T:
          // Get the string from the string table and put it in the vector.
          conditionalAddToVector(readStringFromStringTable ());
          break;
        default:
          out_intEventId = readElement (bytId, in_clsCardId);
          if (out_intEventId != WmlTags.NO_EVENT)
            return out_intEventId;  // Return Id of instrinsic event.
      } // switch
    } // while

    // Reset "c_bolFoundDesiredCard" to False.
    c_bolFoundDesiredCard = false;

    /* The return value will always be 0, unless an intrinsiv event is trigger,
     * such as <onenterforward> <go> ... </go> </onenterforward>.
     */
    return WmlTags.NO_EVENT;
  } // parse

  /**
   * Reads an element from the bytecode array.
   * This method takes in a byte that is the tag and a string that is the
   * name of the card that is to be drawn. If the element is a card tag,
   * <card>, then there are two choices to make:
   *  1) If the card to be drawn is the first card in the deck, this parameter
   *     will be null, therefore the flag c_bolFoundDesiredCard is set to true
   *     and the card and its attributes and elements will be added to the vector.
   *  2) If a specific card is to be drawn, the "in_Card_Id" will not be null
   *     and the card's id is checked. If the id is the same as "inCardId", the
   *     flag c_bolFoundDesiredCard is set to true and the card and its
   *     attributes and elements will be added to the vector.
   * @param   in_bytId  byte representation of the element.
   * @param   in_CardId ID of the card to be parsed.
   * @return  EventId. This will always be WmlTags.NO_EVENT an intrinsic event
   *          that is to be fired.
   */
  public int readElement (byte in_bytId, String in_CardId)
    throws IOException, NullPointerException, J2WapException
  {
    byte bytTag = (byte) (in_bytId & Wbxml.ELEMENT_TAG_ID);
    int out_intEventId;
    int intTemp;

    switch (bytTag)
    {
      case WmlTags.GO:
        if (c_bolInAnchorElement)
          c_intNumOfGoTagsInAnchor++;
        intTemp = readGoEvent (false, hasContent(in_bytId));
        break;
      case WmlTags.PREV:
        if (c_bolInAnchorElement)
          c_intNumOfNonGoTagsInAnchor++;
        intTemp = readPrevOrResfreshEvent (WmlTags.PREV, false);
        break;
      case WmlTags.BYTE_TEMPLATE_TAG:
        c_clsDeck.setTemplateStartPos ();
        c_bolParsingTemplate = true;
        break;
      case WmlTags.REFRESH:
        if (c_bolInAnchorElement)
          c_intNumOfNonGoTagsInAnchor++;
        intTemp = readPrevOrResfreshEvent (WmlTags.REFRESH, false);
        break;
      case WmlTags.NOOP:
        // Don't register a NOOP. Just add it to the vector.
        conditionalAddToVector(WmlTags.END_TAG_NOOP);
        break;
      case WmlTags.BYTE_ONEVENT:
        out_intEventId = readOnevent();
        if (out_intEventId != WmlTags.NO_EVENT && parsingDesiredCardOrTemplate())
          return out_intEventId;
        else
          return WmlTags.NO_EVENT;
      case WmlTags.BYTE_ANCHOR_TAG:
        c_bolInAnchorElement = true;
        // Don't break. Need to process the tag as usual.
      default:
        String tag = c_clsDeck.resolveElementTag (bytTag);

        System.out.println(tag);  // debug

        // Check if the element is the card that we want to draw.
        if (bytTag == WmlTags.BYTE_START_CARD)
        {
          if (in_CardId == null)  // If looing for the first card in the deck.
            c_bolFoundDesiredCard = true; // Found first card.
          else
            checkCard (in_CardId);
        } // if

        /* If we have found the desired card, then flag that a new element
         * start token has been found. Also push the elements end tag onto the
         * end tag stack.
         */
        if (c_bolFoundDesiredCard)
        {
          c_intOpenElements++;
          c_clsStackEndTags.push (c_clsDeck.resolveEndTag (bytTag));
        } // if

        /* Conditionally, if desired card found, add the element to the vector.
         * If the element has attributes add them. Then if the element does not
         * have content add an end tag to the vector.
         */
        conditionalAddToVector (tag);

        if (hasAttributes (in_bytId))
        {
          out_intEventId = readAttributes (bytTag);
          if (out_intEventId != WmlTags.NO_EVENT)
            return out_intEventId;
        } // if

        if (!hasContent (in_bytId))
        {
          if (c_bolFoundDesiredCard)
          {
            conditionalAddToVector ((String) c_clsStackEndTags.pop ());
            closeElement ();
          } // if
        } //if
    } // switch

    return WmlTags.NO_EVENT;
  } // readElement

  /**
   * Reads a <go> element inside an <a> element<p>
   */
  public void readGoEventForA ()
    throws IOException, NullPointerException, J2WapException
  {
    boolean  bolSendRef   = false; // Default value is false.
    Integer clsIntGoId    = null;
    String  clsStrAttr    = null;
    String  clsStrHref    = null;
    String  clsStrMethod  = null;
    String  clsStrEncType = null;

    // Get the attribute start token of the first attribute.
    byte bytId = nextByte ();

    while (bytId != Wbxml.END)
    {
      // Get a string representation of the attribute.
      clsStrAttr = readAttribute (bytId);

      if (c_bolFoundDesiredCard)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -