home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: Java
/
Java.zip
/
jload18.zip
/
htmlscanner.java
< prev
next >
Wrap
Text File
|
1999-02-06
|
6KB
|
268 lines
/* SGML TAG scanner */
import java.util.*;
import java.io.*;
public class htmlscanner
{
/* buffer */
public final static int BUFFERSIZE=4096;
public final static String KSTART="!--";
public final static String KEND="-->";
private transient byte buf[];
private transient int bpos;
private transient int bsize;
private InputStream is;
private OutputStream os;
private int bytes;
htmlscanner(InputStream is,OutputStream os)
{
this.is=is;
this.os=os;
bytes=0;
}
public final Hashtable getElement() throws IOException
{
Hashtable result;
result=null;
int spos=0;
byte b;
// findString("<");
findByte((byte)0x3c);
/* mame zacatek tagu nyni to muze byt budto komentar, nebo klasicky
tag */
b=readByte();
StringBuffer tag,tagv;
String tags;
tag=new StringBuffer();
/* preskocit mozne mezery */
while(Character.isSpace((char)b))
b=readByte();
result=new Hashtable();
/* get tag */
while(true)
{
if(Character.isSpace((char)b)) break;
if(b=='>')
{
/* konec maskarady */
tags=tag.toString().toUpperCase();
if(tags.startsWith(KSTART)) break;
result.put("",tags);
return result;
}
tag.append((char)b);
b=readByte();
}
tags=tag.toString().toUpperCase();
/* mame tag! */
result.put("",tags);
if(tags.startsWith(KSTART)) {
if(tags.endsWith("--")
&& b=='>'
&& tags.length()>3
) return result;
// findString(KEND);
findEndComment();
return result;
}
/* --------------- */
/* get XX=YY pairs! */
while(true)
{
tag=new StringBuffer();
tagv=null;
/* preskocit mozne mezery */
while(Character.isSpace((char)b))
b=readByte();
/* nacitat do mezery nebo do = */
while(true)
{
if(Character.isSpace((char)b)) break;
if(b=='>') {
tags=tag.toString().toUpperCase();
if(tags.length()==0) return result;
result.put(tags,"");
return result;
}
if(b=='=') { tagv=new StringBuffer();break;}
tag.append((char)b);
b=readByte();
} /* get first part of XXXX = YYYY */
/* preskocit mozne mezery */
while(Character.isSpace((char)b))
b=readByte();
if(b=='>') {
tags=tag.toString().toUpperCase();
if(tags.length()==0) return result;
result.put(tags,"");
return result;
}
if(b== '=' )
{
b=readByte();
tagv=new StringBuffer();
/* mezery za = */
while(Character.isSpace((char)b))
b=readByte();
if(b=='"' || b=='\'')
{
byte endchar;
endchar=b;
/* jedeme az do dalsi " */
b=readByte();
/* HACK na chybejici ukoncovaci '" */
while(b!=endchar && b!='>')
{
tagv.append((char)b);
b=readByte();
}
b=readByte();
}
else
{
while(!Character.isSpace((char)b))
{
tagv.append((char)b);
b=readByte();
if(b=='>') break;
}
}
result.put(tag.toString().toUpperCase(),tagv.toString());
continue;
}
result.put(tag.toString().toUpperCase(),"");
}
// return result;
}
private final void findByte(byte what) throws IOException
{
while(what!=readByte())
;
}
private final void findEndComment() throws IOException
{
// -->
int dashsize=0;
while(true)
{
byte b;
b=readByte();
if(b==0x2d)
dashsize++;
else
if(b==0x3e)
if(dashsize>=2)
return;
else
dashsize=0;
else
dashsize=0;
}
}
private final void findString(String what) throws IOException
{
System.out.println("finding "+what);
System.out.println("Tracing finder:");
int l;
l=what.length();
if(l==0) return;
int spos=0;
byte b;
while(true)
{
b=readByte();
System.out.print(new Character((char)b).toString());
if(b == what.charAt(spos))
{
if((++spos) == l) return;
continue;
} else
spos=0;
}/* true */
}
private final byte readByte() throws IOException
{
bytes++;
if(buf==null) buf=new byte[BUFFERSIZE];
if( bpos==bsize )
{
try
{
bpos=0;
bsize=is.read(buf);
// System.out.println("Reading from input. size="+bsize);
}
catch ( InterruptedIOException e2)
{ is.close();
if(os!=null) os.close();
throw e2;
}
if(bsize<1) { is.close();
if(os!=null) os.close();
throw new EOFException();
}
else
if(os!=null) try
{
os.write(buf,0,bsize);
}
catch (IOException ioe)
{
try
{
os.close();
}
catch(IOException ignore) {}
os=null;
}
}
return buf[bpos++];
}
public final void close() throws IOException
{
buf=null;
bpos=bsize=0;
if(os!=null) os.close();
os=null;
if(is==null) return;
is.close();
is=null;
}
public final void finalize() throws Throwable
{
close();
}
}