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

📄 wmldeck.java

📁 An open_source WAP browser. include Java code. support WML documents and WBMP images.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
  /**
   * Create and return a new WmlTask belonging to this
   * Deck.  This method may be invoked by WmlDeckBuilder
   * during construction of this deck.  The new deck is
   * pushed onto a stack of tasks held to the deck
   * and also onto a stack of task which are currently
   * open during parsing.
   *
   * @see WmlTask
   *
   **/
  public WmlTask newTask(){
    int i = tasks.size();
    WmlTask t =  new WmlTask(this, i);
    tasks.push(t);
    if(awaitingtask != null){
	    // we were waiting for this task to arrive!!
	    if(awaitingtask.equals("onenterforward")){
        currentcard.onenterforward("reval(getHref("+i+"));");
	    }else if(awaitingtask.equals("onenterbackward")){
        currentcard.onenterbackward("reval(getHref("+i+"));");
      }else if(awaitingtask.equals("ontimer")){
        currentcard.ontimer("reval(getHref("+i+"));");
      }
    }
    htasks.push(t); // temporary holding area.
    return(t);
  }

  /**
   * Removes a task from a stack of tasks which are open
   * during parsing of the WML content.  This method is invoked
   * by WmlDeckBuilder during construction of the deck.
   **/
  public void endTask(){
    htasks.pop();
  }

  /**
   * Returns the WmlTask at the specified index.
   *
   * @param index The index identifying the wanted task.
   */
  public WmlTask getTask(int index){
    return (WmlTask)tasks.elementAt(index);
  }
 
  /**
   * Returns the current task, if one exists.
   * This method may be invoked by the WmlDeckBuilder
   * during construction of this deck.
   */ 
  public WmlTask currentTask(){
    if(htasks.isEmpty()){
	    return(null);
    }
    return (WmlTask)htasks.peek();
  }

  /**
   * Returns the javascript required to invoke the
   * task at the specified index.
   *
   * @param index The index of the wanted reference.
   */
  public String getHref(int index){
    WmlTask t = this.getTask(index);
    String href = "alert(\"unknown task "+index+"\");";
    if(t != null){
	    href = t.getInvocation();
    }
    return href;
  }

  /**
   * Returns a count of the number of tasks belonging to
   * this deck.
   */
  public int countTasks(){
    return tasks.size();
  }

  /**
   * Resolves and returns a string representing a url.
   * This method may be invoked by WmlDeckBuilder during
   * construction of this deck in order to resolve any
   * url's contained within the deck.
   *
   * @param url A string representing the url to be resolved.
   */
  public String resolveUrl(String url){
    //System.err.println("resolving url:["+url+"]");
    //return docbase+"?url="+url;
    if(url == null || url.equals("")){
      //url = "#?????";
      url = sourceUrl.toString();
    }else if(url.startsWith("#")){
	    // return url;
    }else if(url.toLowerCase().startsWith("http://")
      || url.toLowerCase().startsWith("file://")){
	    // return url;
    }else{
	    //make the url relative to the
	    //originating doc.
	    try{
        url = new URL(sourceUrl, url).toString();
	    }
	    catch(java.net.MalformedURLException E){
        url = "#unknown";
	    }
    }
    //System.err.println("resolved to:["+url+"]");
    // protect any $variables in the url..
     int i=0;
     while(i<url.length()){
       switch(url.charAt(i)){
        case '$':
          url = (i>0?url.substring(0,i):"")
            +"$$"
            +(i<url.length()-1?url.substring(i+1):"");
          i+=2;
          break;
         default:
           i++;
       }
     }
    return(url);
  }

  /**
   * Dump all the cards belonging to this deck.
   * The dump() method will be invoked on each card
   * belonging to this deck.  This method is useful
   * for debugging purposes.
   */
  public void dump(){
    Enumeration e = deck.elements();
    while(e.hasMoreElements()){
	    WmlCard c = (WmlCard) e.nextElement();
	    c.dump();
    }
  }

  /**
   * Expand any $variables in the string with
   * their corresponding values.
   * Escape modes are parsed but ignored.
   * eg $(var:unesc) == $(var) == $(var:escape) == $(var:noesc)
   *
   * @param s The string which may contain variables 
   */
  public String expandVariables(String s) 
    throws java.io.IOException{
    // expand $variables with their
    // corresponding values.
    if(s==null){
	    return null;
    }
    StringBuffer outstr = new StringBuffer();
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    ByteArrayOutputStream var = new ByteArrayOutputStream();
    ByteArrayOutputStream esc = new ByteArrayOutputStream();
    PushbackInputStream p = 
	    new PushbackInputStream(new ByteArrayInputStream(s.getBytes()));
    int c;
    int state = st_outvar;
    boolean vardelim=false; // $var vs $(var)
    int escmode = V_NOESC;

    while((c=p.read())!=-1){
	    switch(state){
        case st_outvar:
          switch(c){
            case '$':
              state = st_beginvar;
              vardelim = false;
              var.reset();
              esc.reset();
              break;
            default:
              os.write(c);
              break;
          }
          break;
        case st_beginvar:
          switch(c){
            case '$':
              state = st_outvar;
              os.write(c);
              if(vardelim){ // $($) ?
                int x;
                if((x=p.read())!=-1){
                  if(x != ')'){
                    p.unread(x);
                  }
                }
              }
              break;
            case '(':
              state = st_beginvar;
              vardelim =  true;
              break;
            default:
              if((c >= 'A' && c <= 'Z')
                 || (c >= 'a' && c <= 'z')
                 || c == '_')
              {
                state = st_invar;
                var.reset();
                var.write(c);
              }else{
                //hmmn... not a variable character
                System.err.println("unexpected byte ["+c+"] in variable name... ignoring");
                state = st_outvar;
                os.write('$');
                p.unread(c);
              }
              break;
          }
          break;
          
        case st_invar:
          switch(c){
            case ':':
              if(vardelim){ // $(foo:esc) vs $foo:esc
                state = st_esc;
                break;
              }// otherwise, fallthrough to default
            default:
              if((c >= 'A' && c <= 'Z')
                 || (c >= 'a' && c <= 'z')
                 || (c >= '0' && c <= '9')
                 || c == '_')
              {
                state = st_invar;
                var.write(c);
              }else{
                state = st_outvar;
                outstr.append(os.toString());
                os.reset();
                if(var.size()>0){
                  outstr.append(this.getVar(var.toString()));
                 }
                var.reset();
                //end of variable...
                if((!vardelim) || c != ')'){
                  p.unread(c);
                }
              }
              break;
          }
          break;
        case st_esc:
          if((c >= 'A' && c <= 'Z')
             || (c >= 'a' && c <= 'z')){
            esc.write(c);
          }else if(c != ')'){
            // unexpected esc char eg: $(foo:esc bar
            System.err.println("unexpected byte ["+c+"] in variable esc mode");
             p.unread(c);
             c = ')';
          }
          if(c == ')'){
            p.unread(c);
            state = st_invar;
            String e = esc.toString().toLowerCase();
            if(e.equals("e") || e.equals("escape")){
              escmode = V_ESCAPE;
            }else if(e.equals("n") || e.equals("noesc")){
              escmode = V_NOESC;
            }else if(e.equals("u") || e.equals("unesc")){
              escmode = V_UNESC;
            }else{
              System.err.println("Unrecognised variable escape mode ["+e+"]");
              escmode = V_NOESC;
            }
          }
          break;
        default:
          break;
	    }  
    }
    if(os.size()>0){    
	    outstr.append(os.toString());
    }
    if(var.size()>0){
      outstr.append(this.getVar(var.toString()));
    }
    return(outstr.toString());
  }
}

⌨️ 快捷键说明

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