/**
 * HTMLParser.java
 *
 * @author      Michael Privat
 * @version 1.0 - March 1998
 */
 
import java.net.*;
import java.io.*;

public class HTMLParser extends PushbackInputStream {

	public HTMLParser(URL url) throws IOException {
		super(new DataInputStream(url.openStream()), 20);
	}

	public Zone getTag(Zone z) throws IOException {
		boolean href = false, bg = false, src = false, applet = false;
		boolean script = z.scriptZone();

		href = isTag("a");
		if(!href) href = isTag("area");
		if(!href) bg = isTag("body");
		if(!href && !bg) src = isTag("img");
		if(!href && !bg && !src) src = isTag("frame");
		if(!href && !bg && !src) applet = isTag("applet");

		if(!script) script = isTag2("script");
		else script &= !isTag2("/script");
		return new Zone(href, bg, src, script, applet);
	}

	public boolean parseSrc(String src) throws IOException {
		byte i;
		byte[] b = new byte[src.length()];

		read(b, 0, b.length);
		String tmp = new String(b);
		if(src.toLowerCase().equals(tmp.toLowerCase())) {
			i=(byte)read();
			if(i==' ' || i=='=' || i=='\n' || i=='"' || i=='\'') {
				while(i==' ' || i=='=' || i=='\n' || i=='"' || i=='\'')
					i = (byte)read();
				unread(i);
				return true;
			}
			else {
				unread(b, 0, b.length);
				unread(i);
				return false;
			}
		}

		unread(b);
		return false;
	}

	public String getReference() throws IOException {
		byte[] b = new byte[256];
		
		int i=0;
		b[0] = (byte)read();
		while((b[i]>='A' && b[i]<='Z') || (b[i]>='a' && b[i]<='z') ||
				b[i]=='/' || b[i]==':' || b[i]=='-' ||
				b[i]=='.' || b[i]=='_' || b[i]=='~' ||
				(b[i]>='0' && b[i]<='9')) b[++i]=(byte)read();

		return new String(b, 0, i);
	}

	private boolean isTag(String tg) throws IOException {
		String tag = new String(tg + " ");
		return isTag2(tag);
	}
	
	private boolean isTag2(String tag) throws IOException {
		boolean result = true;
		byte[] c = new byte[tag.length()];

		int i=0;
		while(i<tag.length()) {
			c[i] = (byte)read();
			if(lowCase(tag.charAt(i)) != c[i] && upCase(tag.charAt(i)) != c[i])
				break;
			i++;
		}

		if(i==tag.length()) return true;

		for(int j=i; j>=0; j--) unread(c[j]);
		return false;
	}

	private byte upCase(char c) {
		if(c>='a' && c<='z') return (byte)(c+'A'-'a');
		return (byte)c;
	}

	private byte lowCase(char c) {
		if(c>='A' && c<='Z') return (byte)(c+'a'-'A');
		return (byte)c;
	}
}
 
