Making a Tag Reader

We want to make a kind of WebReader that just lets us see the HTML tags.

Making a Tag Reader

We want to make a kind of WebReader that just lets us see the HTML tags.

Making a Tag Reader

We want to make a kind of WebReader that just lets us see the HTML tags.

Making a Tag Reader

We'll have to put in an explicit constructor, to match the constructor of the WebReader

Making a Tag Reader

We'll have to put in an explicit constructor, to match the constructor of the WebReader

Making a Tag Reader

We'll have to put in an explicit constructor, to match the constructor of the WebReader

Making a Tag Reader

The constructor just needs to call the WebReader constructor.

Making a Tag Reader

The constructor just needs to call the WebReader constructor.

Making a Tag Reader

The constructor just needs to call the WebReader constructor.

Making a Tag Reader

Only snag is that constructing a WebReader may throw an exception.

Making a Tag Reader

Only snag is that constructing a WebReader may throw an exception.

Making a Tag Reader

Only snag is that constructing a WebReader may throw an exception.

Making a Tag Reader

If we try and read a line from this class, then we will just get the next line from the original web page. We have to override the readLine() method to make it ignore all the text between the tags, so that each 'line' will correspond to a single tag in the WebReader.

Making a Tag Reader

If we try and read a line from this class, then we will just get the next line from the original web page. We have to override the readLine() method to make it ignore all the text between the tags, so that each 'line' will correspond to a single tag in the WebReader.

Making a Tag Reader

If we try and read a line from this class, then we will just get the next line from the original web page. We have to override the readLine() method to make it ignore all the text between the tags, so that each 'line' will correspond to a single tag in the WebReader.

Making a Tag Reader

To do this we need to read characters and return a line, so let's declare some variables.

Making a Tag Reader

To do this we need to read characters and return a line, so let's declare some variables.

Making a Tag Reader

To do this we need to read characters and return a line, so let's declare some variables.

Making a Tag Reader

Why was the 'ch' variable of type int? That's because read returns a deliberately wide type so that it can encode EOF conditions without clashing with any valid value.

Making a Tag Reader

To return a tag, we are going to need to skip any text preceding a '<' character, then collect all the text up to the next > character.

Making a Tag Reader

To return a tag, we are going to need to skip any text preceding a '<' character, then collect all the text up to the next > character.

Making a Tag Reader

To return a tag, we are going to need to skip any text preceding a '<' character, then collect all the text up to the next > character.

Making a Tag Reader

Hang on, how would we recognise when there are no more tags, i.e. when we hit the end of the file?

Making a Tag Reader

Hang on, how would we recognise when there are no more tags, i.e. when we hit the end of the file?

Making a Tag Reader

Hang on, how would we recognise when there are no more tags, i.e. when we hit the end of the file?

Making a Tag Reader

Now let's collect the tag data

Making a Tag Reader

Now let's collect the tag data

Making a Tag Reader

Now let's collect the tag data

Making a Tag Reader

... by appending it to the result ...

Making a Tag Reader

... by appending it to the result ...

Making a Tag Reader

... by appending it to the result ...

Making a Tag Reader

... oops, by appending it as a character ...

Making a Tag Reader

... oops, by appending it as a character ...

Making a Tag Reader

... oops, by appending it as a character ...

Making a Tag Reader

and when we've finished, just return the line (as a String).

Making a Tag Reader

and when we've finished, just return the line (as a String).

Making a Tag Reader

and when we've finished, just return the line (as a String).

Making a Tag Reader

Except of course in the case where there are no more tags left, in which case we'll return a null object

Making a Tag Reader

Except of course in the case where there are no more tags left, in which case we'll return a null object

Making a Tag Reader

Except of course in the case where there are no more tags left, in which case we'll return a null object

Making a Tag Reader

One more thing to deal with: we said that we wanted to return each tag as a separate line. But what happens if there is a line feed or carriage return in the middle of the tag? We'll have to replace that by a space.

