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

📄 templateparser.java

📁 一个用 java写的wap浏览器 对于浏览器感兴趣起的可以看看咯
💻 JAVA
字号:
/*
 * 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:        TemplateParser
 * Description:  This card extends class <code>Parser</code> to provide
 *               specific functionality required to parse a deck's
 *               <code>template</card>.
 * Company:      J2wap.com
 * @author:      Scott campbell
 * @version      1.1
 */
public class TemplateParser extends Parser
{
  /** Flags whether elements should be added to card vector. */
  private boolean         c_bolAddElementToCard       = false;
  /** Holds contents of <code>do</code> elements before dumping to card vector. */
  private Vector          c_clsTempDoVec              = new Vector(1,1);
  /** The card vector built up by the parser. */
  private Vector          c_clsCardVec;
  /**
   *  Reference to vector containing names of <code>do</code> elements found
   *  in the card.
   */
  private Vector          c_clsVecDosInCard;


   /**
   * The method that controls the parsing of a <code>template</code>.
   * It iterates through the bytecode looking for <code>do</code> and event
   * bindings which are not shadowed by the card that wasprviously parsed,
   * and adds them to the vector, passed in as a parameter.
   *
   * Note: This method redefines the parent class's method, which is why the
   * <code>in_clsCardId</code> parameter is passed but not used.
   * @param  in_clsCardId       id attribute of the card to be parsed.
   * @param  in_clsVecstack     Vector representation of the card.
   * @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_clsCardVec,
                    Vector   in_clsVecDosInCard,
                    boolean  in_bolNavDirecFlag)
    throws IOException, NullPointerException, J2WapException
  {
    c_bolNavDirectionFlag = in_bolNavDirecFlag;
    int out_intEventId = 0; // EventId of an intrinsic event.
    char chrEntityBuf [] = new char [1];

    c_clsCardVec     = in_clsCardVec;
    c_clsVecDosInCard = in_clsVecDosInCard;

    System.out.println("Beginning of TemplateParser::parse()");
    c_clsDeck.ResetToTemplateStartPos ();

    // Sert flag and add the template end tag to end tag stack.
    c_bolParsingTemplate = true;
    c_clsStackEndTags.push (c_clsDeck.resolveEndTag (WmlTags.BYTE_TEMPLATE_TAG));

    // Now parse the rest of the template element.
    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:
          // Only code page zero is supported.
          break;
        case Wbxml.END:
          conditionalAddToVector (popEndTag ());
          closeElement ();
          break;
        case Wbxml.ENTITY:
          char charArr[] =  {(char) readMultiByteInteger ()};
          conditionalAddToVector (charArr);
          break;
        case Wbxml.STR_I:
          String s = readInlineString ();
          conditionalAddToVector(s); // Add string to vector.
          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:
          //System.out.println("PI curr. not supp.");
          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);
          if (out_intEventId != WmlTags.NO_EVENT)
            return out_intEventId;  // Return Id of instrinsic event.
      } // switch
    } // while

    // 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 and parses it.
   *
   * @param   in_bytId  byte representation of the element.
   * @return  EventId. This will always be WmlTags.NO_EVENT an intrinsic event
   *          that is to be fired.
   */
  public int readElement (byte in_bytId)
    throws IOException, NullPointerException, J2WapException
  {
    byte bytTag = (byte) (in_bytId & Wbxml.ELEMENT_TAG_ID);
    int out_intEventId;
    int intTemp;

    switch (bytTag)
    {
      case WmlTags.GO:
        intTemp = readGoEvent (false, hasContent(in_bytId));
        break;
      case WmlTags.PREV:
        intTemp = readPrevOrResfreshEvent (WmlTags.PREV, false);
        break;
      case WmlTags.REFRESH:
        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_ANCHOR_TAG:
        c_bolInAnchorElement = true;
        // Don't break. Need to process the tag as usual.
      default:
        String tag = c_clsDeck.resolveElementTag (bytTag);

        if (c_bolParsingTemplate)
        {
          c_intOpenElements++;
          c_clsStackEndTags.push (c_clsDeck.resolveEndTag (bytTag));
        } // if

        // If we are adding the parent element to the card vector also
        // add this element.
        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_bolParsingTemplate)
          {
            // If the element is a <do> then detemine if it was defined in
            // the card. If not add it to the card vector.
            conditionalAddToVector ((String) c_clsStackEndTags.pop ());
            closeElement ();
          } // if
        } //if
    } // switch

    return WmlTags.NO_EVENT;
  } // readElement

   /** Read attributes of an element.
   *  There are some attribute start tokens that include part or
   *  all of the attribute value as well. For example:
   *    type="accept" (0x38)
   *    href="http://" (0x4b)
   *
   *  In the first example, the attribute start token also includes
   *  the whole attribute value.
   *
   *  In the second example, the attribute start token also includes
   *  part of the attribute value. This means that there is a byte or
   *  bytes following, which contain the rest of the attribute value.
   *  @param  in_bytElement byte representation of the element.
   *  @return Event ID of an intrinsic event if one is invoked.
   */
  public int readAttributes (byte in_bytElement)
    throws IOException, NullPointerException, J2WapException
  {
    int out_intEventId;

    switch (in_bytElement)
    {
      case WmlTags.BYTE_TEMPLATE_TAG:
        return (readCardOrTemplateAttrs (false));
      case WmlTags.BYTE_DO_TAG:
        readDoAttributes ();
        break;
      case WmlTags.BYTE_ONEVENT:
        out_intEventId = readOnevent();
        if (c_bolParsingTemplate && out_intEventId != WmlTags.NO_EVENT)
          return out_intEventId;
        else
          return WmlTags.NO_EVENT;
      default:
        break;
    } // switch

    return WmlTags.NO_EVENT;
  } // readAttributes

  /**
   * Check if a do element of with the given name was defined in the parsed
   * card. The BrowserCore has a hashtable containing <do> names that were
   * defined in the last card parsed. Check if this <do> is in that hashable.
   * @param in_clsStr Name of do element.
   */
  private void checkIfDoIsInCard (String in_clsStr)
      throws IOException, NullPointerException
  {
    c_bolAddElementToCard = !c_clsVecDosInCard.contains (in_clsStr);
    if (c_bolAddElementToCard)
    {
      // Add the <do> attributes, held in temporary vector, to card vector.
      conditionalAddToVector (WmlTags.START_TAG_DO);
      int intElems = c_clsTempDoVec.size ();
      for (int i=0; i<intElems; i++)
        conditionalAddToVector (c_clsTempDoVec.elementAt (i));

      // Add do end tag to end tag stack.
      c_clsStackEndTags.push (WmlTags.END_TAG_DO);
    } // if

    c_clsTempDoVec.removeAllElements ();
  } // checkIfDefinedInCard

  /**
   * Add the object to the card vector only if required.
   * @param in_clsObject  Object to be conditionally added to the vector.
   */
  public void conditionalAddToVector (Object in_clsObject)
      throws IOException, NullPointerException
  {
  	if (in_clsObject != null)
	{
	    // </template> will be on the top of stack but we don't add it to the card.
	    // so if there is only one element on the stack don't add it to the card.
	    if (c_bolAddElementToCard && !c_clsStackEndTags.empty())
	      c_clsCardVec.addElement (in_clsObject);
	} // if
  } // conditionalAddToVector

  /**
   * Flags if desired card is currently being parsed.
   * @return  True if desired card is currently being parsed.
   */
  public boolean parsingDesiredCard ()
  {
    return false;
  } // parsingDesiredCard

  /**
   * Flags if desired card or the deck's template is currently being parsed.
   * @return  True if desired card or the deck's template is currently being
   *          parsed.
   */
  public boolean parsingDesiredCardOrTemplate ()
  {
    return true;
  } // parsingDesiredCardOrTemplate

  /**
   * Read attributes of a <code>do</code> element. When it encounters the "name"
   * attribute it is added to "c_clsVecDoElements". If "name" is not found, name
   * defaults to the value of the "type" attribute.
   */
  public void readDoAttributes ()
    throws IOException, NullPointerException, J2WapException
  {
    boolean bolFoundName  = false;
    byte    bytId         = nextByte ();
    int     intDelim;
    String  clsStrAttrStart = new String ();
    String  clsStrAttrVal = new String ();
    String  clsStrAttr    = new String ();
    String  clsStrName    = null;
    String  clsStrType    = null;

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

      // Extract the attribute start value for inspection.
      intDelim = clsStrAttr.indexOf (ATTR_DELIM);
      clsStrAttrStart = clsStrAttr.substring (0, intDelim);

      if (clsStrAttrStart.equals ("name"))
      {
        bolFoundName = true;
        clsStrName = clsStrAttr.substring (intDelim + ATTR_DELIM_LEN,
                                              clsStrAttr.length()-1);
        clsStrAttrVal = clsStrName;
      }
      else if (clsStrAttrStart.equals ("type"))
        clsStrType = clsStrAttr.substring (intDelim + ATTR_DELIM_LEN,
                                           clsStrAttr.length()-1);

      // Add to temp <do> vector.
      c_clsTempDoVec.addElement (clsStrAttr);
      bytId = nextByte ();
    } // while

    // If the "name" has not been found default it to the "type" attribute.
    if (!bolFoundName && (clsStrType != null))
    {
      clsStrName = clsStrType;
      c_clsTempDoVec.addElement ("name=" +  clsStrType + "\"");
    } // if

    // Have all the attributes. Add the end of attributes flag to vector.
    c_clsTempDoVec.addElement (WmlTags.ENDATTRIBS);

    checkIfDoIsInCard (clsStrName);

    // Clean up.
    clsStrAttrStart = null;
    clsStrAttrVal   = null;
    clsStrAttr      = null;
    clsStrName      = null;
    clsStrType      = null;
  } // readDoAttributes

  /**
   * Resets flag to stop elements being added to the card vector.
   */
  protected void closeElement ()
  {
    super.closeElement ();

    /* If there is only one end tag left on the end tag stack then it must
     * be the <code>template<code> end tag and therefore if adding an element
     * to the card, we have just added it's end tag to the card, thus finished
     * adding it.
     */
    if (c_clsStackEndTags.size () <= 1)
      c_bolAddElementToCard = false;
  } // closeElement

  /**
   * Determines if an element should be added to the card vector.
   * @return  True if an element should be added to the card vector.
   */
  protected boolean shouldResigterEvent ()
  {
    return c_bolAddElementToCard;
  } // shouldResigterEvent

} // class TemplateParser

⌨️ 快捷键说明

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