KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SampleTransformer


1 // $Id: SampleTransformer.java,v 1.4 2004/06/29 16:42:32 mike Exp $
2

3 import org.faceless.report.*;
4 import org.faceless.pdf2.*;
5 import org.xml.sax.*;
6 import org.xml.sax.helpers.*;
7 import java.io.*;
8 import java.util.*;
9
10 // This very simple example shows how you could use the Report Generator as
11
// the last step in a sequence of transformations.
12
//
13
// Here we use a simple XMLFilter to dynamically rename tags - the example
14
// below changes every <html> to a <pdf>. More complex transformations could
15
// use XSLT to do this - users of Apache Xalan can look at the
16
// "UserXMLFilters" example supplied with that package to see how.
17
//
18
// To use this example, take any of the samples from the "samples" directory,
19
// change the <pdf> tags to <html>, and then call this program with the
20
// modified file as an argument, e.g "java SampleTransformer HelloWorld.xml".
21
// The output will be sent to "transformed.pdf".
22
//
23
// Don't forget to set the "readerclass" variable to the name of your
24
// XMLReader implementation
25
//
26
//
27
public class SampleTransformer
28 {
29     static final String JavaDoc readerclass = "org.apache.crimson.parser.XMLReaderImpl";
30 // static final String readerclass = "org.apache.xerces.parsers.SAXParser";
31

32     public static void main(String JavaDoc[] args) throws SAXException, IOException
33     {
34     // Create a new reader and it's filter, and set the filter to
35
// remap the <html> tag to <pdf>
36
//
37
XMLReader reader = XMLReaderFactory.createXMLReader(readerclass);
38     TagRemapFilter filter = new TagRemapFilter(reader);
39     filter.setRemap("html", "pdf");
40
41     // Transform and parse the InputSource
42
//
43
ReportParser parser = ReportParser.getInstance();
44     PDF pdf = parser.parse(filter, new InputSource(args[0]));
45
46     // Write the completed PDF to a file
47
//
48
pdf.render(new FileOutputStream("transformed.pdf"));
49     }
50
51
52     /**
53      * A simple XML Filter which allows tags to be remapped on-the-fly
54      * Note although this is fairly functional as it is, it's just an
55      * example - it hasn't been tested on all SAX parsers and doesn't
56      * handle namespaces.
57      */

58     public static class TagRemapFilter extends XMLFilterImpl
59     {
60     private Map remap;
61
62     /**
63      * Create a new TagRemapFilter.
64      * @param parent the XMLReader to use as this filters parent
65      */

66     public TagRemapFilter(XMLReader parent)
67     {
68         super(parent);
69         remap = new HashMap();
70     }
71
72     /**
73      * Set a remapping from one tag to another
74      * @param from the tag to rename
75      * @param to what to rename it to
76      */

77     public void setRemap(String JavaDoc from, String JavaDoc to)
78     {
79         remap.put(from, to);
80     }
81
82         public void startElement(String JavaDoc uri, String JavaDoc local, String JavaDoc qname, Attributes atts)
83         throws SAXException
84     {
85         System.err.println(uri+","+local+","+qname);
86         String JavaDoc to;
87         to = (String JavaDoc)remap.get(local); if (to!=null) local=to;
88         to = (String JavaDoc)remap.get(qname); if (to!=null) qname=to;
89         super.startElement(uri, local, qname, atts);
90     }
91
92     public void endElement(String JavaDoc uri, String JavaDoc local, String JavaDoc qname)
93         throws SAXException
94     {
95         String JavaDoc to;
96         to = (String JavaDoc)remap.get(local); if (to!=null) local=to;
97         to = (String JavaDoc)remap.get(qname); if (to!=null) qname=to;
98         super.endElement(uri, local, qname);
99     }
100     }
101 }
102
Popular Tags