KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSPTest


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 JavaDoc[] args) throws Exception JavaDoc {
8         String JavaDoc 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 JavaDoc htmlText=Util.getString(new InputStreamReader(sourceUrl.openStream()));
16
17         Source source=new Source(htmlText);
18         source.setLogWriter(new OutputStreamWriter(System.err)); // send log messages to stderr
19
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)); // send log messages to stderr
27
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         // Note the fullSequentialParse() call removes all tags from the cache, so those found in the getJSPSegments method will not be displayed below.
33
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         // Assume all common server tags are JSP segments, and add them to list:
41
jspSegments.addAll(commonServerTags);
42         // Find any taglib libraries being used:
43
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             // we now know commonServerTag is a taglib declaration
51
Attribute prefixAttribute=attributes.get("prefix");
52             if (prefixAttribute==null) continue;
53             String JavaDoc prefix=prefixAttribute.getValue();
54             if (prefix!=null) taglibNamespaces.add(prefix);
55         }
56         for (Iterator i=taglibNamespaces.iterator(); i.hasNext();) {
57             String JavaDoc taglibNamespace=(String JavaDoc)i.next();
58             // find all elements in the taglib namespace:
59
List taglibElements=source.findAllElements(taglibNamespace+':');
60             // add the taglib elements to the list of JSP segments:
61
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