jaxp/src/share/classes/com/sun/org/apache/xalan/internal/xsltc/trax/TransformerFactoryImpl.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 6 7f561c08de6b
child 2669 15024792697e
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     1
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     2
 * reserved comment block
7f561c08de6b Initial load
duke
parents:
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
7f561c08de6b Initial load
duke
parents:
diff changeset
     4
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
     5
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     6
 * Copyright 2001-2004 The Apache Software Foundation.
7f561c08de6b Initial load
duke
parents:
diff changeset
     7
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
     8
 * Licensed under the Apache License, Version 2.0 (the "License");
7f561c08de6b Initial load
duke
parents:
diff changeset
     9
 * you may not use this file except in compliance with the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    10
 * You may obtain a copy of the License at
7f561c08de6b Initial load
duke
parents:
diff changeset
    11
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    12
 *     http://www.apache.org/licenses/LICENSE-2.0
7f561c08de6b Initial load
duke
parents:
diff changeset
    13
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    14
 * Unless required by applicable law or agreed to in writing, software
7f561c08de6b Initial load
duke
parents:
diff changeset
    15
 * distributed under the License is distributed on an "AS IS" BASIS,
7f561c08de6b Initial load
duke
parents:
diff changeset
    16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7f561c08de6b Initial load
duke
parents:
diff changeset
    17
 * See the License for the specific language governing permissions and