Making a Tag Reader

One more thing to deal with: we said that we wanted to return each tag as a separate line. But what happens if there is a line feed or carriage return in the middle of the tag? We'll have to replace that by a space.

Making a Tag Reader

One more thing to deal with: we said that we wanted to return each tag as a separate line. But what happens if there is a line feed or carriage return in the middle of the tag? We'll have to replace that by a space.

Making a Tag Reader

And lastly, what happens if there is a network problem? We'll get some kind of exception thrown, so we must catch it and pretend we hit the end of the data...

Making a Tag Reader

And lastly, what happens if there is a network problem? We'll get some kind of exception thrown, so we must catch it and pretend we hit the end of the data...

Making a Tag Reader

And lastly, what happens if there is a network problem? We'll get some kind of exception thrown, so we must catch it and pretend we hit the end of the data...

Making a Tag Reader

And lastly, what happens if there is a network problem? We'll get some kind of exception thrown, so we must catch it and pretend we hit the end of the data...

Making a Tag Reader

And lastly, what happens if there is a network problem? We'll get some kind of exception thrown, so we must catch it and pretend we hit the end of the data...

Making a Tag Reader

Finished code.

class TagReader {
	}
class TagReader {
	}
class TagReader extends WebReader {
	}
class TagReader extends WebReader {
	}
class TagReader extends WebReader {
*	}
class TagReader extends WebReader {
	public TagReader(String url){
	...
	}
	}
class TagReader extends WebReader {
	public TagReader(String url){
	...
	}
	}
class TagReader extends WebReader {
	public TagReader(String url){
	...
	}
	}
class TagReader extends WebReader {
	public TagReader(String url){
	super(url);
	}
	}
class TagReader extends WebReader {
	public TagReader(String url){
	super(url);
	}
	}
class TagReader extends WebReader {
	public TagReader(String url){
	super(url);
	}
	}
class TagReader extends WebReader {
	public TagReader(String url) throws IOException {
	super(url);
	}
	}
class TagReader extends WebReader {
	public TagReader(String url) throws IOException {
	super(url);
	}
	}
class TagReader extends WebReader {
	public TagReader(String url) throws IOException {
	super(url);
	}
*	}
class TagReader extends WebReader {
	public TagReader(String url) throws IOException {
	super(url);
	}

	public String readLine(){
		...
		}
	}
class TagReader extends WebReader {
	public TagReader(String url) throws IOException {
	super(url);
	}

	public String readLine(){
		...
		}
	}
class TagReader extends WebReader {
	public TagReader(String url) throws IOException {
	super(url);
	}

	public String readLine(){
		...
		}
	}
class TagReader extends WebReader {
	public TagReader(String url) throws IOException {
	super(url);
	}

	public String readLine(){
		int ch;
		StringBuffer result=new StringBuffer();

		...
		}
	}
class TagReader extends WebReader {
	public TagReader(String url) throws IOException {
	super(url);
	}

	public String readLine(){
		int ch;
		StringBuffer result=new StringBuffer();

		...
		}
	}
class TagReader extends WebReader {
	public TagReader(String url) throws IOException {
	super(url);
	}

	public String readLine(){
		int ch;
		StringBuffer result=new StringBuffer();

		...
		}
	}
class TagReader extends WebReader {
	public TagReader(String url) throws IOException {
	super(url);
	}

	public String readLine(){
		int ch;
		StringBuffer result=new StringBuffer();

		...
		}
	}
class TagReader extends WebReader {
	public TagReader(String url) throws IOException {
	super(url);
	}

	public String readLine(){
		int ch;
		StringBuffer result=new StringBuffer();

		ch=this.read();
		while(ch!='<')
			ch=this.read();

		...
		}
	}
class TagReader extends WebReader {
	public TagReader(String url) throws IOException {
	super(url);
	}

