KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > PreparedStyleSheet


1 package com.icl.saxon;
2 import com.icl.saxon.om.*;
3 import com.icl.saxon.tree.TreeBuilder;
4 import com.icl.saxon.tree.DocumentImpl;
5 import com.icl.saxon.tree.NodeFactory;
6 import com.icl.saxon.style.StyleNodeFactory;
7 import com.icl.saxon.style.StyleElement;
8 import com.icl.saxon.style.LiteralResultElement;
9 import com.icl.saxon.style.XSLStyleSheet;
10 import com.icl.saxon.output.Outputter;
11
12 import java.util.Properties JavaDoc;
13
14 import javax.xml.transform.*;
15 import javax.xml.transform.sax.SAXSource JavaDoc;
16 import javax.xml.transform.dom.DOMSource JavaDoc;
17
18 import org.w3c.dom.Document JavaDoc;
19 import org.xml.sax.SAXParseException JavaDoc;
20
21 /**
22   * This <B>PreparedStyleSheet</B> class represents a StyleSheet that has been
23   * prepared for execution (or "compiled").
24   */

25   
26 public class PreparedStyleSheet implements Templates {
27
28     private DocumentImpl styleDoc;
29     private TransformerFactoryImpl factory;
30     private NamePool namePool;
31     private StyleNodeFactory nodeFactory;
32     private int errorCount = 0;
33     
34     /**
35     * Constructor: deliberately protected
36     */

37
38     protected PreparedStyleSheet(TransformerFactoryImpl factory) {
39         this.factory = factory;
40     }
41
42     /**
43     * Make a Transformer from this Templates object.
44     */

45
46     public Transformer newTransformer() {
47         Controller c = new Controller(factory);
48         c.setPreparedStyleSheet(this);
49         return c;
50     }
51
52     /**
53     * Get the TransformerFactory used to create this PreparedStyleSheet
54     */

55
56     public TransformerFactoryImpl getTransformerFactory() {
57         return factory;
58     }
59
60     /**
61     * Set the name pool to be used
62     */

63     
64     public void setNamePool(NamePool pool) {
65         namePool = pool;
66     }
67     
68     /**
69     * Get the name pool in use
70     */

71     
72     public NamePool getNamePool() {
73         return namePool;
74     }
75
76     /**
77     * Get the StyleNodeFactory in use
78     */

79     
80     public StyleNodeFactory getStyleNodeFactory() {
81         return nodeFactory;
82     }
83
84     /**
85     * Prepare a stylesheet from an InputSource
86     */

87
88     protected void prepare(SAXSource JavaDoc styleSource) throws TransformerConfigurationException {
89
90         if (namePool==null || namePool.isSealed()) {
91             namePool = NamePool.getDefaultNamePool();
92         }
93
94         nodeFactory = new StyleNodeFactory(namePool);
95         //namePool.setStylesheetSignature(this);
96

97         StylesheetStripper styleStripper = new StylesheetStripper();
98         styleStripper.setStylesheetRules(namePool);
99                
100         TreeBuilder styleBuilder = new TreeBuilder();
101         styleBuilder.setNamePool(namePool);
102         styleBuilder.setErrorListener(factory.getErrorListener());
103         styleBuilder.setStripper(styleStripper);
104         styleBuilder.setSystemId(styleSource.getSystemId());
105         styleBuilder.setNodeFactory(nodeFactory);
106         styleBuilder.setDiscardCommentsAndPIs(true);
107         styleBuilder.setLineNumbering(true);
108         
109         // build the stylesheet document
110

111         DocumentImpl doc;
112         try {
113             doc = (DocumentImpl)styleBuilder.build(styleSource);
114         } catch (TransformerException err) {
115             Throwable JavaDoc cause = err.getException();
116             if (cause != null) {
117                 if (cause instanceof SAXParseException JavaDoc) {
118                     // details already reported, don't repeat them
119
throw new TransformerConfigurationException("Failed to parse stylesheet");
120                 } else if (cause instanceof TransformerConfigurationException) {
121                     throw (TransformerConfigurationException)cause;
122                 } else {
123                     throw new TransformerConfigurationException(cause);
124                 }
125             }
126             throw new TransformerConfigurationException(err);
127         }
128         
129         if (doc.getDocumentElement()==null) {
130             throw new TransformerConfigurationException("Stylesheet is empty or absent");
131         }
132         
133         setStyleSheetDocument(doc);
134
135         if (errorCount > 0) {
136             throw new TransformerConfigurationException(
137                             "Failed to compile stylesheet. " +
138                             errorCount +
139                             (errorCount==1 ? " error " : " errors ") +
140                             "detected.");
141         }
142
143     }
144
145
146     /**
147     * Create a PreparedStyleSheet from a supplied DocumentInfo
148     * Note: the document must have been built using the StyleNodeFactory
149     */

150
151     protected void setStyleSheetDocument(DocumentImpl doc)
152     throws TransformerConfigurationException {
153
154         styleDoc = doc;
155         namePool = doc.getNamePool();
156         //namePool.setStylesheetSignature(this);
157
nodeFactory = new StyleNodeFactory(namePool);
158                 
159         // If top-level node is a literal result element, stitch it into a skeleton stylesheet
160

161         StyleElement topnode = (StyleElement)styleDoc.getDocumentElement();
162         if (topnode instanceof LiteralResultElement) {
163             styleDoc = ((LiteralResultElement)topnode).makeStyleSheet(this);
164         }
165             
166         if (!(styleDoc.getDocumentElement() instanceof XSLStyleSheet)) {
167             throw new TransformerConfigurationException(
168                         "Top-level element of stylesheet is not xsl:stylesheet or xsl:transform or literal result element");
169         }
170
171         XSLStyleSheet top = (XSLStyleSheet)styleDoc.getDocumentElement();
172        
173         // Preprocess the stylesheet, performing validation and preparing template definitions
174

175         top.setPreparedStyleSheet(this);
176         top.preprocess();
177     }
178
179     /**
180     * Get the root node of the principal stylesheet document
181     */

182
183     public DocumentImpl getStyleSheetDocument() {
184         return styleDoc;
185     }
186
187     /**
188     * Get the properties for xsl:output. TRAX method. The object returned will
189     * be a clone of the internal values, and thus it can be mutated
190     * without mutating the Templates object, and then handed in to
191     * the process method.
192     * @return A OutputProperties object that may be mutated. Note that
193     * if any attributes of xsl:output are written as attribute value templates,
194     * the values returned will be unexpanded.
195     *
196     */

197    
198     public Properties JavaDoc getOutputProperties() {
199         
200         Properties JavaDoc defaults = new Properties JavaDoc();
201         //defaults.put(OutputKeys.METHOD, "xml");
202
// "xml" is wrong because the default is determined at run-time.
203
defaults.put(OutputKeys.ENCODING, "utf-8");
204         //defaults.put(OutputKeys.VERSION, "1.0"); - default depends on method
205
defaults.put(OutputKeys.OMIT_XML_DECLARATION, "no");
206         //defaults.put(OutputKeys.STANDALONE, ""); - default is "absent"
207
//defaults.put(OutputKeys.DOCTYPE_PUBLIC, ""); - default is "absent"
208
//defaults.put(OutputKeys.DOCTYPE_SYSTEM, ""); - default is "absent"
209
defaults.put(OutputKeys.CDATA_SECTION_ELEMENTS, "");
210         //defaults.put(OutputKeys.INDENT, "no"); - default depends on method
211
//defaults.put(OutputKeys.MEDIA_TYPE, "text/xml"); - default depends on method
212

213         Properties JavaDoc details = new Properties JavaDoc(defaults);
214         ((XSLStyleSheet)styleDoc.getDocumentElement()).gatherOutputProperties(details);
215         return details;
216     }
217
218     /**
219     * Report a compile time error. This calls the errorListener to output details
220     * of the error, and increments an error count.
221     */

222     
223     public void reportError(TransformerException err) throws TransformerException {
224         errorCount++;
225         factory.getErrorListener().error(err);
226     }
227
228     /**
229     * Use the xsl:strip-space directives in this stylesheet to strip spaces from a
230     * source document. The rest of the stylesheet is ignored.
231     */

232     
233     public DocumentInfo stripWhitespace(Document JavaDoc doc) throws TransformerException {
234         XSLStyleSheet top = (XSLStyleSheet)styleDoc.getDocumentElement();
235         if (top.stripsWhitespace() || (!(doc instanceof DocumentInfo))) {
236             Builder b = ((Controller)newTransformer()).makeBuilder();
237             b.setNamePool(namePool);
238             return b.build(factory.getSAXSource(new DOMSource JavaDoc(doc), false));
239         } else {
240             return (DocumentInfo)doc;
241         }
242     }
243 }
244
245 //
246
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
247
// you may not use this file except in compliance with the License. You may obtain a copy of the
248
// License at http://www.mozilla.org/MPL/
249
//
250
// Software distributed under the License is distributed on an "AS IS" basis,
251
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
252
// See the License for the specific language governing rights and limitations under the License.
253
//
254
// The Original Code is: all this file.
255
//
256
// The Initial Developer of the Original Code is
257
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
258
//
259
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
260
//
261
// Contributor(s): none.
262
//
263
Popular Tags