Code

/*
Please download the zip file from <a>http://www.cc.gatech.edu/~parthb/HTML.zip</a>

Extract this zip in the default folder and run the file as you would in processing.

*/
//Jazz happens to be one of my favorite genres of music, and I enjoy it due to its unstructured nature. 
// HTML as we know follows this strict tag structure. I thought it might be interesting to see how I could perhaps 
// mix and match HTML and see what kind of sounds it can create. As of now, this code will only generate the sound for certain URLs that I hardcode.
// I plan to add the functionality where the user can type in a url and the program will take it from there.
import java.applet.*;
import javax.swing.*;
import java.net.*;
import java.awt.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
import javax.swing.text.html.parser.*;
import java.io.*;
import java.util.*;
import javax.imageio.*;

class AudioViz extends BApplet {
  Sample test=null;
  HTMLReader html;
  static ArrayList visited = new ArrayList();
  static ArrayList queued = new ArrayList();
  public static Vector ambient=new Vector();
  public static Vector drums=new Vector();
  public static Vector guitars=new Vector();
  public static Vector pianos=new Vector();
  public static Vector playlist=new Vector();
  public static Vector textlist=new Vector();
  boolean init=false;
  BFont ft;
  String strtmp=" ";
  void setup(){
    ft=loadFont("Arial_Rounded_MT_Bold.vlw");
    textFont(ft,44);

    framerate(25000);
    background(0);
    size(600,600);
    boolean soniaInstalled;
    soniaInstalled = Sonia.getStatus();
    if(soniaInstalled){
      println("Sonia has been successfully installed");
      Sonia.start(this);
    }
    else{
      println("You are not online and Sonia is not installed");
    }
    //This will initialize all our instruments.
    loadSamples();
    queued.add(new ImageParserCallbacks("http://www.lcc.gatech.edu", 2));
    textlist.add("Jazzing up the HTML");
  }

  void loadSamples(){
    drums=loadSounds("sketchbook\\default\\audio_viz\\data\\drum");
    pianos=loadSounds("sketchbook\\default\\audio_viz\\data\\piano");
    guitars=loadSounds("sketchbook\\default\\audio_viz\\data\\guitar");
    ambient=loadSounds("sketchbook\\default\\audio_viz\\data\\ambient");
    println("Loaded File");
  }

  Vector loadSounds(String directory){
    Vector temp=new Vector();
    File dir=null;
    try{
      dir=new File(directory);
    }catch(Exception e){
      e.printStackTrace();
      println("File not Found");
    }
    if(dir.isDirectory()){
      String[] children=dir.list();
      String newFile=null;
      Sample s=null;
      for(int i=0;i<children.length;i++){
        try{
          String tempFile=dir.getAbsolutePath()+"\\"+children[i];
          newFile=tempFile.substring(tempFile.lastIndexOf("data")+5);
          s=new Sample(newFile);
          temp.add(s);
        }catch(Exception e){
          e.printStackTrace();
        }
      }
    }
    return temp;
  }

  void loop() {
    ImageParserCallbacks temp=(ImageParserCallbacks)queued.get(0);
    startParse(temp);
    if(playlist.size()>100000){
      playlist.clear();
    }
    for(int i =0;i<playlist.size();i++){
      test=(Sample)playlist.get(i);
      if(test.isPlaying()){
        continue;
      }
      else{
        playlist.remove(i);
        test.repeat();
      }
    }
    for(int j=0;j<textlist.size();j++){
    try{
        strtmp=(String)textlist.get(j);
        textlist.remove(j);
        if(strtmp==null)
          continue;
        textSize((int)random(1,30));
        fill((int)random(50,255),(int)random(50,255),(int)random(50,255));
        text(strtmp,(int)random(0,400),(int)random(0,600));

        }catch(Exception e){
          e.printStackTrace();
          println("TEXT "+strtmp);
          continue;
        }
   }
    
  }

  void startParse(ImageParserCallbacks pageToParse) {
    int count = pageToParse.depthCount;
    String href = pageToParse.href;
    if (count < 3) {
      html = new HTMLReader();
      html.parse(href, pageToParse);
    }
  }

  boolean checkLink(String href) {
    if(href == "" || href == null)
    return true;
    for(int i = 0; i < visited.size(); i++) {
      String temp = (String)visited.get(i);
      if(temp.equals(href)) {
        return true;
      }
    }
    return false;
  }
  //This method cleans the Relative URL that is present in the web page and completes it with the act
  String cleanRelativeURL(String relLink, String currLink) {
    String tempHref = relLink;
    String href = currLink;
    //System.out.println("THE URL IS"+relLink);
    if((tempHref != null) && (!tempHref.equals(""))) {
      if(!tempHref.startsWith("http://")) {
        String prefix = href.substring(0, href.lastIndexOf('/') + 1);
        if(tempHref.charAt(0) == '/') {
          if (tempHref.length() > 1) {
            tempHref = tempHref.substring(1, tempHref.length());
          } else {
            tempHref = "";
          }
        }
        tempHref = prefix + tempHref;
      }
    }
    return tempHref;
  }

