KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xalan > internal > xsltc > trax > TemplatesHandlerImpl


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: TemplatesHandlerImpl.java,v 1.1.2.1 2006/09/19 01:07:39 jeffsuttor Exp $
18  */

19
20 package com.sun.org.apache.xalan.internal.xsltc.trax;
21
22 import javax.xml.XMLConstants JavaDoc;
23 import javax.xml.transform.Source JavaDoc;
24 import javax.xml.transform.Templates JavaDoc;
25 import javax.xml.transform.TransformerException JavaDoc;
26 import javax.xml.transform.URIResolver JavaDoc;
27 import javax.xml.transform.sax.TemplatesHandler JavaDoc;
28
29 import com.sun.org.apache.xalan.internal.xsltc.compiler.CompilerException;
30 import com.sun.org.apache.xalan.internal.xsltc.compiler.Parser;
31 import com.sun.org.apache.xalan.internal.xsltc.compiler.SourceLoader;
32 import com.sun.org.apache.xalan.internal.xsltc.compiler.Stylesheet;
33 import com.sun.org.apache.xalan.internal.xsltc.compiler.SyntaxTreeNode;
34 import com.sun.org.apache.xalan.internal.xsltc.compiler.XSLTC;
35 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
36
37 import org.xml.sax.ContentHandler JavaDoc;
38 import org.xml.sax.InputSource JavaDoc;
39 import org.xml.sax.Locator JavaDoc;
40 import org.xml.sax.SAXException JavaDoc;
41 import org.xml.sax.Attributes JavaDoc;
42
43 import java.util.Vector JavaDoc;
44
45 /**
46  * Implementation of a JAXP1.1 TemplatesHandler
47  * @author Morten Jorgensen
48  * @author Santiago Pericas-Geertsen
49  */

50 public class TemplatesHandlerImpl
51     implements ContentHandler JavaDoc, TemplatesHandler JavaDoc, SourceLoader
52 {
53     /**
54      * System ID for this stylesheet.
55      */

56     private String JavaDoc _systemId;
57
58     /**
59      * Number of spaces to add for output indentation.
60      */

61     private int _indentNumber;
62
63     /**
64      * This URIResolver is passed to all Transformers.
65      */

66     private URIResolver JavaDoc _uriResolver = null;
67
68     /**
69      * A reference to the transformer factory that this templates
70      * object belongs to.
71      */

72     private TransformerFactoryImpl _tfactory = null;
73
74     /**
75      * A reference to XSLTC's parser object.
76      */

77     private Parser _parser = null;
78
79     /**
80      * The created Templates object.
81      */

82     private TemplatesImpl _templates = null;
83
84     /**
85      * Default constructor
86      */

87     protected TemplatesHandlerImpl(int indentNumber,
88     TransformerFactoryImpl tfactory)
89     {
90     _indentNumber = indentNumber;
91     _tfactory = tfactory;
92
93         // Instantiate XSLTC and get reference to parser object
94
XSLTC xsltc = new XSLTC();
95         if (tfactory.getFeature(XMLConstants.FEATURE_SECURE_PROCESSING))
96             xsltc.setSecureProcessing(true);
97
98         // Instantiate XSLTC and get reference to parser object
99
_parser = new XSLTC().getParser();
100     }
101
102     /**
103      * Implements javax.xml.transform.sax.TemplatesHandler.getSystemId()
104      * Get the base ID (URI or system ID) from where relative URLs will be
105      * resolved.
106      * @return The systemID that was set with setSystemId(String id)
107      */

108     public String JavaDoc getSystemId() {
109     return _systemId;
110     }
111
112     /**
113      * Implements javax.xml.transform.sax.TemplatesHandler.setSystemId()
114      * Get the base ID (URI or system ID) from where relative URLs will be
115      * resolved.
116      * @param id Base URI for this stylesheet
117      */

118     public void setSystemId(String JavaDoc id) {
119     _systemId = id;
120     }
121
122     /**
123      * Store URIResolver needed for Transformers.
124      */

125     public void setURIResolver(URIResolver JavaDoc resolver) {
126     _uriResolver = resolver;
127     }
128
129     /**
130      * Implements javax.xml.transform.sax.TemplatesHandler.getTemplates()
131      * When a TemplatesHandler object is used as a ContentHandler or
132      * DocumentHandler for the parsing of transformation instructions, it
133      * creates a Templates object, which the caller can get once the SAX
134      * events have been completed.
135      * @return The Templates object that was created during the SAX event
136      * process, or null if no Templates object has been created.
137      */

138     public Templates JavaDoc getTemplates() {
139         return _templates;
140     }
141
142     /**
143      * This method implements XSLTC's SourceLoader interface. It is used to
144      * glue a TrAX URIResolver to the XSLTC compiler's Input and Import classes.
145      *
146      * @param href The URI of the document to load
147      * @param context The URI of the currently loaded document
148      * @param xsltc The compiler that resuests the document
149      * @return An InputSource with the loaded document
150      */

151     public InputSource JavaDoc loadSource(String JavaDoc href, String JavaDoc context, XSLTC xsltc) {
152     try {
153         // A _uriResolver must be set if this method is called
154
final Source JavaDoc source = _uriResolver.resolve(href, context);
155         if (source != null) {
156         return Util.getInputSource(xsltc, source);
157         }
158     }
159     catch (TransformerException JavaDoc e) {
160         // Falls through
161
}
162     return null;
163     }
164
165     // -- ContentHandler --------------------------------------------------
166

167     /**
168      * Re-initialize parser and forward SAX2 event.
169      */

170     public void startDocument() {
171         XSLTC xsltc = _parser.getXSLTC();
172         xsltc.init(); // calls _parser.init()
173
xsltc.setOutputType(XSLTC.BYTEARRAY_OUTPUT);
174         _parser.startDocument();
175     }
176
177     /**
178      * Just forward SAX2 event to parser object.
179      */

180     public void endDocument() throws SAXException JavaDoc {
181         _parser.endDocument();
182
183         // create the templates
184
try {
185             XSLTC xsltc = _parser.getXSLTC();
186
187             // Set the translet class name if not already set
188
String JavaDoc transletName = null;
189             if (_systemId != null) {
190                 transletName = Util.baseName(_systemId);
191             }
192             else {
193                 transletName = (String JavaDoc)_tfactory.getAttribute("translet-name");
194             }
195             xsltc.setClassName(transletName);
196
197             // Get java-legal class name from XSLTC module
198
transletName = xsltc.getClassName();
199
200             Stylesheet stylesheet = null;
201             SyntaxTreeNode root = _parser.getDocumentRoot();
202
203             // Compile the translet - this is where the work is done!
204
if (!_parser.errorsFound() && root != null) {
205                 // Create a Stylesheet element from the root node
206
stylesheet = _parser.makeStylesheet(root);
207                 stylesheet.setSystemId(_systemId);
208                 stylesheet.setParentStylesheet(null);
209
210                 // Set a document loader (for xsl:include/import) if defined
211
if (_uriResolver != null) {
212                     stylesheet.setSourceLoader(this);
213                 }
214
215                 _parser.setCurrentStylesheet(stylesheet);
216
217                 // Set it as top-level in the XSLTC object
218
xsltc.setStylesheet(stylesheet);
219
220                 // Create AST under the Stylesheet element
221
_parser.createAST(stylesheet);
222             }
223
224             // Generate the bytecodes and output the translet class(es)
225
if (!_parser.errorsFound() && stylesheet != null) {
226                 stylesheet.setMultiDocument(xsltc.isMultiDocument());
227                 stylesheet.setHasIdCall(xsltc.hasIdCall());
228
229                 // Class synchronization is needed for BCEL
230
synchronized (xsltc.getClass()) {
231                     stylesheet.translate();
232                 }
233             }
234
235             if (!_parser.errorsFound()) {
236                 // Check that the transformation went well before returning
237
final byte[][] bytecodes = xsltc.getBytecodes();
238                 if (bytecodes != null) {
239                     _templates =
240                     new TemplatesImpl(xsltc.getBytecodes(), transletName,
241                         _parser.getOutputProperties(), _indentNumber, _tfactory);
242
243                     // Set URIResolver on templates object
244
if (_uriResolver != null) {
245                         _templates.setURIResolver(_uriResolver);
246                     }
247                 }
248             }
249             else {
250                 StringBuffer JavaDoc errorMessage = new StringBuffer JavaDoc();
251                 Vector JavaDoc errors = _parser.getErrors();
252                 final int count = errors.size();
253                 for (int i = 0; i < count; i++) {
254                     if (errorMessage.length() > 0)
255                         errorMessage.append('\n');
256                     errorMessage.append(errors.elementAt(i).toString());
257                 }
258                 throw new SAXException JavaDoc(ErrorMsg.JAXP_COMPILE_ERR, new TransformerException JavaDoc(errorMessage.toString()));
259             }
260         }
261         catch (CompilerException e) {
262             throw new SAXException JavaDoc(ErrorMsg.JAXP_COMPILE_ERR, e);
263         }
264     }
265
266     /**
267      * Just forward SAX2 event to parser object.
268      */

269     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri) {
270         _parser.startPrefixMapping(prefix, uri);
271     }
272
273     /**
274      * Just forward SAX2 event to parser object.
275      */

