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
  • 0807 // Pass compiler warnings to the error listener
  • 0808 if (_errorListener != this) {
  • 0809 try {
  • 0810 passWarningsToListener(xsltc.getWarnings());
  • 0811 }
  • 0812 catch (TransformerException e) {
  • 0813 throw new TransformerConfigurationException(e);
  • 0814 }
  • 0815 }
  • 0816 else {
  • 0817 xsltc.printWarnings();
  • 0818 }
  • 0819
  • 0820 // Check that the transformation went well before returning
  • 0821 if (bytecodes == null) {
  • 0822
  • 0823 ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_COMPILE_ERR);
  • 0824 TransformerConfigurationException exc = new TransformerConfigurationException(err.toString());
  • 0825
  • 0826 // Pass compiler errors to the error listener
  • 0827 if (_errorListener != null) {
  • 0828 passErrorsToListener(xsltc.getErrors());
  • 0829
  • 0830 // As required by TCK 1.2, send a fatalError to the
  • 0831 // error listener because compilation of the stylesheet
  • 0832 // failed and no further processing will be possible.
  • 0833 try {
  • 0834 _errorListener.fatalError(exc);
  • 0835 } catch (TransformerException te) {
  • 0836 // well, we tried.
  • 0837 }
  • 0838 }
  • 0839 else {
  • 0840 xsltc.printErrors();
  • 0841 }
  • 0842 throw exc;
  • 0843 }
  • 0844
  • 0845 return new TemplatesImpl(bytecodes, transletName,
  • 0846 xsltc.getOutputProperties(), _indentNumber, this);
  • 0847 }
  • 0848
  • 0849 /**
  • 0850 * javax.xml.transform.sax.SAXTransformerFactory implementation.
  • 0851 * Get a TemplatesHandler object that can process SAX ContentHandler
  • 0852 * events into a Templates object.
  • 0853 *
  • 0854 * @return A TemplatesHandler object that can handle SAX events
  • 0855 * @throws TransformerConfigurationException
  • 0856 */
  • 0857 public TemplatesHandler newTemplatesHandler()
  • 0858 throws TransformerConfigurationException
  • 0859 {
  • 0860 final TemplatesHandlerImpl handler =
  • 0861 new TemplatesHandlerImpl(_indentNumber, this);
  • 0862 if (_uriResolver != null) {
  • 0863 handler.setURIResolver(_uriResolver);
  • 0864 }
  • 0865 return handler;
  • 0866 }
  • 0867
  • 0868 /**
  • 0869 * javax.xml.transform.sax.SAXTransformerFactory implementation.
  • 0870 * Get a TransformerHandler object that can process SAX ContentHandler
  • 0871 * events into a Result. This method will return a pure copy transformer.
  • 0872 *
  • 0873 * @return A TransformerHandler object that can handle SAX events
  • 0874 * @throws TransformerConfigurationException
  • 0875 */
  • 0876 public TransformerHandler newTransformerHandler()
  • 0877 throws TransformerConfigurationException
  • 0878 {
  • 0879 final Transformer transformer = newTransformer();
  • 0880 if (_uriResolver != null) {
  • 0881 transformer.setURIResolver(_uriResolver);
  • 0882 }
  • 0883 return new TransformerHandlerImpl((TransformerImpl) transformer);
  • 0884 }
  • 0885
  • 0886 /**
  • 0887 * javax.xml.transform.sax.SAXTransformerFactory implementation.
  • 0888 * Get a TransformerHandler object that can process SAX ContentHandler
  • 0889 * events into a Result, based on the transformation instructions
  • 0890 * specified by the argument.
  • 0891 *
  • 0892 * @param src The source of the transformation instructions.
  • 0893 * @return A TransformerHandler object that can handle SAX events
  • 0894 * @throws TransformerConfigurationException
  • 0895 */
  • 0896 public TransformerHandler newTransformerHandler(Source src)
  • 0897 throws TransformerConfigurationException
  • 0898 {
  • 0899 final Transformer transformer = newTransformer(src);
  • 0900 if (_uriResolver != null) {
  • 0901 transformer.setURIResolver(_uriResolver);
  • 0902 }
  • 0903 return new TransformerHandlerImpl((TransformerImpl) transformer);
  • 0904 }
  • 0905
  • 0906 /**
  • 0907 * javax.xml.transform.sax.SAXTransformerFactory implementation.
  • 0908 * Get a TransformerHandler object that can process SAX ContentHandler
  • 0909 * events into a Result, based on the transformation instructions
  • 0910 * specified by the argument.
  • 0911 *
  • 0912 * @param templates Represents a pre-processed stylesheet
  • 0913 * @return A TransformerHandler object that can handle SAX events
  • 0914 * @throws TransformerConfigurationException
  • 0915 */
  • 0916 public TransformerHandler newTransformerHandler(Templates templates)
  • 0917 throws TransformerConfigurationException
  • 0918 {
  • 0919 final Transformer transformer = templates.newTransformer();
  • 0920 final TransformerImpl internal = (TransformerImpl)transformer;
  • 0921 return new TransformerHandlerImpl(internal);
  • 0922 }
  • 0923
  • 0924 /**
  • 0925 * javax.xml.transform.sax.SAXTransformerFactory implementation.
  • 0926 * Create an XMLFilter that uses the given source as the
  • 0927 * transformation instructions.
  • 0928 *
  • 0929 * @param src The source of the transformation instructions.
  • 0930 * @return An XMLFilter object, or null if this feature is not supported.
  • 0931 * @throws TransformerConfigurationException
  • 0932 */
  • 0933 public XMLFilter newXMLFilter(Source src)
  • 0934 throws TransformerConfigurationException
  • 0935 {
  • 0936 Templates templates = newTemplates(src);
  • 0937 if (templates == null) return null;
  • 0938 return newXMLFilter(templates);
  • 0939 }
  • 0940
  • 0941 /**
  • 0942 * javax.xml.transform.sax.SAXTransformerFactory implementation.
  • 0943 * Create an XMLFilter that uses the given source as the
  • 0944 * transformation instructions.
  • 0945 *
  • 0946 * @param templates The source of the transformation instructions.
  • 0947 * @return An XMLFilter object, or null if this feature is not supported.
  • 0948 * @throws TransformerConfigurationException
  • 0949 */
  • 0950 public XMLFilter newXMLFilter(Templates templates)
  • 0951 throws TransformerConfigurationException
  • 0952 {
  • 0953 try {
  • 0954 return new com.sun.org.apache.xalan.internal.xsltc.trax.TrAXFilter(templates);
  • 0955 }
  • 0956 catch (TransformerConfigurationException e1) {
  • 0957 if (_errorListener != null) {
  • 0958 try {
  • 0959 _errorListener.fatalError(e1);
  • 0960 return null;
  • 0961 }
  • 0962 catch (TransformerException e2) {
  • 0963 new TransformerConfigurationException(e2);
  • 0964 }
  • 0965 }
  • 0966 throw e1;
  • 0967 }
  • 0968 }
  • 0969
  • 0970 /**
  • 0971 * Receive notification of a recoverable error.
  • 0972 * The transformer must continue to provide normal parsing events after
  • 0973 * invoking this method. It should still be possible for the application
  • 0974 * to process the document through to the end.
  • 0975 *
  • 0976 * @param e The warning information encapsulated in a transformer
  • 0977 * exception.
  • 0978 * @throws TransformerException if the application chooses to discontinue
  • 0979 * the transformation (always does in our case).
  • 0980 */
  • 0981 public void error(TransformerException e)
  • 0982 throws TransformerException
  • 0983 {
  • 0984 Throwable wrapped = e.getException();
  • 0985 if (wrapped != null) {
  • 0986 System.err.println(new ErrorMsg(ErrorMsg.ERROR_PLUS_WRAPPED_MSG,
  • 0987 e.getMessageAndLocation(),
  • 0988 wrapped.getMessage()));
  • 0989 } else {
  • 0990 System.err.println(new ErrorMsg(ErrorMsg.ERROR_MSG,
  • 0991 e.getMessageAndLocation()));
  • 0992 }
  • 0993 throw e;
  • 0994 }
  • 0995
  • 0996 /**
  • 0997 * Receive notification of a non-recoverable error.
  • 0998 * The application must assume that the transformation cannot continue
  • 0999 * after the Transformer has invoked this method, and should continue
  • 1000 * (if at all) only to collect addition error messages. In fact,
  • 1001 * Transformers are free to stop reporting events once this method has
  • 1002 * been invoked.
  • 1003 *
  • 1004 * @param e warning information encapsulated in a transformer
  • 1005 * exception.
  • 1006 * @throws TransformerException if the application chooses to discontinue
  • 1007 * the transformation (always does in our case).
  • 1008 */
  • 1009 public void fatalError(TransformerException e)
  • 1010 throws TransformerException
  • 1011 {
  • 1012 Throwable wrapped = e.getException();
  • 1013 if (wrapped != null) {
  • 1014 System.err.println(new ErrorMsg(ErrorMsg.FATAL_ERR_PLUS_WRAPPED_MSG,
  • 1015 e.getMessageAndLocation(),
  • 1016 wrapped.getMessage()));
  • 1017 } else {
  • 1018 System.err.println(new ErrorMsg(ErrorMsg.FATAL_ERR_MSG,
  • 1019 e.getMessageAndLocation()));
  • 1020 }
  • 1021 throw e;
  • 1022 }
  • 1023
  • 1024 /**
  • 1025 * Receive notification of a warning.
  • 1026 * Transformers can use this method to report conditions that are not
  • 1027 * errors or fatal errors. The default behaviour is to take no action.
  • 1028 * After invoking this method, the Transformer must continue with the
  • 1029 * transformation. It should still be possible for the application to
  • 1030 * process the document through to the end.
  • 1031 *
  • 1032 * @param e The warning information encapsulated in a transformer
  • 1033 * exception.
  • 1034 * @throws TransformerException if the application chooses to discontinue
  • 1035 * the transformation (never does in our case).
  • 1036 */
  • 1037 public void warning(TransformerException e)
  • 1038 throws TransformerException
  • 1039 {
  • 1040 Throwable wrapped = e.getException();
  • 1041 if (wrapped != null) {
  • 1042 System.err.println(new ErrorMsg(ErrorMsg.WARNING_PLUS_WRAPPED_MSG,
  • 1043 e.getMessageAndLocation(),
  • 1044 wrapped.getMessage()));
  • 1045 } else {
  • 1046 System.err.println(new ErrorMsg(ErrorMsg.WARNING_MSG,
  • 1047 e.getMessageAndLocation()));
  • 1048 }
  • 1049 }
  • 1050
  • 1051 /**
  • 1052 * This method implements XSLTC's SourceLoader interface. It is used to
  • 1053 * glue a TrAX URIResolver to the XSLTC compiler's Input and Import classes.
  • 1054 *
  • 1055 * @param href The URI of the document to load
  • 1056 * @param context The URI of the currently loaded document
  • 1057 * @param xsltc The compiler that resuests the document
  • 1058 * @return An InputSource with the loaded document
  • 1059 */
  • 1060 public InputSource loadSource(String href, String context, XSLTC xsltc) {
  • 1061 try {
  • 1062 if (_uriResolver != null) {
  • 1063 final Source source = _uriResolver.resolve(href, context);
  • 1064 if (source != null) {
  • 1065 return Util.getInputSource(xsltc, source);
  • 1066 }
  • 1067 }
  • 1068 }
  • 1069 catch (TransformerException e) {
  • 1070 // Falls through
  • 1071 }
  • 1072 return null;
  • 1073 }
  • 1074
  • 1075 /**
  • 1076 * Reset the per-session attributes to their default values
  • 1077 */
  • 1078 private void resetTransientAttributes() {
  • 1079 _transletName = DEFAULT_TRANSLET_NAME;
  • 1080 _destinationDirectory = null;
  • 1081 _packageName = null;
  • 1082 _jarFileName = null;
  • 1083 }
  • 1084
  • 1085 /**
  • 1086 * Load the translet classes from local .class files and return
  • 1087 * the bytecode array.
  • 1088 *
  • 1089 * @param source The xsl source
  • 1090 * @param fullClassName The full name of the translet
  • 1091 * @return The bytecode array
  • 1092 */
  • 1093 private byte[][] getBytecodesFromClasses(Source source, String fullClassName)
  • 1094 {
  • 1095 if (fullClassName == null)
  • 1096 return null;
  • 1097
  • 1098 String xslFileName = getStylesheetFileName(source);
  • 1099 File xslFile = null;
  • 1100 if (xslFileName != null)
  • 1101 xslFile = new File(xslFileName);
  • 1102
  • 1103 // Find the base name of the translet
  • 1104 final String transletName;
  • 1105 int lastDotIndex = fullClassName.lastIndexOf('.');
  • 1106 if (lastDotIndex > 0)
  • 1107 transletName = fullClassName.substring(lastDotIndex+1);
  • 1108 else
  • 1109 transletName = fullClassName;
  • 1110
  • 1111 // Construct the path name for the translet class file
  • 1112 String transletPath = fullClassName.replace('.', '/');
  • 1113 if (_destinationDirectory != null) {
  • 1114 transletPath = _destinationDirectory + "/" + transletPath + ".class";
  • 1115 }
  • 1116 else {
  • 1117 if (xslFile != null && xslFile.getParent() != null)
  • 1118 transletPath = xslFile.getParent() + "/" + transletPath + ".class";
  • 1119 else
  • 1120 transletPath = transletPath + ".class";
  • 1121 }
  • 1122
  • 1123 // Return null if the translet class file does not exist.
  • 1124 File transletFile = new File(transletPath);
  • 1125 if (!transletFile.exists())
  • 1126 return null;
  • 1127
  • 1128 // Compare the timestamps of the translet and the xsl file.
  • 1129 // If the translet is older than the xsl file, return null
  • 1130 // so that the xsl file is used for the transformation and
  • 1131 // the translet is regenerated.
  • 1132 if (xslFile != null && xslFile.exists()) {
  • 1133 long xslTimestamp = xslFile.lastModified();
  • 1134 long transletTimestamp = transletFile.lastModified();
  • 1135 if (transletTimestamp < xslTimestamp)
  • 1136 return null;
  • 1137 }
  • 1138
  • 1139 // Load the translet into a bytecode array.
  • 1140 Vector bytecodes = new Vector();
  • 1141 int fileLength = (int)transletFile.length();
  • 1142 if (fileLength > 0) {
  • 1143 FileInputStream input = null;
  • 1144 try {
  • 1145 input = new FileInputStream(transletFile);
  • 1146 }
  • 1147 catch (FileNotFoundException e) {
  • 1148 return null;
  • 1149 }
  • 1150
  • 1151 byte[] bytes = new byte[fileLength];
  • 1152 try {
  • 1153 readFromInputStream(bytes, input, fileLength);
  • 1154 input.close();
  • 1155 }
  • 1156 catch (IOException e) {
  • 1157 return null;
  • 1158 }
  • 1159
  • 1160 bytecodes.addElement(bytes);
  • 1161 }
  • 1162 else
  • 1163 return null;
  • 1164
  • 1165 // Find the parent directory of the translet.
  • 1166 String transletParentDir = transletFile.getParent();
  • 1167 if (transletParentDir == null)
  • 1168 transletParentDir = System.getProperty("user.dir");
  • 1169
  • 1170 File transletParentFile = new File(transletParentDir);
  • 1171
  • 1172 // Find all the auxiliary files which have a name pattern of "transletClass$nnn.class".
  • 1173 final String transletAuxPrefix = transletName + "$";
  • 1174 File[] auxfiles = transletParentFile.listFiles(new FilenameFilter() {
  • 1175 public boolean accept(File dir, String name)
  • 1176 {
  • 1177 return (name.endsWith(".class") && name.startsWith(transletAuxPrefix));
  • 1178 }
  • 1179 });
  • 1180
  • 1181 // Load the auxiliary class files and add them to the bytecode array.
  • 1182 for (int i = 0; i < auxfiles.length; i++)
  • 1183 {
  • 1184 File auxfile = auxfiles[i];
  • 1185 int auxlength = (int)auxfile.length();
  • 1186 if (auxlength > 0) {
  • 1187 FileInputStream auxinput = null;
  • 1188 try {
  • 1189 auxinput = new FileInputStream(auxfile);
  • 1190 }
  • 1191 catch (FileNotFoundException e) {
  • 1192 continue;
  • 1193 }
  • 1194
  • 1195 byte[] bytes = new byte[auxlength];
  • 1196
  • 1197 try {
  • 1198 readFromInputStream(bytes, auxinput, auxlength);
  • 1199 auxinput.close();
  • 1200 }
  • 1201 catch (IOException e) {
  • 1202 continue;
  • 1203 }
  • 1204
  • 1205 bytecodes.addElement(bytes);
  • 1206 }
  • 1207 }
  • 1208
  • 1209 // Convert the Vector of byte[] to byte[][].
  • 1210 final int count = bytecodes.size();
  • 1211 if ( count > 0) {
  • 1212 final byte[][] result = new byte[count][1];
  • 1213 for (int i = 0; i < count; i++) {
  • 1214 result[i] = (byte[])bytecodes.elementAt(i);
  • 1215 }
  • 1216
  • 1217 return result;
  • 1218 }
  • 1219 else
  • 1220 return null;
  • 1221 }
  • 1222
  • 1223 /**
  • 1224 * Load the translet classes from the jar file and return the bytecode.
  • 1225 *
  • 1226 * @param source The xsl source
  • 1227 * @param fullClassName The full name of the translet
  • 1228 * @return The bytecode array
  • 1229 */
  • 1230 private byte[][] getBytecodesFromJar(Source source, String fullClassName)
  • 1231 {
  • 1232 String xslFileName = getStylesheetFileName(source);
  • 1233 File xslFile = null;
  • 1234 if (xslFileName != null)
  • 1235 xslFile = new File(xslFileName);
  • 1236
  • 1237 // Construct the path for the jar file
  • 1238 String jarPath = null;
  • 1239 if (_destinationDirectory != null)
  • 1240 jarPath = _destinationDirectory + "/" + _jarFileName;
  • 1241 else {
  • 1242 if (xslFile != null && xslFile.getParent() != null)
  • 1243 jarPath = xslFile.getParent() + "/" + _jarFileName;
  • 1244 else
  • 1245 jarPath = _jarFileName;
  • 1246 }
  • 1247
  • 1248 // Return null if the jar file does not exist.
  • 1249 File file = new File(jarPath);
  • 1250 if (!file.exists())
  • 1251 return null;
  • 1252
  • 1253 // Compare the timestamps of the jar file and the xsl file. Return null
  • 1254 // if the xsl file is newer than the jar file.
  • 1255 if (xslFile != null && xslFile.exists()) {
  • 1256 long xslTimestamp = xslFile.lastModified();
  • 1257 long transletTimestamp = file.lastModified();
  • 1258 if (transletTimestamp < xslTimestamp)
  • 1259 return null;
  • 1260 }
  • 1261
  • 1262 // Create a ZipFile object for the jar file
  • 1263 ZipFile jarFile = null;
  • 1264 try {
  • 1265 jarFile = new ZipFile(file);
  • 1266 }
  • 1267 catch (IOException e) {
  • 1268 return null;
  • 1269 }
  • 1270
  • 1271 String transletPath = fullClassName.replace('.', '/');
  • 1272 String transletAuxPrefix = transletPath + "$";
  • 1273 String transletFullName = transletPath + ".class";
  • 1274
  • 1275 Vector bytecodes = new Vector();
  • 1276
  • 1277 // Iterate through all entries in the jar file to find the
  • 1278 // translet and auxiliary classes.
  • 1279 Enumeration entries = jarFile.entries();
  • 1280 while (entries.hasMoreElements())
  • 1281 {
  • 1282 ZipEntry entry = (ZipEntry)entries.nextElement();
  • 1283 String entryName = entry.getName();
  • 1284 if (entry.getSize() > 0 &&
  • 1285 (entryName.equals(transletFullName) ||
  • 1286 (entryName.endsWith(".class") &&
  • 1287 entryName.startsWith(transletAuxPrefix))))
  • 1288 {
  • 1289 try {
  • 1290 InputStream input = jarFile.getInputStream(entry);
  • 1291 int size = (int)entry.getSize();
  • 1292 byte[] bytes = new byte[size];
  • 1293 readFromInputStream(bytes, input, size);
  • 1294 input.close();
  • 1295 bytecodes.addElement(bytes);
  • 1296 }
  • 1297 catch (IOException e) {
  • 1298 return null;
  • 1299 }
  • 1300 }
  • 1301 }
  • 1302
  • 1303 // Convert the Vector of byte[] to byte[][].
  • 1304 final int count = bytecodes.size();
  • 1305 if (count > 0) {
  • 1306 final byte[][] result = new byte[count][1];
  • 1307 for (int i = 0; i < count; i++) {
  • 1308 result[i] = (byte[])bytecodes.elementAt(i);
  • 1309 }
  • 1310
  • 1311 return result;
  • 1312 }
  • 1313 else
  • 1314 return null;
  • 1315 }
  • 1316
  • 1317 /**
  • 1318 * Read a given number of bytes from the InputStream into a byte array.
  • 1319 *
  • 1320 * @param bytes The byte array to store the input content.
  • 1321 * @param input The input stream.
  • 1322 * @param size The number of bytes to read.
  • 1323 */
  • 1324 private void readFromInputStream(byte[] bytes, InputStream input, int size)
  • 1325 throws IOException
  • 1326 {
  • 1327 int n = 0;
  • 1328 int offset = 0;
  • 1329 int length = size;
  • 1330 while (length > 0 && (n = input.read(bytes, offset, length)) > 0) {
  • 1331 offset = offset + n;
  • 1332 length = length - n;
  • 1333 }
  • 1334 }
  • 1335
  • 1336 /**
  • 1337 * Return the base class name of the translet.
  • 1338 * The translet name is resolved using the following rules:
  • 1339 * 1. if the _transletName attribute is set and its value is not "GregorSamsa",
  • 1340 * then _transletName is returned.
  • 1341 * 2. otherwise get the translet name from the base name of the system ID
  • 1342 * 3. return "GregorSamsa" if the result from step 2 is null.
  • 1343 *
  • 1344 * @param source The input Source
  • 1345 * @return The name of the translet class
  • 1346 */
  • 1347 private String getTransletBaseName(Source source)
  • 1348 {
  • 1349 String transletBaseName = null;
  • 1350 if (!_transletName.equals(DEFAULT_TRANSLET_NAME))
  • 1351 return _transletName;
  • 1352 else {
  • 1353 String systemId = source.getSystemId();
  • 1354 if (systemId != null) {
  • 1355 String baseName = Util.baseName(systemId);
  • 1356 if (baseName != null) {
  • 1357 baseName = Util.noExtName(baseName);
  • 1358 transletBaseName = Util.toJavaName(baseName);
  • 1359 }
  • 1360 }
  • 1361 }
  • 1362
  • 1363 return (transletBaseName != null) ? transletBaseName : DEFAULT_TRANSLET_NAME;
  • 1364 }
  • 1365
  • 1366 /**
  • 1367 * Return the local file name from the systemId of the Source object
  • 1368 *
  • 1369 * @param source The Source
  • 1370 * @return The file name in the local filesystem, or null if the
  • 1371 * systemId does not represent a local file.
  • 1372 */
  • 1373 private String getStylesheetFileName(Source source)
  • 1374 {
  • 1375 String systemId = source.getSystemId();
  • 1376 if (systemId != null) {
  • 1377 File file = new File(systemId);
  • 1378 if (file.exists())
  • 1379 return systemId;
  • 1380 else {
  • 1381 URL url = null;
  • 1382 try {
  • 1383 url = new URL(systemId);
  • 1384 }
  • 1385 catch (MalformedURLException e) {
  • 1386 return null;
  • 1387 }
  • 1388
  • 1389 if ("file".equals(url.getProtocol()))
  • 1390 return url.getFile();
  • 1391 else
  • 1392 return null;
  • 1393 }
  • 1394 }
  • 1395 else
  • 1396 return null;
  • 1397 }
  • 1398
  • 1399 /**
  • 1400 * Returns the Class object the provides the XSLTC DTM Manager service.
  • 1401 */
  • 1402 protected Class getDTMManagerClass() {
  • 1403 return m_DTMManagerClass;
  • 1404 }
  • 1405}

文件:TransformerFactoryImpl.java
包名:com.sun.org.apache.xalan.internal.xsltc.trax
类名:TransformerFactoryImpl
继承:SAXTransformerFactory
接口:[SourceLoader][ErrorListener]