	public String readLine(){
		int ch;
		StringBuffer result=new StringBuffer();

		ch=this.read();
		while(ch!='<')
			ch=this.read();

		...
		}
	}
class TagReader extends WebReader {
	public TagReader(String url) throws IOException {
	super(url);
	}

	public String readLine(){
		int ch;
		StringBuffer result=new StringBuffer();

		ch=this.read();
		while(ch!='<')
			ch=this.read();

		...
		}
	}
class TagReader extends WebReader {
	public TagReader(String url) throws IOException {
	super(url);
	}

	public String readLine(){
		int ch;
		StringBuffer result=new StringBuffer();

		ch=this.read();
		while(ch!='<' && ch!=-1)
			ch=this.read();

		...
		}
	}
class TagReader extends WebReader {
	public TagReader(String url) throws IOException {
	super(url);
	}

	public String readLine(){
		int ch;
		StringBuffer result=new StringBuffer();

		ch=this.read();
		while(ch!='<' && ch!=-1)
			ch=this.read();

		...
		}
	}
class TagReader extends WebReader {
	public TagReader(String url) throws IOException {
	super(url);
	}

	public String readLine(){
		int ch;
		StringBuffer result=new StringBuffer();

		ch=this.read();
		while(ch!='<' && ch!=-1)
			ch=this.read();

		...
		}
	}
class TagReader extends WebReader {
	public TagReader(String url) throws IOException {
	super(url);
	}

	public String readLine(){
		int ch;
		StringBuffer result=new StringBuffer();

		ch=this.read();
		while(ch!='<' && ch!=-1)
			ch=this.read();

		ch=this.read();
		while(ch!='>' && ch!=-1){
			???
			ch=this.read();
			}


		...
		}
	}
class TagReader extends WebReader {
	public TagReader(String url) throws IOException {
	super(url);
	}

	public String readLine(){
		int ch;
		StringBuffer result=new StringBuffer();

		ch=this.read();
		while(ch!='<' && ch!=-1)
			ch=this.read();

		ch=this.read();
		while(ch!='>' && ch!=-1){
			???
			ch=this.read();
			}


		...
		}
	}
class TagReader extends WebReader {
	public TagReader(String url) throws IOException {
	super(url);
	}

	public String readLine(){
		int ch;
		StringBuffer result=new StringBuffer();

		ch=this.read();
		while(ch!='<' && ch!=-1)
			ch=this.read();

		ch=this.read();
		while(ch!='>' && ch!=-1){
			???
			ch=this.read();
			}


		...
		}
	}
class TagReader extends WebReader {
	public TagReader(String url) throws IOException {
	super(url);
	}

	public String readLine(){
		int ch;
		StringBuffer result=new StringBuffer();

		ch=this.read();
		while(ch!='<' && ch!=-1)
			ch=this.read();

		ch=this.read();
		while(ch!='>' && ch!=-1){
			line.append(ch);
			ch=this.read();
			}


		...
		}
	}
class TagReader extends WebReader {
	public TagReader(String url) throws IOException {
	super(url);
	}

	public String readLine(){
		int ch;
		StringBuffer result=new StringBuffer();

		ch=this.read();
		while(ch!='<' && ch!=-1)
			ch=this.read();

		ch=this.read();
		while(ch!='>' && ch!=-1){
			line.append(ch);
			ch=this.read();
			}


		...
		}
	}
class TagReader extends WebReader {
	public TagReader(String url) throws IOException {
	super(url);
	}

	public String readLine(){
		int ch;
		StringBuffer result=new StringBuffer();

		ch=this.read();
		while(ch!='<' && ch!=-1)
			ch=this.read();

		ch=this.read();
		while(ch!='>' && ch!=-1){
			line.append(ch);
			ch=this.read();
			}


		...
		}
	}
class TagReader extends WebReader {
	public TagReader(String url) throws IOException {
	super(url);
	}