276     public void endPrefixMapping(String JavaDoc prefix) {
277         _parser.endPrefixMapping(prefix);
278     }
279
280     /**
281      * Just forward SAX2 event to parser object.
282      */

283     public void startElement(String JavaDoc uri, String JavaDoc localname, String JavaDoc qname,
284         Attributes JavaDoc attributes) throws SAXException JavaDoc
285     {
286         _parser.startElement(uri, localname, qname, attributes);
287     }
288
289     /**
290      * Just forward SAX2 event to parser object.
291      */

292     public void endElement(String JavaDoc uri, String JavaDoc localname, String JavaDoc qname) {
293         _parser.endElement(uri, localname, qname);
294     }
295
296     /**
297      * Just forward SAX2 event to parser object.
298      */

299     public void characters(char[] ch, int start, int length) {
300         _parser.characters(ch, start, length);
301     }
302
303     /**
304      * Just forward SAX2 event to parser object.
305      */

306     public void processingInstruction(String JavaDoc name, String JavaDoc value) {
307         _parser.processingInstruction(name, value);
308     }
309
310     /**
311      * Just forward SAX2 event to parser object.
312      */

313     public void ignorableWhitespace(char[] ch, int start, int length) {
314         _parser.ignorableWhitespace(ch, start, length);
315     }
316
317     /**
318      * Just forward SAX2 event to parser object.
319      */

320     public void skippedEntity(String JavaDoc name) {
321         _parser.skippedEntity(name);
322     }
323
324     /**
325      * Set internal system Id and forward SAX2 event to parser object.
326      */

327     public void setDocumentLocator(Locator JavaDoc locator) {
328         setSystemId(locator.getSystemId());
329         _parser.setDocumentLocator(locator);
330     }
331 }
332
333
334
Popular Tags