  class ImageParserCallbacks extends HTMLEditorKit.ParserCallback {
    int depthCount;
    String href;
    boolean gottitle=false;
    
    ImageParserCallbacks(String href, int count) {
      depthCount = count;
      this.href = href;
    }

    public void handleSimpleTag(HTML.Tag tag, MutableAttributeSet attrib, int pos) {
      if (tag.equals(HTML.Tag.IMG)) {
        AudioViz.playlist.add((Sample)AudioViz.drums.get((int)random(0,AudioViz.drums.size())));
        if (attrib.getAttribute(HTML.Attribute.ALT) != null) {
          String text = (String)attrib.getAttribute(HTML.Attribute.ALT);
          String imageURL = cleanRelativeURL((String)attrib.getAttribute(HTML.Attribute.SRC), href);
          AudioViz.textlist.add(imageURL);
        }
        
      }
           
    }

    public void handleStartTag(HTML.Tag tag, MutableAttributeSet attrib, int pos) {
      //Handle <HTML> tag here
      if(tag.equals(HTML.Tag.HTML)){
        AudioViz.playlist.add((Sample)AudioViz.ambient.get((int)random(0,AudioViz.ambient.size())));
        AudioViz.textlist.add("<html>");
      }
      if(tag.equals(HTML.Tag.HEAD)){
        AudioViz.textlist.add("<head>");
      }
      if(tag.equals(HTML.Tag.TITLE)){
        gottitle=true;
      }
      if(tag.equals(HTML.Tag.META)){
            String content= (String)attrib.getAttribute(HTML.Attribute.CONTENT);
            AudioViz.textlist.add(content);
      }
      //Handle <BODY> tag here.
      if(tag.equals(HTML.Tag.BODY)){
        AudioViz.playlist.add((Sample)AudioViz.ambient.get((int)random(0,AudioViz.ambient.size())));
        //Get all the attributes of the Body tag and add them to a string.
        String bodyString="<body ";
        bodyString+="bgcolor="+(String)attrib.getAttribute(HTML.Attribute.BGCOLOR);
        bodyString+=" text="+(String)attrib.getAttribute(HTML.Attribute.TEXT);
        bodyString+=">";
        AudioViz.textlist.add(bodyString);
      }
      //Handle Anchor tags here.
      if (tag.equals(HTML.Tag.A)) {
        String tempHref = (String)attrib.getAttribute(HTML.Attribute.HREF);
        tempHref = cleanRelativeURL(tempHref, href);
        if(!checkLink(tempHref)) {
          queued.add(new ImageParserCallbacks(tempHref, depthCount + 1));
          visited.add(new String(tempHref));
        }
        AudioViz.playlist.add((Sample)AudioViz.guitars.get((int)random(0,AudioViz.guitars.size())));
        AudioViz.textlist.add(tempHref);
      }

      //Handle P tag here.
      if(tag.equals(HTML.Tag.P)){
        AudioViz.textlist.add("<p>");
        AudioViz.playlist.add((Sample)AudioViz.pianos.get((int)random(0,AudioViz.pianos.size())));
      }
      //Handle table tag here.
      if(tag.equals(HTML.Tag.TABLE)){
        AudioViz.textlist.add("<table");
        AudioViz.playlist.add((Sample)AudioViz.drums.get((int)random(0,AudioViz.drums.size())));
        String tableString="";
        tableString+=" bgcolor="+(String)attrib.getAttribute(HTML.Attribute.BGCOLOR);
        tableString+=" cellspacing="+(String)attrib.getAttribute(HTML.Attribute.CELLSPACING);
        tableString+=" cellpadding="+(String)attrib.getAttribute(HTML.Attribute.CELLPADDING);
        tableString+=" width="+(String)attrib.getAttribute(HTML.Attribute.WIDTH);
        tableString+=" height="+(String)attrib.getAttribute(HTML.Attribute.HEIGHT);
        tableString+="/>";
        
      }
      if(tag.equals(HTML.Tag.TR)){
        AudioViz.playlist.add((Sample)AudioViz.guitars.get((int)random(0,AudioViz.guitars.size())));
        AudioViz.textlist.add("<tr></tr>");
      }
    }

    public void handleText(char[] data, int pos) {
      String text=new String(data);
      if(gottitle){
        AudioViz.textlist.add("<title>"+data+"</title>");
        gottitle=false;
      }
      else{
        if(text!=null)
          AudioViz.textlist.add(text);
      }
      
    }
  }
}

Project 4: Create an applet that dynamically does something to one or more web pages.

Statement:Hypertext was conceived as a computer-aided form of reading and writing whose structure matches that of the human mind (a tangled web of association), thus enabling humans to make sense of the exponential growth of knowledge experienced in the 20th century. The World-Wide Web, while a rather anemic implementation of hypertext, makes up for these deficiencies by providing us a sneak peak at what it might be like to have a truly global repository of knowledge. But making sense of the world is not just a matter of structure, but of process, of the dynamic construction of meaning. And as we've been discovering together, computation is fundamentally a process medium. What would you do to the web? Create an applet that dynamically does something to one or more web pages (e.g. collage, systematic distortion, re-layout, ironic superposition, etc.).

hide brief