6
|
1 |
/*
|
|
2 |
* reserved comment block
|
|
3 |
* DO NOT REMOVE OR ALTER!
|
|
4 |
*/
|
|
5 |
/*
|
|
6 |
* Copyright 2001-2004 The Apache Software Foundation.
|
|
7 |
*
|
|
8 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
9 |
* you may not use this file except in compliance with the License.
|
|
10 |
* You may obtain a copy of the License at
|
|
11 |
*
|
|
12 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
13 |
*
|
|
14 |
* Unless required by applicable law or agreed to in writing, software
|
|
15 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
16 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
17 |
* See the License for the specific language governing permissions and
|
|
18 |
* limitations under the License.
|
|
19 |
*/
|
|
20 |
/*
|
|
21 |
* $Id: TemplatesHandlerImpl.java,v 1.2.4.1 2005/09/06 12:09:03 pvedula Exp $
|
|
22 |
*/
|
|
23 |
|
|
24 |
package com.sun.org.apache.xalan.internal.xsltc.trax;
|
|
25 |
|
|
26 |
import javax.xml.XMLConstants;
|
|
27 |
import javax.xml.transform.Source;
|
|
28 |
import javax.xml.transform.Templates;
|
|
29 |
import javax.xml.transform.TransformerException;
|
|
30 |
import javax.xml.transform.URIResolver;
|
|
31 |
import javax.xml.transform.sax.TemplatesHandler;
|
|
32 |
|
|
33 |
import com.sun.org.apache.xalan.internal.xsltc.compiler.CompilerException;
|
|
34 |
import com.sun.org.apache.xalan.internal.xsltc.compiler.Parser;
|
|
35 |
import com.sun.org.apache.xalan.internal.xsltc.compiler.SourceLoader;
|
|
36 |
import com.sun.org.apache.xalan.internal.xsltc.compiler.Stylesheet;
|
|
37 |
import com.sun.org.apache.xalan.internal.xsltc.compiler.SyntaxTreeNode;
|
|
38 |
import com.sun.org.apache.xalan.internal.xsltc.compiler.XSLTC;
|
|
39 |
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
|
|
40 |
|
|
41 |
import org.xml.sax.ContentHandler;
|
|
42 |
import org.xml.sax.InputSource;
|
|
43 |
import org.xml.sax.Locator;
|
|
44 |
import org.xml.sax.SAXException;
|
|
45 |
import org.xml.sax.Attributes;
|
|
46 |
|
|
47 |
import java.util.Vector;
|
|
48 |
|
|
49 |
/**
|
|
50 |
* Implementation of a JAXP1.1 TemplatesHandler
|
|
51 |
* @author Morten Jorgensen
|
|
52 |
* @author Santiago Pericas-Geertsen
|
|
53 |
*/
|
|
54 |
public class TemplatesHandlerImpl
|
|
55 |
implements ContentHandler, TemplatesHandler, SourceLoader
|
|
56 |
{
|
|
57 |
/**
|
|
58 |
* System ID for this stylesheet.
|
|
59 |
*/
|
|
60 |
private String _systemId;
|
|
61 |
|
|
62 |
/**
|
|
63 |
* Number of spaces to add for output indentation.
|
|
64 |
*/
|
|
65 |
private int _indentNumber;
|
|
66 |
|
|
67 |
/**
|
|
68 |
* This URIResolver is passed to all Transformers.
|
|
69 |
*/
|
|
70 |
private URIResolver _uriResolver = null;
|
|
71 |
|
|
72 |
/**
|
|
73 |
* A reference to the transformer factory that this templates
|
|
74 |
* object belongs to.
|
|
75 |
*/
|
|
76 |
private TransformerFactoryImpl _tfactory = null;
|
|
77 |
|
|
78 |
/**
|
|
79 |
* A reference to XSLTC's parser object.
|
|
80 |
*/
|
|
81 |
private Parser _parser = null;
|
|
82 |
|
|
83 |
/**
|
|
84 |
* The created Templates object.
|
|
85 |
*/
|
|
86 |
private TemplatesImpl _templates = null;
|
|
87 |
|
|
88 |
/**
|
|
89 |
* Default constructor
|
|
90 |
*/
|
|
91 |
protected TemplatesHandlerImpl(int indentNumber,
|
|
92 |
TransformerFactoryImpl tfactory)
|
|
93 |
{
|
|
94 |
_indentNumber = indentNumber;
|
|
95 |
_tfactory = tfactory;
|
|
96 |
|
|
97 |
// Instantiate XSLTC and get reference to parser object
|
12458
|
98 |
XSLTC xsltc = new XSLTC(tfactory.useServicesMechnism());
|
6
|
99 |
if (tfactory.getFeature(XMLConstants.FEATURE_SECURE_PROCESSING))
|
|
100 |
xsltc.setSecureProcessing(true);
|
|
101 |
|
17534
|
102 |
xsltc.setProperty(XMLConstants.ACCESS_EXTERNAL_STYLESHEET,
|
|
103 |
(String)tfactory.getAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET));
|
|
104 |
xsltc.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD,
|
|
105 |
(String)tfactory.getAttribute(XMLConstants.ACCESS_EXTERNAL_DTD));
|
|
106 |
|
|
107 |
|
12458
|
108 |
if ("true".equals(tfactory.getAttribute(TransformerFactoryImpl.ENABLE_INLINING)))
|
|
109 |
xsltc.setTemplateInlining(true);
|
|
110 |
else
|
|
111 |
xsltc.setTemplateInlining(false);
|
|
112 |
|
6
|
113 |
_parser = xsltc.getParser();
|
|
114 |
}
|
|
115 |
|
|
116 |
/**
|
|
117 |
* Implements javax.xml.transform.sax.TemplatesHandler.getSystemId()
|
|
118 |
* Get the base ID (URI or system ID) from where relative URLs will be
|
|
119 |
* resolved.
|
|
120 |
* @return The systemID that was set with setSystemId(String id)
|
|
121 |
*/
|
|
122 |
public String getSystemId() {
|
|
123 |
return _systemId;
|
|
124 |
}
|
|
125 |
|
|
126 |
/**
|
|
127 |
* Implements javax.xml.transform.sax.TemplatesHandler.setSystemId()
|
|
128 |
* Get the base ID (URI or system ID) from where relative URLs will be
|
|
129 |
* resolved.
|
|
130 |
* @param id Base URI for this stylesheet
|
|
131 |
*/
|
|
132 |
public void setSystemId(String id) {
|
|
133 |
_systemId = id;
|
|
134 |
}
|
|
135 |
|
|
136 |
/**
|
|
137 |
* Store URIResolver needed for Transformers.
|
|
138 |
*/
|
|
139 |
public void setURIResolver(URIResolver resolver) {
|
|
140 |
_uriResolver = resolver;
|
|
141 |
}
|
|
142 |
|
|
143 |
/**
|
|
144 |
* Implements javax.xml.transform.sax.TemplatesHandler.getTemplates()
|
|
145 |
* When a TemplatesHandler object is used as a ContentHandler or
|
|
146 |
* DocumentHandler for the parsing of transformation instructions, it
|
|
147 |
* creates a Templates object, which the caller can get once the SAX
|
|
148 |
* events have been completed.
|
|
149 |
* @return The Templates object that was created during the SAX event
|
|
150 |
* process, or null if no Templates object has been created.
|
|
151 |
*/
|
|
152 |
public Templates getTemplates() {
|
|
153 |
return _templates;
|
|
154 |
}
|
|
155 |
|
|
156 |
/**
|
|
157 |
* This method implements XSLTC's SourceLoader interface. It is used to
|
|
158 |
* glue a TrAX URIResolver to the XSLTC compiler's Input and Import classes.
|
|
159 |
*
|
|
160 |
* @param href The URI of the document to load
|
|
161 |
* @param context The URI of the currently loaded document
|
|
162 |
* @param xsltc The compiler that resuests the document
|
|
163 |
* @return An InputSource with the loaded document
|
|
164 |
*/
|
|
165 |
public InputSource loadSource(String href, String context, XSLTC xsltc) {
|
|
166 |
try {
|
|
167 |
// A _uriResolver must be set if this method is called
|
|
168 |
final Source source = _uriResolver.resolve(href, context);
|
|
169 |
if (source != null) {
|
|
170 |
return Util.getInputSource(xsltc, source);
|
|
171 |
}
|
|
172 |
}
|
|
173 |
catch (TransformerException e) {
|
|
174 |
// Falls through
|
|
175 |
}
|
|
176 |
return null;
|
|
177 |
}
|
|
178 |
|
|
179 |
// -- ContentHandler --------------------------------------------------
|
|
180 |
|
|
181 |
/**
|
|
182 |
* Re-initialize parser and forward SAX2 event.
|
|
183 |
*/
|
|
184 |
public void startDocument() {
|
|
185 |
XSLTC xsltc = _parser.getXSLTC();
|
|
186 |
xsltc.init(); // calls _parser.init()
|
|
187 |
xsltc.setOutputType(XSLTC.BYTEARRAY_OUTPUT);
|
|
188 |
_parser.startDocument();
|
|
189 |
}
|
|
190 |
|
|
191 |
/**
|
|
192 |
* Just forward SAX2 event to parser object.
|
|
193 |
*/
|
|
194 |
public void endDocument() throws SAXException {
|
|
195 |
_parser.endDocument();
|
|
196 |
|
|
197 |
// create the templates
|
|
198 |
try {
|
|
199 |
XSLTC xsltc = _parser.getXSLTC();
|
|
200 |
|
|
201 |
// Set the translet class name if not already set
|
12458
|
202 |
String transletName;
|
6
|
203 |
if (_systemId != null) {
|
|
204 |
transletName = Util.baseName(_systemId);
|
|
205 |
}
|
|
206 |
else {
|
|
207 |
transletName = (String)_tfactory.getAttribute("translet-name");
|
|
208 |
}
|
|
209 |
xsltc.setClassName(transletName);
|
|
210 |
|
|
211 |
// Get java-legal class name from XSLTC module
|
|
212 |
transletName = xsltc.getClassName();
|
|
213 |
|
|
214 |
Stylesheet stylesheet = null;
|
|
215 |
SyntaxTreeNode root = _parser.getDocumentRoot();
|
|
216 |
|
|
217 |
// Compile the translet - this is where the work is done!
|
|
218 |
if (!_parser.errorsFound() && root != null) {
|
|
219 |
// Create a Stylesheet element from the root node
|
|
220 |
stylesheet = _parser.makeStylesheet(root);
|
|
221 |
stylesheet.setSystemId(_systemId);
|
|
222 |
stylesheet.setParentStylesheet(null);
|
|
223 |
|
12458
|
224 |
if (xsltc.getTemplateInlining())
|
|
225 |
stylesheet.setTemplateInlining(true);
|
|
226 |
else
|
|
227 |
stylesheet.setTemplateInlining(false);
|
|
228 |
|
6
|
229 |
// Set a document loader (for xsl:include/import) if defined
|
|
230 |
if (_uriResolver != null) {
|
|
231 |
stylesheet.setSourceLoader(this);
|
|
232 |
}
|
|
233 |
|
|
234 |
_parser.setCurrentStylesheet(stylesheet);
|
|
235 |
|
|
236 |
// Set it as top-level in the XSLTC object
|
|
237 |
xsltc.setStylesheet(stylesheet);
|
|
238 |
|
|
239 |
// Create AST under the Stylesheet element
|
|
240 |
_parser.createAST(stylesheet);
|
|
241 |
}
|
|
242 |
|
|
243 |
// Generate the bytecodes and output the translet class(es)
|
|
244 |
if (!_parser.errorsFound() && stylesheet != null) {
|
|
245 |
stylesheet.setMultiDocument(xsltc.isMultiDocument());
|
|
246 |
stylesheet.setHasIdCall(xsltc.hasIdCall());
|
|
247 |
|
|
248 |
// Class synchronization is needed for BCEL
|
|
249 |
synchronized (xsltc.getClass()) {
|
|
250 |
stylesheet.translate();
|
|
251 |
}
|
|
252 |
}
|
|
253 |
|
|
254 |
if (!_parser.errorsFound()) {
|
|
255 |
// Check that the transformation went well before returning
|
|
256 |
final byte[][] bytecodes = xsltc.getBytecodes();
|
|
257 |
if (bytecodes != null) {
|
|
258 |
_templates =
|
|
259 |
new TemplatesImpl(xsltc.getBytecodes(), transletName,
|
|
260 |
_parser.getOutputProperties(), _indentNumber, _tfactory);
|
|
261 |
|
|
262 |
// Set URIResolver on templates object
|
|
263 |
if (_uriResolver != null) {
|
|
264 |
_templates.setURIResolver(_uriResolver);
|
|
265 |
}
|
|
266 |
}
|
|
267 |
}
|
|
268 |
else {
|
|
269 |
StringBuffer errorMessage = new StringBuffer();
|
|
270 |
Vector errors = _parser.getErrors();
|
|
271 |
final int count = errors.size();
|
|
272 |
for (int i = 0; i < count; i++) {
|
|
273 |
if (errorMessage.length() > 0)
|
|
274 |
errorMessage.append('\n');
|
|
275 |
errorMessage.append(errors.elementAt(i).toString());
|
|
276 |
}
|
|
277 |
throw new SAXException(ErrorMsg.JAXP_COMPILE_ERR, new TransformerException(errorMessage.toString()));
|
|
278 |
}
|
|
279 |
}
|
|
280 |
catch (CompilerException e) {
|
|
281 |
throw new SAXException(ErrorMsg.JAXP_COMPILE_ERR, e);
|
|
282 |
}
|
|
283 |
}
|
|
284 |
|
|
285 |
/**
|
|
286 |
* Just forward SAX2 event to parser object.
|
|
287 |
*/
|
|
288 |
public void startPrefixMapping(String prefix, String uri) {
|
|
289 |
_parser.startPrefixMapping(prefix, uri);
|
|
290 |
}
|
|
291 |
|
|
292 |
/**
|
|
293 |
* Just forward SAX2 event to parser object.
|
|
294 |
*/
|
|
295 |
public void endPrefixMapping(String prefix) {
|
|
296 |
_parser.endPrefixMapping(prefix);
|
|
297 |
}
|
|
298 |
|
|
299 |
/**
|
|
300 |
* Just forward SAX2 event to parser object.
|
|
301 |
*/
|
|
302 |
public void startElement(String uri, String localname, String qname,
|
|
303 |
Attributes attributes) throws SAXException
|
|
304 |
{
|
|
305 |
_parser.startElement(uri, localname, qname, attributes);
|
|
306 |
}
|
|
307 |
|
|
308 |
/**
|
|
309 |
* Just forward SAX2 event to parser object.
|
|
310 |
*/
|
|
311 |
public void endElement(String uri, String localname, String qname) {
|
|
312 |
_parser.endElement(uri, localname, qname);
|
|
313 |
}
|
|
314 |
|
|
315 |
/**
|
|
316 |
* Just forward SAX2 event to parser object.
|
|
317 |
*/
|
|
318 |
public void characters(char[] ch, int start, int length) {
|
|
319 |
_parser.characters(ch, start, length);
|
|
320 |
}
|
|
321 |
|
|
322 |
/**
|
|
323 |
* Just forward SAX2 event to parser object.
|
|
324 |
*/
|
|
325 |
public void processingInstruction(String name, String value) {
|
|
326 |
_parser.processingInstruction(name, value);
|
|
327 |
}
|
|
328 |
|
|
329 |
/**
|
|
330 |
* Just forward SAX2 event to parser object.
|
|
331 |
*/
|
|
332 |
public void ignorableWhitespace(char[] ch, int start, int length) {
|
|
333 |
_parser.ignorableWhitespace(ch, start, length);
|
|
334 |
}
|
|
335 |
|
|
336 |
/**
|
|
337 |
* Just forward SAX2 event to parser object.
|
|
338 |
*/
|
|
339 |
public void skippedEntity(String name) {
|
|
340 |
_parser.skippedEntity(name);
|
|
341 |
}
|
|
342 |
|
|
343 |
/**
|
|
344 |
* Set internal system Id and forward SAX2 event to parser object.
|
|
345 |
*/
|
|
346 |
public void setDocumentLocator(Locator locator) {
|
|
347 |
setSystemId(locator.getSystemId());
|
|
348 |
_parser.setDocumentLocator(locator);
|
|
349 |
}
|
|
350 |
}
|