home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Java / Java.zip / jload18.zip / htmlscanner.java < prev    next >
Text File  |  1999-02-06  |  6KB  |  268 lines

  1. /* SGML TAG scanner */
  2.  
  3. import java.util.*;
  4. import java.io.*;
  5.  
  6. public class htmlscanner
  7. {
  8.  
  9.  /* buffer */
  10.  public final static int BUFFERSIZE=4096;
  11.  public final static String KSTART="!--";
  12.  public final static String KEND="-->";
  13.  
  14.  private transient byte buf[];
  15.  private transient int bpos;
  16.  private transient int bsize;
  17.  
  18.  private InputStream is;
  19.  private OutputStream os;
  20.  private int bytes;
  21.  
  22.  htmlscanner(InputStream is,OutputStream os)
  23.  {
  24.   this.is=is;
  25.   this.os=os;
  26.   bytes=0;
  27.  }
  28.  
  29.   public final Hashtable getElement() throws IOException
  30.   {
  31.     Hashtable result;
  32.     result=null;
  33.     int spos=0;
  34.     byte b;
  35.     
  36.     // findString("<");
  37.     findByte((byte)0x3c);
  38.     
  39.     /* mame zacatek tagu nyni to muze byt budto komentar, nebo klasicky
  40.     tag */
  41.     b=readByte();
  42.     
  43.     StringBuffer tag,tagv;
  44.     String tags;
  45.     tag=new StringBuffer();
  46.     
  47.     /* preskocit mozne mezery */
  48.     while(Character.isSpace((char)b))
  49.      b=readByte();
  50.  
  51.      result=new Hashtable();     
  52.     /* get tag */
  53.     while(true)
  54.     {
  55.      if(Character.isSpace((char)b)) break;
  56.      if(b=='>')
  57.       {
  58.        /* konec maskarady */
  59.        tags=tag.toString().toUpperCase();
  60.        if(tags.startsWith(KSTART)) break;       
  61.        result.put("",tags);
  62.        return result;
  63.       }
  64.      tag.append((char)b);
  65.      b=readByte();
  66.     }
  67.     tags=tag.toString().toUpperCase();
  68.     /* mame tag! */
  69.     result.put("",tags);
  70.     
  71.     if(tags.startsWith(KSTART)) { 
  72.                                   if(tags.endsWith("--") 
  73.                                     &&  b=='>'
  74.                                     &&  tags.length()>3
  75.                                     ) return result; 
  76.                                    
  77.                                   // findString(KEND);
  78.                                   findEndComment();
  79.                                   return result;
  80.                                   }
  81.     /* ---------------  */
  82.     /* get XX=YY pairs! */
  83.     
  84.     while(true)
  85.     {
  86.      tag=new StringBuffer();
  87.      tagv=null;
  88.      /* preskocit mozne mezery */
  89.       while(Character.isSpace((char)b))
  90.         b=readByte();
  91.     
  92.     /* nacitat do mezery nebo do = */
  93.     while(true)
  94.     {
  95.      if(Character.isSpace((char)b)) break;
  96.      if(b=='>') {
  97.                   tags=tag.toString().toUpperCase();
  98.                   if(tags.length()==0) return result;
  99.                   result.put(tags,"");
  100.                   return result;
  101.                 }
  102.     if(b=='=') { tagv=new StringBuffer();break;}
  103.     tag.append((char)b);
  104.     b=readByte();
  105.      
  106.     } /* get first part of XXXX = YYYY */
  107.     
  108.      /* preskocit mozne mezery */
  109.       while(Character.isSpace((char)b))
  110.         b=readByte();
  111.  
  112.      if(b=='>') {
  113.                   tags=tag.toString().toUpperCase();
  114.                   if(tags.length()==0) return result;
  115.                   result.put(tags,"");
  116.                   return result;
  117.                 }
  118.         
  119.             
  120.     if(b== '=' )
  121.                  {
  122.                   b=readByte();
  123.                   tagv=new StringBuffer();
  124.                   /* mezery za = */
  125.                   while(Character.isSpace((char)b))
  126.                    b=readByte();
  127.                    
  128.                   if(b=='"' || b=='\'')
  129.                      {
  130.                       byte endchar;
  131.                       endchar=b;
  132.                       /* jedeme az do dalsi " */
  133.                       b=readByte();
  134.                       /* HACK na chybejici ukoncovaci '" */
  135.                       while(b!=endchar && b!='>')
  136.                        {
  137.                         tagv.append((char)b);
  138.                         b=readByte();
  139.                        }
  140.                        b=readByte();
  141.                      }
  142.                   else
  143.                     {  
  144.                       while(!Character.isSpace((char)b))
  145.                        {
  146.                         tagv.append((char)b);
  147.                         b=readByte();
  148.                         if(b=='>') break;
  149.                        }
  150.                     }
  151.                   result.put(tag.toString().toUpperCase(),tagv.toString());
  152.                   continue;
  153.                  }
  154.     result.put(tag.toString().toUpperCase(),"");
  155.     } 
  156. //    return result;  
  157.   }  
  158.  
  159.     
  160.   private final void findByte(byte what) throws IOException
  161.   {
  162.    while(what!=readByte())
  163.     ;
  164.   
  165.   }
  166.   private final void findEndComment() throws IOException
  167.   {
  168.    // -->
  169.    int dashsize=0;
  170.    while(true)
  171.    {
  172.     byte b;
  173.     b=readByte();
  174.     if(b==0x2d) 
  175.       dashsize++;
  176.     else
  177.       if(b==0x3e)
  178.         if(dashsize>=2) 
  179.           return;
  180.         else
  181.           dashsize=0;
  182.       else
  183.         dashsize=0;
  184.    }
  185.   }
  186.   
  187.   private final void findString(String what) throws IOException
  188.   {
  189.     System.out.println("finding "+what); 
  190.     System.out.println("Tracing finder:");
  191.     
  192.     int l;
  193.     l=what.length();
  194.     if(l==0) return;
  195.     int spos=0;
  196.     byte b;
  197.     while(true)
  198.     {
  199.      b=readByte();
  200.      System.out.print(new Character((char)b).toString());
  201.      if(b == what.charAt(spos)) 
  202.        {
  203.            if((++spos) == l) return;
  204.            continue;
  205.        } else
  206.     spos=0;
  207.     }/* true */
  208.   }
  209.     
  210.   private final byte readByte() throws IOException
  211.   {
  212.     bytes++;
  213.     if(buf==null) buf=new byte[BUFFERSIZE];
  214.     if( bpos==bsize )
  215.       {
  216.        try
  217.        {
  218.         bpos=0;
  219.         bsize=is.read(buf);
  220.         // System.out.println("Reading from input. size="+bsize);
  221.        }
  222.        catch ( InterruptedIOException e2)
  223.          { is.close();
  224.            if(os!=null) os.close();
  225.            throw e2;
  226.          }
  227.        
  228.        if(bsize<1) { is.close();
  229.                      if(os!=null) os.close();
  230.                      throw new EOFException();
  231.                    } 
  232.           else
  233.            if(os!=null) try
  234.                          {
  235.                            os.write(buf,0,bsize);
  236.                          }
  237.                         catch (IOException ioe)
  238.                         {
  239.                          try
  240.                           {
  241.                            os.close();
  242.                           }
  243.                          catch(IOException ignore) {}
  244.                          os=null;
  245.                         }
  246.       }
  247.       
  248.    return buf[bpos++];
  249.   }
  250.   
  251.   public final void close() throws IOException
  252.   {
  253.    buf=null;
  254.    bpos=bsize=0;
  255.    if(os!=null) os.close();
  256.    os=null;  
  257.    if(is==null) return;
  258.    is.close();
  259.    is=null;
  260.   }
  261.   
  262.   public final void finalize() throws Throwable
  263.   {
  264.    close();
  265.   }
  266.  
  267. }
  268.