// created: 23.11.00
// modified: 29.12.00

import java.util.*;
import java.io.*;
import java.net.*;
import javax.swing.text.html.parser.*;
import java.util.*;

/*****************/
/* class WebPage */
/*****************/

public class WebPage
 {

 String theName = "";
 String theLocation = null;
 URL theURL = null;
 boolean parsed = false;
 //Attribute [] theAttributes = new Attribute[3];
 String theExtension = "";
 int amountOfText = 0;
 int numberOfImages = 0;
 int numberOfTables = 0;
 int numberOfLinks = 0;
 int numberOfImageDownloads = 0;
 int numberOfSoundDownloads = 0;
 int numberOfVideoDownloads = 0; 
 int numberOfOtherDownloads = 0;
 boolean frames = false;
 float theValue = 0.0f;
  
 /***************/
 /* constructor */
 /***************/

 public WebPage(String loc) throws MalformedURLException
  {
  theLocation = loc;
  theURL = new URL(loc);
  theExtension = parseForExtension(loc);
  }

 /****************/
 /* method parse */
 /*********************************************************************/
 /* parses the contents of the URL line by line to set the parameters */
 /*********************************************************************/
 
 public void parse() throws IOException
  {
  InputStreamReader reader = new InputStreamReader(theURL.openStream());
  ParserDelegator parser = new ParserDelegator();
  parser.parse(reader,new MyCallback(this),false);
  parsed = true;
  }

 /****************************/
 /* method parseForExtension */
 /****************************/

 private String parseForExtension(String loc)
  {
  StringTokenizer tokenizer = new StringTokenizer(loc,"/");
  String siteName = tokenizer.nextToken();
  siteName = tokenizer.nextToken();
  tokenizer = new StringTokenizer(siteName,".");
  String previousToken = "";
  String token = "";
  if (tokenizer.countTokens()>1)  // only set an extension if there is one
    while (tokenizer.hasMoreTokens())
     {
     previousToken = token;
     token = tokenizer.nextToken();
     }
  String ext;
  if ((previousToken.equals("co") || previousToken.equals("ac")) && token.length() == 2)
    ext = new String(previousToken+"."+token);
  else 
    ext = new String(token);  
  return ext;
  }

 /***************/
 /* set methods */
 /***************/

 void setName(String name)
  {
  theName = name;
  }

 void setValue(float value)
  {
  theValue = value;
  }

 void setNumLinks(int num)
  {
  if (num >= 0)
    numberOfLinks = num;
  }

 void setNumImages(int num)
  {
  if (num >= 0)
   numberOfImages = num;
  }

 void setNumTables(int num)
  {
  if (num >= 0)
   numberOfTables = num;
  }

 void setNumOtherDownloads(int num)
  {
  if (num >= 0)
   numberOfOtherDownloads = num;
  }

 void setNumImageDownloads(int num)
  {
  if (num >= 0)
   numberOfImageDownloads = num;
  }

 void setNumSoundDownloads(int num)
  {
  if (num >= 0)
   numberOfSoundDownloads = num;
  }

 void setNumVideoDownloads(int num)
  {
  if (num >= 0)
   numberOfVideoDownloads = num;
  }

 void setAmountText(int num)
  {
  if (num >= 0)
   amountOfText = num;
  }
 
 void setFrames(boolean bool)
  {
  frames = bool;
  } 

 /***************/
 /* get methods */
 /***************/

 public float getValue()
  {
  return theValue;
  }

 public String getName()
  {
  return theName;
  }
 
 public String getLocation()
  {
  return theLocation;
  }

 public String getExtension()
  {
  return theExtension;
  }

 public int getNumLinks()
  {
  return numberOfLinks;
  }
 
 public int getNumImages()
  {
  return numberOfImages;
  }

 public int getNumTables()
  {
  return numberOfTables;
  }

 public int getNumDownloads()
  {
  return numberOfOtherDownloads+numberOfImageDownloads+numberOfSoundDownloads+numberOfVideoDownloads;
  }

 public int getNumOtherDownloads()
  {
  return numberOfOtherDownloads;
  }
 
 public int getNumImageDownloads()
  {
  return numberOfImageDownloads;
  }

 public int getNumSoundDownloads()
  {
  return numberOfSoundDownloads;
  }

 public int getNumVideoDownloads()
  {
  return numberOfVideoDownloads;
  } 

 public int getAmountText()
  {
  return amountOfText;
  }

 public boolean hasFrames()
  {
  return frames;
  }

 boolean isParsed()
  {
  return parsed;
  }

 /**************************/
 /* method clearAttributes */
 /**************************/

 void clearAttributes()
  {
  theName = "";
  amountOfText = 0;
  numberOfImages = 0;
  numberOfTables = 0;
  numberOfLinks = 0;
  numberOfOtherDownloads = 0;
  numberOfImageDownloads = 0;
  numberOfSoundDownloads = 0;
  numberOfVideoDownloads = 0;
  frames = false;
  theValue = 0.0f;  
  parsed = false;
  }
 
 }
