KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > dappit > Dapper > parser > example > ParserExample


1 /**
2  *
3  */

4 package com.dappit.Dapper.parser.example;
5
6 import java.io.File JavaDoc;
7
8 import org.dom4j.DocumentException;
9 import org.w3c.dom.Document JavaDoc;
10
11 import com.dappit.Dapper.parser.EnviromentController;
12 import com.dappit.Dapper.parser.MozillaParser;
13 import com.dappit.Dapper.parser.ParserInitializationException;
14
15 /**
16  * @author Ohad Serfaty
17  *
18  */

19 public class ParserExample {
20     
21     public static void main(String JavaDoc[] args) throws Exception JavaDoc
22     {
23         // parser library :
24

25         File JavaDoc parserLibraryFile = new File JavaDoc("native/bin/MozillaParser" + EnviromentController.getSharedLibraryExtension());
26         String JavaDoc parserLibrary = parserLibraryFile.getAbsolutePath();
27         System.out.println("Loading Parser Library :" + parserLibrary);
28         // mozilla.dist.bin directory :
29
final File JavaDoc mozillaDistBinDirectory = new File JavaDoc("mozilla.dist.bin."+EnviromentController.getOperatingSystemName());
30         
31         
32         MozillaParser.init(parserLibrary,mozillaDistBinDirectory.getAbsolutePath());
33     
34         Thread JavaDoc thread1 = new Thread JavaDoc()
35         {
36             
37             public void run(){
38                 try
39                 {
40                     
41                 MozillaParser parser = new MozillaParser();
42                 
43                     String JavaDoc html = "<html><body>";
44                     for (int i=0; i<100; i++)
45                         html += "<li><table><tr><td>1111111111111111</table>\n";
46                     html += "</body></html>";
47                     Document JavaDoc document = parser.parse(html);
48                     System.out.println("Generated document :" + ((org.dom4j.Document)document).asXML());
49                 }
50                 catch (ParserInitializationException e)
51                 {
52                     e.printStackTrace();
53                 } catch (DocumentException e) {
54                     // TODO Auto-generated catch block
55
e.printStackTrace();
56                 }
57                 System.out.println("done...");
58             }
59         };
60         
61         Thread JavaDoc thread2 = new Thread JavaDoc(){
62             
63             public void run(){
64                 try
65                 {
66                 //MozillaParser.initXPCOM(mozillaDistBinDirectory.getAbsolutePath());
67
MozillaParser parser = new MozillaParser();
68                 System.out.println("parsing...");
69                 
70                     String JavaDoc html = "<html><body>";
71                     for (int i=0; i<100; i++)
72                         html += "<li><table><tr><td>222222222222222</table>\n";
73                     html += "</body></html>";
74                     Document JavaDoc document = parser.parse(html);
75                     System.out.println("Generated document :" + ((org.dom4j.Document)document).asXML());
76                 }
77                 catch (Exception JavaDoc e)
78                 {
79                     e.printStackTrace();
80                 }
81                 System.out.println("done...");
82             }
83         };
84         
85         thread1.start();
86         thread2.start();
87         thread1.join();
88         thread2.join();
89         
90 // // start the mozilla parser with the right library/components directory :
91
// MozillaParser.init(parseLibrary, mozillaDistBinDirectory.getAbsolutePath());
92
//
93
// // parse the document :
94
// Document domDocument = MozillaParser.getInstance().parse("<html>Hello world!<html>");
95
//
96
// // stop the parser ( essential for a clean exit ).
97
// MozillaParser.getInstance().stopRunning();
98
}
99
100 }
101
Popular Tags