// created: 28.12.00
// modified:

import javax.swing.text.*;
import javax.swing.text.html.*;

class MyCallback extends HTMLEditorKit.ParserCallback
 {

 WebPage thePage = null;
 boolean title = false; // indicates when to set the title
 
 MyCallback(WebPage page)
  {
  thePage = page;
  thePage.clearAttributes();
  }

 public void handleText(char[] data, int pos)
  { 
  if (title)
    {
    title = false;
    thePage.setName(String.valueOf(data));
    }
  thePage.setAmountText(thePage.getAmountText()+data.length);
  }

 public void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos)
  {
  if (t == HTML.Tag.TITLE)
    title = true;
  else if (t == HTML.Tag.A)
    {
    String theAttribute = (String) a.getAttribute(HTML.Attribute.HREF);
    if (theAttribute == null)  // if the attribute is null, do nothing
      return;
    if (theAttribute.toLowerCase().endsWith(".html") 
        || theAttribute.toLowerCase().endsWith(".htm"))  
      {
      thePage.setNumLinks(thePage.getNumLinks()+1);
      }
    else if (theAttribute.toLowerCase().endsWith(".jpg") 
             || theAttribute.toLowerCase().endsWith(".gif")
             || theAttribute.toLowerCase().endsWith(".bmp"))
      {
      thePage.setNumImageDownloads(thePage.getNumImageDownloads()+1);
      }
    else if (theAttribute.toLowerCase().endsWith(".mp3") 
             || theAttribute.toLowerCase().endsWith(".wav")
             || theAttribute.toLowerCase().endsWith(".midi")
             || theAttribute.toLowerCase().endsWith(".mid"))
      {
      thePage.setNumSoundDownloads(thePage.getNumSoundDownloads()+1);
      }
    else if (theAttribute.toLowerCase().endsWith(".mov") 
             || theAttribute.toLowerCase().endsWith(".qt")
             || theAttribute.toLowerCase().endsWith(".mpeg")
             || theAttribute.toLowerCase().endsWith(".mpg")
             || theAttribute.toLowerCase().endsWith(".avi"))
      {
      thePage.setNumVideoDownloads(thePage.getNumVideoDownloads()+1);
      }
    else
      {
      thePage.setNumOtherDownloads(thePage.getNumOtherDownloads()+1);
      }
    }
  else if (t == HTML.Tag.TABLE)
    thePage.setNumTables(thePage.getNumTables()+1);  
  }

 public void handleEndTag(HTML.Tag t, int pos)
  {
  }

 public void handleSimpleTag(HTML.Tag t, MutableAttributeSet a, int pos)
  {
  if (t == HTML.Tag.IMG)
    thePage.setNumImages(thePage.getNumImages()+1);
  else if (t == HTML.Tag.FRAME)
    thePage.setFrames(true);
  }

 }