KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ShowBooks


1 import com.icl.saxon.Controller;
2 import com.icl.saxon.Context;
3 import com.icl.saxon.RuleManager;
4 import com.icl.saxon.ExtendedInputSource;
5 import com.icl.saxon.handlers.ElementHandlerBase;
6 import com.icl.saxon.expr.Expression;
7 import com.icl.saxon.expr.StandaloneContext;
8 import com.icl.saxon.expr.SortedSelection;
9 import com.icl.saxon.om.NodeInfo;
10 import com.icl.saxon.om.DocumentInfo;
11 import com.icl.saxon.om.Builder;
12 import com.icl.saxon.output.Outputter;
13 import com.icl.saxon.output.SaxonOutputKeys;
14 import com.icl.saxon.sort.SortKeyDefinition;
15
16 import javax.xml.transform.Result JavaDoc;
17 import javax.xml.transform.OutputKeys JavaDoc;
18 import javax.xml.transform.TransformerException JavaDoc;
19 import javax.xml.transform.sax.SAXSource JavaDoc;
20 import javax.xml.transform.stream.StreamResult JavaDoc;
21
22 import java.util.Hashtable JavaDoc;
23 import java.util.Properties JavaDoc;
24 import java.io.File JavaDoc;
25
26 /**
27   * Class ShowBooks:
28   * This class produces an HTML rendition of a List of Books supplied in XML.
29   * It is intended to be run with the input file books.xml (which is
30   * adapted from the MSXML distribution).
31   *
32   * @author Michael H. Kay (Michael.Kay@icl.com)
33   * @version 20 September 2000
34   */

