//Extract links //nodes.child[0] = parent node index //nodes.child[+] = child nodes //nodes.url[0] = name of node //nodes.url[+] = address of resources TextField inputLine = new TextField("input URL", 30); Button sub = new Button("submit"); BFont metaBold; linkMap [] nodes = new linkMap[1]; int stage = 1; int c = 0; void setup(){ size (400,400); background (200); smooth(); noStroke(); //framerate(4); metaBold = loadFont("Meta-Bold.vlw.gz"); textFont(metaBold, 20); add(inputLine); add(sub); sub.addActionListener(new ButtonListener()); } void loop(){ if (stage == 2){ updateScreen(); } }//loop; class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { println(e.getActionCommand()); String iLine = inputLine.getText(); if (!iLine.substring(0,7).equals("http://") ){ iLine = "http://" + iLine; } else if (!iLine.substring(0,7).equals("http://www.")){ iLine = "http://www." + iLine; } remove(inputLine); remove(sub); println("request: " + iLine); nodes[0] = new linkMap(iLine, -1); nodeChildren(0); nodeResource(0); stage = 2; } } String getLinks (String name){ String returnThing = "bad link"; String lines[] = loadStrings(name); if (lines == null){ return returnThing; } else { String docu = join(lines," "); //link extraction int mark = 0; String type = "HREF"; if (stringNum(docu," 0){ type = "FRAME SRC"; } if (stringNum(docu,type) > 0){ String [] url = new String[stringNum(docu,type)]; for (int ih = 0; ih < url.length; ih++){ int q1 = docu.indexOf('"', docu.toUpperCase().indexOf(type,mark))+1; int q2 = docu.indexOf('"', q1); mark = q2; url[ih] = docu.substring(q1,q2); if (cull(url[ih])){ url[ih] = ""; println("bad"); } println(ih+":"+url[ih]); //non local repair String direc = name; if (stringNum(direc,"/") > 2){ direc = direc.substring(0,direc.lastIndexOf('/')); } try{ if (!url[ih].substring(0,7).equals("http://") ){ if (url[ih].substring(0,1).equals("/")){ url[ih] = url[ih].substring(1,url[ih].length()); } while (url[ih].substring(0,3).equals("../")){ direc = direc.substring(0,direc.lastIndexOf('/')); url[ih] = url[ih].substring(url[ih].indexOf('/')+1,url[ih].length()); } url[ih] = direc + "/" + url[ih]; } } catch (StringIndexOutOfBoundsException e){ println("bad path"); } println(ih+type+":"+url[ih]); }//for; returnThing = join (url, ","); } }//; return returnThing; }//String; String getResource (String name){ println("here"); String returnThing = "bad link"; String lines[] = loadStrings(name); if (lines == null){ return returnThing; } else { String docu = join(lines," "); //link extraction int mark = 0; String type = "IMG SRC"; if (stringNum(docu,type) > 0){ String [] url = new String[stringNum(docu,type)]; for (int ih = 0; ih < url.length; ih++){ int q1 = docu.indexOf('"', docu.toUpperCase().indexOf(type,mark))+1; int q2 = docu.indexOf('"', q1); mark = q2; url[ih] = docu.substring(q1,q2); //if (cull(url[ih])){ // url[ih] = ""; // println("bad"); //} println(ih+":"+url[ih]); //non local repair String direc = name; if (stringNum(direc,"/") > 2){ direc = direc.substring(0,direc.lastIndexOf('/')); } try{ if (!url[ih].substring(0,7).equals("http://") ){ if (url[ih].substring(0,1).equals("/")){ url[ih] = url[ih].substring(1,url[ih].length()); } while (url[ih].substring(0,3).equals("../")){ direc = direc.substring(0,direc.lastIndexOf('/')); url[ih] = url[ih].substring(url[ih].indexOf('/')+1,url[ih].length()); } url[ih] = direc + "/" + url[ih]; } } catch (StringIndexOutOfBoundsException e){ println("bad path"); } println(ih+type+":"+url[ih]); }//for; returnThing = join (url, ","); } }//; return returnThing; }//String; boolean cull (String item){ if(item.substring(0,4).equals("java")){ return true; } else if(item.substring(0,1).equals("#")){ return true; } else if(item.substring(item.length()-3,item.length()).equals("css")){ return true; } else { return false; } } void nodeChildren (int node){ String linkList[] = splitStrings(getLinks(nodes[node].url[0]),','); if (!linkList[0].equals("bad link")){ for (int i = 0;i < linkList.length;i++){ if (!linkList[i].equals("")){ mapLength(nodes[nodes.length-1]); nodes[nodes.length-1] = new linkMap(linkList[i], node); nodes[node].addChild(nodes.length-1); } } } }//void; void nodeResource (int node){ String linkList[] = splitStrings(getResource(nodes[node].url[0]),','); if (!linkList[0].equals("bad link")){ for (int i = 0;i < linkList.length;i++){ if (!linkList[i].equals("")){ nodes[node].addUrl(linkList[i]); } } } }//void; class linkMap { int [] child = new int[1]; String [] url = new String[1]; linkMap (String name, int parent){ url[0] = name; child[0] = parent; } void addChild (int no){ childLength (child[child.length-1]); child[child.length-1] = no; }//void; void addUrl (String name){ urlLength (url[url.length-1]); url[url.length-1] = name; }//void; void childLength (int newc){ int[] tempc = new int[child.length + 1]; System.arraycopy(child, 0, tempc, 0, child.length); tempc[child.length] = newc; child = tempc; }//void; void urlLength (String newS){ String[] tempS = new String[url.length + 1]; System.arraycopy(url, 0, tempS, 0, url.length); tempS[url.length] = newS; url = tempS; }//void; }//class; int stringNum (String body, String item){ int ct = 0; if (item.length() > 1){ for (int i = 0; i < body.length()-item.length(); i++){ if (body.substring(i,i+item.length()).toUpperCase().equals(item.toUpperCase())){ ct++; } } } else { for (int i = 0; i < body.length(); i++){ if (body.substring(i,i+item.length()).toUpperCase().equals(item.toUpperCase())){ ct++; } } } return ct; }//int; void mapLength (linkMap newM){ linkMap[] tempM = new linkMap[nodes.length + 1]; System.arraycopy(nodes, 0, tempM, 0, nodes.length); tempM[nodes.length] = newM; nodes = tempM; }//void; void updateScreen(){ float xw = width / 2; float yh = height /2; float theta; String textTop = " "; background (150); fill(175); ellipse(xw-20,yh-20,40,40); fill(255); text(nodes[0].url[0],0,height-20); if (nodes[0].child.length > 1 && c == 0){ for (int i = 0; i < nodes[0].child.length - 1; i++){ theta = (TWO_PI / (nodes[0].child.length - 1)) * i; float x = xw + (cos(theta) * (xw / 2)); float y = yh + (sin(theta) * (yh / 2)); ellipse (x-10,y-10,20,20); if (overRect(mouseX,mouseY,x-10,y-10,x+10,y+10)){ textTop = nodes[i+1].url[0]; if (mousePressed){ link(nodes[i+1].url[0], "_new"); } } } } if (nodes[0].url.length > 1 && c == 1){ for (int i = 0; i < nodes[0].url.length - 1; i++){ theta = (TWO_PI / (nodes[0].url.length - 1)) * i; float x = xw + (cos(theta) * (xw / 2)); float y = yh + (sin(theta) * (yh / 2)); ellipse (x-10,y-10,20,20); if (overRect(mouseX,mouseY,x-10,y-10,x+10,y+10)){ textTop = nodes[0].url[i+1]; if (mousePressed){ link(nodes[0].url[i+1], "_new"); } } } } fill(200); ellipse (10,yh-40,20,20); ellipse (10,yh+20,20,20); if (overRect(mouseX,mouseY,10,yh-40,30,yh-20)){ textTop = "Links"; if (mousePressed){ c = 0; } } if (overRect(mouseX,mouseY,10,yh+20,30,yh+40)){ textTop = "Images"; if (mousePressed){ c = 1; } } fill(200,200,200,100); if (c == 0){ ellipse (5,yh-45,30,30); } else if (c == 1){ ellipse (5,yh+15,30,30); } fill(255); text(textTop,0,20); }//void; boolean overRect(float xo, float yo, float x1, float y1, float x2, float y2){ if (xo >= x1 && xo <= x2 && yo >= y1 && yo <= y2) { return true; } else { return false; } }