KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SampleApplication


1 import org.faceless.report.*;
2 import org.faceless.pdf2.*;
3 import org.xml.sax.*;
4 import java.io.*;
5
6 // Usage: java Sample test.xml [test2.xml test3.xml...]
7
//
8
// Create PDF documents from XML documents specified on the command line.
9
//
10
// The output file is the same as the input files, but with a ".pdf" suffix,
11
// e.g. test.pdf, test2.pdf, test3.pdf
12
//
13
public class SampleApplication
14 {
15     public static void main(String JavaDoc[] args)
16     {
17         try {
18         doParse(args);
19     } catch (SAXException e) {
20         if (e.getException()!=null) {
21             e.getException().printStackTrace();
22         } else {
23         e.printStackTrace();
24         }
25     } catch (Exception JavaDoc e) {
26         e.printStackTrace();
27     }
28     }
29
30     public static void doParse(String JavaDoc[] args)
31         throws SAXException, IOException
32     {
33     if (args.length==0) {
34         System.err.println("Usage: java Sample <infile1.xml> [<infile2.xml> ...]");
35         System.exit(0);
36     }
37
38     // Create the parser
39
//
40
ReportParser p = ReportParser.getInstance();
41
42     System.out.println("---------------------------------------------------");
43     System.out.println("Parsing files. Note the first file will take longer");
44     System.out.println("as it must load various static resources.");
45     System.out.println("---------------------------------------------------");
46
47     for (int i=0;i<args.length;i++)
48     {
49         // Work out the filename for the output file
50
//
51
File infile = new File(args[i]);
52         String JavaDoc outfilename = infile.getName();
53         if (outfilename.indexOf('.')>0) {
54             outfilename = outfilename.substring(0,outfilename.lastIndexOf('.'));
55         }
56         outfilename = (infile.getParent()==null ? "" : infile.getParent()+File.separator)+outfilename;
57         File outfile = new File(outfilename+".pdf");
58
59
60         // Parse and render the PDF
61
//
62
System.out.print("Parsing \""+infile+"\"... ");
63         long now = System.currentTimeMillis();
64
65         PDF pdf = p.parse(infile);
66         pdf.render(new FileOutputStream(outfile));
67
68         System.out.println("created \""+outfile+"\" in "+(System.currentTimeMillis()-now)+"ms");
69     }
70     System.out.println("---------------------------------------------------");
71     }
72 }
73
Popular Tags