home *** CD-ROM | disk | FTP | other *** search
/ Chip 1997 October / Chip_1997-10_cd.bin / tema / sybase / powerj / samples.z / PageSeeker.wxc < prev    next >
Text File  |  1997-01-28  |  6KB  |  253 lines

  1. Save Format v2.0(1)
  2. @begin ClassFile "PageSeeker"
  3.  Exported 1;
  4.  Abstract 0;
  5.  Interface 0;
  6.  PackageName "";
  7.  Language "Java";
  8.  
  9.  @begin UserFunction "run()"
  10.   GencodeSrcLine 13;
  11.   FunctionName "PageSeeker::run()";
  12.  @end;
  13.  
  14.  @begin UserFunction "Prototype for run()"
  15.   Private 1;
  16.   GencodeSrcLine -1;
  17.   FunctionName "PageSeeker::Prototype for run()";
  18.  @end;
  19.  
  20.  @begin UserFunction "loadPage(URL pageURL)"
  21.   GencodeSrcLine 60;
  22.   FunctionName "PageSeeker::loadPage(URL pageURL)";
  23.  @end;
  24.  
  25.  @begin UserFunction "Prototype for loadPage(URL pageURL)"
  26.   Private 1;
  27.   GencodeSrcLine -1;
  28.   FunctionName "PageSeeker::Prototype for loadPage(URL pageURL)";
  29.  @end;
  30.  
  31.  @begin UserFunction "extractLinks(String webPage)"
  32.   GencodeSrcLine 74;
  33.   FunctionName "PageSeeker::extractLinks(String webPage)";
  34.  @end;
  35.  
  36.  @begin UserFunction "Prototype for extractLinks(String webPage)"
  37.   Private 1;
  38.   GencodeSrcLine -1;
  39.   FunctionName "PageSeeker::Prototype for extractLinks(String webPage)";
  40.  @end;
  41.  
  42.  @begin UserFunction "PageSeeker(String pageURL)"
  43.   GencodeSrcLine 114;
  44.   FunctionName "PageSeeker::PageSeeker(String pageURL)";
  45.  @end;
  46.  
  47.  @begin UserFunction "Prototype for PageSeeker(String pageURL)"
  48.   Private 1;
  49.   GencodeSrcLine -1;
  50.   FunctionName "PageSeeker::Prototype for PageSeeker(String pageURL)";
  51.  @end;
  52.  
  53.  @begin HPPPrefixBlock
  54. @begin-code HPPPrefix
  55.  
  56. // add your custom import statements here
  57. import java.util.*;
  58. import java.net.*;
  59. import java.io.*;
  60.  
  61. @end-code;
  62.   GencodeSrcLine 6;
  63.  @end;
  64.  
  65.  @begin ClassContentsBlock
  66. @begin-code ClassContents
  67.  
  68.     // add your data members here
  69.     private final static int MAXTHREADS = 4;
  70.     private static Hashtable _linkTable = new Hashtable();
  71.     URL _pageToFetch;
  72.     static TokenIssuer _threadLimiter = new TokenIssuer(MAXTHREADS);
  73.     WebCrawler _parent;
  74.  
  75. @end-code;
  76.   GencodeSrcLine 128;
  77.  @end;
  78.  
  79. @begin-code BaseClassList
  80.  
  81. extends Thread
  82.  
  83. @end-code;
  84.  
  85. @begin-code GeneratedClassContents
  86.  
  87.  
  88. @end-code;
  89.  
  90. @begin-code Code "PageSeeker::run()"
  91.  
  92. //****************************
  93. /**
  94.  * Creates a @CLASSNAME@ object which performs the following:
  95.  * 1. loads a page from a given URL
  96.  * 2. parses out all the imbedded links
  97.  * 3. checks links against a hashtable and adds them if they
  98.  *    are new
  99.  * 4. spawns new @CLASSNAME@ threads to visit each new link
  100. */
  101.  
  102.     public void run()
  103.     //****************************
  104.     {
  105.         this.setPriority(MIN_PRIORITY);
  106.         String webPage = new String();
  107.         Vector pageLinks;
  108.  
  109.         // only run if there is a free token
  110.         _threadLimiter.getToken();
  111.  
  112.         try {
  113.             webPage = loadPage(_pageToFetch);
  114.         } catch (java.io.IOException badIO) {
  115.             System.out.println(_pageToFetch + " returned a bad I/O");
  116.         }
  117.  
  118.         pageLinks = extractLinks(webPage);
  119.  
  120.         Enumeration enum = pageLinks.elements();
  121.         while(enum.hasMoreElements()) {
  122.             String page = (String) enum.nextElement();
  123.  
  124.             if ( !_linkTable.containsKey(page)) 
  125.             {
  126.                 _linkTable.put(page, page);
  127.                 _parent.addLink(page);
  128.                 new @CLASSNAME@(_parent, page);
  129.             }
  130.         }
  131.  
  132.         _threadLimiter.relinquishToken();
  133.  
  134.         // wait some random time to give all waiting threads a chance
  135.         try {
  136.             Thread.sleep( (int) (Math.random()*200) );
  137.         } catch (Exception e) {}
  138.     }
  139.  
  140. @end-code;
  141.  
  142. @begin-code Code "PageSeeker::Prototype for run()"
  143.  
  144.     public:
  145.         void run();
  146.  
  147. @end-code;
  148.  
  149. @begin-code Code "PageSeeker::loadPage(URL pageURL)"
  150.  
  151.     public String loadPage(URL pageURL) throws IOException
  152.     //****************************
  153.     {
  154.  
  155.         InputStream is = pageURL.openStream();
  156.         int oneChar;
  157.         StringBuffer sb = new StringBuffer();
  158.  
  159.         while ((oneChar=is.read()) != -1)
  160.             sb.append((char)oneChar);
  161.         is.close();
  162.         return sb.toString();
  163.  
  164.     }
  165.  
  166. @end-code;
  167.  
  168. @begin-code Code "PageSeeker::Prototype for loadPage(URL pageURL)"
  169.  
  170.     public:
  171.         String loadPage(URL pageURL);
  172.  
  173. @end-code;
  174.  
  175. @begin-code Code "PageSeeker::extractLinks(String webPage)"
  176.  
  177.     public Vector extractLinks(String webPage)
  178.     //****************************
  179.     {
  180.         int lastPosition = 0;
  181.         int endOfURL;
  182.         String link;
  183.         Vector newLinks = new Vector();
  184.  
  185.         while(lastPosition != -1 ) {
  186.             lastPosition = webPage.indexOf("http://", lastPosition);
  187.  
  188.             if (lastPosition != -1) {
  189.  
  190.                 endOfURL = webPage.indexOf(">", lastPosition + 1 );
  191.  
  192.                 // extract found hypertext link
  193.                 link = webPage.substring(lastPosition, endOfURL);
  194.                 link = link.trim();
  195.                 if (link.endsWith("\"")) {
  196.                     link = link.substring(0, link.length() - 1 );
  197.                 }
  198.  
  199.                 // ignore references
  200.                 if (link.indexOf("#") != -1) {
  201.                     link = link.substring(0, link.indexOf("#"));
  202.                 }
  203.  
  204.                 // discard links which point explicitly to images
  205.                 if ( link.endsWith(".gif") ||
  206.                     link.endsWith(".jpg")    ) {
  207.                     ;
  208.                 } else {    // collect all others
  209.                     newLinks.addElement( link );
  210.                 }
  211.                 lastPosition++;     // skip current link
  212.             }
  213.         }
  214.         return newLinks;
  215.  
  216.     }
  217.  
  218. @end-code;
  219.  
  220. @begin-code Code "PageSeeker::Prototype for extractLinks(String webPage)"
  221.  
  222.     public:
  223.         Vector extractLinks(String webPage);
  224.  
  225. @end-code;
  226.  
  227. @begin-code Code "PageSeeker::PageSeeker(String pageURL)"
  228.  
  229.     public @CLASSNAME@(WebCrawler parent, String startingPage)
  230.     //****************************
  231.  
  232.     {
  233.         _parent = parent;
  234.         try {
  235.             _pageToFetch = new URL(startingPage);
  236.             this.setName(startingPage);
  237.             start();
  238.         } catch (MalformedURLException badURL) {
  239.             System.out.println(startingPage + " bad URL");
  240.         }
  241.  
  242.     }
  243.  
  244. @end-code;
  245.  
  246. @begin-code Code "PageSeeker::Prototype for PageSeeker(String pageURL)"
  247.  
  248.     public:
  249.         @@CLASSNAME@(String pageURL);
  250.  
  251. @end-code;
  252. @end;
  253.