KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.icl.saxon;
2 import com.icl.saxon.om.NamePool;
3 import com.icl.saxon.tree.TreeBuilder;
4 import com.icl.saxon.tree.DocumentImpl;
5 import com.icl.saxon.style.StyleNodeFactory;
6
7 import javax.xml.transform.*;
8 import javax.xml.transform.sax.TemplatesHandler JavaDoc;
9
10 import org.xml.sax.*;
11
12
13 /**
14   * <b>TemplatesHandlerImpl</b> implements the javax.xml.transform.sax.TemplatesHandler
15   * interface. It acts as a ContentHandler which receives a stream of
16   * SAX events representing a stylesheet, and returns a Templates object that
17   * represents the compiled form of this stylesheet.
18   * @author Michael H. Kay (mhkay@iclway.co.uk)
19   */

20   
21 public class TemplatesHandlerImpl extends ContentEmitter implements TemplatesHandler JavaDoc {
22
23     TransformerFactoryImpl factory;
24     TreeBuilder builder;
25     Templates templates;
26     String JavaDoc systemId;
27  
28     /**
29     * Create a TemplatesHandlerImpl and initialise variables. The constructor is protected, because
30     * the Filter should be created using newTemplatesHandler() in the SAXTransformerFactory
31     * class
32     */

33
34     protected TemplatesHandlerImpl(TransformerFactoryImpl factory) {
35         NamePool pool = NamePool.getDefaultNamePool();
36         setNamePool(pool);
37         this.factory = factory;
38         builder = new TreeBuilder();
39         builder.setNamePool(pool);
40         StyleNodeFactory nodeFactory = new StyleNodeFactory(pool);
41         //pool.setStylesheetSignature(this);
42

43         StylesheetStripper styleStripper = new StylesheetStripper();
44         styleStripper.setStylesheetRules(pool);
45                
46         builder = new TreeBuilder();
47         builder.setNamePool(pool);
48         builder.setStripper(styleStripper);
49         builder.setNodeFactory(nodeFactory);
50         builder.setDiscardCommentsAndPIs(true);
51         builder.setLineNumbering(true);
52         
53         this.setEmitter(styleStripper);
54         styleStripper.setUnderlyingEmitter(builder);
55         
56     }
57
58     /**
59     * Get the Templates object to used for a transformation
60     */

61     
62     public Templates getTemplates() {
63         if (templates==null) {
64             DocumentImpl doc = (DocumentImpl)builder.getCurrentDocument();
65             if (doc==null) {
66                 return null;
67             }
68             PreparedStyleSheet sheet = new PreparedStyleSheet(factory);
69             try {
70                 sheet.setStyleSheetDocument(doc);
71                 templates = sheet;
72             } catch (TransformerConfigurationException tce) {
73                 // TODO: don't know why we aren't allowed to just throw it!
74
System.err.println(tce.getMessage());
75                 return null;
76             }
77         }
78             
79         return templates;
80     }
81
82     /**
83     * Set the SystemId of the document
84     */

85     
86     public void setSystemId(String JavaDoc url) {
87         systemId = url;
88         builder.setSystemId(url);
89     }
90     
91     /**
92     * Get the systemId of the document
93     */

94     
95     public String JavaDoc getSystemId() {
96         return systemId;
97     }
98
99 }
100
101 //
102
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
103
// you may not use this file except in compliance with the License. You may obtain a copy of the
104
// License at http://www.mozilla.org/MPL/
105
//
106
// Software distributed under the License is distributed on an "AS IS" basis,
107
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
108
// See the License for the specific language governing rights and limitations under the License.
109
//
110
// The Original Code is: all this file.
111
//
112
// The Initial Developer of the Original Code is
113
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
114
//
115
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
116
//
117
// Contributor(s): None
118
//
119
Popular Tags