	public String readLine(){
		int ch;
		StringBuffer result=new StringBuffer();

		ch=this.read();
		while(ch!='<' && ch!=-1)
			ch=this.read();

		ch=this.read();
		while(ch!='>' && ch!=-1){
			line.append((char)ch);
			ch=this.read();
			}


		...
		}
	}
class TagReader extends WebReader {
	public TagReader(String url) throws IOException {
	super(url);
	}

	public String readLine(){
		int ch;
		StringBuffer result=new StringBuffer();

		ch=this.read();
		while(ch!='<' && ch!=-1)
			ch=this.read();

		ch=this.read();
		while(ch!='>' && ch!=-1){
			line.append((char)ch);
			ch=this.read();
			}


		...
		}
	}
class TagReader extends WebReader {
	public TagReader(String url) throws IOException {
	super(url);
	}

	public String readLine(){
		int ch;
		StringBuffer result=new StringBuffer();

		ch=this.read();
		while(ch!='<' && ch!=-1)
			ch=this.read();

		ch=this.read();
		while(ch!='>' && ch!=-1){
			line.append((char)ch);
			ch=this.read();
			}


		...
		}
	}
class TagReader extends WebReader {
	public TagReader(String url) throws IOException {
	super(url);
	}

	public String readLine(){
		int ch;
		StringBuffer result=new StringBuffer();

		ch=this.read();
		while(ch!='<' && ch!=-1)
			ch=this.read();

		ch=this.read();
		while(ch!='>' && ch!=-1){
			line.append((char)ch);
			ch=this.read();
			}


		return line.toString();
		}
	}
class TagReader extends WebReader {
	public TagReader(String url) throws IOException {
	super(url);
	}

	public String readLine(){
		int ch;
		StringBuffer result=new StringBuffer();

		ch=this.read();
		while(ch!='<' && ch!=-1)
			ch=this.read();

		ch=this.read();
		while(ch!='>' && ch!=-1){
			line.append((char)ch);
			ch=this.read();
			}


		return line.toString();
		}
	}
class TagReader extends WebReader {
	public TagReader(String url) throws IOException {
	super(url);
	}

	public String readLine(){
		int ch;
		StringBuffer result=new StringBuffer();

		ch=this.read();
		while(ch!='<' && ch!=-1)
			ch=this.read();

		ch=this.read();
		while(ch!='>' && ch!=-1){
			line.append((char)ch);
			ch=this.read();
			}


		*return line.toString();
		}
	}
class TagReader extends WebReader {
	public TagReader(String url) throws IOException {
	super(url);
	}

	public String readLine(){
		int ch;
		StringBuffer result=new StringBuffer();

		ch=this.read();
		while(ch!='<' && ch!=-1)
			ch=this.read();

		ch=this.read();
		while(ch!='>' && ch!=-1){
			line.append((char)ch);
			ch=this.read();
			}


		if(ch==-1)return null;
		return line.toString();
		}
	}
class TagReader extends WebReader {
	public TagReader(String url) throws IOException {
	super(url);
	}

	public String readLine(){
		int ch;
		StringBuffer result=new StringBuffer();

		ch=this.read();
		while(ch!='<' && ch!=-1)
			ch=this.read();

		ch=this.read();
		while(ch!='>' && ch!=-1){
			line.append((char)ch);
			ch=this.read();
			}


		if(ch==-1)return null;
		return line.toString();
		}
	}
class TagReader extends WebReader {
	public TagReader(String url) throws IOException {
	super(url);
	}

	public String readLine(){
		int ch;
		StringBuffer result=new StringBuffer();

		ch=this.read();
		while(ch!='<' && ch!=-1)
			ch=this.read();

		ch=this.read();
		while(ch!='>' && ch!=-1){
			*line.append((char)ch);
			ch=this.read();
			}


		if(ch==-1)return null;
		return line.toString();
		}
	}
