KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > FindSpecificTags


1 import au.id.jericho.lib.html.*;
2 import java.util.*;
3 import java.io.*;
4 import java.net.*;
5
6 public class FindSpecificTags {
7     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
8         String JavaDoc sourceUrlString="data/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         MasonTagTypes.register();
15         Source source=new Source(new URL(sourceUrlString));
16         source.setLogWriter(new OutputStreamWriter(System.err)); // send log messages to stderr
17
System.out.println("\n*******************************************************************************\n");
18
19         System.out.println("XML Declarations:");
20         displaySegments(source.findAllTags(StartTagType.XML_DECLARATION));
21
22         System.out.println("XML Processing instructions:");
23         displaySegments(source.findAllTags(StartTagType.XML_PROCESSING_INSTRUCTION));
24
25         PHPTagTypes.register(); // register PHPTagTypes after searching for XML processing instructions, otherwise PHP short tags override them.
26
StartTagType.XML_DECLARATION.deregister(); // deregister XML declarations so they are recognised as PHP short tags, consistent with the real PHP parser.
27
source=new Source(source); // have to create a new Source object after changing tag type registrations otherwise cache might contain tags found with previous configuration.
28
source.setLogWriter(new OutputStreamWriter(System.err)); // send log messages to stderr again
29
System.out.println("##################### PHP tag types now added to register #####################\n");
30
31         System.out.println("H2 Elements:");
32         displaySegments(source.findAllElements(Tag.H2));
33
34         System.out.println("Document Type Declarations:");
35         displaySegments(source.findAllTags(StartTagType.DOCTYPE_DECLARATION));
36
37
38         System.out.println("CDATA sections:");
39         displaySegments(source.findAllTags(StartTagType.CDATA_SECTION));
40
41         System.out.println("Common server tags: (eg ASP, JSP, PSP, ASP-style PHP or Mason substitution tag)");
42         displaySegments(source.findAllTags(StartTagType.SERVER_COMMON));
43
44         System.out.println("Tags starting with <%=");
45         displaySegments(source.findAllStartTags("%="));
46
47         System.out.println("Tags starting with <%=var");
48         displaySegments(source.findAllStartTags("%=var"));
49
50         System.out.println("HTML Comments:");
51         displaySegments(source.findAllTags(StartTagType.COMMENT));
52
53         System.out.println("Elements in namespace \"o\" (generated by MS-Word):");
54         displaySegments(source.findAllElements("o:"));
55
56         System.out.println("Tags starting with <![ (commonly generated by MS-Word):");
57         displaySegments(source.findAllStartTags("!["));
58
59         // Note: The end of a PHP tag can not be reliably found without the use of a PHP parser,
60
// meaning any PHP tag found by this library is not guaranteed to have the correct end position.
61
System.out.println("Standard PHP tags:");
62         displaySegments(source.findAllTags(PHPTagTypes.PHP_STANDARD));
63
64         System.out.println("Short PHP tags:");
65         displaySegments(source.findAllTags(PHPTagTypes.PHP_SHORT));
66
67         System.out.println("Mason Component Calls:");
68         displaySegments(source.findAllTags(MasonTagTypes.MASON_COMPONENT_CALL));
69
70         System.out.println("Mason Components Called With Content:");
71         displaySegments(source.findAllElements(MasonTagTypes.MASON_COMPONENT_CALLED_WITH_CONTENT));
72
73         System.out.println("Mason Named Blocks:");
74         displaySegments(source.findAllElements(MasonTagTypes.MASON_NAMED_BLOCK));
75
76         System.out.println("Unregistered start tags:");
77         displaySegments(source.findAllTags(StartTagType.UNREGISTERED));
78
79         System.out.println("Unregistered end tags:");
80         displaySegments(source.findAllTags(EndTagType.UNREGISTERED));
81         
82         System.out.println(source.getCacheDebugInfo());
83   }
84
85     private static void displaySegments(List segments) {
86         for (Iterator i=segments.iterator(); i.hasNext();) {
87             Segment segment=(Segment)i.next();
88             System.out.println("-------------------------------------------------------------------------------");
89             System.out.println(segment.getDebugInfo());
90             System.out.println(segment);
91         }
92         System.out.println("\n*******************************************************************************\n");
93     }
94 }
95
Popular Tags