35   
36 public class ShowBooks extends Controller
37 {
38             // table of category codes and descriptions
39

40     private Hashtable JavaDoc categories = new Hashtable JavaDoc();
41     private StandaloneContext expContext;
42     
43     /**
44       * main()<BR>
45       * Expects one argument, the input filename<BR>
46       * It produces an HTML rendition on the standard output<BR>
47       */

48
49     public static void main (String JavaDoc args[])
50     throws java.lang.Exception JavaDoc
51     {
52         // Check the command-line arguments
53

54         if (args.length != 1) {
55             System.err.println("Usage: java ShowBooks input-file >output-file");
56             System.exit(1);
57         }
58         ShowBooks app = new ShowBooks();
59         app.go(args[0]);
60     }
61     
62     public void go(String JavaDoc filename) throws TransformerException JavaDoc {
63
64         // Set up element handlers
65
RuleManager rm = new RuleManager(getNamePool());
66         expContext = rm.getStandaloneContext();
67         setRuleManager(rm);
68         prepare(rm);
69
70         // Set up output destination details
71
Properties JavaDoc details = new Properties JavaDoc();
72         Result JavaDoc result = new StreamResult JavaDoc(System.out);
73         details.setProperty(OutputKeys.METHOD, "html");
74         details.setProperty(OutputKeys.INDENT, "yes");
75         details.setProperty(SaxonOutputKeys.INDENT_SPACES, "6");
76         details.setProperty(OutputKeys.ENCODING, "iso-8859-1");
77         changeOutputDestination(details, result);
78         
79         // Build the source document tree, stripping whitespace nodes
80
Builder builder = makeBuilder();
81         builder.getStripper().setStripAll();
82
83         SAXSource JavaDoc ss = new SAXSource JavaDoc(new ExtendedInputSource( new File JavaDoc(filename) ));
84         DocumentInfo doc = builder.build(ss);
85
86         // run the parse, calling registered handlers as appropriate
87
run(doc);
88
89         // close the output file
90
resetOutputDestination(null);
91         
92         System.exit(0);
93     }
94
95     /**
96     * Register the element handlers
97     */

98
99     public void prepare(RuleManager rm) throws TransformerException JavaDoc {
100
101         // define how each XML element type should be handled
102

103         rm.setHandler( "BOOKLIST", new BookListHandler());
104
105         rm.setHandler( "BOOKS", new BooksHandler() );
106         
107         rm.setHandler( "ITEM", new ItemHandler() );
108                          
109         rm.setHandler( "CATEGORY", new CategoryHandler() );
110                          
111     }
112
113     /////////////////////////////////////////////////////////////////////////////
114
// INNER CLASSES
115
/////////////////////////////////////////////////////////////////////////////
116

117     /**
118     * Handler for the BOOKLIST element (the outermost element)
119     */

120
121     private class BookListHandler extends ElementHandlerBase {
122
123         public void startElement(NodeInfo e, Context c) throws TransformerException JavaDoc {
124             Expression categories = Expression.make("//CATEGORY", expContext);
125             Expression books = Expression.make("BOOKS", expContext);
126
127             // process the categories
128
c.getController().applyTemplates(c, categories, null, null);
129
130             // process the books
131
c.getController().applyTemplates(c, books, null, null);
132         }
133     }
134
135     /**
136     * Handler for the BOOKS element.
137     * This extends ItemRenderer, which has the capability to display an HTML string before and
138     * after the element content.
139     */

140
141     private class BooksHandler extends ElementHandlerBase {
142
143         String JavaDoc before = "<HTML><HEAD><TITLE>Book List</TITLE></HEAD>\n" +
144                         "<BODY><H1>Book List</H1><TABLE BORDER='1'>\n" +
145                         "<TR><TH>Category</TH><TH>Author</TH><TH>Title</TH>" +
146                         "<TH>Publisher</TH><TH>Quantity</TH><TH>Price</TH></TR>";
147         String JavaDoc after = "</TABLE></BODY></HTML>";
148         
149         public void startElement(NodeInfo e, Context c) throws TransformerException JavaDoc {
150
151             Controller ctrl = c.getController();
152             
153             // write the "before" string
154
ctrl.getOutputter().write(before);
155
156             // create an expression to select the child elements of this node and sort them
157
Expression children = Expression.make("*", expContext);
158             SortedSelection sortedChildren = new SortedSelection(children, 2);
159             
160             SortKeyDefinition sk1 = new SortKeyDefinition();
161             sk1.setSortKey(Expression.make("AUTHOR", expContext));
162             sortedChildren.setSortKey(sk1, 0);
163             
164             SortKeyDefinition sk2 = new SortKeyDefinition();
165             sk2.setSortKey(Expression.make("TITLE", expContext));
166             sortedChildren.setSortKey(sk2, 1);
167
168             // process the nodes selected by this expression
169
ctrl.applyTemplates(c, sortedChildren, null, null);
170
171             // write the "after" string
172
ctrl.getOutputter().write(after);
173
174         }
175
176     }
177
178     /**
179     * CategoryHandler keeps track of category codes and descriptions
180     * in a local hash table for use while processing the book details
181     */

182
183     private class CategoryHandler extends ElementHandlerBase {
184         public void startElement( NodeInfo e, Context context )
185         throws TransformerException JavaDoc {
186             String JavaDoc code = e.getAttributeValue("", "CODE");
187             String JavaDoc desc = e.getAttributeValue("", "DESC");
188             categories.put(code, desc);
189         }
190     }
191
192     /**
193     * Handler for ITEM elements (representing individual books)
194     */

195
196     private class ItemHandler extends ElementHandlerBase {
197
198         public void startElement( NodeInfo e, Context c ) throws TransformerException JavaDoc {
199             Outputter out = c.getOutputter();
200             int tr = getNamePool().allocate("", "", "TR");
201             out.writeStartTag(tr);
202             writeEntry(out, (String JavaDoc)categories.get(e.getAttributeValue("", "CAT")));
203             writeEntry(out, Expression.make("AUTHOR", expContext).evaluateAsString(c));
204             writeEntry(out, Expression.make("TITLE", expContext).evaluateAsString(c));
205             writeEntry(out, Expression.make("PUBLISHER", expContext).evaluateAsString(c));
206             writeEntry(out, Expression.make("QUANTITY", expContext).evaluateAsString(c));
207             writeEntry(out, Expression.make("PRICE", expContext).evaluateAsString(c));
208             out.writeEndTag(tr);
209         }
210
211         private void writeEntry(Outputter out, String JavaDoc val) throws TransformerException JavaDoc {
212             int td = getNamePool().allocate("", "", "TD");
213             out.writeStartTag(td);
214             out.writeContent(val);
215             out.writeEndTag(td);
216         }
217         
218     }
219 }
220
Popular Tags