problem while parsing script tag from HTMLEditorKit
Posted: Nov 11, 2004 12:46 AM
Advertisement
HI, I have written the following program called process.java to parse a file called login.html which is also given below. But I am not able to get the data which is in <SCRIPT></SCRIPT> tag. Check out the output I got for the process.java. One more thing I getting "?" in place of   when I copy the output and run it. Please find out the errors and give me the solution for two problems. process.java ------------ import java.util.*; import java.io.*; import javax.swing.text.*; import javax.swing.text.html.*; import javax.swing.text.html.parser.*; import java.net.*; import ParserGetter.*; class parse1 extends HTMLEditorKit.ParserCallback { StringBuffer sb=new StringBuffer(); public void handleText(char[] text,int position) { sb.append(text); System.out.println(text); } public void handleEndTag(HTML.Tag tag,int position) { sb.append("<").append("/").append(tag).append(">"); System.out.println("<"+"/"+tag+">"); } public void handleSimpleTag(HTML.Tag tag,MutableAttributeSet attributes,int position) { int ind=0; String store1=""; String tok[]=new String[50]; sb.append("<").append(tag); System.out.print("<"+tag); Enumeration e=attributes.getAttributeNames(); while(e.hasMoreElements()) { Object name=e.nextElement(); Object value=attributes.getAttribute(name); sb.append(" ").append(name).append("=").append(value); System.out.print(" "+name+"="+value); } sb.append(">"); System.out.print(">"); System.out.println(); } public void handleStartTag(HTML.Tag tag,MutableAttributeSet attributes,int position) { sb.append("<").append(tag); System.out.print("<"+tag); Enumeration e=attributes.getAttributeNames(); while(e.hasMoreElements()) { Object name=e.nextElement(); Object value=attributes.getAttribute(name); sb.append(" ").append(name).append("=").append(value); } sb.append(">"); System.out.print(">"); } } public class process { ParserGetter kit=null; HTMLEditorKit.Parser parser=null; public process() { kit=new ParserGetter(); parser=kit.getParser(); try { URL u=new URL("http://localhost:8080/examples/jsp/project/login.html"); URLConnection uc = u.openConnection(); InputStream in=u.openStream(); in=new BufferedInputStream(in); Reader r=new InputStreamReader(in); parse1 p=new parse1(); HTMLEditorKit.ParserCallback call=p; parser.parse(r,call,true); //ignores the charaset } catch(Exception e) { e.printStackTrace(); } } public static void main(String a[]) { new process(); } } login.html ---------- <HTML> <TITLE>Home Page of Leave Approval form</TITLE> <SCRIPT> function validate() { if(document.login.id.value=="") { alert("Please Enter The User-id"); document.login.id.focus(); return false; } if(document.login.pw.value=="") { alert("Please Enter The Password"); document.login.pw.focus(); return false; } return true; } function fnSubmit() { if(!validate()) { return; } document.login.submit(); } </SCRIPT> <BODY bgcolor="cream"> <FORM NAME=login TYPE ="post" ACTION="http://localhost:8080/examples/jsp/project/login.jsp"> <BR>&l t;BR><BR> <h1><CENTER> L O G I N      D E T A I L S</CENTER></H1> <font size = 5 BGCOLOR="#EEEEFF"> <BR><BR><BR> <CENTER> <TABLE > <TR> <TD><CENTER><FONT SIZE =4 color="green"> Login: </FONT> <TD><INPUT TYPE = text NAME = id> </TR> <TD> <TD> <TR> <TD><CENTER><FONT SIZE =4 color="green"> Password: </FONT> <TD><INPUT TYPE = password NAME = pw> </TR> <TD> <TD> <TR> <TD> <TD><CENTER><INPUT TYPE=button NAME = Submit VALUE=Submit onClick="fnSubmit()"> </TR> </TABLE> </FONT> </BODY> </HTML> OUTPUT for process.java: ------------------------ <html><head><title>Ho me Page of Leave Approval form </title> <script></script> </head> <body>< ;form><br> <br> <br> <h1><center>L O G I N D E T A I L S </center> </h1> <font><br> <br> <br> < center><table><tr><td><center><font>Login: < /font> </center> </td> <td><input type=text name=id> </td> <td> </td> <td> </td> </tr> <tr><td><center><font>Password : </font> </center> </td> <td><input type=password name=pw> </td> <td> </td> <td> </td> </tr> <tr><td> </td> <td><center><input onclick=fnSubmit() value=Submit type=button name=Submit> </center> </td> </tr> </table> </cen ter> </font> </form> </body> </html> Thank you very much, With regards, pushyamitra.