class TagReader extends WebReader {
	public TagReader(String url) throws IOException {
	super(url);
	}

	public String readLine(){
		int ch;
		StringBuffer result=new StringBuffer();

		ch=this.read();
		while(ch!='<' && ch!=-1)
			ch=this.read();

		ch=this.read();
		while(ch!='>' && ch!=-1){
			if(Character.isWhitespace((char)ch))line.append(' ');
			else line.append((char)ch);
			ch=this.read();
			}


		if(ch==-1)return null;
		return line.toString();
		}
	}
class TagReader extends WebReader {
	public TagReader(String url) throws IOException {
	super(url);
	}

	public String readLine(){
		int ch;
		StringBuffer result=new StringBuffer();

		ch=this.read();
		while(ch!='<' && ch!=-1)
			ch=this.read();

		ch=this.read();
		while(ch!='>' && ch!=-1){
			if(Character.isWhitespace((char)ch))line.append(' ');
			else line.append((char)ch);
			ch=this.read();
			}


		if(ch==-1)return null;
		return line.toString();
		}
	}
class TagReader extends WebReader {
	public TagReader(String url) throws IOException {
	super(url);
	}

	public String readLine(){
		int ch;
		StringBuffer result=new StringBuffer();

		ch=this.read();
		*while(ch!='<' && ch!=-1)
			ch=this.read();

		ch=this.read();
		while(ch!='>' && ch!=-1){
			if(Character.isWhitespace((char)ch))line.append(' ');
			else line.append((char)ch);
			ch=this.read();
			}


		if(ch==-1)return null;
		return line.toString();
		}
	}
class TagReader extends WebReader {
	public TagReader(String url) throws IOException {
	super(url);
	}

	public String readLine(){
		int ch;
		StringBuffer result=new StringBuffer();

		ch=this.read();
		try{
		while(ch!='<' && ch!=-1)
			ch=this.read();

		ch=this.read();
		while(ch!='>' && ch!=-1){
			if(Character.isWhitespace((char)ch))line.append(' ');
			else line.append((char)ch);
			ch=this.read();
			}


		if(ch==-1)return null;
		return line.toString();
		}
	}
class TagReader extends WebReader {
	public TagReader(String url) throws IOException {
	super(url);
	}

	public String readLine(){
		int ch;
		StringBuffer result=new StringBuffer();

		ch=this.read();
		try{
		while(ch!='<' && ch!=-1)
			ch=this.read();

		ch=this.read();
		while(ch!='>' && ch!=-1){
			if(Character.isWhitespace((char)ch))line.append(' ');
			else line.append((char)ch);
			ch=this.read();
			}


		if(ch==-1)return null;
		return line.toString();
		}
	}
class TagReader extends WebReader {
	public TagReader(String url) throws IOException {
	super(url);
	}

	public String readLine(){
		int ch;
		StringBuffer result=new StringBuffer();

		ch=this.read();
		try{
		while(ch!='<' && ch!=-1)
			ch=this.read();

		ch=this.read();
		while(ch!='>' && ch!=-1){
			if(Character.isWhitespace((char)ch))line.append(' ');
			else line.append((char)ch);
			ch=this.read();
			}

		}
		catch(IOException e){return null;}

		if(ch==-1)return null;
		return line.toString();
		}
	}
class TagReader extends WebReader {
	public TagReader(String url) throws IOException {
	super(url);
	}

	public String readLine(){
		int ch;
		StringBuffer result=new StringBuffer();

		ch=this.read();
		try{
		while(ch!='<' && ch!=-1)
			ch=this.read();

		ch=this.read();
		while(ch!='>' && ch!=-1){
			if(Character.isWhitespace((char)ch))line.append(' ');
			else line.append((char)ch);
			ch=this.read();
			}

		}
		catch(IOException e){return null;}

		if(ch==-1)return null;
		return line.toString();
		}
	}