1 import au.id.jericho.lib.html.*; 2 import java.util.*; 3 import java.io.*; 4 import java.net.*; 5 6 public class JSPTest { 7 public static void main(String [] args) throws Exception { 8 String sourceUrlString="data/jsp_test.html"; 9 if (args.length==0) 10 System.err.println("Using default argument of \""+sourceUrlString+'"'); 11 else 12 sourceUrlString=args[0]; 13 if (sourceUrlString.indexOf(':')==-1) sourceUrlString="file:"+sourceUrlString; 14 URL sourceUrl=new URL(sourceUrlString); 15 String htmlText=Util.getString(new InputStreamReader(sourceUrl.openStream())); 16 17 Source source=new Source(htmlText); 18 source.setLogWriter(new OutputStreamWriter(System.err)); System.out.println("The following elements are found without first ignoring JSP tags:\n"); 20 System.out.println("(Notice the errors encountered by the parser in some of the HTML elements)\n"); 21 source.fullSequentialParse(); 22 display(source.findAllElements()); 23 24 System.out.println("*******************************************************************************\n\n\n"); 25 source=new Source(htmlText); 26 source.setLogWriter(new OutputStreamWriter(System.err)); List jspSegments=getJSPSegments(source); 28 System.out.println("The following are the JSP segments in the page:\n"); 29 display(jspSegments); 30 source.ignoreWhenParsing(jspSegments); 31 source.fullSequentialParse(); 32 System.out.println("\n\nThe following elements are found after ignoring the JSP segments:\n"); 34 display(source.findAllElements()); 35 } 36 37 private static List getJSPSegments(Source source) { 38 List jspSegments=new ArrayList(); 39 List commonServerTags=source.findAllTags(StartTagType.SERVER_COMMON); 40 jspSegments.addAll(commonServerTags); 42 Set taglibNamespaces=new HashSet(); 44 taglibNamespaces.add("jsp"); 45 for (Iterator i=commonServerTags.iterator(); i.hasNext();) { 46 StartTag commonServerTag=(StartTag)i.next(); 47 if (commonServerTag.charAt(2)!='@') continue; 48 Attributes attributes=commonServerTag.parseAttributes(); 49 if (attributes==null || !source.toString().startsWith("taglib",attributes.getBegin())) continue; 50 Attribute prefixAttribute=attributes.get("prefix"); 52 if (prefixAttribute==null) continue; 53 String prefix=prefixAttribute.getValue(); 54 if (prefix!=null) taglibNamespaces.add(prefix); 55 } 56 for (Iterator i=taglibNamespaces.iterator(); i.hasNext();) { 57 String taglibNamespace=(String )i.next(); 58 List taglibElements=source.findAllElements(taglibNamespace+':'); 60 jspSegments.addAll(taglibElements); 62 } 63 Collections.sort(jspSegments); 64 return jspSegments; 65 } 66 67 private static void display(Collection segments) { 68 for (Iterator i=segments.iterator(); i.hasNext();) { 69 Segment segment=(Segment)i.next(); 70 System.out.println("-------------------------------------------------------------------------------"); 71 System.out.println(segment.getDebugInfo()); 72 System.out.println(segment); 73 } 74 } 75 } 76 | Popular Tags |