Last active
June 15, 2016 20:12
-
-
Save umuturan/79a4a472c5c2dbad3e52 to your computer and use it in GitHub Desktop.
Lab02-HTML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import cs1.SimpleURLReader; | |
public class HTMLFilteredReader extends MySimpleURLReader { | |
public HTMLFilteredReader(String arg0) { | |
super(arg0); | |
// TODO Auto-generated constructor stub | |
} | |
public String getUnfilteredPageContents() { | |
// called to return the original page complete with html codes. | |
return super.getPageContents(); | |
} | |
@Override | |
public String getPageContents() { | |
int length; | |
int a, b; | |
boolean done; | |
String finalString; | |
a = 0; // first index to omit | |
b = 0; // last index to omit | |
done = false; | |
finalString = ""; | |
length = super.getPageContents().length(); | |
for (int i = 0; i < getUnfilteredPageContents().length(); i++) { | |
if (getUnfilteredPageContents().charAt(i) == '<') { | |
a = i; | |
} | |
if (getUnfilteredPageContents().charAt(i) == '>') { | |
b = i; | |
} | |
if (a != 0 && b != 0) { | |
finalString += getUnfilteredPageContents().substring(b + 1, a); | |
a = 0; | |
b = 0; | |
} | |
} | |
return finalString; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cs1.SimpleURLReader; | |
public class Lab02 { | |
public static void main(String args[]){ | |
SimpleURLReader s1; | |
s1 = new SimpleURLReader("http://www.cs.bilkent.edu.tr/~david/housman.htm"); | |
System.out.println(s1.getPageContents()); | |
System.out.println("------------------------------------"); | |
System.out.println(s1.getLineCount()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cs1.SimpleURLReader; | |
public class MySimpleURLReader extends SimpleURLReader { | |
String url; | |
public MySimpleURLReader(String arg0) { | |
super(arg0); | |
url=arg0; | |
// generated subclass constructor | |
} | |
public String getURL() { | |
return url; | |
/* returns the url String used to create the SimpleURLReader object | |
*/ | |
} | |
public String getName() { | |
boolean check; | |
int i; | |
i=0; | |
check=false; | |
for(i=url.length()-1; (!check) && i>0;i--){ | |
if(url.charAt(i)== '/'){ | |
check=true; | |
} | |
} | |
return url.substring(i+1,url.length()); | |
/* that returns the filename part of the url ,i.e, the part of the url | |
* following the last '/' character | |
*/ | |
} | |
@Override | |
public String getPageContents() { | |
/* | |
* fixes the bug in SimpleURLReader's getPageContents() method whereby the | |
* String "null" is added to the beginning of the String it returns. Do | |
* this by overriding the corresponding method in your new sub-class. | |
*/ | |
return super.getPageContents().replace("null", ""); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import cs1.SimpleURLReader; | |
public class SuperHTMLFilteredReader extends HTMLFilteredReader { | |
public SuperHTMLFilteredReader(String arg0) { | |
super(arg0); | |
// TODO Auto-generated constructor stub | |
} | |
public ArrayList<String> getLinks() { | |
ArrayList<String> list; | |
ArrayList<Integer> listInt; | |
ArrayList<Integer> listLast; | |
String addList; | |
boolean check; | |
addList = ""; | |
list = new ArrayList<String>(); | |
listInt = new ArrayList<Integer>(); | |
listLast = new ArrayList<Integer>(); | |
check = true; | |
for (int i = 0; i < getUnfilteredPageContents().length() - 3; i++) { | |
if (getUnfilteredPageContents().charAt(i) == 'h' && getUnfilteredPageContents().charAt(i + 1) == 'r' | |
&& getUnfilteredPageContents().charAt(i + 3) == 'f') { | |
listInt.add(i + 3); // the index of f at "href" | |
} | |
} | |
for (int i = 0; i < listInt.size(); i++) { | |
check = true; | |
for (int j = listInt.get(i) + 2; j < getUnfilteredPageContents().length() && check; j++) { | |
if (getUnfilteredPageContents().charAt(j) == '>') { | |
listLast.add(j); | |
check = false; | |
} | |
} | |
} | |
for (int i = 0; i < listInt.size(); i++) { | |
list.add(getUnfilteredPageContents().substring(listInt.get(i) + 3, listLast.get(i) - 1)); | |
} | |
return list; | |
} | |
public double overHead() { | |
return getPageContents().length() * 100 / getUnfilteredPageContents().length(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TestHTMLFilteredReader { | |
public static void main(String[] args) { | |
// TODO Auto-generated method stub | |
HTMLFilteredReader read = new HTMLFilteredReader("http://www.cs.bilkent.edu.tr/~david/housman.htm"); | |
System.out.println(read.getPageContents()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TesMytSimpleURLReader { | |
public static void main(String[] args){ | |
MySimpleURLReader r1; | |
r1= new MySimpleURLReader("http://www.cs.bilkent.edu.tr/~david/housman.txt"); | |
System.out.println(r1.getPageContents()); | |
System.out.println(r1.getURL()); | |
System.out.println(r1.getName()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TestSuperHTMLFilteredReader { | |
public static void main(String[] args) { | |
// TODO Auto-generated method stub | |
SuperHTMLFilteredReader supRead = new SuperHTMLFilteredReader("http://www.cs.bilkent.edu.tr/~david/index.html"); | |
for(int i = 0 ; i< supRead.getLinks().size();i++){ | |
System.out.println(supRead.getLinks().get(i)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment