mikeliu1976 - 2005-02-04

regards:

Why would happen this kind of Exception?...@@
http://www.wretch.cc/album/show.php?i=otp&b=1&f=1107539444&p=1
Followings is my codes.

Any suggestion is welcome.

thank you 
May goodness be with you all

--------------------------------------------------
code MyVisitor.java
--------------------------------------------------
import org.htmlparser.Parser;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.ParserException;
import org.htmlparser.visitors.NodeVisitor;
import org.htmlparser.*;
import org.htmlparser.tags.ScriptTag;
import org.htmlparser.tags.ImageTag;
import java.util.Vector;
import java.util.Enumeration;
import org.htmlparser.tags.*;
import org.htmlparser.tags.StyleTag;

public class MyVisitor extends NodeVisitor
{
     public MyVisitor()
     {
        
     }   
     public void visitTag(Tag tag)
     {
         Vector attributes; 
      Attribute attribute; 
         attributes = tag.getAttributesEx(); 
     attribute = (Attribute)attributes.elementAt(0);
         attribute.setName(attribute.getName().toLowerCase());

       //  System.out.print(tag.toHtml()); 
            char MyQuote='"';
         Vector AttributeVector;
         AttributeVector=tag.getAttributesEx();
         for (Enumeration e = AttributeVector.elements() ; e.hasMoreElements();) {
         ((Attribute)e).setQuote(MyQuote);
        }
       

         
        if (tag instanceof LinkTag){
  
     System.out.print(tag.getAttribute("href"));
        System.out.print("****");
        tag.setAttribute("href","http://www.yzu.edu.tw/"+tag.getAttribute("href"));
        System.out.print(tag.getAttribute("href"));
        System.out.print("////");
       
        Vector AttributeLinkTagVector;
        String JavaScriptbuffer;

        AttributeLinkTagVector=tag.getAttributesEx();
        for (Enumeration e2th=AttributeVector.elements() ; e2th.hasMoreElements();) {
          
            JavaScriptbuffer=((Attribute)e2th).getRawValue();
            if(JavaScriptbuffer.startsWith("JavaScript"))
            {
                tag.removeAttribute("href");
            tag.removeAttribute("onMouseOver");
            tag.removeAttribute("onMouseOut");
         }
       
        }
        
         }
      if (tag instanceof DoctypeTag){
          //System.out.print(tag.toHtml());
      }
     
      if (tag instanceof ImageTag){
        tag.setAttribute("alt","*");
      //System.out.print(tag.toHtml());
       
          System.out.print(tag.getAttribute("src"));
        System.out.print("****");
        tag.setAttribute("src","http://www.yzu.edu.tw"+tag.getAttribute("src"));
        System.out.print(tag.getAttribute("src"));
        System.out.print("////");
      }
     
      if (tag instanceof Html){
         
      /*    String  DOCTYPEHEADER1="<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"";
    String  DOCTYPEHEADER2="\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";
    String  DOCTYPEHEADER=DOCTYPEHEADER1+DOCTYPEHEADER2;
          */
          tag.setAttribute("xmlns","http://www.w3.org/1999/xhtml");
          tag.setAttribute("xml:lang","en-US");
          tag.setAttribute("lang","en-US");
        
        //<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
        // Set attribute with given key, value pair.
          System.out.print(tag.toHtml());
      }
      if(tag instanceof StyleTag){
      tag.setAttribute("type","text/css",'"');
      System.out.print(tag.toHtml());
     }
     
      if(tag instanceof StyleTag){
      tag.setAttribute("type","text/css",'"');
      System.out.print(tag.toHtml());
     }
}
}
--------------------------------------------------
code ToHtmlDemoTest2.java
--------------------------------------------------
import org.htmlparser.Parser;
import org.htmlparser.util.NodeIterator;
import org.htmlparser.util.*;
import org.htmlparser.util.ParserException;
import org.htmlparser.visitors.HtmlPage;
import org.htmlparser.tags.*;
import org.htmlparser.visitors.NodeVisitor;
import org.htmlparser.*;
import org.htmlparser.filters.*;
import org.htmlparser.filters.*;
import java.io.*;

public class ToHtmlDemoTest2
{
public static void main (String[] args) throws ParserException
{

Parser parser = new Parser("http://www.yzu.edu.tw");

MyVisitor visitor = new MyVisitor();
NodeList list = new NodeList();
NodeFilter filter=new NotFilter(new TagNameFilter("Script"));

NodeIterator iterator = parser.elements();
while(iterator.hasMoreNodes()){
list.add(iterator.nextNode());
}
list.keepAllNodesThatMatch(filter,true);
parser.visitAllNodesWith(visitor);
}

}