KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > TemplatesHandlerImpl


1 package net.sf.saxon;
2 import net.sf.saxon.event.CommentStripper;
3 import net.sf.saxon.event.ReceivingContentHandler;
4 import net.sf.saxon.event.StartTagBuffer;
5 import net.sf.saxon.style.StyleNodeFactory;
6 import net.sf.saxon.style.StylesheetStripper;
7 import net.sf.saxon.style.UseWhenFilter;
8 import net.sf.saxon.trans.XPathException;
9 import net.sf.saxon.tree.DocumentImpl;
10 import net.sf.saxon.tree.TreeBuilder;
11 import org.xml.sax.Locator JavaDoc;
12
13 import javax.xml.transform.Templates JavaDoc;
14 import javax.xml.transform.sax.TemplatesHandler JavaDoc;
15
16
17 /**
18   * <b>TemplatesHandlerImpl</b> implements the javax.xml.transform.sax.TemplatesHandler
19   * interface. It acts as a ContentHandler which receives a stream of
20   * SAX events representing a stylesheet, and returns a Templates object that
21   * represents the compiled form of this stylesheet.
22   * @author Michael H. Kay
23   */

24
25 public class TemplatesHandlerImpl extends ReceivingContentHandler implements TemplatesHandler JavaDoc {
26
27     private TreeBuilder builder;
28     private StyleNodeFactory nodeFactory;
29     private Templates JavaDoc templates;
30     private String JavaDoc systemId;
31
32     /**
33     * Create a TemplatesHandlerImpl and initialise variables. The constructor is protected, because
34     * the Filter should be created using newTemplatesHandler() in the SAXTransformerFactory
35     * class
36     */

37
38     protected TemplatesHandlerImpl(Configuration config) {
39
40         setPipelineConfiguration(config.makePipelineConfiguration());
41
42         nodeFactory = new StyleNodeFactory(config);
43
44         builder = new TreeBuilder();
45         builder.setPipelineConfiguration(getPipelineConfiguration());
46         builder.setNodeFactory(nodeFactory);
47         builder.setLineNumbering(true);
48
49         StartTagBuffer startTagBuffer = new StartTagBuffer();
50
51         UseWhenFilter useWhenFilter = new UseWhenFilter(startTagBuffer);
52         useWhenFilter.setUnderlyingReceiver(builder);
53         useWhenFilter.setPipelineConfiguration(getPipelineConfiguration());
54
55         startTagBuffer.setUnderlyingReceiver(useWhenFilter);
56         startTagBuffer.setPipelineConfiguration(getPipelineConfiguration());
57
58         StylesheetStripper styleStripper = new StylesheetStripper();
59         styleStripper.setStylesheetRules(config.getNamePool());
60         styleStripper.setUnderlyingReceiver(startTagBuffer);
61         styleStripper.setPipelineConfiguration(getPipelineConfiguration());
62
63         CommentStripper commentStripper = new CommentStripper();
64         commentStripper.setUnderlyingReceiver(styleStripper);
65         commentStripper.setPipelineConfiguration(getPipelineConfiguration());
66
67         this.setReceiver(commentStripper);
68
69     }
70
71     /**
72     * Get the Templates object to used for a transformation
73     */

74
75     public Templates JavaDoc getTemplates() {
76         if (templates==null) {
77             DocumentImpl doc = (DocumentImpl)builder.getCurrentRoot();
78             if (doc==null) {
79                 return null;
80             }
81             PreparedStylesheet sheet = new PreparedStylesheet(getConfiguration());
82             try {
83                 sheet.setStylesheetDocument(doc, nodeFactory);
84                 templates = sheet;
85             } catch (XPathException tce) {
86                 // don't know why we aren't allowed to just throw it!
87
throw new UnsupportedOperationException JavaDoc(tce.getMessage());
88             }
89         }
90
91         return templates;
92     }
93
94     /**
95     * Set the SystemId of the document
96     */

97
98     public void setSystemId(String JavaDoc url) {
99         systemId = url;
100         builder.setSystemId(url);
101     }
102
103     /**
104     * Callback interface for SAX: not for application use
105     */

106
107     public void setDocumentLocator (Locator JavaDoc locator) {
108         super.setDocumentLocator(locator);
109         setSystemId(locator.getSystemId());
110     }
111
112     /**
113     * Get the systemId of the document
114     */

115
116     public String JavaDoc getSystemId() {
117         return systemId;
118     }
119
120 }
121
122 //
123
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
124
// you may not use this file except in compliance with the License. You may obtain a copy of the
125
// License at http://www.mozilla.org/MPL/
126
//
127
// Software distributed under the License is distributed on an "AS IS" basis,
128
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
129
// See the License for the specific language governing rights and limitations under the License.
130
//
131
// The Original Code is: all this file.
132
//
133
// The Initial Developer of the Original Code is Michael H. Kay.
134
//
135
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
136
//
137
// Contributor(s): None
138
//
139
Popular Tags