7f561c08de6b Initial load
duke
parents:
diff changeset
    18
 * limitations under the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    19
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    20
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
    21
 * $Id: TransformerFactoryImpl.java,v 1.2.4.1 2005/09/15 06:15:38 pvedula Exp $
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
package com.sun.org.apache.xalan.internal.xsltc.trax;
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
import java.io.File;
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
import java.io.FileInputStream;
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
import java.io.FileNotFoundException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
import java.io.FilenameFilter;
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
import java.io.IOException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
import java.io.InputStream;
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
import java.net.MalformedURLException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
import java.net.URL;
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
import java.util.Enumeration;
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
import java.util.Hashtable;
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
import java.util.Properties;
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
import java.util.Vector;
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
import java.util.zip.ZipEntry;
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
import java.util.zip.ZipFile;
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
import javax.xml.XMLConstants;
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
import javax.xml.parsers.SAXParserFactory;
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
import javax.xml.parsers.SAXParser;
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
import javax.xml.parsers.ParserConfigurationException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
import javax.xml.transform.ErrorListener;
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
import javax.xml.transform.Source;
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
import javax.xml.transform.Templates;
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
import javax.xml.transform.Transformer;
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
import javax.xml.transform.TransformerConfigurationException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
import javax.xml.transform.TransformerException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
import javax.xml.transform.URIResolver;
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
import javax.xml.transform.dom.DOMResult;
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
import javax.xml.transform.dom.DOMSource;
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
import javax.xml.transform.sax.SAXResult;
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
import javax.xml.transform.sax.SAXSource;
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
import javax.xml.transform.sax.SAXTransformerFactory;
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
import javax.xml.transform.sax.TemplatesHandler;
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
import javax.xml.transform.sax.TransformerHandler;
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
import javax.xml.transform.stream.StreamResult;
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
import javax.xml.transform.stream.StreamSource;
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
import com.sun.org.apache.xml.internal.utils.StylesheetPIHandler;
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
import com.sun.org.apache.xml.internal.utils.StopParseException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
import com.sun.org.apache.xalan.internal.xsltc.compiler.Constants;
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
import com.sun.org.apache.xalan.internal.xsltc.compiler.SourceLoader;
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
import com.sun.org.apache.xalan.internal.xsltc.compiler.XSLTC;
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
import com.sun.org.apache.xalan.internal.xsltc.dom.XSLTCDTMManager;
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
import org.xml.sax.InputSource;
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
import org.xml.sax.XMLFilter;
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
import org.xml.sax.XMLReader;
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
import org.xml.sax.helpers.XMLReaderFactory;
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
 * Implementation of a JAXP1.1 TransformerFactory for Translets.
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
 * @author G. Todd Miller
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
 * @author Morten Jorgensen
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
 * @author Santiago Pericas-Geertsen
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
public class TransformerFactoryImpl
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
    extends SAXTransformerFactory implements SourceLoader, ErrorListener
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
{
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
    // Public constants for attributes supported by the XSLTC TransformerFactory.
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
    public final static String TRANSLET_NAME = "translet-name";
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
    public final static String DESTINATION_DIRECTORY = "destination-directory";
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
    public final static String PACKAGE_NAME = "package-name";
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
    public final static String JAR_NAME = "jar-name";
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
    public final static String GENERATE_TRANSLET = "generate-translet";
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
    public final static String AUTO_TRANSLET = "auto-translet";
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
    public final static String USE_CLASSPATH = "use-classpath";
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
    public final static String DEBUG = "debug";
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
    public final static String ENABLE_INLINING = "enable-inlining";
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
    public final static String INDENT_NUMBER = "indent-number";
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
     * This error listener is used only for this factory and is not passed to
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
     * the Templates or Transformer objects that we create.
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
    private ErrorListener _errorListener = this;
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
     * This URIResolver is passed to all created Templates and Transformers
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
    private URIResolver _uriResolver = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
     * As Gregor Samsa awoke one morning from uneasy dreams he found himself
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
     * transformed in his bed into a gigantic insect. He was lying on his hard,
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
     * as it were armour plated, back, and if he lifted his head a little he
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
     * could see his big, brown belly divided into stiff, arched segments, on
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
     * top of which the bed quilt could hardly keep in position and was about
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
     * to slide off completely. His numerous legs, which were pitifully thin
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
     * compared to the rest of his bulk, waved helplessly before his eyes.
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
     * "What has happened to me?", he thought. It was no dream....
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
    protected final static String DEFAULT_TRANSLET_NAME = "GregorSamsa";
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
     * The class name of the translet
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
    private String _transletName = DEFAULT_TRANSLET_NAME;
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
     * The destination directory for the translet
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
    private String _destinationDirectory = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
     * The package name prefix for all generated translet classes
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
    private String _packageName = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
     * The jar file name which the translet classes are packaged into
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
    private String _jarFileName = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
     * This Hashtable is used to store parameters for locating
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
     * <?xml-stylesheet ...?> processing instructions in XML docs.
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
    private Hashtable _piParams = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
     * The above hashtable stores objects of this class.
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
    private static class PIParamWrapper {
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
        public String _media = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
        public String _title = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
        public String _charset = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
        public PIParamWrapper(String media, String title, String charset) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
            _media = media;
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
            _title = title;
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
            _charset = charset;
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
     * Set to <code>true</code> when debugging is enabled.
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
    private boolean _debug = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
     * Set to <code>true</code> when templates are inlined.
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
    private boolean _enableInlining = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
     * Set to <code>true</code> when we want to generate
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
     * translet classes from the stylesheet.
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
    private boolean _generateTranslet = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
     * If this is set to <code>true</code>, we attempt to use translet classes
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
     * for transformation if possible without compiling the stylesheet. The
7f561c08de6b Initial load
duke
parents:
diff changeset
   182
     * translet class is only used if its timestamp is newer than the timestamp
7f561c08de6b Initial load
duke
parents:
diff changeset
   183
     * of the stylesheet.
7f561c08de6b Initial load
duke
parents:
diff changeset
   184
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   185
    private boolean _autoTranslet = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
7f561c08de6b Initial load
duke
parents:
diff changeset
   187
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   188
     * If this is set to <code>true</code>, we attempt to load the translet
7f561c08de6b Initial load
duke
parents:
diff changeset
   189
     * from the CLASSPATH.
7f561c08de6b Initial load
duke
parents:
diff changeset
   190
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   191
    private boolean _useClasspath = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   192
7f561c08de6b Initial load
duke
parents:
diff changeset
   193
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   194
     * Number of indent spaces when indentation is turned on.
7f561c08de6b Initial load
duke
parents:
diff changeset
   195
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   196
    private int _indentNumber = -1;
7f561c08de6b Initial load
duke
parents:
diff changeset
   197
7f561c08de6b Initial load
duke
parents:
diff changeset
   198
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   199
     * The provider of the XSLTC DTM Manager service.  This is fixed for any
7f561c08de6b Initial load
duke
parents:
diff changeset
   200
     * instance of this class.  In order to change service providers, a new
7f561c08de6b Initial load
duke
parents:
diff changeset
   201
     * XSLTC <code>TransformerFactory</code> must be instantiated.
7f561c08de6b Initial load
duke
parents:
diff changeset
   202
     * @see XSLTCDTMManager#getDTMManagerClass()
7f561c08de6b Initial load
duke
parents:
diff changeset
   203
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   204
    private Class m_DTMManagerClass;
7f561c08de6b Initial load
duke
parents:
diff changeset
   205
7f561c08de6b Initial load
duke
parents:
diff changeset
   206
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   207
     * <p>State of secure processing feature.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   208
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   209
    private boolean _isSecureProcessing = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   210
7f561c08de6b Initial load
duke
parents:
diff changeset
   211
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   212
     * javax.xml.transform.sax.TransformerFactory implementation.
7f561c08de6b Initial load
duke
parents:
diff changeset
   213
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   214
    public TransformerFactoryImpl() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   215
        m_DTMManagerClass = XSLTCDTMManager.getDTMManagerClass();
7f561c08de6b Initial load
duke
parents:
diff changeset
   216
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   217
7f561c08de6b Initial load
duke
parents:
diff changeset
   218
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   219
     * javax.xml.transform.sax.TransformerFactory implementation.
7f561c08de6b Initial load
duke
parents:
diff changeset
   220
     * Set the error event listener for the TransformerFactory, which is used
7f561c08de6b Initial load
duke
parents:
diff changeset
   221
     * for the processing of transformation instructions, and not for the
7f561c08de6b Initial load
duke
parents:
diff changeset
   222
     * transformation itself.
7f561c08de6b Initial load
duke
parents:
diff changeset
   223
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   224
     * @param listener The error listener to use with the TransformerFactory
7f561c08de6b Initial load
duke
parents:
diff changeset
   225
     * @throws IllegalArgumentException
7f561c08de6b Initial load
duke
parents:
diff changeset
   226
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   227
    public void setErrorListener(ErrorListener listener)
7f561c08de6b Initial load
duke
parents:
diff changeset
   228
        throws IllegalArgumentException
7f561c08de6b Initial load
duke
parents:
diff changeset
   229
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   230
        if (listener == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   231
            ErrorMsg err = new ErrorMsg(ErrorMsg.ERROR_LISTENER_NULL_ERR,
7f561c08de6b Initial load
duke
parents:
diff changeset
   232
                                        "TransformerFactory");
7f561c08de6b Initial load
duke
parents:
diff changeset
   233
            throw new IllegalArgumentException(err.toString());
7f561c08de6b Initial load
duke
parents:
diff changeset
   234
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   235
        _errorListener = listener;
7f561c08de6b Initial load
duke
parents:
diff changeset
   236
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   237
7f561c08de6b Initial load
duke
parents:
diff changeset
   238
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   239
     * javax.xml.transform.sax.TransformerFactory implementation.
7f561c08de6b Initial load
duke
parents:
diff changeset
   240
     * Get the error event handler for the TransformerFactory.
7f561c08de6b Initial load
duke
parents:
diff changeset
   241
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   242
     * @return The error listener used with the TransformerFactory
7f561c08de6b Initial load
duke
parents:
diff changeset
   243
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   244
    public ErrorListener getErrorListener() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   245
        return _errorListener;
7f561c08de6b Initial load
duke
parents:
diff changeset
   246
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   247
7f561c08de6b Initial load
duke
parents:
diff changeset
   248
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   249
     * javax.xml.transform.sax.TransformerFactory implementation.
7f561c08de6b Initial load
duke
parents:
diff changeset
   250
     * Returns the value set for a TransformerFactory attribute
7f561c08de6b Initial load
duke
parents:
diff changeset
   251
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   252
     * @param name The attribute name
7f561c08de6b Initial load
duke
parents:
diff changeset
   253
     * @return An object representing the attribute value
7f561c08de6b Initial load
duke
parents:
diff changeset
   254
     * @throws IllegalArgumentException
7f561c08de6b Initial load
duke
parents:
diff changeset
   255
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   256
    public Object getAttribute(String name)
7f561c08de6b Initial load
duke
parents:
diff changeset
   257
        throws IllegalArgumentException
7f561c08de6b Initial load
duke
parents:
diff changeset
   258
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   259
        // Return value for attribute 'translet-name'
7f561c08de6b Initial load
duke
parents:
diff changeset
   260
        if (name.equals(TRANSLET_NAME)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   261
            return _transletName;
7f561c08de6b Initial load
duke
parents:
diff changeset
   262
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   263
        else if (name.equals(GENERATE_TRANSLET)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   264
            return new Boolean(_generateTranslet);
7f561c08de6b Initial load
duke
parents:
diff changeset
   265
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   266
        else if (name.equals(AUTO_TRANSLET)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   267
            return new Boolean(_autoTranslet);
7f561c08de6b Initial load
duke
parents:
diff changeset
   268
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   269
7f561c08de6b Initial load
duke
parents:
diff changeset
   270
        // Throw an exception for all other attributes
7f561c08de6b Initial load
duke
parents:
diff changeset
   271
        ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_INVALID_ATTR_ERR, name);
7f561c08de6b Initial load
duke
parents:
diff changeset
   272
        throw new IllegalArgumentException(err.toString());
7f561c08de6b Initial load
duke
parents:
diff changeset
   273
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   274
7f561c08de6b Initial load
duke
parents:
diff changeset
   275
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   276
     * javax.xml.transform.sax.TransformerFactory implementation.
7f561c08de6b Initial load
duke
parents:
diff changeset
   277
     * Sets the value for a TransformerFactory attribute.
7f561c08de6b Initial load
duke
parents:
diff changeset
   278
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   279
     * @param name The attribute name
7f561c08de6b Initial load
duke
parents:
diff changeset
   280
     * @param value An object representing the attribute value
7f561c08de6b Initial load
duke
parents:
diff changeset
   281
     * @throws IllegalArgumentException
7f561c08de6b Initial load
duke
parents:
diff changeset
   282
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   283
    public void setAttribute(String name, Object value)
7f561c08de6b Initial load
duke
parents:
diff changeset
   284
        throws IllegalArgumentException
7f561c08de6b Initial load
duke
parents:
diff changeset
   285
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   286
        // Set the default translet name (ie. class name), which will be used
7f561c08de6b Initial load
duke
parents:
diff changeset
   287
        // for translets that cannot be given a name from their system-id.
7f561c08de6b Initial load
duke
parents:
diff changeset
   288
        if (name.equals(TRANSLET_NAME) && value instanceof String) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   289
            _transletName = (String) value;
7f561c08de6b Initial load
duke
parents:
diff changeset
   290
            return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   291
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   292
        else if (name.equals(DESTINATION_DIRECTORY) && value instanceof String) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   293
            _destinationDirectory = (String) value;
7f561c08de6b Initial load
duke
parents:
diff changeset
   294
            return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   295
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   296
        else if (name.equals(PACKAGE_NAME) && value instanceof String) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   297
            _packageName = (String) value;
7f561c08de6b Initial load
duke
parents:
diff changeset
   298
            return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   299
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   300
        else if (name.equals(JAR_NAME) && value instanceof String) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   301
            _jarFileName = (String) value;
7f561c08de6b Initial load
duke
parents:
diff changeset
   302
            return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   303
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   304
        else if (name.equals(GENERATE_TRANSLET)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   305
            if (value instanceof Boolean) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   306
                _generateTranslet = ((Boolean) value).booleanValue();
7f561c08de6b Initial load
duke
parents:
diff changeset
   307
                return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   308
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   309
            else if (value instanceof String) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   310
                _generateTranslet = ((String) value).equalsIgnoreCase("true");
7f561c08de6b Initial load
duke
parents:
diff changeset
   311
                return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   312
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   313
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   314
        else if (name.equals(AUTO_TRANSLET)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   315
            if (value instanceof Boolean) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   316
                _autoTranslet = ((Boolean) value).booleanValue();
7f561c08de6b Initial load
duke
parents:
diff changeset
   317
                return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   318
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   319
            else if (value instanceof String) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   320
                _autoTranslet = ((String) value).equalsIgnoreCase("true");
7f561c08de6b Initial load
duke
parents:
diff changeset
   321
                return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   322
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   323
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   324
        else if (name.equals(USE_CLASSPATH)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   325
            if (value instanceof Boolean) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   326
                _useClasspath = ((Boolean) value).booleanValue();
7f561c08de6b Initial load
duke
parents:
diff changeset
   327
                return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   328
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   329
            else if (value instanceof String) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   330
                _useClasspath = ((String) value).equalsIgnoreCase("true");
7f561c08de6b Initial load
duke
parents:
diff changeset
   331
                return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   332
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   333
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   334
        else if (name.equals(DEBUG)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   335
            if (value instanceof Boolean) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   336
                _debug = ((Boolean) value).booleanValue();
7f561c08de6b Initial load
duke
parents:
diff changeset
   337
                return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   338
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   339
            else if (value instanceof String) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   340
                _debug = ((String) value).equalsIgnoreCase("true");
7f561c08de6b Initial load
duke
parents:
diff changeset
   341
                return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   342
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   343
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   344
        else if (name.equals(ENABLE_INLINING)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   345
            if (value instanceof Boolean) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   346
                _enableInlining = ((Boolean) value).booleanValue();
7f561c08de6b Initial load
duke
parents:
diff changeset
   347
                return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   348
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   349
            else if (value instanceof String) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   350
                _enableInlining = ((String) value).equalsIgnoreCase("true");
7f561c08de6b Initial load
duke
parents:
diff changeset
   351
                return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   352
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   353
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   354
        else if (name.equals(INDENT_NUMBER)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   355
            if (value instanceof String) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   356
                try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   357
                    _indentNumber = Integer.parseInt((String) value);
7f561c08de6b Initial load
duke
parents:
diff changeset
   358
                    return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   359
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   360
                catch (NumberFormatException e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   361
                    // Falls through
7f561c08de6b Initial load
duke
parents:
diff changeset
   362
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   363
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   364
            else if (value instanceof Integer) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   365
                _indentNumber = ((Integer) value).intValue();
7f561c08de6b Initial load
duke
parents:
diff changeset
   366
                return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   367
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   368
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   369
7f561c08de6b Initial load
duke
parents:
diff changeset
   370
        // Throw an exception for all other attributes
7f561c08de6b Initial load
duke
parents:
diff changeset
   371
        final ErrorMsg err
7f561c08de6b Initial load
duke
parents:
diff changeset
   372
            = new ErrorMsg(ErrorMsg.JAXP_INVALID_ATTR_ERR, name);
7f561c08de6b Initial load
duke
parents:
diff changeset
   373
        throw new IllegalArgumentException(err.toString());
7f561c08de6b Initial load
duke
parents:
diff changeset
   374
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   375
7f561c08de6b Initial load
duke
parents:
diff changeset
   376
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   377
     * <p>Set a feature for this <code>TransformerFactory</code> and <code>Transformer</code>s
7f561c08de6b Initial load
duke
parents:
diff changeset
   378
     * or <code>Template</code>s created by this factory.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   379
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   380
     * <p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   381
     * Feature names are fully qualified {@link java.net.URI}s.
7f561c08de6b Initial load
duke
parents:
diff changeset
   382
     * Implementations may define their own features.
7f561c08de6b Initial load
duke
parents:
diff changeset
   383
     * An {@link TransformerConfigurationException} is thrown if this <code>TransformerFactory</code> or the
7f561c08de6b Initial load
duke
parents:
diff changeset
   384
     * <code>Transformer</code>s or <code>Template</code>s it creates cannot support the feature.
7f561c08de6b Initial load
duke
parents:
diff changeset
   385
     * It is possible for an <code>TransformerFactory</code> to expose a feature value but be unable to change its state.
7f561c08de6b Initial load
duke
parents:
diff changeset
   386
     * </p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   387
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   388
     * <p>See {@link javax.xml.transform.TransformerFactory} for full documentation of specific features.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   389
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   390
     * @param name Feature name.
7f561c08de6b Initial load
duke
parents:
diff changeset
   391
     * @param value Is feature state <code>true</code> or <code>false</code>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   392
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   393
     * @throws TransformerConfigurationException if this <code>TransformerFactory</code>
7f561c08de6b Initial load
duke
parents:
diff changeset
   394
     *   or the <code>Transformer</code>s or <code>Template</code>s it creates cannot support this feature.
7f561c08de6b Initial load
duke
parents:
diff changeset
   395
     * @throws NullPointerException If the <code>name</code> parameter is null.
7f561c08de6b Initial load
duke
parents:
diff changeset
   396
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   397
    public void setFeature(String name, boolean value)
7f561c08de6b Initial load
duke
parents:
diff changeset
   398
        throws TransformerConfigurationException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   399
7f561c08de6b Initial load
duke
parents:
diff changeset
   400
        // feature name cannot be null
7f561c08de6b Initial load
duke
parents:
diff changeset
   401
        if (name == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   402
            ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_SET_FEATURE_NULL_NAME);
7f561c08de6b Initial load
duke
parents:
diff changeset
   403
            throw new NullPointerException(err.toString());
7f561c08de6b Initial load
duke
parents:
diff changeset
   404
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   405
        // secure processing?
7f561c08de6b Initial load
duke
parents:
diff changeset
   406
        else if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   407
            _isSecureProcessing = value;
7f561c08de6b Initial load
duke
parents:
diff changeset
   408
            // all done processing feature
7f561c08de6b Initial load
duke
parents:
diff changeset
   409
            return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   410
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   411
        else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   412
            // unknown feature
7f561c08de6b Initial load
duke
parents:
diff changeset
   413
            ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_UNSUPPORTED_FEATURE, name);
7f561c08de6b Initial load
duke
parents:
diff changeset
   414
            throw new TransformerConfigurationException(err.toString());
7f561c08de6b Initial load
duke
parents:
diff changeset
   415
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   416
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   417
7f561c08de6b Initial load
duke
parents:
diff changeset
   418
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   419
     * javax.xml.transform.sax.TransformerFactory implementation.
7f561c08de6b Initial load
duke
parents:
diff changeset
   420
     * Look up the value of a feature (to see if it is supported).
7f561c08de6b Initial load
duke
parents:
diff changeset
   421
     * This method must be updated as the various methods and features of this
7f561c08de6b Initial load
duke
parents:
diff changeset
   422
     * class are implemented.
7f561c08de6b Initial load
duke
parents:
diff changeset
   423
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   424
     * @param name The feature name
7f561c08de6b Initial load
duke
parents:
diff changeset
   425
     * @return 'true' if feature is supported, 'false' if not
7f561c08de6b Initial load
duke
parents:
diff changeset
   426
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   427
    public boolean getFeature(String name) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   428
        // All supported features should be listed here
7f561c08de6b Initial load
duke
parents:
diff changeset
   429
        String[] features = {
7f561c08de6b Initial load
duke
parents:
diff changeset
   430
            DOMSource.FEATURE,
7f561c08de6b Initial load
duke
parents:
diff changeset
   431
            DOMResult.FEATURE,
7f561c08de6b Initial load
duke
parents:
diff changeset
   432
            SAXSource.FEATURE,
7f561c08de6b Initial load
duke
parents:
diff changeset
   433
            SAXResult.FEATURE,
7f561c08de6b Initial load
duke
parents:
diff changeset
   434
            StreamSource.FEATURE,
7f561c08de6b Initial load
duke
parents:
diff changeset
   435
            StreamResult.FEATURE,
7f561c08de6b Initial load
duke
parents:
diff changeset
   436
            SAXTransformerFactory.FEATURE,
7f561c08de6b Initial load
duke
parents:
diff changeset
   437
            SAXTransformerFactory.FEATURE_XMLFILTER
7f561c08de6b Initial load
duke
parents:
diff changeset
   438
        };
7f561c08de6b Initial load
duke
parents:
diff changeset
   439
7f561c08de6b Initial load
duke
parents:
diff changeset
   440
        // feature name cannot be null
7f561c08de6b Initial load
duke
parents:
diff changeset
   441
        if (name == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   442
            ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_GET_FEATURE_NULL_NAME);
7f561c08de6b Initial load
duke
parents:
diff changeset
   443
            throw new NullPointerException(err.toString());
7f561c08de6b Initial load
duke
parents:
diff changeset
   444
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   445
7f561c08de6b Initial load
duke
parents:
diff changeset
   446
        // Inefficient, but array is small
7f561c08de6b Initial load
duke
parents:
diff changeset
   447
        for (int i =0; i < features.length; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   448
            if (name.equals(features[i])) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   449
                return true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   450
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   451
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   452
        // secure processing?
7f561c08de6b Initial load
duke
parents:
diff changeset
   453
        if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   454
                return _isSecureProcessing;
7f561c08de6b Initial load
duke
parents:
diff changeset
   455
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   456
7f561c08de6b Initial load
duke
parents:
diff changeset
   457
        // Feature not supported
7f561c08de6b Initial load
duke
parents:
diff changeset
   458
        return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   459
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   460
7f561c08de6b Initial load
duke
parents:
diff changeset
   461
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   462
     * javax.xml.transform.sax.TransformerFactory implementation.
7f561c08de6b Initial load
duke
parents:
diff changeset
   463
     * Get the object that is used by default during the transformation to
7f561c08de6b Initial load
duke
parents:
diff changeset
   464
     * resolve URIs used in document(), xsl:import, or xsl:include.
7f561c08de6b Initial load
duke
parents:
diff changeset
   465
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   466
     * @return The URLResolver used for this TransformerFactory and all
7f561c08de6b Initial load
duke
parents:
diff changeset
   467
     * Templates and Transformer objects created using this factory
7f561c08de6b Initial load
duke
parents:
diff changeset
   468
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   469
    public URIResolver getURIResolver() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   470
        return _uriResolver;
7f561c08de6b Initial load
duke
parents:
diff changeset
   471
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   472
7f561c08de6b Initial load
duke
parents:
diff changeset
   473
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   474
     * javax.xml.transform.sax.TransformerFactory implementation.
7f561c08de6b Initial load
duke
parents:
diff changeset
   475
     * Set the object that is used by default during the transformation to
7f561c08de6b Initial load
duke
parents:
diff changeset
   476
     * resolve URIs used in document(), xsl:import, or xsl:include. Note that
7f561c08de6b Initial load
duke
parents:
diff changeset
   477
     * this does not affect Templates and Transformers that are already
7f561c08de6b Initial load
duke
parents:
diff changeset
   478
     * created with this factory.
7f561c08de6b Initial load
duke
parents:
diff changeset
   479
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   480
     * @param resolver The URLResolver used for this TransformerFactory and all
7f561c08de6b Initial load
duke
parents:
diff changeset
   481
     * Templates and Transformer objects created using this factory
7f561c08de6b Initial load
duke
parents:
diff changeset
   482
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   483
    public void setURIResolver(URIResolver resolver) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   484
        _uriResolver = resolver;
7f561c08de6b Initial load
duke
parents:
diff changeset
   485
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   486
7f561c08de6b Initial load
duke
parents:
diff changeset
   487
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   488
     * javax.xml.transform.sax.TransformerFactory implementation.
7f561c08de6b Initial load
duke
parents:
diff changeset
   489
     * Get the stylesheet specification(s) associated via the xml-stylesheet
7f561c08de6b Initial load
duke
parents:
diff changeset
   490
     * processing instruction (see http://www.w3.org/TR/xml-stylesheet/) with
7f561c08de6b Initial load
duke
parents:
diff changeset
   491
     * the document document specified in the source parameter, and that match
7f561c08de6b Initial load
duke
parents:
diff changeset
   492
     * the given criteria.
7f561c08de6b Initial load
duke
parents:
diff changeset
   493
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   494
     * @param source The XML source document.
7f561c08de6b Initial load
duke
parents:
diff changeset
   495
     * @param media The media attribute to be matched. May be null, in which
7f561c08de6b Initial load
duke
parents:
diff changeset
   496
     * case the prefered templates will be used (i.e. alternate = no).
7f561c08de6b Initial load
duke
parents:
diff changeset
   497
     * @param title The value of the title attribute to match. May be null.
7f561c08de6b Initial load
duke
parents:
diff changeset
   498
     * @param charset The value of the charset attribute to match. May be null.
7f561c08de6b Initial load
duke
parents:
diff changeset
   499
     * @return A Source object suitable for passing to the TransformerFactory.
7f561c08de6b Initial load
duke
parents:
diff changeset
   500
     * @throws TransformerConfigurationException
7f561c08de6b Initial load
duke
parents:
diff changeset
   501
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   502
    public Source  getAssociatedStylesheet(Source source, String media,
7f561c08de6b Initial load
duke
parents:
diff changeset
   503
                                          String title, String charset)
7f561c08de6b Initial load
duke
parents:
diff changeset
   504
        throws TransformerConfigurationException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   505
7f561c08de6b Initial load
duke
parents:
diff changeset
   506
        String baseId;
7f561c08de6b Initial load
duke
parents:
diff changeset
   507
        XMLReader reader = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   508
        InputSource isource = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   509
7f561c08de6b Initial load
duke
parents:
diff changeset
   510
7f561c08de6b Initial load
duke
parents:
diff changeset
   511
        /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   512
         * Fix for bugzilla bug 24187
7f561c08de6b Initial load
duke
parents:
diff changeset
   513
         */
7f561c08de6b Initial load
duke
parents:
diff changeset
   514
        StylesheetPIHandler _stylesheetPIHandler = new StylesheetPIHandler(null,media,title,charset);
7f561c08de6b Initial load
duke
parents:
diff changeset
   515
7f561c08de6b Initial load
duke
parents:
diff changeset
   516
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   517
7f561c08de6b Initial load
duke
parents:
diff changeset
   518
            if (source instanceof DOMSource ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   519
                final DOMSource domsrc = (DOMSource) source;
7f561c08de6b Initial load
duke
parents:
diff changeset
   520
                baseId = domsrc.getSystemId();
7f561c08de6b Initial load
duke
parents:
diff changeset
   521
                final org.w3c.dom.Node node = domsrc.getNode();
7f561c08de6b Initial load
duke
parents:
diff changeset
   522
                final DOM2SAX dom2sax = new DOM2SAX(node);
7f561c08de6b Initial load
duke
parents:
diff changeset
   523
7f561c08de6b Initial load
duke
parents:
diff changeset
   524
                _stylesheetPIHandler.setBaseId(baseId);
7f561c08de6b Initial load
duke
parents:
diff changeset
   525
7f561c08de6b Initial load
duke
parents:
diff changeset
   526
                dom2sax.setContentHandler( _stylesheetPIHandler);
7f561c08de6b Initial load
duke
parents:
diff changeset
   527
                dom2sax.parse();
7f561c08de6b Initial load
duke
parents:
diff changeset
   528
            } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   529
                isource = SAXSource.sourceToInputSource(source);
7f561c08de6b Initial load
duke
parents:
diff changeset
   530
                baseId = isource.getSystemId();
7f561c08de6b Initial load
duke
parents:
diff changeset
   531
7f561c08de6b Initial load
duke
parents:
diff changeset
   532
                SAXParserFactory factory = SAXParserFactory.newInstance();
7f561c08de6b Initial load
duke
parents:
diff changeset
   533
                factory.setNamespaceAware(true);
7f561c08de6b Initial load
duke
parents:
diff changeset
   534
7f561c08de6b Initial load
duke
parents:
diff changeset
   535
                if (_isSecureProcessing) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   536
                    try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   537
                        factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
7f561c08de6b Initial load
duke
parents:
diff changeset
   538
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   539
                    catch (org.xml.sax.SAXException e) {}
7f561c08de6b Initial load
duke
parents:
diff changeset
   540
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   541
7f561c08de6b Initial load
duke
parents:
diff changeset
   542
                SAXParser jaxpParser = factory.newSAXParser();
7f561c08de6b Initial load
duke
parents:
diff changeset
   543
7f561c08de6b Initial load
duke
parents:
diff changeset
   544
                reader = jaxpParser.getXMLReader();
7f561c08de6b Initial load
duke
parents:
diff changeset
   545
                if (reader == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   546
                    reader = XMLReaderFactory.createXMLReader();
7f561c08de6b Initial load
duke
parents:
diff changeset
   547
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   548
7f561c08de6b Initial load
duke
parents:
diff changeset
   549
                _stylesheetPIHandler.setBaseId(baseId);
7f561c08de6b Initial load
duke
parents:
diff changeset
   550
                reader.setContentHandler(_stylesheetPIHandler);
7f561c08de6b Initial load
duke
parents:
diff changeset
   551
                reader.parse(isource);
7f561c08de6b Initial load
duke
parents:
diff changeset
   552
7f561c08de6b Initial load
duke
parents:
diff changeset
   553
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   554
7f561c08de6b Initial load
duke
parents:
diff changeset
   555
            if (_uriResolver != null ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   556
                _stylesheetPIHandler.setURIResolver(_uriResolver);
7f561c08de6b Initial load
duke
parents:
diff changeset
   557
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   558
7f561c08de6b Initial load
duke
parents:
diff changeset
   559
        } catch (StopParseException e ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   560
          // startElement encountered so do not parse further
7f561c08de6b Initial load
duke
parents:
diff changeset
   561
7f561c08de6b Initial load
duke
parents:
diff changeset
   562
        } catch (javax.xml.parsers.ParserConfigurationException e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   563
7f561c08de6b Initial load
duke
parents:
diff changeset
   564
             throw new TransformerConfigurationException(
7f561c08de6b Initial load
duke
parents:
diff changeset
   565
             "getAssociatedStylesheets failed", e);
7f561c08de6b Initial load
duke
parents:
diff changeset
   566
7f561c08de6b Initial load
duke
parents:
diff changeset
   567
        } catch (org.xml.sax.SAXException se) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   568
7f561c08de6b Initial load
duke
parents:
diff changeset
   569
             throw new TransformerConfigurationException(
7f561c08de6b Initial load
duke
parents:
diff changeset
   570
             "getAssociatedStylesheets failed", se);
7f561c08de6b Initial load
duke
parents:
diff changeset
   571
7f561c08de6b Initial load
duke
parents:
diff changeset
   572
7f561c08de6b Initial load
duke
parents:
diff changeset
   573
        } catch (IOException ioe ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   574
           throw new TransformerConfigurationException(
7f561c08de6b Initial load
duke
parents:
diff changeset
   575
           "getAssociatedStylesheets failed", ioe);
7f561c08de6b Initial load
duke
parents:
diff changeset
   576
7f561c08de6b Initial load
duke
parents:
diff changeset
   577
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   578
7f561c08de6b Initial load
duke
parents:
diff changeset
   579
         return _stylesheetPIHandler.getAssociatedStylesheet();
7f561c08de6b Initial load
duke
parents:
diff changeset
   580
7f561c08de6b Initial load
duke
parents:
diff changeset
   581
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   582
7f561c08de6b Initial load
duke
parents:
diff changeset
   583
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   584
     * javax.xml.transform.sax.TransformerFactory implementation.
7f561c08de6b Initial load
duke
parents:
diff changeset
   585
     * Create a Transformer object that copies the input document to the result.
7f561c08de6b Initial load
duke
parents:
diff changeset
   586
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   587
     * @return A Transformer object that simply copies the source to the result.
7f561c08de6b Initial load
duke
parents:
diff changeset
   588
     * @throws TransformerConfigurationException
7f561c08de6b Initial load
duke
parents:
diff changeset
   589
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   590
    public Transformer newTransformer()
7f561c08de6b Initial load
duke
parents:
diff changeset
   591
        throws TransformerConfigurationException
7f561c08de6b Initial load
duke
parents:
diff changeset
   592
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   593
        TransformerImpl result = new TransformerImpl(new Properties(),
7f561c08de6b Initial load
duke
parents:
diff changeset
   594
            _indentNumber, this);
7f561c08de6b Initial load
duke
parents:
diff changeset
   595
        if (_uriResolver != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   596
            result.setURIResolver(_uriResolver);
7f561c08de6b Initial load
duke
parents:
diff changeset
   597
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   598
7f561c08de6b Initial load
duke
parents:
diff changeset
   599
        if (_isSecureProcessing) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   600
            result.setSecureProcessing(true);
7f561c08de6b Initial load
duke
parents:
diff changeset
   601
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   602
        return result;
7f561c08de6b Initial load
duke
parents:
diff changeset
   603
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   604
7f561c08de6b Initial load
duke
parents:
diff changeset
   605
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   606
     * javax.xml.transform.sax.TransformerFactory implementation.
7f561c08de6b Initial load
duke
parents:
diff changeset
   607
     * Process the Source into a Templates object, which is a a compiled
7f561c08de6b Initial load
duke
parents:
diff changeset
   608
     * representation of the source. Note that this method should not be
7f561c08de6b Initial load
duke
parents:
diff changeset
   609
     * used with XSLTC, as the time-consuming compilation is done for each
7f561c08de6b Initial load
duke
parents:
diff changeset
   610
     * and every transformation.
7f561c08de6b Initial load
duke
parents:
diff changeset
   611
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   612
     * @return A Templates object that can be used to create Transformers.
7f561c08de6b Initial load
duke
parents:
diff changeset
   613
     * @throws TransformerConfigurationException
7f561c08de6b Initial load
duke
parents:
diff changeset
   614
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   615
    public Transformer newTransformer(Source source) throws
7f561c08de6b Initial load
duke
parents:
diff changeset
   616
        TransformerConfigurationException
7f561c08de6b Initial load
duke
parents:
diff changeset
   617
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   618
        final Templates templates = newTemplates(source);
7f561c08de6b Initial load
duke
parents:
diff changeset
   619
        final Transformer transformer = templates.newTransformer();
7f561c08de6b Initial load
duke
parents:
diff changeset
   620
        if (_uriResolver != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   621
            transformer.setURIResolver(_uriResolver);
7f561c08de6b Initial load
duke
parents:
diff changeset
   622
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   623
        return(transformer);
7f561c08de6b Initial load
duke
parents:
diff changeset
   624
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   625
7f561c08de6b Initial load
duke
parents:
diff changeset
   626
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   627
     * Pass warning messages from the compiler to the error listener
7f561c08de6b Initial load
duke
parents:
diff changeset
   628
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   629
    private void passWarningsToListener(Vector messages)
7f561c08de6b Initial load
duke
parents:
diff changeset
   630
        throws TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
   631
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   632
        if (_errorListener == null || messages == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   633
            return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   634
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   635
        // Pass messages to listener, one by one
7f561c08de6b Initial load
duke
parents:
diff changeset
   636
        final int count = messages.size();
7f561c08de6b Initial load
duke
parents:
diff changeset
   637
        for (int pos = 0; pos < count; pos++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   638
            ErrorMsg msg = (ErrorMsg)messages.elementAt(pos);
7f561c08de6b Initial load
duke
parents:
diff changeset
   639
            // Workaround for the TCK failure ErrorListener.errorTests.error001.
7f561c08de6b Initial load
duke
parents:
diff changeset
   640
            if (msg.isWarningError())
7f561c08de6b Initial load
duke
parents:
diff changeset
   641
                _errorListener.error(
7f561c08de6b Initial load
duke
parents:
diff changeset
   642
                    new TransformerConfigurationException(msg.toString()));
7f561c08de6b Initial load
duke
parents:
diff changeset
   643
            else
7f561c08de6b Initial load
duke
parents:
diff changeset
   644
                _errorListener.warning(
7f561c08de6b Initial load
duke
parents:
diff changeset
   645
                    new TransformerConfigurationException(msg.toString()));
7f561c08de6b Initial load
duke
parents:
diff changeset
   646
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   647
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   648
7f561c08de6b Initial load
duke
parents:
diff changeset
   649
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   650
     * Pass error messages from the compiler to the error listener
7f561c08de6b Initial load
duke
parents:
diff changeset
   651
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   652
    private void passErrorsToListener(Vector messages) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   653
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   654
            if (_errorListener == null || messages == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   655
                return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   656
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   657
            // Pass messages to listener, one by one
7f561c08de6b Initial load
duke
parents:
diff changeset
   658
            final int count = messages.size();
7f561c08de6b Initial load
duke
parents:
diff changeset
   659
            for (int pos = 0; pos < count; pos++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   660
                String message = messages.elementAt(pos).toString();
7f561c08de6b Initial load
duke
parents:
diff changeset
   661
                _errorListener.error(new TransformerException(message));
7f561c08de6b Initial load
duke
parents:
diff changeset
   662
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   663
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   664
        catch (TransformerException e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   665
            // nada
7f561c08de6b Initial load
duke
parents:
diff changeset
   666
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   667
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   668
7f561c08de6b Initial load
duke
parents:
diff changeset
   669
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   670
     * javax.xml.transform.sax.TransformerFactory implementation.
7f561c08de6b Initial load
duke
parents:
diff changeset
   671
     * Process the Source into a Templates object, which is a a compiled
7f561c08de6b Initial load
duke
parents:
diff changeset
   672
     * representation of the source.
7f561c08de6b Initial load
duke
parents:
diff changeset
   673
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   674
     * @param source The input stylesheet - DOMSource not supported!!!
7f561c08de6b Initial load
duke
parents:
diff changeset
   675
     * @return A Templates object that can be used to create Transformers.
7f561c08de6b Initial load
duke
parents:
diff changeset
   676
     * @throws TransformerConfigurationException
7f561c08de6b Initial load
duke
parents:
diff changeset
   677
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   678
    public Templates newTemplates(Source source)
7f561c08de6b Initial load
duke
parents:
diff changeset
   679
        throws TransformerConfigurationException
7f561c08de6b Initial load
duke
parents:
diff changeset
   680
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   681
        // If the _useClasspath attribute is true, try to load the translet from
7f561c08de6b Initial load
duke
parents:
diff changeset
   682
        // the CLASSPATH and create a template object using the loaded
7f561c08de6b Initial load
duke
parents:
diff changeset
   683
        // translet.
7f561c08de6b Initial load
duke
parents:
diff changeset
   684
        if (_useClasspath) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   685
            String transletName = getTransletBaseName(source);
7f561c08de6b Initial load
duke
parents:
diff changeset
   686
7f561c08de6b Initial load
duke
parents:
diff changeset
   687
            if (_packageName != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   688
                transletName = _packageName + "." + transletName;
7f561c08de6b Initial load
duke
parents:
diff changeset
   689
7f561c08de6b Initial load
duke
parents:
diff changeset
   690
            try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   691
                final Class clazz = ObjectFactory.findProviderClass(
7f561c08de6b Initial load
duke
parents:
diff changeset
   692
                    transletName, ObjectFactory.findClassLoader(), true);
7f561c08de6b Initial load
duke
parents:
diff changeset
   693
                resetTransientAttributes();
7f561c08de6b Initial load
duke
parents:
diff changeset
   694
7f561c08de6b Initial load
duke
parents:
diff changeset
   695
                return new TemplatesImpl(new Class[]{clazz}, transletName, null, _indentNumber, this);
7f561c08de6b Initial load
duke
parents:
diff changeset
   696
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   697
            catch (ClassNotFoundException cnfe) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   698
                ErrorMsg err = new ErrorMsg(ErrorMsg.CLASS_NOT_FOUND_ERR, transletName);
7f561c08de6b Initial load
duke
parents:
diff changeset
   699
                throw new TransformerConfigurationException(err.toString());
7f561c08de6b Initial load
duke
parents:
diff changeset
   700
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   701
            catch (Exception e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   702
                ErrorMsg err = new ErrorMsg(
7f561c08de6b Initial load
duke
parents:
diff changeset
   703
                                     new ErrorMsg(ErrorMsg.RUNTIME_ERROR_KEY)
7f561c08de6b Initial load
duke
parents:
diff changeset
   704
                                     + e.getMessage());
7f561c08de6b Initial load
duke
parents:
diff changeset
   705
                throw new TransformerConfigurationException(err.toString());
7f561c08de6b Initial load
duke
parents:
diff changeset
   706
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   707
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   708
7f561c08de6b Initial load
duke
parents:
diff changeset
   709
        // If _autoTranslet is true, we will try to load the bytecodes
7f561c08de6b Initial load
duke
parents:
diff changeset
   710
        // from the translet classes without compiling the stylesheet.
7f561c08de6b Initial load
duke
parents:
diff changeset
   711
        if (_autoTranslet)  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   712
            byte[][] bytecodes = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   713
            String transletClassName = getTransletBaseName(source);
7f561c08de6b Initial load
duke
parents:
diff changeset
   714
7f561c08de6b Initial load
duke
parents:
diff changeset
   715
            if (_packageName != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   716
                transletClassName = _packageName + "." + transletClassName;
7f561c08de6b Initial load
duke
parents:
diff changeset
   717
7f561c08de6b Initial load
duke
parents:
diff changeset
   718
            if (_jarFileName != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   719
                bytecodes = getBytecodesFromJar(source, transletClassName);
7f561c08de6b Initial load
duke
parents:
diff changeset
   720
            else
7f561c08de6b Initial load
duke
parents:
diff changeset
   721
                bytecodes = getBytecodesFromClasses(source, transletClassName);
7f561c08de6b Initial load
duke
parents:
diff changeset
   722
7f561c08de6b Initial load
duke
parents:
diff changeset
   723
            if (bytecodes != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   724
                if (_debug) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   725
                    if (_jarFileName != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   726
                        System.err.println(new ErrorMsg(
7f561c08de6b Initial load
duke
parents:
diff changeset
   727
                            ErrorMsg.TRANSFORM_WITH_JAR_STR, transletClassName, _jarFileName));
7f561c08de6b Initial load
duke
parents:
diff changeset
   728
                    else
7f561c08de6b Initial load
duke
parents:
diff changeset
   729
                        System.err.println(new ErrorMsg(
7f561c08de6b Initial load
duke
parents:
diff changeset
   730
                            ErrorMsg.TRANSFORM_WITH_TRANSLET_STR, transletClassName));
7f561c08de6b Initial load
duke
parents:
diff changeset
   731
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   732
7f561c08de6b Initial load
duke
parents:
diff changeset
   733
                // Reset the per-session attributes to their default values
7f561c08de6b Initial load
duke
parents:
diff changeset
   734
                // after each newTemplates() call.
7f561c08de6b Initial load
duke
parents:
diff changeset
   735
                resetTransientAttributes();
7f561c08de6b Initial load
duke
parents:
diff changeset
   736
7f561c08de6b Initial load
duke
parents:
diff changeset
   737
                return new TemplatesImpl(bytecodes, transletClassName, null, _indentNumber, this);
7f561c08de6b Initial load
duke
parents:
diff changeset
   738
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   739
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   740
7f561c08de6b Initial load
duke
parents:
diff changeset
   741
        // Create and initialize a stylesheet compiler
7f561c08de6b Initial load
duke
parents:
diff changeset
   742
        final XSLTC xsltc = new XSLTC();
7f561c08de6b Initial load
duke
parents:
diff changeset
   743
        if (_debug) xsltc.setDebug(true);
7f561c08de6b Initial load
duke
parents:
diff changeset
   744
        if (_enableInlining) xsltc.setTemplateInlining(true);
7f561c08de6b Initial load
duke
parents:
diff changeset
   745
        if (_isSecureProcessing) xsltc.setSecureProcessing(true);
7f561c08de6b Initial load
duke
parents:
diff changeset
   746
        xsltc.init();
7f561c08de6b Initial load
duke
parents:
diff changeset
   747
7f561c08de6b Initial load
duke
parents:
diff changeset
   748
        // Set a document loader (for xsl:include/import) if defined
7f561c08de6b Initial load
duke
parents:
diff changeset
   749
        if (_uriResolver != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   750
            xsltc.setSourceLoader(this);
7f561c08de6b Initial load
duke
parents:
diff changeset
   751
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   752
7f561c08de6b Initial load
duke
parents:
diff changeset
   753
        // Pass parameters to the Parser to make sure it locates the correct
7f561c08de6b Initial load
duke
parents:
diff changeset
   754
        // <?xml-stylesheet ...?> PI in an XML input document
7f561c08de6b Initial load
duke
parents:
diff changeset
   755
        if ((_piParams != null) && (_piParams.get(source) != null)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   756
            // Get the parameters for this Source object
7f561c08de6b Initial load
duke
parents:
diff changeset
   757
            PIParamWrapper p = (PIParamWrapper)_piParams.get(source);
7f561c08de6b Initial load
duke
parents:
diff changeset
   758
            // Pass them on to the compiler (which will pass then to the parser)
7f561c08de6b Initial load
duke
parents:
diff changeset
   759
            if (p != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   760
                xsltc.setPIParameters(p._media, p._title, p._charset);
7f561c08de6b Initial load
duke
parents:
diff changeset
   761
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   762
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   763
7f561c08de6b Initial load
duke
parents:
diff changeset
   764
        // Set the attributes for translet generation
7f561c08de6b Initial load
duke
parents:
diff changeset
   765
        int outputType = XSLTC.BYTEARRAY_OUTPUT;
7f561c08de6b Initial load
duke
parents:
diff changeset
   766
        if (_generateTranslet || _autoTranslet) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   767
            // Set the translet name
7f561c08de6b Initial load
duke
parents:
diff changeset
   768
            xsltc.setClassName(getTransletBaseName(source));
7f561c08de6b Initial load
duke
parents:
diff changeset
   769
7f561c08de6b Initial load
duke
parents:
diff changeset
   770
            if (_destinationDirectory != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   771
                xsltc.setDestDirectory(_destinationDirectory);
7f561c08de6b Initial load
duke
parents:
diff changeset
   772
            else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   773
                String xslName = getStylesheetFileName(source);
7f561c08de6b Initial load
duke
parents:
diff changeset
   774
                if (xslName != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   775
                    File xslFile = new File(xslName);
7f561c08de6b Initial load
duke
parents:
diff changeset
   776
                    String xslDir = xslFile.getParent();
7f561c08de6b Initial load
duke
parents:
diff changeset
   777
7f561c08de6b Initial load
duke
parents:
diff changeset
   778
                    if (xslDir != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   779
                        xsltc.setDestDirectory(xslDir);
7f561c08de6b Initial load
duke
parents:
diff changeset
   780
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   781
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   782
7f561c08de6b Initial load
duke
parents:
diff changeset
   783
            if (_packageName != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   784
                xsltc.setPackageName(_packageName);
7f561c08de6b Initial load
duke
parents:
diff changeset
   785
7f561c08de6b Initial load
duke
parents:
diff changeset
   786
            if (_jarFileName != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   787
                xsltc.setJarFileName(_jarFileName);
7f561c08de6b Initial load
duke
parents:
diff changeset
   788
                outputType = XSLTC.BYTEARRAY_AND_JAR_OUTPUT;
7f561c08de6b Initial load
duke
parents:
diff changeset
   789
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   790
            else
7f561c08de6b Initial load
duke
parents:
diff changeset
   791
                outputType = XSLTC.BYTEARRAY_AND_FILE_OUTPUT;
7f561c08de6b Initial load
duke
parents:
diff changeset
   792
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   793
7f561c08de6b Initial load
duke
parents:
diff changeset
   794
        // Compile the stylesheet
7f561c08de6b Initial load
duke
parents:
diff changeset
   795
        final InputSource input = Util.getInputSource(xsltc, source);
7f561c08de6b Initial load
duke
parents:
diff changeset
   796
        byte[][] bytecodes = xsltc.compile(null, input, outputType);
7f561c08de6b Initial load
duke
parents:
diff changeset
   797
        final String transletName = xsltc.getClassName();
7f561c08de6b Initial load
duke
parents:
diff changeset
   798
7f561c08de6b Initial load
duke
parents:
diff changeset
   799
        // Output to the jar file if the jar file name is set.
7f561c08de6b Initial load
duke
parents:
diff changeset
   800
        if ((_generateTranslet || _autoTranslet)
7f561c08de6b Initial load
duke
parents:
diff changeset
   801
                && bytecodes != null && _jarFileName != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   802
            try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   803
                xsltc.outputToJar();
7f561c08de6b Initial load
duke
parents:
diff changeset
   804
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   805
            catch (java.io.IOException e) { }
7f561c08de6b Initial load
duke
parents:
diff changeset
   806
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   807
7f561c08de6b Initial load
duke
parents:
diff changeset
   808
        // Reset the per-session attributes to their default values
7f561c08de6b Initial load
duke
parents:
diff changeset
   809
        // after each newTemplates() call.
7f561c08de6b Initial load
duke
parents:
diff changeset
   810
        resetTransientAttributes();
7f561c08de6b Initial load
duke
parents:
diff changeset
   811
7f561c08de6b Initial load
duke
parents:
diff changeset
   812
        // Pass compiler warnings to the error listener
7f561c08de6b Initial load
duke
parents:
diff changeset
   813
        if (_errorListener != this) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   814
            try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   815
                passWarningsToListener(xsltc.getWarnings());
7f561c08de6b Initial load
duke
parents:
diff changeset
   816
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   817
            catch (TransformerException e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   818
                throw new TransformerConfigurationException(e);
7f561c08de6b Initial load
duke
parents:
diff changeset
   819
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   820
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   821
        else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   822
            xsltc.printWarnings();
7f561c08de6b Initial load
duke
parents:
diff changeset
   823
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   824
7f561c08de6b Initial load
duke
parents:
diff changeset
   825
        // Check that the transformation went well before returning
7f561c08de6b Initial load
duke
parents:
diff changeset
   826
    if (bytecodes == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   827
7f561c08de6b Initial load
duke
parents:
diff changeset
   828
        ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_COMPILE_ERR);
7f561c08de6b Initial load
duke
parents:
diff changeset
   829
        TransformerConfigurationException exc =  new TransformerConfigurationException(err.toString());
7f561c08de6b Initial load
duke
parents:
diff changeset
   830
7f561c08de6b Initial load
duke
parents:
diff changeset
   831
        // Pass compiler errors to the error listener
7f561c08de6b Initial load
duke
parents:
diff changeset
   832
        if (_errorListener != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   833
            passErrorsToListener(xsltc.getErrors());
7f561c08de6b Initial load
duke
parents:
diff changeset
   834
7f561c08de6b Initial load
duke
parents:
diff changeset
   835
            // As required by TCK 1.2, send a fatalError to the
7f561c08de6b Initial load
duke
parents:
diff changeset
   836
            // error listener because compilation of the stylesheet
7f561c08de6b Initial load
duke
parents:
diff changeset
   837
            // failed and no further processing will be possible.
7f561c08de6b Initial load
duke
parents:
diff changeset
   838
            try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   839
                _errorListener.fatalError(exc);
7f561c08de6b Initial load
duke
parents:
diff changeset
   840
            } catch (TransformerException te) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   841
                // well, we tried.
7f561c08de6b Initial load
duke
parents:
diff changeset
   842
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   843
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   844
        else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   845
            xsltc.printErrors();
7f561c08de6b Initial load
duke
parents:
diff changeset
   846
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   847
        throw exc;
7f561c08de6b Initial load
duke
parents:
diff changeset
   848
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   849
7f561c08de6b Initial load
duke
parents:
diff changeset
   850
        return new TemplatesImpl(bytecodes, transletName,
7f561c08de6b Initial load
duke
parents:
diff changeset
   851
            xsltc.getOutputProperties(), _indentNumber, this);
7f561c08de6b Initial load
duke
parents:
diff changeset
   852
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   853
7f561c08de6b Initial load
duke
parents:
diff changeset
   854
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   855
     * javax.xml.transform.sax.SAXTransformerFactory implementation.
7f561c08de6b Initial load
duke
parents:
diff changeset
   856
     * Get a TemplatesHandler object that can process SAX ContentHandler
7f561c08de6b Initial load
duke
parents:
diff changeset
   857
     * events into a Templates object.
7f561c08de6b Initial load
duke
parents:
diff changeset
   858
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   859
     * @return A TemplatesHandler object that can handle SAX events
7f561c08de6b Initial load
duke
parents:
diff changeset
   860
     * @throws TransformerConfigurationException
7f561c08de6b Initial load
duke
parents:
diff changeset
   861
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   862
    public TemplatesHandler newTemplatesHandler()
7f561c08de6b Initial load
duke
parents:
diff changeset
   863
        throws TransformerConfigurationException
7f561c08de6b Initial load
duke
parents:
diff changeset
   864
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   865
        final TemplatesHandlerImpl handler =
7f561c08de6b Initial load
duke
parents:
diff changeset
   866
            new TemplatesHandlerImpl(_indentNumber, this);
7f561c08de6b Initial load
duke
parents:
diff changeset
   867
        if (_uriResolver != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   868
            handler.setURIResolver(_uriResolver);
7f561c08de6b Initial load
duke
parents:
diff changeset
   869
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   870
        return handler;
7f561c08de6b Initial load
duke
parents:
diff changeset
   871
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   872
7f561c08de6b Initial load
duke
parents:
diff changeset
   873
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   874
     * javax.xml.transform.sax.SAXTransformerFactory implementation.
7f561c08de6b Initial load
duke
parents:
diff changeset
   875
     * Get a TransformerHandler object that can process SAX ContentHandler
7f561c08de6b Initial load
duke
parents:
diff changeset
   876
     * events into a Result. This method will return a pure copy transformer.
7f561c08de6b Initial load
duke
parents:
diff changeset
   877
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   878
     * @return A TransformerHandler object that can handle SAX events
7f561c08de6b Initial load
duke
parents:
diff changeset
   879
     * @throws TransformerConfigurationException
7f561c08de6b Initial load
duke
parents:
diff changeset
   880
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   881
    public TransformerHandler newTransformerHandler()
7f561c08de6b Initial load
duke
parents:
diff changeset
   882
        throws TransformerConfigurationException
7f561c08de6b Initial load
duke
parents:
diff changeset
   883
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   884
        final Transformer transformer = newTransformer();
7f561c08de6b Initial load
duke
parents:
diff changeset
   885
        if (_uriResolver != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   886
            transformer.setURIResolver(_uriResolver);
7f561c08de6b Initial load
duke
parents:
diff changeset
   887
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   888
        return new TransformerHandlerImpl((TransformerImpl) transformer);
7f561c08de6b Initial load
duke
parents:
diff changeset
   889
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   890
7f561c08de6b Initial load
duke
parents:
diff changeset
   891
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   892
     * javax.xml.transform.sax.SAXTransformerFactory implementation.
7f561c08de6b Initial load
duke
parents:
diff changeset
   893
     * Get a TransformerHandler object that can process SAX ContentHandler
7f561c08de6b Initial load
duke
parents:
diff changeset
   894
     * events into a Result, based on the transformation instructions
7f561c08de6b Initial load
duke
parents:
diff changeset
   895
     * specified by the argument.
7f561c08de6b Initial load
duke
parents:
diff changeset
   896
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   897
     * @param src The source of the transformation instructions.
7f561c08de6b Initial load
duke
parents:
diff changeset
   898
     * @return A TransformerHandler object that can handle SAX events
7f561c08de6b Initial load
duke
parents:
diff changeset
   899
     * @throws TransformerConfigurationException
7f561c08de6b Initial load
duke
parents:
diff changeset
   900
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   901
    public TransformerHandler newTransformerHandler(Source src)
7f561c08de6b Initial load
duke
parents:
diff changeset
   902
        throws TransformerConfigurationException
7f561c08de6b Initial load
duke
parents:
diff changeset
   903
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   904
        final Transformer transformer = newTransformer(src);
7f561c08de6b Initial load
duke
parents:
diff changeset
   905
        if (_uriResolver != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   906
            transformer.setURIResolver(_uriResolver);
7f561c08de6b Initial load
duke
parents:
diff changeset
   907
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   908
        return new TransformerHandlerImpl((TransformerImpl) transformer);
7f561c08de6b Initial load
duke
parents:
diff changeset
   909
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   910
7f561c08de6b Initial load
duke
parents:
diff changeset
   911
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   912
     * javax.xml.transform.sax.SAXTransformerFactory implementation.
7f561c08de6b Initial load
duke
parents:
diff changeset
   913
     * Get a TransformerHandler object that can process SAX ContentHandler
7f561c08de6b Initial load
duke
parents:
diff changeset
   914
     * events into a Result, based on the transformation instructions
7f561c08de6b Initial load
duke
parents:
diff changeset
   915
     * specified by the argument.
7f561c08de6b Initial load
duke
parents:
diff changeset
   916
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   917
     * @param templates Represents a pre-processed stylesheet
7f561c08de6b Initial load
duke
parents:
diff changeset
   918
     * @return A TransformerHandler object that can handle SAX events
7f561c08de6b Initial load
duke
parents:
diff changeset
   919
     * @throws TransformerConfigurationException
7f561c08de6b Initial load
duke
parents:
diff changeset
   920
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   921
    public TransformerHandler newTransformerHandler(Templates templates)
7f561c08de6b Initial load
duke
parents:
diff changeset
   922
        throws TransformerConfigurationException
7f561c08de6b Initial load
duke
parents:
diff changeset
   923
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   924
        final Transformer transformer = templates.newTransformer();
7f561c08de6b Initial load
duke
parents:
diff changeset
   925
        final TransformerImpl internal = (TransformerImpl)transformer;
7f561c08de6b Initial load
duke
parents:
diff changeset
   926
        return new TransformerHandlerImpl(internal);
7f561c08de6b Initial load
duke
parents:
diff changeset
   927
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   928
7f561c08de6b Initial load
duke
parents:
diff changeset
   929
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   930
     * javax.xml.transform.sax.SAXTransformerFactory implementation.
7f561c08de6b Initial load
duke
parents:
diff changeset
   931
     * Create an XMLFilter that uses the given source as the
7f561c08de6b Initial load
duke
parents:
diff changeset
   932
     * transformation instructions.
7f561c08de6b Initial load
duke
parents:
diff changeset
   933
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   934
     * @param src The source of the transformation instructions.
7f561c08de6b Initial load
duke
parents:
diff changeset
   935
     * @return An XMLFilter object, or null if this feature is not supported.
7f561c08de6b Initial load
duke
parents:
diff changeset
   936
     * @throws TransformerConfigurationException
7f561c08de6b Initial load
duke
parents:
diff changeset
   937
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   938
    public XMLFilter newXMLFilter(Source src)
7f561c08de6b Initial load
duke
parents:
diff changeset
   939
        throws TransformerConfigurationException
7f561c08de6b Initial load
duke
parents:
diff changeset
   940
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   941
        Templates templates = newTemplates(src);
7f561c08de6b Initial load
duke
parents:
diff changeset
   942
        if (templates == null) return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   943
        return newXMLFilter(templates);
7f561c08de6b Initial load
duke
parents:
diff changeset
   944
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   945
7f561c08de6b Initial load
duke
parents:
diff changeset
   946
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   947
     * javax.xml.transform.sax.SAXTransformerFactory implementation.
7f561c08de6b Initial load
duke
parents:
diff changeset
   948
     * Create an XMLFilter that uses the given source as the
7f561c08de6b Initial load
duke
parents:
diff changeset
   949
     * transformation instructions.
7f561c08de6b Initial load
duke
parents:
diff changeset
   950
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   951
     * @param templates The source of the transformation instructions.
7f561c08de6b Initial load
duke
parents:
diff changeset
   952
     * @return An XMLFilter object, or null if this feature is not supported.
7f561c08de6b Initial load
duke
parents:
diff changeset
   953
     * @throws TransformerConfigurationException
7f561c08de6b Initial load
duke
parents:
diff changeset
   954
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   955
    public XMLFilter newXMLFilter(Templates templates)
7f561c08de6b Initial load
duke
parents:
diff changeset
   956
        throws TransformerConfigurationException
7f561c08de6b Initial load
duke
parents:
diff changeset
   957
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   958
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   959
            return new com.sun.org.apache.xalan.internal.xsltc.trax.TrAXFilter(templates);
7f561c08de6b Initial load
duke
parents:
diff changeset
   960
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   961
        catch (TransformerConfigurationException e1) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   962
            if (_errorListener != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   963
                try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   964
                    _errorListener.fatalError(e1);
7f561c08de6b Initial load
duke
parents:
diff changeset
   965
                    return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   966
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   967
                catch (TransformerException e2) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   968
                    new TransformerConfigurationException(e2);
7f561c08de6b Initial load
duke
parents:
diff changeset
   969
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   970
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   971
            throw e1;
7f561c08de6b Initial load
duke
parents:
diff changeset
   972
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   973
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   974
7f561c08de6b Initial load
duke
parents:
diff changeset
   975
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   976
     * Receive notification of a recoverable error.
7f561c08de6b Initial load
duke
parents:
diff changeset
   977
     * The transformer must continue to provide normal parsing events after
7f561c08de6b Initial load
duke
parents:
diff changeset
   978
     * invoking this method. It should still be possible for the application
7f561c08de6b Initial load
duke
parents:
diff changeset
   979
     * to process the document through to the end.
7f561c08de6b Initial load
duke
parents:
diff changeset
   980
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   981
     * @param e The warning information encapsulated in a transformer
7f561c08de6b Initial load
duke
parents:
diff changeset
   982
     * exception.
7f561c08de6b Initial load
duke
parents:
diff changeset
   983
     * @throws TransformerException if the application chooses to discontinue
7f561c08de6b Initial load
duke
parents:
diff changeset
   984
     * the transformation (always does in our case).
7f561c08de6b Initial load
duke
parents:
diff changeset
   985
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   986
    public void error(TransformerException e)
7f561c08de6b Initial load
duke
parents:
diff changeset
   987
        throws TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
   988
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   989
        Throwable wrapped = e.getException();
7f561c08de6b Initial load
duke
parents:
diff changeset
   990
        if (wrapped != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   991
            System.err.println(new ErrorMsg(ErrorMsg.ERROR_PLUS_WRAPPED_MSG,
7f561c08de6b Initial load
duke
parents:
diff changeset
   992
                                            e.getMessageAndLocation(),
7f561c08de6b Initial load
duke
parents:
diff changeset
   993
                                            wrapped.getMessage()));
7f561c08de6b Initial load
duke
parents:
diff changeset
   994
        } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   995
            System.err.println(new ErrorMsg(ErrorMsg.ERROR_MSG,
7f561c08de6b Initial load
duke
parents:
diff changeset
   996
                                            e.getMessageAndLocation()));
7f561c08de6b Initial load
duke
parents:
diff changeset
   997
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   998
        throw e;
7f561c08de6b Initial load
duke
parents:
diff changeset
   999
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1000
7f561c08de6b Initial load
duke
parents:
diff changeset
  1001
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1002
     * Receive notification of a non-recoverable error.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1003
     * The application must assume that the transformation cannot continue
7f561c08de6b Initial load
duke
parents:
diff changeset
  1004
     * after the Transformer has invoked this method, and should continue
7f561c08de6b Initial load
duke
parents:
diff changeset
  1005
     * (if at all) only to collect addition error messages. In fact,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1006
     * Transformers are free to stop reporting events once this method has
7f561c08de6b Initial load
duke
parents:
diff changeset
  1007
     * been invoked.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1008
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1009
     * @param e warning information encapsulated in a transformer
7f561c08de6b Initial load
duke
parents:
diff changeset
  1010
     * exception.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1011
     * @throws TransformerException if the application chooses to discontinue
7f561c08de6b Initial load
duke
parents:
diff changeset
  1012
     * the transformation (always does in our case).
7f561c08de6b Initial load
duke
parents:
diff changeset
  1013
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1014
    public void fatalError(TransformerException e)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1015
        throws TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
  1016
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1017
        Throwable wrapped = e.getException();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1018
        if (wrapped != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1019
            System.err.println(new ErrorMsg(ErrorMsg.FATAL_ERR_PLUS_WRAPPED_MSG,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1020
                                            e.getMessageAndLocation(),
7f561c08de6b Initial load
duke
parents:
diff changeset
  1021
                                            wrapped.getMessage()));
7f561c08de6b Initial load
duke
parents:
diff changeset
  1022
        } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1023
            System.err.println(new ErrorMsg(ErrorMsg.FATAL_ERR_MSG,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1024
                                            e.getMessageAndLocation()));
7f561c08de6b Initial load
duke
parents:
diff changeset
  1025
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1026
        throw e;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1027
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1028
7f561c08de6b Initial load
duke
parents:
diff changeset
  1029
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1030
     * Receive notification of a warning.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1031
     * Transformers can use this method to report conditions that are not
7f561c08de6b Initial load
duke
parents:
diff changeset
  1032
     * errors or fatal errors. The default behaviour is to take no action.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1033
     * After invoking this method, the Transformer must continue with the
7f561c08de6b Initial load
duke
parents:
diff changeset
  1034
     * transformation. It should still be possible for the application to
7f561c08de6b Initial load
duke
parents:
diff changeset
  1035
     * process the document through to the end.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1036
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1037
     * @param e The warning information encapsulated in a transformer
7f561c08de6b Initial load
duke
parents:
diff changeset
  1038
     * exception.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1039
     * @throws TransformerException if the application chooses to discontinue
7f561c08de6b Initial load
duke
parents:
diff changeset
  1040
     * the transformation (never does in our case).
7f561c08de6b Initial load
duke
parents:
diff changeset
  1041
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1042
    public void warning(TransformerException e)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1043
        throws TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
  1044
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1045
        Throwable wrapped = e.getException();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1046
        if (wrapped != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1047
            System.err.println(new ErrorMsg(ErrorMsg.WARNING_PLUS_WRAPPED_MSG,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1048
                                            e.getMessageAndLocation(),
7f561c08de6b Initial load
duke
parents:
diff changeset
  1049
                                            wrapped.getMessage()));
7f561c08de6b Initial load
duke
parents:
diff changeset
  1050
        } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1051
            System.err.println(new ErrorMsg(ErrorMsg.WARNING_MSG,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1052
                                            e.getMessageAndLocation()));
7f561c08de6b Initial load
duke
parents:
diff changeset
  1053
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1054
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1055
7f561c08de6b Initial load
duke
parents:
diff changeset
  1056
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1057
     * This method implements XSLTC's SourceLoader interface. It is used to
7f561c08de6b Initial load
duke
parents:
diff changeset
  1058
     * glue a TrAX URIResolver to the XSLTC compiler's Input and Import classes.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1059
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1060
     * @param href The URI of the document to load
7f561c08de6b Initial load
duke
parents:
diff changeset
  1061
     * @param context The URI of the currently loaded document
7f561c08de6b Initial load
duke
parents:
diff changeset
  1062
     * @param xsltc The compiler that resuests the document
7f561c08de6b Initial load
duke
parents:
diff changeset
  1063
     * @return An InputSource with the loaded document
7f561c08de6b Initial load
duke
parents:
diff changeset
  1064
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1065
    public InputSource loadSource(String href, String context, XSLTC xsltc) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1066
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1067
            if (_uriResolver != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1068
                final Source source = _uriResolver.resolve(href, context);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1069
                if (source != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1070
                    return Util.getInputSource(xsltc, source);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1071
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1072
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1073
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1074
        catch (TransformerException e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1075
            // should catch it when the resolver explicitly throws the exception
7f561c08de6b Initial load
duke
parents:
diff changeset
  1076
            final ErrorMsg msg = new ErrorMsg(ErrorMsg.INVALID_URI_ERR, href + "\n" + e.getMessage(), this);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1077
            xsltc.getParser().reportError(Constants.FATAL, msg);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1078
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1079
7f561c08de6b Initial load
duke
parents:
diff changeset
  1080
        return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1081
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1082
7f561c08de6b Initial load
duke
parents:
diff changeset
  1083
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1084
     * Reset the per-session attributes to their default values
7f561c08de6b Initial load
duke
parents:
diff changeset
  1085
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1086
    private void resetTransientAttributes() {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1087
        _transletName = DEFAULT_TRANSLET_NAME;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1088
        _destinationDirectory = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1089
        _packageName = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1090
        _jarFileName = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1091
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1092
7f561c08de6b Initial load
duke
parents:
diff changeset
  1093
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1094
     * Load the translet classes from local .class files and return
7f561c08de6b Initial load
duke
parents:
diff changeset
  1095
     * the bytecode array.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1096
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1097
     * @param source The xsl source
7f561c08de6b Initial load
duke
parents:
diff changeset
  1098
     * @param fullClassName The full name of the translet
7f561c08de6b Initial load
duke
parents:
diff changeset
  1099
     * @return The bytecode array
7f561c08de6b Initial load
duke
parents:
diff changeset
  1100
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1101
    private byte[][] getBytecodesFromClasses(Source source, String fullClassName)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1102
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1103
        if (fullClassName == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1104
            return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1105
7f561c08de6b Initial load
duke
parents:
diff changeset
  1106
        String xslFileName = getStylesheetFileName(source);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1107
        File xslFile = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1108
        if (xslFileName != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1109
            xslFile = new File(xslFileName);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1110
7f561c08de6b Initial load
duke
parents:
diff changeset
  1111
        // Find the base name of the translet
7f561c08de6b Initial load
duke
parents:
diff changeset
  1112
        final String transletName;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1113
        int lastDotIndex = fullClassName.lastIndexOf('.');
7f561c08de6b Initial load
duke
parents:
diff changeset
  1114
        if (lastDotIndex > 0)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1115
            transletName = fullClassName.substring(lastDotIndex+1);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1116
        else
7f561c08de6b Initial load
duke
parents:
diff changeset
  1117
            transletName = fullClassName;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1118
7f561c08de6b Initial load
duke
parents:
diff changeset
  1119
        // Construct the path name for the translet class file
7f561c08de6b Initial load
duke
parents:
diff changeset
  1120
        String transletPath = fullClassName.replace('.', '/');
7f561c08de6b Initial load
duke
parents:
diff changeset
  1121
        if (_destinationDirectory != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1122
            transletPath = _destinationDirectory + "/" + transletPath + ".class";
7f561c08de6b Initial load
duke
parents:
diff changeset
  1123
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1124
        else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1125
            if (xslFile != null && xslFile.getParent() != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1126
                transletPath = xslFile.getParent() + "/" + transletPath + ".class";
7f561c08de6b Initial load
duke
parents:
diff changeset
  1127
            else
7f561c08de6b Initial load
duke
parents:
diff changeset
  1128
                transletPath = transletPath + ".class";
7f561c08de6b Initial load
duke
parents:
diff changeset
  1129
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1130
7f561c08de6b Initial load
duke
parents:
diff changeset
  1131
        // Return null if the translet class file does not exist.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1132
        File transletFile = new File(transletPath);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1133
        if (!transletFile.exists())
7f561c08de6b Initial load
duke
parents:
diff changeset
  1134
            return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1135
7f561c08de6b Initial load
duke
parents:
diff changeset
  1136
        // Compare the timestamps of the translet and the xsl file.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1137
        // If the translet is older than the xsl file, return null
7f561c08de6b Initial load
duke
parents:
diff changeset
  1138
        // so that the xsl file is used for the transformation and
7f561c08de6b Initial load
duke
parents:
diff changeset
  1139
        // the translet is regenerated.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1140
        if (xslFile != null && xslFile.exists()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1141
            long xslTimestamp = xslFile.lastModified();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1142
            long transletTimestamp = transletFile.lastModified();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1143
            if (transletTimestamp < xslTimestamp)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1144
                return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1145
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1146
7f561c08de6b Initial load
duke
parents:
diff changeset
  1147
        // Load the translet into a bytecode array.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1148
        Vector bytecodes = new Vector();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1149
        int fileLength = (int)transletFile.length();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1150
        if (fileLength > 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1151
            FileInputStream input = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1152
            try {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1153
                input = new FileInputStream(transletFile);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1154
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1155
            catch (FileNotFoundException e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1156
                return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1157
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1158
7f561c08de6b Initial load
duke
parents:
diff changeset
  1159
            byte[] bytes = new byte[fileLength];
7f561c08de6b Initial load
duke
parents:
diff changeset
  1160
            try {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1161
                readFromInputStream(bytes, input, fileLength);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1162
                input.close();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1163
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1164
            catch (IOException e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1165
                return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1166
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1167
7f561c08de6b Initial load
duke
parents:
diff changeset
  1168
            bytecodes.addElement(bytes);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1169
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1170
        else
7f561c08de6b Initial load
duke
parents:
diff changeset
  1171
            return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1172
7f561c08de6b Initial load
duke
parents:
diff changeset
  1173
        // Find the parent directory of the translet.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1174
        String transletParentDir = transletFile.getParent();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1175
        if (transletParentDir == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1176
            transletParentDir = System.getProperty("user.dir");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1177
7f561c08de6b Initial load
duke
parents:
diff changeset
  1178
        File transletParentFile = new File(transletParentDir);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1179
7f561c08de6b Initial load
duke
parents:
diff changeset
  1180
        // Find all the auxiliary files which have a name pattern of "transletClass$nnn.class".
7f561c08de6b Initial load
duke
parents:
diff changeset
  1181
        final String transletAuxPrefix = transletName + "$";
7f561c08de6b Initial load
duke
parents:
diff changeset
  1182
        File[] auxfiles = transletParentFile.listFiles(new FilenameFilter() {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1183
                public boolean accept(File dir, String name)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1184
                {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1185
                    return (name.endsWith(".class") && name.startsWith(transletAuxPrefix));
7f561c08de6b Initial load
duke
parents:
diff changeset
  1186
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1187
              });
7f561c08de6b Initial load
duke
parents:
diff changeset
  1188
7f561c08de6b Initial load
duke
parents:
diff changeset
  1189
        // Load the auxiliary class files and add them to the bytecode array.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1190
        for (int i = 0; i < auxfiles.length; i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1191
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1192
            File auxfile = auxfiles[i];
7f561c08de6b Initial load
duke
parents:
diff changeset
  1193
            int auxlength = (int)auxfile.length();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1194
            if (auxlength > 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1195
                FileInputStream auxinput = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1196
                try {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1197
                    auxinput = new FileInputStream(auxfile);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1198
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1199
                catch (FileNotFoundException e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1200
                    continue;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1201
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1202
7f561c08de6b Initial load
duke
parents:
diff changeset
  1203
                byte[] bytes = new byte[auxlength];
7f561c08de6b Initial load
duke
parents:
diff changeset
  1204
7f561c08de6b Initial load
duke
parents:
diff changeset
  1205
                try {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1206
                    readFromInputStream(bytes, auxinput, auxlength);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1207
                    auxinput.close();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1208
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1209
                catch (IOException e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1210
                    continue;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1211
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1212
7f561c08de6b Initial load
duke
parents:
diff changeset
  1213
                bytecodes.addElement(bytes);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1214
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1215
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1216
7f561c08de6b Initial load
duke
parents:
diff changeset
  1217
        // Convert the Vector of byte[] to byte[][].
7f561c08de6b Initial load
duke
parents:
diff changeset
  1218
        final int count = bytecodes.size();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1219
        if ( count > 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1220
            final byte[][] result = new byte[count][1];
7f561c08de6b Initial load
duke
parents:
diff changeset
  1221
            for (int i = 0; i < count; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1222
                result[i] = (byte[])bytecodes.elementAt(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1223
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1224
7f561c08de6b Initial load
duke
parents:
diff changeset
  1225
            return result;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1226
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1227
        else
7f561c08de6b Initial load
duke
parents:
diff changeset
  1228
            return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1229
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1230
7f561c08de6b Initial load
duke
parents:
diff changeset
  1231
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1232
     * Load the translet classes from the jar file and return the bytecode.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1233
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1234
     * @param source The xsl source
7f561c08de6b Initial load
duke
parents:
diff changeset
  1235
     * @param fullClassName The full name of the translet
7f561c08de6b Initial load
duke
parents:
diff changeset
  1236
     * @return The bytecode array
7f561c08de6b Initial load
duke
parents:
diff changeset
  1237
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1238
    private byte[][] getBytecodesFromJar(Source source, String fullClassName)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1239
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1240
        String xslFileName = getStylesheetFileName(source);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1241
        File xslFile = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1242
        if (xslFileName != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1243
            xslFile = new File(xslFileName);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1244
7f561c08de6b Initial load
duke
parents:
diff changeset
  1245
        // Construct the path for the jar file
7f561c08de6b Initial load
duke
parents:
diff changeset
  1246
        String jarPath = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1247
        if (_destinationDirectory != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1248
            jarPath = _destinationDirectory + "/" + _jarFileName;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1249
        else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1250
            if (xslFile != null && xslFile.getParent() != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1251
                jarPath = xslFile.getParent() + "/" + _jarFileName;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1252
            else
7f561c08de6b Initial load
duke
parents:
diff changeset
  1253
                jarPath = _jarFileName;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1254
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1255
7f561c08de6b Initial load
duke
parents:
diff changeset
  1256
        // Return null if the jar file does not exist.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1257
        File file = new File(jarPath);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1258
        if (!file.exists())
7f561c08de6b Initial load
duke
parents:
diff changeset
  1259
            return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1260
7f561c08de6b Initial load
duke
parents:
diff changeset
  1261
        // Compare the timestamps of the jar file and the xsl file. Return null
7f561c08de6b Initial load
duke
parents:
diff changeset
  1262
        // if the xsl file is newer than the jar file.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1263
        if (xslFile != null && xslFile.exists()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1264
            long xslTimestamp = xslFile.lastModified();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1265
            long transletTimestamp = file.lastModified();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1266
            if (transletTimestamp < xslTimestamp)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1267
                return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1268
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1269
7f561c08de6b Initial load
duke
parents:
diff changeset
  1270
        // Create a ZipFile object for the jar file
7f561c08de6b Initial load
duke
parents:
diff changeset
  1271
        ZipFile jarFile = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1272
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1273
            jarFile = new ZipFile(file);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1274
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1275
        catch (IOException e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1276
            return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1277
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1278
7f561c08de6b Initial load
duke
parents:
diff changeset
  1279
        String transletPath = fullClassName.replace('.', '/');
7f561c08de6b Initial load
duke
parents:
diff changeset
  1280
        String transletAuxPrefix = transletPath + "$";
7f561c08de6b Initial load
duke
parents:
diff changeset
  1281
        String transletFullName = transletPath + ".class";
7f561c08de6b Initial load
duke
parents:
diff changeset
  1282
7f561c08de6b Initial load
duke
parents:
diff changeset
  1283
        Vector bytecodes = new Vector();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1284
7f561c08de6b Initial load
duke
parents:
diff changeset
  1285
        // Iterate through all entries in the jar file to find the
7f561c08de6b Initial load
duke
parents:
diff changeset
  1286
        // translet and auxiliary classes.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1287
        Enumeration entries = jarFile.entries();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1288
        while (entries.hasMoreElements())
7f561c08de6b Initial load
duke
parents:
diff changeset
  1289
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1290
            ZipEntry entry = (ZipEntry)entries.nextElement();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1291
            String entryName = entry.getName();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1292
            if (entry.getSize() > 0 &&
7f561c08de6b Initial load
duke
parents:
diff changeset
  1293
                  (entryName.equals(transletFullName) ||
7f561c08de6b Initial load
duke
parents:
diff changeset
  1294
                  (entryName.endsWith(".class") &&
7f561c08de6b Initial load
duke
parents:
diff changeset
  1295
                      entryName.startsWith(transletAuxPrefix))))
7f561c08de6b Initial load
duke
parents:
diff changeset
  1296
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1297
                try {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1298
                    InputStream input = jarFile.getInputStream(entry);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1299
                    int size = (int)entry.getSize();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1300
                    byte[] bytes = new byte[size];
7f561c08de6b Initial load
duke
parents:
diff changeset
  1301
                    readFromInputStream(bytes, input, size);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1302
                    input.close();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1303
                    bytecodes.addElement(bytes);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1304
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1305
                catch (IOException e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1306
                    return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1307
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1308
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1309
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1310
7f561c08de6b Initial load
duke
parents:
diff changeset
  1311
        // Convert the Vector of byte[] to byte[][].
7f561c08de6b Initial load
duke
parents:
diff changeset
  1312
        final int count = bytecodes.size();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1313
        if (count > 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1314
            final byte[][] result = new byte[count][1];
7f561c08de6b Initial load
duke
parents:
diff changeset
  1315
            for (int i = 0; i < count; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1316
                result[i] = (byte[])bytecodes.elementAt(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1317
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1318
7f561c08de6b Initial load
duke
parents:
diff changeset
  1319
            return result;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1320
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1321
        else
7f561c08de6b Initial load
duke
parents:
diff changeset
  1322
            return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1323
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1324
7f561c08de6b Initial load
duke
parents:
diff changeset
  1325
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1326
     * Read a given number of bytes from the InputStream into a byte array.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1327
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1328
     * @param bytes The byte array to store the input content.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1329
     * @param input The input stream.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1330
     * @param size The number of bytes to read.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1331
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1332
    private void readFromInputStream(byte[] bytes, InputStream input, int size)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1333
        throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
  1334
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1335
      int n = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1336
      int offset = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1337
      int length = size;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1338
      while (length > 0 && (n = input.read(bytes, offset, length)) > 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1339
          offset = offset + n;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1340
          length = length - n;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1341
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1342
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1343
7f561c08de6b Initial load
duke
parents:
diff changeset
  1344
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1345
     * Return the base class name of the translet.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1346
     * The translet name is resolved using the following rules:
7f561c08de6b Initial load
duke
parents:
diff changeset
  1347
     * 1. if the _transletName attribute is set and its value is not "GregorSamsa",
7f561c08de6b Initial load
duke
parents:
diff changeset
  1348
     *    then _transletName is returned.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1349
     * 2. otherwise get the translet name from the base name of the system ID
7f561c08de6b Initial load
duke
parents:
diff changeset
  1350
     * 3. return "GregorSamsa" if the result from step 2 is null.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1351
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1352
     * @param source The input Source
7f561c08de6b Initial load
duke
parents:
diff changeset
  1353
     * @return The name of the translet class
7f561c08de6b Initial load
duke
parents:
diff changeset
  1354
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1355
    private String getTransletBaseName(Source source)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1356
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1357
        String transletBaseName = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1358
        if (!_transletName.equals(DEFAULT_TRANSLET_NAME))
7f561c08de6b Initial load
duke
parents:
diff changeset
  1359
            return _transletName;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1360
        else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1361
            String systemId = source.getSystemId();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1362
            if (systemId != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1363
                String baseName = Util.baseName(systemId);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1364
                if (baseName != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1365
                    baseName = Util.noExtName(baseName);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1366
                    transletBaseName = Util.toJavaName(baseName);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1367
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1368
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1369
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1370
7f561c08de6b Initial load
duke
parents:
diff changeset
  1371
        return (transletBaseName != null) ? transletBaseName : DEFAULT_TRANSLET_NAME;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1372
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1373
7f561c08de6b Initial load
duke
parents:
diff changeset
  1374
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1375
     *  Return the local file name from the systemId of the Source object
7f561c08de6b Initial load
duke
parents:
diff changeset
  1376
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1377
     * @param source The Source
7f561c08de6b Initial load
duke
parents:
diff changeset
  1378
     * @return The file name in the local filesystem, or null if the
7f561c08de6b Initial load
duke
parents:
diff changeset
  1379
     * systemId does not represent a local file.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1380
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1381
    private String getStylesheetFileName(Source source)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1382
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1383
        String systemId = source.getSystemId();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1384
        if (systemId != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1385
            File file = new File(systemId);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1386
            if (file.exists())
7f561c08de6b Initial load
duke
parents:
diff changeset
  1387
                return systemId;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1388
            else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1389
                URL url = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1390
                try {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1391
                    url = new URL(systemId);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1392
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1393
                catch (MalformedURLException e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1394
                    return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1395
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1396
7f561c08de6b Initial load
duke
parents:
diff changeset
  1397
                if ("file".equals(url.getProtocol()))
7f561c08de6b Initial load
duke
parents:
diff changeset
  1398
                    return url.getFile();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1399
                else
7f561c08de6b Initial load
duke
parents:
diff changeset
  1400
                    return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1401
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1402
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1403
        else
7f561c08de6b Initial load
duke
parents:
diff changeset
  1404
            return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1405
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1406
7f561c08de6b Initial load
duke
parents:
diff changeset
  1407
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1408
     * Returns the Class object the provides the XSLTC DTM Manager service.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1409
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1410
    protected Class getDTMManagerClass() {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1411
        return m_DTMManagerClass;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1412
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1413
}