HtmlParser类的说明

 HtmlParser类:

这是一个处理网页源文件的类。这里涉及到了比较底层的东西。由于现在无法调试,而且无碍大局,暂时就不深究它了。

没事的时候可以看看:

  1. /**
  2.  * The HTMLParse class is used to parse an HTML page.  It is
  3.  * just a utility class, and does NOT store any values.
  4.  */
  5. package com.heaton.bot;
  6. import com.heaton.bot.*;
  7. public class HTMLParser extends Parse {
  8.     
  9.   public HTMLTag getTag()
  10.   {
  11.     int i;
  12.     HTMLTag tag = new HTMLTag();
  13.     tag.setName(this.tag);
  14.     /*
  15.      * 派生类中的对象可以直接调用其父类的方法
  16.      */
  17.     for ( i=0;i<vec.size();i++ )
  18.       tag.add( (Attribute)get(i).clone() );
  19.     return tag;
  20.   }
  21.   public String buildTag()
  22.   {
  23.     String buffer="<";
  24.     buffer+=tag;
  25.     int i=0;
  26.     while ( get(i)!=null ) 
  27.     {// has attributes
  28.       buffer+=" ";
  29.       if ( get(i).getValue() == null ) 
  30.       {
  31.         if ( get(i).getDelim()!=0 )
  32.           buffer+=get(i).getDelim();
  33.         buffer+=get(i).getName();
  34.         if ( get(i).getDelim()!=0 )
  35.           buffer+=get(i).getDelim();
  36.       } 
  37.       else 
  38.       {
  39.         buffer+=get(i).getName();
  40.         if ( get(i).getValue()!=null ) {
  41.           buffer+="=";
  42.           if ( get(i).getDelim()!=0 )
  43.             buffer+=get(i).getDelim();
  44.           buffer+=get(i).getValue();
  45.           if ( get(i).getDelim()!=0 )
  46.             buffer+=get(i).getDelim();
  47.         }
  48.       }
  49.       i++;
  50.     }
  51.     buffer+=">";
  52.     return buffer;
  53.   }
  54.   protected void parseTag()
  55.   {
  56.     idx++;
  57.     tag="";
  58.     clear();
  59.     
  60.     /*
  61.      * Is it a comment?
  62.      * 如果是注释。
  63.      */
  64.     if ( (source.charAt(idx)=='!') &
  65.          (source.charAt(idx+1)=='-')&
  66.          (source.charAt(idx+2)=='-') ) 
  67.     {
  68.       while ( !eof() ) 
  69.       {
  70.         if ( (source.charAt(idx)=='-') &
  71.              (source.charAt(idx+1)=='-')&
  72.              (source.charAt(idx+2)=='>') )
  73.           break;
  74.         if ( source.charAt(idx)!='/r' )
  75.           tag+=source.charAt(idx);
  76.         idx++;
  77.       }
  78.       tag+="--";
  79.       idx+=3;
  80.       parseDelim=0;
  81.       return;
  82.     }
  83.     // Find the tag name
  84.     while ( !eof() ) 
  85.     {
  86.       if ( isWhiteSpace(source.charAt(idx)) || 
  87.               (source.charAt(idx)=='>') )
  88.         break;
  89.       tag+=source.charAt(idx);
  90.       idx++;
  91.     }
  92.     eatWhiteSpace();
  93.     // get the attributes
  94.     while ( source.charAt(idx)!='>' ) {
  95.       parseName = "";
  96.       parseValue = "";
  97.       parseDelim=0;
  98.       parseAttributeName();
  99.       if( eof() )
  100.           break;
  101.       
  102.       if ( source.charAt(idx)=='>' ) {
  103.         addAttribute();
  104.         break;
  105.       }
  106.       // get the value(if any)
  107.       parseAttributeValue();
  108.       addAttribute();
  109.     }
  110.     idx++;
  111.   }
  112.   public char get()
  113.   {
  114.     if ( source.charAt(idx)=='<' ) {
  115.       char ch=Character.toUpperCase(source.charAt(idx+1));
  116.       if ( (ch>='A') && (ch<='Z') || (ch=='!') || (ch=='/') ) {
  117.         parseTag();
  118.         return 0;
  119.       } else return(source.charAt(idx++));
  120.     } else return(source.charAt(idx++));
  121.   }
  122. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值