现在的位置: 首页 > 综合 > 正文

HTMLPage类的说明

2013年07月27日 ⁄ 综合 ⁄ 共 9863字 ⁄ 字号 评论关闭

 HTMLPage类:

HTMLPage类中主要也就几种用途,而从HTMLPage类中抓取图片是一个非常重要的一个功能,诚然还有超链接和表单。而在HTMLPage类的内置类Parser中,大部分工作都是由handleSimpleTag(简单标签)和和handleStartTag(起始标签方法来完成。

详细代码清单如下:

  1. package com.heaton.bot;
  2. import java.util.*;
  3. import com.heaton.bot.*;
  4. import java.net.*;
  5. import java.io.*;
  6. import javax.swing.text.*;
  7. import javax.swing.text.html.*;
  8. /**
  9.  * The HTMLPage class is used to parse an HTML page and store
  10.  * that page, in a parsed form, in memory.
  11.  * are exchanged with a webserver.
  12.  */
  13. public class HTMLPage {
  14.   /**
  15.    * A list of images on this page.
  16.    */
  17.   protected Vector images = new Vector();
  18.   /**
  19.    * A list of links on this page.
  20.    */
  21.   protected Vector links = new Vector();
  22.   /**
  23.    * A list of forms on this page.
  24.    */
  25.   protected Vector forms = new Vector();
  26.   /**
  27.    * The underlying HTTP object for this page.
  28.    */
  29.   protected HTTP http;
  30.   /**
  31.    * The base URL to resolve relative URL's.
  32.    */
  33.   protected String base;
  34.   /**
  35.    * Construct an HTMLPage object.
  36.    *
  37.    * @param http The HTTP object(or subclass) to use to
  38.    * download pages.
  39.    */
  40.   public HTMLPage(HTTP http)
  41.   {
  42.     this.http = http;
  43.   }
  44.   /**
  45.    * Called to open a page and read it in. If null
  46.    * is specified for the callback(回调), then the other
  47.    * methods in this class may be used to look at
  48.    * images, links and forms.
  49.    * open是进入HTMLPage类的入口点。
  50.    * @param url The URL to read.
  51.    * @param callback A callback class to handle the parse, or null
  52.    * to use the built in one.
  53.    * @exception java.io.IOException
  54.    * @exception javax.swing.text.BadLocationException
  55.    */
  56.   public void open(String url,
  57.                    HTMLEditorKit.ParserCallback callback)
  58.   throws IOException,BadLocationException
  59.   {
  60.     http.send(url,null);
  61.     base = url;
  62.     processPage(callback);
  63.   }
  64.   /**
  65.    * Internal function called to start the parse.
  66.    *
  67.    * @param callback The callback object to use.
  68.    * @exception java.io.IOException
  69.    */
  70.   protected void processPage(HTMLEditorKit.ParserCallback callback)
  71.   throws IOException
  72.   {
  73.     /*
  74.      * 创建一个字符串阅读器。
  75.      */
  76.     StringReader r = new StringReader(http.getBody());
  77.     /*
  78.      * 创建新的解析器。
  79.      */
  80.     HTMLEditorKit.Parser parse = new HTMLParse().getParser();
  81.     /*
  82.      * 程序检查是否提供了定制的回调类。如果提供了回调类,则
  83.      * 使用该会掉泪,从而结束了HTMLPage类的工作。如果没有提
  84.      * 供回调类,HTMLPage则使用内置的回调类,该回调类为Parser。
  85.      */
  86.     if ( callback==null ) {
  87.       HTMLPage.Parser p=new HTMLPage.Parser();
  88.       parse.parse(r,p,true);
  89.     } else
  90.       parse.parse(r,callback,false);
  91.   }
  92.   /**
  93.    * Get the underlying HTTP object that was
  94.    * sent to the constructor.
  95.    *
  96.    * @return The underlying HTTP object.
  97.    */
  98.   public HTTP getHTTP()
  99.   {
  100.     return http;
  101.   }
  102.   /**
  103.    * Get a list of all of the links from this page.
  104.    * If this is to be used then null must have been
  105.    * passed as the callback object to the open method.
  106.    *
  107.    * @return All links on this page.
  108.    */
  109.   public Vector getLinks()
  110.   {
  111.     return links;
  112.   }
  113.   /**
  114.    * Get a list of all of the images from this page.
  115.    * If this is to be used then null must have been
  116.    * passed as the callback object to the open method.
  117.    *
  118.    * @return A list of all of the images on this page.
  119.    */
  120.   public Vector getImages()
  121.   {
  122.     return images;
  123.   }
  124.   /**
  125.    * Get a list of all of the forms from this page.
  126.    * If this is to be used then null must have been
  127.    * passed as the callback object to the open method.
  128.    *
  129.    * @return A list of forms.
  130.    */
  131.   public Vector getForms()
  132.   {
  133.     return forms;
  134.   }
  135.   /**
  136.    * Called to perform a post for the specified form.
  137.    *
  138.    * @param form The form object to post.
  139.    * @exception java.io.IOException
  140.    */
  141.   public void post(HTMLForm form)
  142.   throws IOException
  143.   {
  144.     http.getClientHeaders().set("Content-Type",
  145.                                  "application/x-www-form-urlencoded");
  146.     http.send(form.getAction(),form.toString());
  147.     processPage(null);
  148.   }
  149.   /**
  150.    * Get the URL that is represented by this page.
  151.    *
  152.    * @return The URL that is represented by this page.
  153.    */
  154.   public String getURL()
  155.   {
  156.     return http.getURL();
  157.   }
  158.   /**
  159.    * Called internally to add an image to the list.
  160.    *
  161.    * @param img The image to add.
  162.    */
  163.   protected void addImage(String img)
  164.   {
  165.     img = URLUtility.resolveBase(base,img);
  166.     for ( int i=0;i<images.size();i++ ) {
  167.       String s = (String)images.elementAt(i);
  168.       if ( s.equalsIgnoreCase(img) )
  169.         return;
  170.     }
  171.     images.addElement(img);
  172.   }
  173.   /**
  174.    * A HTML parser callback used by this class to
  175.    * detect links, images and forms.
  176.    * 定义一个名为Parser的内部类,该类实现一个专门的解析器回
  177.    * 调函数,用来跟踪超链接,图像和表单。
  178.    */
  179.   protected class Parser
  180.     extends HTMLEditorKit.ParserCallback {
  181.     /**
  182.      * Used to build up data for an HTML form.
  183.      */
  184.     protected HTMLForm tempForm;
  185.     /**
  186.      * Used to build up options for an HTML form.
  187.      */
  188.     protected AttributeList tempOptions;
  189.     /**
  190.      * Used to build up options for an HTML form.
  191.      */
  192.     protected Attribute tempElement = new Attribute();
  193.     /**
  194.      * Holds the prompt text(just before or after a control.
  195.      */
  196.     protected String tempPrompt = "";
  197.     /**
  198.      * Holds the link till the end link is found
  199.      */
  200.     protected Link tempLink;
  201.     /**
  202.      * Called to handle comments.
  203.      *
  204.      * @param data The comment.
  205.      * @param pos The position.
  206.      */
  207.     public void handleComment(char[] data,int pos)
  208.     {
  209.     }
  210.     /**
  211.      * Called to handle an ending tag.
  212.      *
  213.      * @param t The ending tag.
  214.      * @param pos The position.
  215.      */
  216.     public void handleEndTag(HTML.Tag t,int pos)
  217.     {
  218.       if ( t==HTML.Tag.OPTION ) {
  219.         if ( tempElement!=null ) {
  220.           tempElement.setName(tempPrompt);
  221.           tempOptions.add(tempElement);
  222.           tempPrompt = "";
  223.         }
  224.         tempElement = null;
  225.       } else if ( t==HTML.Tag.FORM ) {
  226.         if ( tempForm!=null )
  227.           forms.addElement(tempForm);
  228.         tempPrompt = "";
  229.       } else if ( t==HTML.Tag.A ) {
  230.         if ( tempLink!=null )
  231.           tempLink.setPrompt(tempPrompt);
  232.         tempPrompt = "";
  233.       }
  234.     }
  235.     /**
  236.      * Called to handle an error. Not used.
  237.      *
  238.      * @param errorMsg The error.
  239.      * @param pos The position.
  240.      */
  241.     public void handleError(String errorMsg,int pos)
  242.     {
  243.     }
  244.     /**
  245.      * Called to handle a simple tag.
  246.      *
  247.      * @param t The simple tag.
  248.      * @param a The attribute list.
  249.      * @param pos The position.
  250.      */
  251.     public void handleSimpleTag(HTML.Tag t,
  252.                                 MutableAttributeSet a,int pos)
  253.     {
  254.       handleStartTag(t,a,pos);
  255.     }
  256.     /**
  257.      * Called to handle a starting tag.
  258.      *
  259.      * @param t The starting tag.
  260.      * @param a The attribute list.
  261.      * @param pos The position.
  262.      */
  263.     public void handleStartTag(HTML.Tag t,
  264.                                MutableAttributeSet a,int pos)
  265.     {
  266.       String type = "";
  267.       // is it some sort of a link
  268.       String href = (String)a.getAttribute(HTML.Attribute.HREF);
  269.       if ( (href!=null) && (t!=HTML.Tag.BASE) ) {
  270.         String alt = (String)a.getAttribute(HTML.Attribute.ALT);
  271.         Link link = new Link(
  272.                             alt,
  273.                             URLUtility.resolveBase(base,href),
  274.                             null);
  275.         links.addElement(tempLink=link);
  276.       } else if ( t==HTML.Tag.OPTION ) {
  277.         tempElement = new Attribute();
  278.         tempElement.setName("");
  279.         tempElement.setValue((String)a.getAttribute(HTML.Attribute.VALUE));
  280.       } else if ( t==HTML.Tag.SELECT ) {
  281.         if ( tempForm==null )
  282.           return;
  283.         tempOptions = new AttributeList();
  284.         tempForm.addInput(
  285.                           (String)a.getAttribute(HTML.Attribute.NAME),
  286.                           null,
  287.                           "select",
  288.                           tempPrompt,
  289.                           tempOptions);
  290.         tempPrompt = "";
  291.       } else if ( t==HTML.Tag.TEXTAREA ) {
  292.         if ( tempForm==null )
  293.           return;
  294.         tempForm.addInput(
  295.                           (String)a.getAttribute(HTML.Attribute.NAME),
  296.                           null,
  297.                           "textarea",
  298.                           tempPrompt,
  299.                           null);
  300.         tempPrompt = "";
  301.       }
  302.       else if ( t==HTML.Tag.FORM ) {
  303.         if ( tempForm!=null )
  304.           forms.addElement(tempForm);
  305.         String action =
  306.         (String)a.getAttribute(HTML.Attribute.ACTION);
  307.         if ( action!=null ) {
  308.           try {
  309.             URL aurl = new URL(new URL(http.getURL()),action);
  310.             action = aurl.toString();
  311.           } catch ( MalformedURLException e ) {
  312.             action = null;
  313.           }
  314.         }
  315.         tempForm = new HTMLForm(
  316.                                 (String)a.getAttribute(HTML.Attribute.METHOD),
  317.                                 action );
  318.         tempPrompt = "";
  319.       } else if ( t==HTML.Tag.INPUT ) {
  320.         if ( tempForm==null )
  321.           return;
  322.         if ( t!=HTML.Tag.INPUT ) {
  323.           type = (String)a.getAttribute(HTML.Attribute.TYPE);
  324.           if ( type==null )
  325.             return;
  326.         } else
  327.           type = "select";
  328.         if ( type.equalsIgnoreCase("text") ||
  329.              type.equalsIgnoreCase("edit") ||
  330.              type.equalsIgnoreCase("password") ||
  331.              type.equalsIgnoreCase("select") ||
  332.              type.equalsIgnoreCase("hidden") ) {
  333.           tempForm.addInput(
  334.                             (String)a.getAttribute(HTML.Attribute.NAME),
  335.                             (String)a.getAttribute(HTML.Attribute.VALUE),
  336.                             type,
  337.                             tempPrompt,
  338.                             null);
  339.           tempOptions = new AttributeList();
  340.         }
  341.       } else if ( t==HTML.Tag.BASE ) {
  342.         href = (String)a.getAttribute(HTML.Attribute.HREF);
  343.         if ( href!=null )
  344.           base = href;
  345.       } else if ( t==HTML.Tag.IMG ) {
  346.         String src = (String)a.getAttribute(HTML.Attribute.SRC);
  347.         if ( src!=null )
  348.           addImage(src);
  349.       }
  350.     }
  351.     /**
  352.      * Called to handle text.
  353.      *
  354.      * @param data The text.
  355.      * @param pos The position.
  356.      */
  357.     public void handleText(char[] data,int pos)
  358.     {
  359.       tempPrompt += new String(data) + " ";
  360.     }
  361.   }
  362. }

抱歉!评论已关闭.