Source Home >> Java Source 1.6.0 >> com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl V 0.09
  • 0001/*
  • 0002 * Copyright 2001-2004 The Apache Software Foundation.
  • 0003 *
  • 0004 * Licensed under the Apache License, Version 2.0 (the "License");
  • 0005 * you may not use this file except in compliance with the License.
  • 0006 * You may obtain a copy of the License at
  • 0007 *
  • 0008 * http://www.apache.org/licenses/LICENSE-2.0
  • 0009 *
  • 0010 * Unless required by applicable law or agreed to in writing, software
  • 0011 * distributed under the License is distributed on an "AS IS" BASIS,
  • 0012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  • 0013 * See the License for the specific language governing permissions and
  • 0014 * limitations under the License.
  • 0015 */
  • 0016/*
  • 0017 * $Id: TransformerFactoryImpl.java,v 1.2.4.1 2005/09/15 06:15:38 pvedula Exp $
  • 0018 */
  • 0019
  • 0020package com.sun.org.apache.xalan.internal.xsltc.trax;
  • 0021
  • 0022import java.io.File;
  • 0023import java.io.FileInputStream;
  • 0024import java.io.FileNotFoundException;
  • 0025import java.io.FilenameFilter;
  • 0026import java.io.IOException;
  • 0027import java.io.InputStream;
  • 0028import java.net.MalformedURLException;
  • 0029import java.net.URL;
  • 0030import java.util.Enumeration;
  • 0031import java.util.Hashtable;
  • 0032import java.util.Properties;
  • 0033import java.util.Vector;
  • 0034import java.util.zip.ZipEntry;
  • 0035import java.util.zip.ZipFile;
  • 0036
  • 0037import javax.xml.XMLConstants;
  • 0038import javax.xml.parsers.SAXParserFactory;
  • 0039import javax.xml.parsers.SAXParser;
  • 0040import javax.xml.parsers.ParserConfigurationException;
  • 0041
  • 0042import javax.xml.transform.ErrorListener;
  • 0043import javax.xml.transform.Source;
  • 0044import javax.xml.transform.Templates;
  • 0045import javax.xml.transform.Transformer;
  • 0046import javax.xml.transform.TransformerConfigurationException;
  • 0047import javax.xml.transform.TransformerException;
  • 0048import javax.xml.transform.URIResolver;
  • 0049import javax.xml.transform.dom.DOMResult;
  • 0050import javax.xml.transform.dom.DOMSource;
  • 0051import javax.xml.transform.sax.SAXResult;
  • 0052import javax.xml.transform.sax.SAXSource;
  • 0053import javax.xml.transform.sax.SAXTransformerFactory;
  • 0054import javax.xml.transform.sax.TemplatesHandler;
  • 0055import javax.xml.transform.sax.TransformerHandler;
  • 0056import javax.xml.transform.stream.StreamResult;
  • 0057import javax.xml.transform.stream.StreamSource;
  • 0058
  • 0059import com.sun.org.apache.xml.internal.utils.StylesheetPIHandler;
  • 0060import com.sun.org.apache.xml.internal.utils.StopParseException;
  • 0061
  • 0062import com.sun.org.apache.xalan.internal.xsltc.compiler.SourceLoader;
  • 0063import com.sun.org.apache.xalan.internal.xsltc.compiler.XSLTC;
  • 0064import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
  • 0065import com.sun.org.apache.xalan.internal.xsltc.dom.XSLTCDTMManager;
  • 0066
  • 0067
  • 0068import org.xml.sax.InputSource;
  • 0069import org.xml.sax.XMLFilter;
  • 0070import org.xml.sax.XMLReader;
  • 0071import org.xml.sax.helpers.XMLReaderFactory;
  • 0072
  • 0073/**
  • 0074 * Implementation of a JAXP1.1 TransformerFactory for Translets.
  • 0075 * @author G. Todd Miller
  • 0076 * @author Morten Jorgensen
  • 0077 * @author Santiago Pericas-Geertsen
  • 0078 */
  • 0079public class TransformerFactoryImpl
  • 0080 extends SAXTransformerFactory implements SourceLoader, ErrorListener
  • 0081{
  • 0082 // Public constants for attributes supported by the XSLTC TransformerFactory.
  • 0083 public final static String TRANSLET_NAME = "translet-name";
  • 0084 public final static String DESTINATION_DIRECTORY = "destination-directory";
  • 0085 public final static String PACKAGE_NAME = "package-name";
  • 0086 public final static String JAR_NAME = "jar-name";
  • 0087 public final static String GENERATE_TRANSLET = "generate-translet";
  • 0088 public final static String AUTO_TRANSLET = "auto-translet";
  • 0089 public final static String USE_CLASSPATH = "use-classpath";
  • 0090 public final static String DEBUG = "debug";
  • 0091 public final static String ENABLE_INLINING = "enable-inlining";
  • 0092 public final static String INDENT_NUMBER = "indent-number";
  • 0093
  • 0094 /**
  • 0095 * This error listener is used only for this factory and is not passed to
  • 0096 * the Templates or Transformer objects that we create.
  • 0097 */
  • 0098 private ErrorListener _errorListener = this;
  • 0099
  • 0100 /**
  • 0101 * This URIResolver is passed to all created Templates and Transformers
  • 0102 */
  • 0103 private URIResolver _uriResolver = null;
  • 0104
  • 0105 /**
  • 0106 * As Gregor Samsa awoke one morning from uneasy dreams he found himself
  • 0107 * transformed in his bed into a gigantic insect. He was lying on his hard,
  • 0108 * as it were armour plated, back, and if he lifted his head a little he
  • 0109 * could see his big, brown belly divided into stiff, arched segments, on
  • 0110 * top of which the bed quilt could hardly keep in position and was about
  • 0111 * to slide off completely. His numerous legs, which were pitifully thin
  • 0112 * compared to the rest of his bulk, waved helplessly before his eyes.
  • 0113 * "What has happened to me?", he thought. It was no dream....
  • 0114 */
  • 0115 protected final static String DEFAULT_TRANSLET_NAME = "GregorSamsa";
  • 0116
  • 0117 /**
  • 0118 * The class name of the translet
  • 0119 */
  • 0120 private String _transletName = DEFAULT_TRANSLET_NAME;
  • 0121
  • 0122 /**
  • 0123 * The destination directory for the translet
  • 0124 */
  • 0125 private String _destinationDirectory = null;
  • 0126
  • 0127 /**
  • 0128 * The package name prefix for all generated translet classes
  • 0129 */
  • 0130 private String _packageName = null;
  • 0131
  • 0132 /**
  • 0133 * The jar file name which the translet classes are packaged into
  • 0134 */
  • 0135 private String _jarFileName = null;
  • 0136
  • 0137 /**
  • 0138 * This Hashtable is used to store parameters for locating
  • 0139 * <?xml-stylesheet ...?> processing instructions in XML docs.
  • 0140 */
  • 0141 private Hashtable _piParams = null;
  • 0142
  • 0143 /**
  • 0144 * The above hashtable stores objects of this class.
  • 0145 */
  • 0146 private static class PIParamWrapper {
  • 0147 public String _media = null;
  • 0148 public String _title = null;
  • 0149 public String _charset = null;
  • 0150
  • 0151 public PIParamWrapper(String media, String title, String charset) {
  • 0152 _media = media;
  • 0153 _title = title;
  • 0154 _charset = charset;
  • 0155 }
  • 0156 }
  • 0157
  • 0158 /**
  • 0159 * Set to <code>true</code> when debugging is enabled.
  • 0160 */
  • 0161 private boolean _debug = false;
  • 0162
  • 0163 /**
  • 0164 * Set to <code>true</code> when templates are inlined.
  • 0165 */
  • 0166 private boolean _enableInlining = false;
  • 0167
  • 0168 /**
  • 0169 * Set to <code>true</code> when we want to generate
  • 0170 * translet classes from the stylesheet.
  • 0171 */
  • 0172 private boolean _generateTranslet = false;
  • 0173
  • 0174 /**
  • 0175 * If this is set to <code>true</code>, we attempt to use translet classes
  • 0176 * for transformation if possible without compiling the stylesheet. The
  • 0177 * translet class is only used if its timestamp is newer than the timestamp
  • 0178 * of the stylesheet.
  • 0179 */
  • 0180 private boolean _autoTranslet = false;
  • 0181
  • 0182 /**
  • 0183 * If this is set to <code>true</code>, we attempt to load the translet
  • 0184 * from the CLASSPATH.
  • 0185 */
  • 0186 private boolean _useClasspath = false;
  • 0187
  • 0188 /**
  • 0189 * Number of indent spaces when indentation is turned on.
  • 0190 */
  • 0191 private int _indentNumber = -1;
  • 0192
  • 0193 /**
  • 0194 * The provider of the XSLTC DTM Manager service. This is fixed for any
  • 0195 * instance of this class. In order to change service providers, a new
  • 0196 * XSLTC <code>TransformerFactory</code> must be instantiated.
  • 0197 * @see XSLTCDTMManager#getDTMManagerClass()
  • 0198 */
  • 0199 private Class m_DTMManagerClass;
  • 0200
  • 0201 /**
  • 0202 * <p>State of secure processing feature.</p>
  • 0203 */
  • 0204 private boolean _isSecureProcessing = false;
  • 0205
  • 0206 /**
  • 0207 * javax.xml.transform.sax.TransformerFactory implementation.
  • 0208 */
  • 0209 public TransformerFactoryImpl() {
  • 0210 m_DTMManagerClass = XSLTCDTMManager.getDTMManagerClass();
  • 0211 }
  • 0212
  • 0213 /**
  • 0214 * javax.xml.transform.sax.TransformerFactory implementation.
  • 0215 * Set the error event listener for the TransformerFactory, which is used
  • 0216 * for the processing of transformation instructions, and not for the
  • 0217 * transformation itself.
  • 0218 *
  • 0219 * @param listener The error listener to use with the TransformerFactory
  • 0220 * @throws IllegalArgumentException
  • 0221 */
  • 0222 public void setErrorListener(ErrorListener listener)
  • 0223 throws IllegalArgumentException
  • 0224 {
  • 0225 if (listener == null) {
  • 0226 ErrorMsg err = new ErrorMsg(ErrorMsg.ERROR_LISTENER_NULL_ERR,
  • 0227 "TransformerFactory");
  • 0228 throw new IllegalArgumentException(err.toString());
  • 0229 }
  • 0230 _errorListener = listener;
  • 0231 }
  • 0232
  • 0233 /**
  • 0234 * javax.xml.transform.sax.TransformerFactory implementation.
  • 0235 * Get the error event handler for the TransformerFactory.
  • 0236 *
  • 0237 * @return The error listener used with the TransformerFactory
  • 0238 */
  • 0239 public ErrorListener getErrorListener() {
  • 0240 return _errorListener;
  • 0241 }
  • 0242
  • 0243 /**
  • 0244 * javax.xml.transform.sax.TransformerFactory implementation.
  • 0245 * Returns the value set for a TransformerFactory attribute
  • 0246 *
  • 0247 * @param name The attribute name
  • 0248 * @return An object representing the attribute value
  • 0249 * @throws IllegalArgumentException
  • 0250 */
  • 0251 public Object getAttribute(String name)
  • 0252 throws IllegalArgumentException
  • 0253 {
  • 0254 // Return value for attribute 'translet-name'
  • 0255 if (name.equals(TRANSLET_NAME)) {
  • 0256 return _transletName;
  • 0257 }
  • 0258 else if (name.equals(GENERATE_TRANSLET)) {
  • 0259 return new Boolean(_generateTranslet);
  • 0260 }
  • 0261 else if (name.equals(AUTO_TRANSLET)) {
  • 0262 return new Boolean(_autoTranslet);
  • 0263 }
  • 0264
  • 0265 // Throw an exception for all other attributes
  • 0266 ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_INVALID_ATTR_ERR, name);
  • 0267 throw new IllegalArgumentException(err.toString());
  • 0268 }
  • 0269
  • 0270 /**
  • 0271 * javax.xml.transform.sax.TransformerFactory implementation.
  • 0272 * Sets the value for a TransformerFactory attribute.
  • 0273 *
  • 0274 * @param name The attribute name
  • 0275 * @param value An object representing the attribute value
  • 0276 * @throws IllegalArgumentException
  • 0277 */
  • 0278 public void setAttribute(String name, Object value)
  • 0279 throws IllegalArgumentException
  • 0280 {
  • 0281 // Set the default translet name (ie. class name), which will be used
  • 0282 // for translets that cannot be given a name from their system-id.
  • 0283 if (name.equals(TRANSLET_NAME) && value instanceof String) {
  • 0284 _transletName = (String) value;
  • 0285 return;
  • 0286 }
  • 0287 else if (name.equals(DESTINATION_DIRECTORY) && value instanceof String) {
  • 0288 _destinationDirectory = (String) value;
  • 0289 return;
  • 0290 }
  • 0291 else if (name.equals(PACKAGE_NAME) && value instanceof String) {
  • 0292 _packageName = (String) value;
  • 0293 return;
  • 0294 }
  • 0295 else if (name.equals(JAR_NAME) && value instanceof String) {
  • 0296 _jarFileName = (String) value;
  • 0297 return;
  • 0298 }
  • 0299 else if (name.equals(GENERATE_TRANSLET)) {
  • 0300 if (value instanceof Boolean) {
  • 0301 _generateTranslet = ((Boolean) value).booleanValue();
  • 0302 return;
  • 0303 }
  • 0304 else if (value instanceof String) {
  • 0305 _generateTranslet = ((String) value).equalsIgnoreCase("true");
  • 0306 return;
  • 0307 }
  • 0308 }
  • 0309 else if (name.equals(AUTO_TRANSLET)) {
  • 0310 if (value instanceof Boolean) {
  • 0311 _autoTranslet = ((Boolean) value).booleanValue();
  • 0312 return;
  • 0313 }
  • 0314 else if (value instanceof String) {
  • 0315 _autoTranslet = ((String) value).equalsIgnoreCase("true");
  • 0316 return;
  • 0317 }
  • 0318 }
  • 0319 else if (name.equals(USE_CLASSPATH)) {
  • 0320 if (value instanceof Boolean) {
  • 0321 _useClasspath = ((Boolean) value).booleanValue();
  • 0322 return;
  • 0323 }
  • 0324 else if (value instanceof String) {
  • 0325 _useClasspath = ((String) value).equalsIgnoreCase("true");
  • 0326 return;
  • 0327 }
  • 0328 }
  • 0329 else if (name.equals(DEBUG)) {
  • 0330 if (value instanceof Boolean) {
  • 0331 _debug = ((Boolean) value).booleanValue();
  • 0332 return;
  • 0333 }
  • 0334 else if (value instanceof String) {
  • 0335 _debug = ((String) value).equalsIgnoreCase("true");
  • 0336 return;
  • 0337 }
  • 0338 }
  • 0339 else if (name.equals(ENABLE_INLINING)) {
  • 0340 if (value instanceof Boolean) {
  • 0341 _enableInlining = ((Boolean) value).booleanValue();
  • 0342 return;
  • 0343 }
  • 0344 else if (value instanceof String) {
  • 0345 _enableInlining = ((String) value).equalsIgnoreCase("true");
  • 0346 return;
  • 0347 }
  • 0348 }
  • 0349 else if (name.equals(INDENT_NUMBER)) {
  • 0350 if (value instanceof String) {
  • 0351 try {
  • 0352 _indentNumber = Integer.parseInt((String) value);
  • 0353 return;
  • 0354 }
  • 0355 catch (NumberFormatException e) {
  • 0356 // Falls through
  • 0357 }
  • 0358 }
  • 0359 else if (value instanceof Integer) {
  • 0360 _indentNumber = ((Integer) value).intValue();
  • 0361 return;
  • 0362 }
  • 0363 }
  • 0364
  • 0365 // Throw an exception for all other attributes
  • 0366 final ErrorMsg err
  • 0367 = new ErrorMsg(ErrorMsg.JAXP_INVALID_ATTR_ERR, name);
  • 0368 throw new IllegalArgumentException(err.toString());
  • 0369 }
  • 0370
  • 0371 /**
  • 0372 * <p>Set a feature for this <code>TransformerFactory</code> and <code>Transformer</code>s
  • 0373 * or <code>Template</code>s created by this factory.</p>
  • 0374 *
  • 0375 * <p>
  • 0376 * Feature names are fully qualified {@link java.net.URI}s.
  • 0377 * Implementations may define their own features.
  • 0378 * An {@link TransformerConfigurationException} is thrown if this <code>TransformerFactory</code> or the
  • 0379 * <code>Transformer</code>s or <code>Template</code>s it creates cannot support the feature.
  • 0380 * It is possible for an <code>TransformerFactory</code> to expose a feature value but be unable to change its state.
  • 0381 * </p>
  • 0382 *
  • 0383 * <p>See {@link javax.xml.transform.TransformerFactory} for full documentation of specific features.</p>
  • 0384 *
  • 0385 * @param name Feature name.
  • 0386 * @param value Is feature state <code>true</code> or <code>false</code>.
  • 0387 *
  • 0388 * @throws TransformerConfigurationException if this <code>TransformerFactory</code>
  • 0389 * or the <code>Transformer</code>s or <code>Template</code>s it creates cannot support this feature.
  • 0390 * @throws NullPointerException If the <code>name</code> parameter is null.
  • 0391 */
  • 0392 public void setFeature(String name, boolean value)
  • 0393 throws TransformerConfigurationException {
  • 0394
  • 0395 // feature name cannot be null
  • 0396 if (name == null) {
  • 0397 ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_SET_FEATURE_NULL_NAME);
  • 0398 throw new NullPointerException(err.toString());
  • 0399 }
  • 0400 // secure processing?
  • 0401 else if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) {
  • 0402 _isSecureProcessing = value;
  • 0403 // all done processing feature
  • 0404 return;
  • 0405 }
  • 0406 else {
  • 0407 // unknown feature
  • 0408 ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_UNSUPPORTED_FEATURE, name);
  • 0409 throw new TransformerConfigurationException(err.toString());
  • 0410 }
  • 0411 }
  • 0412
  • 0413 /**
  • 0414 * javax.xml.transform.sax.TransformerFactory implementation.
  • 0415 * Look up the value of a feature (to see if it is supported).
  • 0416 * This method must be updated as the various methods and features of this
  • 0417 * class are implemented.
  • 0418 *
  • 0419 * @param name The feature name
  • 0420 * @return 'true' if feature is supported, 'false' if not
  • 0421 */
  • 0422 public boolean getFeature(String name) {
  • 0423 // All supported features should be listed here
  • 0424 String[] features = {
  • 0425 DOMSource.FEATURE,
  • 0426 DOMResult.FEATURE,
  • 0427 SAXSource.FEATURE,
  • 0428 SAXResult.FEATURE,
  • 0429 StreamSource.FEATURE,
  • 0430 StreamResult.FEATURE,
  • 0431 SAXTransformerFactory.FEATURE,
  • 0432 SAXTransformerFactory.FEATURE_XMLFILTER
  • 0433 };
  • 0434
  • 0435 // feature name cannot be null
  • 0436 if (name == null) {
  • 0437 ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_GET_FEATURE_NULL_NAME);
  • 0438 throw new NullPointerException(err.toString());
  • 0439 }
  • 0440
  • 0441 // Inefficient, but array is small
  • 0442 for (int i =0; i < features.length; i++) {
  • 0443 if (name.equals(features[i])) {
  • 0444 return true;
  • 0445 }
  • 0446 }
  • 0447 // secure processing?
  • 0448 if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) {
  • 0449 return _isSecureProcessing;
  • 0450 }
  • 0451
  • 0452 // Feature not supported
  • 0453 return false;
  • 0454 }
  • 0455
  • 0456 /**
  • 0457 * javax.xml.transform.sax.TransformerFactory implementation.
  • 0458 * Get the object that is used by default during the transformation to
  • 0459 * resolve URIs used in document(), xsl:import, or xsl:include.
  • 0460 *
  • 0461 * @return The URLResolver used for this TransformerFactory and all
  • 0462 * Templates and Transformer objects created using this factory
  • 0463 */
  • 0464 public URIResolver getURIResolver() {
  • 0465 return _uriResolver;
  • 0466 }
  • 0467
  • 0468 /**
  • 0469 * javax.xml.transform.sax.TransformerFactory implementation.
  • 0470 * Set the object that is used by default during the transformation to
  • 0471 * resolve URIs used in document(), xsl:import, or xsl:include. Note that
  • 0472 * this does not affect Templates and Transformers that are already
  • 0473 * created with this factory.
  • 0474 *
  • 0475 * @param resolver The URLResolver used for this TransformerFactory and all
  • 0476 * Templates and Transformer objects created using this factory
  • 0477 */
  • 0478 public void setURIResolver(URIResolver resolver) {
  • 0479 _uriResolver = resolver;
  • 0480 }
  • 0481
  • 0482 /**
  • 0483 * javax.xml.transform.sax.TransformerFactory implementation.
  • 0484 * Get the stylesheet specification(s) associated via the xml-stylesheet
  • 0485 * processing instruction (see http://www.w3.org/TR/xml-stylesheet/) with
  • 0486 * the document document specified in the source parameter, and that match
  • 0487 * the given criteria.
  • 0488 *
  • 0489 * @param source The XML source document.
  • 0490 * @param media The media attribute to be matched. May be null, in which
  • 0491 * case the prefered templates will be used (i.e. alternate = no).
  • 0492 * @param title The value of the title attribute to match. May be null.
  • 0493 * @param charset The value of the charset attribute to match. May be null.
  • 0494 * @return A Source object suitable for passing to the TransformerFactory.
  • 0495 * @throws TransformerConfigurationException
  • 0496 */
  • 0497 public Source getAssociatedStylesheet(Source source, String media,
  • 0498 String title, String charset)
  • 0499 throws TransformerConfigurationException {
  • 0500
  • 0501 String baseId;
  • 0502 XMLReader reader = null;
  • 0503 InputSource isource = null;
  • 0504
  • 0505
  • 0506 /**
  • 0507 * Fix for bugzilla bug 24187
  • 0508 */
  • 0509 StylesheetPIHandler _stylesheetPIHandler = new StylesheetPIHandler(null,media,title,charset);
  • 0510
  • 0511 try {
  • 0512
  • 0513 if (source instanceof DOMSource ) {
  • 0514 final DOMSource domsrc = (DOMSource) source;
  • 0515 baseId = domsrc.getSystemId();
  • 0516 final org.w3c.dom.Node node = domsrc.getNode();
  • 0517 final DOM2SAX dom2sax = new DOM2SAX(node);
  • 0518
  • 0519 _stylesheetPIHandler.setBaseId(baseId);
  • 0520
  • 0521 dom2sax.setContentHandler( _stylesheetPIHandler);
  • 0522 dom2sax.parse();
  • 0523 } else {
  • 0524 isource = SAXSource.sourceToInputSource(source);
  • 0525 baseId = isource.getSystemId();
  • 0526
  • 0527 SAXParserFactory factory = SAXParserFactory.newInstance();
  • 0528 factory.setNamespaceAware(true);
  • 0529
  • 0530 if (_isSecureProcessing) {
  • 0531 try {
  • 0532 factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
  • 0533 }
  • 0534 catch (org.xml.sax.SAXException e) {}
  • 0535 }
  • 0536
  • 0537 SAXParser jaxpParser = factory.newSAXParser();
  • 0538
  • 0539 reader = jaxpParser.getXMLReader();
  • 0540 if (reader == null) {
  • 0541 reader = XMLReaderFactory.createXMLReader();
  • 0542 }
  • 0543
  • 0544 _stylesheetPIHandler.setBaseId(baseId);
  • 0545 reader.setContentHandler(_stylesheetPIHandler);
  • 0546 reader.parse(isource);
  • 0547
  • 0548 }
  • 0549
  • 0550 if (_uriResolver != null ) {
  • 0551 _stylesheetPIHandler.setURIResolver(_uriResolver);
  • 0552 }
  • 0553
  • 0554 } catch (StopParseException e ) {
  • 0555 // startElement encountered so do not parse further
  • 0556
  • 0557 } catch (javax.xml.parsers.ParserConfigurationException e) {
  • 0558
  • 0559 throw new TransformerConfigurationException(
  • 0560 "getAssociatedStylesheets failed", e);
  • 0561
  • 0562 } catch (org.xml.sax.SAXException se) {
  • 0563
  • 0564 throw new TransformerConfigurationException(
  • 0565 "getAssociatedStylesheets failed", se);
  • 0566
  • 0567
  • 0568 } catch (IOException ioe ) {
  • 0569 throw new TransformerConfigurationException(
  • 0570 "getAssociatedStylesheets failed", ioe);
  • 0571
  • 0572 }
  • 0573
  • 0574 return _stylesheetPIHandler.getAssociatedStylesheet();
  • 0575
  • 0576 }
  • 0577
  • 0578 /**
  • 0579 * javax.xml.transform.sax.TransformerFactory implementation.
  • 0580 * Create a Transformer object that copies the input document to the result.
  • 0581 *
  • 0582 * @return A Transformer object that simply copies the source to the result.
  • 0583 * @throws TransformerConfigurationException
  • 0584 */
  • 0585 public Transformer newTransformer()
  • 0586 throws TransformerConfigurationException
  • 0587 {
  • 0588 TransformerImpl result = new TransformerImpl(new Properties(),
  • 0589 _indentNumber, this);
  • 0590 if (_uriResolver != null) {
  • 0591 result.setURIResolver(_uriResolver);
  • 0592 }
  • 0593
  • 0594 if (_isSecureProcessing) {
  • 0595 result.setSecureProcessing(true);
  • 0596 }
  • 0597 return result;
  • 0598 }
  • 0599
  • 0600 /**
  • 0601 * javax.xml.transform.sax.TransformerFactory implementation.
  • 0602 * Process the Source into a Templates object, which is a a compiled
  • 0603 * representation of the source. Note that this method should not be
  • 0604 * used with XSLTC, as the time-consuming compilation is done for each
  • 0605 * and every transformation.
  • 0606 *
  • 0607 * @return A Templates object that can be used to create Transformers.
  • 0608 * @throws TransformerConfigurationException
  • 0609 */
  • 0610 public Transformer newTransformer(Source source) throws
  • 0611 TransformerConfigurationException
  • 0612 {
  • 0613 final Templates templates = newTemplates(source);
  • 0614 final Transformer transformer = templates.newTransformer();
  • 0615 if (_uriResolver != null) {
  • 0616 transformer.setURIResolver(_uriResolver);
  • 0617 }
  • 0618 return(transformer);
  • 0619 }
  • 0620
  • 0621 /**
  • 0622 * Pass warning messages from the compiler to the error listener
  • 0623 */
  • 0624 private void passWarningsToListener(Vector messages)
  • 0625 throws TransformerException
  • 0626 {
  • 0627 if (_errorListener == null || messages == null) {
  • 0628 return;
  • 0629 }
  • 0630 // Pass messages to listener, one by one
  • 0631 final int count = messages.size();
  • 0632 for (int pos = 0; pos < count; pos++) {
  • 0633 ErrorMsg msg = (ErrorMsg)messages.elementAt(pos);
  • 0634 // Workaround for the TCK failure ErrorListener.errorTests.error001.
  • 0635 if (msg.isWarningError())
  • 0636 _errorListener.error(
  • 0637 new TransformerConfigurationException(msg.toString()));
  • 0638 else
  • 0639 _errorListener.warning(
  • 0640 new TransformerConfigurationException(msg.toString()));
  • 0641 }
  • 0642 }
  • 0643
  • 0644 /**
  • 0645 * Pass error messages from the compiler to the error listener
  • 0646 */
  • 0647 private void passErrorsToListener(Vector messages) {
  • 0648 try {
  • 0649 if (_errorListener == null || messages == null) {
  • 0650 return;
  • 0651 }
  • 0652 // Pass messages to listener, one by one
  • 0653 final int count = messages.size();
  • 0654 for (int pos = 0; pos < count; pos++) {
  • 0655 String message = messages.elementAt(pos).toString();
  • 0656 _errorListener.error(new TransformerException(message));
  • 0657 }
  • 0658 }
  • 0659 catch (TransformerException e) {
  • 0660 // nada
  • 0661 }
  • 0662 }
  • 0663
  • 0664 /**
  • 0665 * javax.xml.transform.sax.TransformerFactory implementation.
  • 0666 * Process the Source into a Templates object, which is a a compiled
  • 0667 * representation of the source.
  • 0668 *
  • 0669 * @param source The input stylesheet - DOMSource not supported!!!
  • 0670 * @return A Templates object that can be used to create Transformers.
  • 0671 * @throws TransformerConfigurationException
  • 0672 */
  • 0673 public Templates newTemplates(Source source)
  • 0674 throws TransformerConfigurationException
  • 0675 {
  • 0676 // If the _useClasspath attribute is true, try to load the translet from
  • 0677 // the CLASSPATH and create a template object using the loaded
  • 0678 // translet.
  • 0679 if (_useClasspath) {
  • 0680 String transletName = getTransletBaseName(source);
  • 0681
  • 0682 if (_packageName != null)
  • 0683 transletName = _packageName + "." + transletName;
  • 0684
  • 0685 try {
  • 0686 final Class clazz = ObjectFactory.findProviderClass(
  • 0687 transletName, ObjectFactory.findClassLoader(), true);
  • 0688 resetTransientAttributes();
  • 0689
  • 0690 return new TemplatesImpl(new Class[]{clazz}, transletName, null, _indentNumber, this);
  • 0691 }
  • 0692 catch (ClassNotFoundException cnfe) {
  • 0693 ErrorMsg err = new ErrorMsg(ErrorMsg.CLASS_NOT_FOUND_ERR, transletName);
  • 0694 throw new TransformerConfigurationException(err.toString());
  • 0695 }
  • 0696 catch (Exception e) {
  • 0697 ErrorMsg err = new ErrorMsg(
  • 0698 new ErrorMsg(ErrorMsg.RUNTIME_ERROR_KEY)
  • 0699 + e.getMessage());
  • 0700 throw new TransformerConfigurationException(err.toString());
  • 0701 }
  • 0702 }
  • 0703
  • 0704 // If _autoTranslet is true, we will try to load the bytecodes
  • 0705 // from the translet classes without compiling the stylesheet.
  • 0706 if (_autoTranslet) {
  • 0707 byte[][] bytecodes = null;
  • 0708 String transletClassName = getTransletBaseName(source);
  • 0709
  • 0710 if (_packageName != null)
  • 0711 transletClassName = _packageName + "." + transletClassName;
  • 0712
  • 0713 if (_jarFileName != null)
  • 0714 bytecodes = getBytecodesFromJar(source, transletClassName);
  • 0715 else
  • 0716 bytecodes = getBytecodesFromClasses(source, transletClassName);
  • 0717
  • 0718 if (bytecodes != null) {
  • 0719 if (_debug) {
  • 0720 if (_jarFileName != null)
  • 0721 System.err.println(new ErrorMsg(
  • 0722 ErrorMsg.TRANSFORM_WITH_JAR_STR, transletClassName, _jarFileName));
  • 0723 else
  • 0724 System.err.println(new ErrorMsg(
  • 0725 ErrorMsg.TRANSFORM_WITH_TRANSLET_STR, transletClassName));
  • 0726 }
  • 0727
  • 0728 // Reset the per-session attributes to their default values
  • 0729 // after each newTemplates() call.
  • 0730 resetTransientAttributes();
  • 0731
  • 0732 return new TemplatesImpl(bytecodes, transletClassName, null, _indentNumber, this);
  • 0733 }
  • 0734 }
  • 0735
  • 0736 // Create and initialize a stylesheet compiler
  • 0737 final XSLTC xsltc = new XSLTC();
  • 0738 if (_debug) xsltc.setDebug(true);
  • 0739 if (_enableInlining) xsltc.setTemplateInlining(true);
  • 0740 if (_isSecureProcessing) xsltc.setSecureProcessing(true);
  • 0741 xsltc.init();
  • 0742
  • 0743 // Set a document loader (for xsl:include/import) if defined
  • 0744 if (_uriResolver != null) {
  • 0745 xsltc.setSourceLoader(this);
  • 0746 }
  • 0747
  • 0748 // Pass parameters to the Parser to make sure it locates the correct
  • 0749 // <?xml-stylesheet ...?> PI in an XML input document
  • 0750 if ((_piParams != null) && (_piParams.get(source) != null)) {
  • 0751 // Get the parameters for this Source object
  • 0752 PIParamWrapper p = (PIParamWrapper)_piParams.get(source);
  • 0753 // Pass them on to the compiler (which will pass then to the parser)
  • 0754 if (p != null) {
  • 0755 xsltc.setPIParameters(p._media, p._title, p._charset);
  • 0756 }
  • 0757 }
  • 0758
  • 0759 // Set the attributes for translet generation
  • 0760 int outputType = XSLTC.BYTEARRAY_OUTPUT;
  • 0761 if (_generateTranslet || _autoTranslet) {
  • 0762 // Set the translet name
  • 0763 xsltc.setClassName(getTransletBaseName(source));
  • 0764
  • 0765 if (_destinationDirectory != null)
  • 0766 xsltc.setDestDirectory(_destinationDirectory);
  • 0767 else {
  • 0768 String xslName = getStylesheetFileName(source);
  • 0769 if (xslName != null) {
  • 0770 File xslFile = new File(xslName);
  • 0771 String xslDir = xslFile.getParent();
  • 0772
  • 0773 if (xslDir != null)
  • 0774 xsltc.setDestDirectory(xslDir);
  • 0775 }
  • 0776 }
  • 0777
  • 0778 if (_packageName != null)
  • 0779 xsltc.setPackageName(_packageName);
  • 0780
  • 0781 if (_jarFileName != null) {
  • 0782 xsltc.setJarFileName(_jarFileName);
  • 0783 outputType = XSLTC.BYTEARRAY_AND_JAR_OUTPUT;
  • 0784 }
  • 0785 else
  • 0786 outputType = XSLTC.BYTEARRAY_AND_FILE_OUTPUT;
  • 0787 }
  • 0788
  • 0789 // Compile the stylesheet
  • 0790 final InputSource input = Util.getInputSource(xsltc, source);
  • 0791 byte[][] bytecodes = xsltc.compile(null, input, outputType);
  • 0792 final String transletName = xsltc.getClassName();
  • 0793
  • 0794 // Output to the jar file if the jar file name is set.
  • 0795 if ((_generateTranslet || _autoTranslet)
  • 0796 && bytecodes != null && _jarFileName != null) {
  • 0797 try {
  • 0798 xsltc.outputToJar();
  • 0799 }
  • 0800 catch (java.io.IOException e) { }
  • 0801 }
  • 0802
  • 0803 // Reset the per-session attributes to their default values
  • 0804 // after each newTemplates() call.
  • 0805 resetTransientAttributes();
  • 0806