jaxp/src/com/sun/org/apache/xalan/internal/xsltc/compiler/XSLTC.java
author joehw
Mon, 18 Feb 2013 11:33:35 -0800
changeset 16953 a44e04deb948
parent 12458 d601e4bba306
child 17534 21dc0b2762da
permissions -rw-r--r--
6657673: Issues with JAXP Reviewed-by: alanb, lancea, ahgross, mullan
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: XSLTC.java,v 1.2.4.1 2005/09/05 09:51: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.compiler;
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
import java.io.BufferedOutputStream;
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
import java.io.ByteArrayOutputStream;
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
import java.io.File;
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
import java.io.FileOutputStream;
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.URL;
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
import java.util.Date;
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.Map;
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
import java.util.Properties;
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
import java.util.Vector;
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
import java.util.jar.JarEntry;
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
import java.util.jar.JarOutputStream;
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
import java.util.jar.Manifest;
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
import com.sun.org.apache.bcel.internal.classfile.JavaClass;
16953
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
    44
import com.sun.org.apache.xalan.internal.utils.SecuritySupport;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util;
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
import com.sun.org.apache.xml.internal.dtm.DTM;
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
16953
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
    49
import com.sun.org.apache.xalan.internal.utils.SecuritySupport;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
import org.xml.sax.InputSource;
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
import org.xml.sax.XMLReader;
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
 * @author Jacek Ambroziak
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
 * @author Santiago Pericas-Geertsen
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
 * @author G. Todd Miller
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
 * @author Morten Jorgensen
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
 * @author John Howard (johnh@schemasoft.com)
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
public final class XSLTC {
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
    // A reference to the main stylesheet parser object.
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
    private Parser _parser;
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
    // A reference to an external XMLReader (SAX parser) passed to us
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
    private XMLReader _reader = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
    // A reference to an external SourceLoader (for use with include/import)
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
    private SourceLoader _loader = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
    // A reference to the stylesheet being compiled.
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
    private Stylesheet _stylesheet;
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
    // Counters used by various classes to generate unique names.
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
    // private int _variableSerial     = 1;
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
    private int _modeSerial         = 1;
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
    private int _stylesheetSerial   = 1;
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
    private int _stepPatternSerial  = 1;
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
    private int _helperClassSerial  = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
    private int _attributeSetSerial = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
    private int[] _numberFieldIndexes;
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
    // Name index tables
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
    private int       _nextGType;  // Next available element type
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
    private Vector    _namesIndex; // Index of all registered QNames
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
    private Hashtable _elements;   // Hashtable of all registered elements
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
    private Hashtable _attributes; // Hashtable of all registered attributes
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
    // Namespace index tables
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
    private int       _nextNSType; // Next available namespace type
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
    private Vector    _namespaceIndex; // Index of all registered namespaces
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
    private Hashtable _namespaces; // Hashtable of all registered namespaces
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
    private Hashtable _namespacePrefixes;// Hashtable of all registered namespace prefixes
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
    // All literal text in the stylesheet
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
    private Vector m_characterData;
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
    // These define the various methods for outputting the translet
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
    public static final int FILE_OUTPUT        = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
    public static final int JAR_OUTPUT         = 1;
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
    public static final int BYTEARRAY_OUTPUT   = 2;
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
    public static final int CLASSLOADER_OUTPUT = 3;
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
    public static final int BYTEARRAY_AND_FILE_OUTPUT = 4;
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
    public static final int BYTEARRAY_AND_JAR_OUTPUT  = 5;
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
    // Compiler options (passed from command line or XSLTC client)
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
    private boolean _debug = false;      // -x
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
    private String  _jarFileName = null; // -j <jar-file-name>
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
    private String  _className = null;   // -o <class-name>
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
    private String  _packageName = null; // -p <package-name>
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
    private File    _destDir = null;     // -d <directory-name>
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
    private int     _outputType = FILE_OUTPUT; // by default
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
    private Vector  _classes;
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
    private Vector  _bcelClasses;
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
    private boolean _callsNodeset = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
    private boolean _multiDocument = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
    private boolean _hasIdCall = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
     * Set to true if template inlining is requested. Template
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
     * inlining used to be the default, but we have found that
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
     * Hotspots does a better job with shorter methods, so the
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
     * default is *not* to inline now.
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
    private boolean _templateInlining = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
     * State of the secure processing feature.
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
    private boolean _isSecureProcessing = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   136
    private boolean _useServicesMechanism = true;
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   137
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
     * XSLTC compiler constructor
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
     */
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   141
    public XSLTC(boolean useServicesMechanism) {
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   142
        _parser = new Parser(this, useServicesMechanism);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
     * Set the state of the secure processing feature.
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
    public void setSecureProcessing(boolean flag) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
        _isSecureProcessing = flag;
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
     * Return the state of the secure processing feature.
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
    public boolean isSecureProcessing() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
        return _isSecureProcessing;
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
    }
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   158
    /**
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   159
     * Return the state of the services mechanism feature.
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   160
     */
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   161
    public boolean useServicesMechnism() {
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   162
        return _useServicesMechanism;
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   163
    }
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   164
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   165
    /**
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   166
     * Set the state of the services mechanism feature.
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   167
     */
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   168
    public void setServicesMechnism(boolean flag) {
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   169
        _useServicesMechanism = flag;
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   170
    }
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
     * Only for user by the internal TrAX implementation.
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
    public Parser getParser() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
        return _parser;
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
     * Only for user by the internal TrAX implementation.
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   182
    public void setOutputType(int type) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   183
        _outputType = type;
7f561c08de6b Initial load
duke
parents:
diff changeset
   184
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   185
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   187
     * Only for user by the internal TrAX implementation.
7f561c08de6b Initial load
duke
parents:
diff changeset
   188
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   189
    public Properties getOutputProperties() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   190
        return _parser.getOutputProperties();
7f561c08de6b Initial load
duke
parents:
diff changeset
   191
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   192
7f561c08de6b Initial load
duke
parents:
diff changeset
   193
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   194
     * Initializes the compiler to compile a new stylesheet
7f561c08de6b Initial load
duke
parents:
diff changeset
   195
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   196
    public void init() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   197
        reset();
7f561c08de6b Initial load
duke
parents:
diff changeset
   198
        _reader = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   199
        _classes = new Vector();
7f561c08de6b Initial load
duke
parents:
diff changeset
   200
        _bcelClasses = new Vector();
7f561c08de6b Initial load
duke
parents:
diff changeset
   201
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   202
7f561c08de6b Initial load
duke
parents:
diff changeset
   203
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   204
     * Initializes the compiler to produce a new translet
7f561c08de6b Initial load
duke
parents:
diff changeset
   205
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   206
    private void reset() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   207
        _nextGType      = DTM.NTYPES;
7f561c08de6b Initial load
duke
parents:
diff changeset
   208
        _elements       = new Hashtable();
7f561c08de6b Initial load
duke
parents:
diff changeset
   209
        _attributes     = new Hashtable();
7f561c08de6b Initial load
duke
parents:
diff changeset
   210
        _namespaces     = new Hashtable();
7f561c08de6b Initial load
duke
parents:
diff changeset
   211
        _namespaces.put("",new Integer(_nextNSType));
7f561c08de6b Initial load
duke
parents:
diff changeset
   212
        _namesIndex     = new Vector(128);
7f561c08de6b Initial load
duke
parents:
diff changeset
   213
        _namespaceIndex = new Vector(32);
7f561c08de6b Initial load
duke
parents:
diff changeset
   214
        _namespacePrefixes = new Hashtable();
7f561c08de6b Initial load
duke
parents:
diff changeset
   215
        _stylesheet     = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   216
        _parser.init();
7f561c08de6b Initial load
duke
parents:
diff changeset
   217
        //_variableSerial     = 1;
7f561c08de6b Initial load
duke
parents:
diff changeset
   218
        _modeSerial         = 1;
7f561c08de6b Initial load
duke
parents:
diff changeset
   219
        _stylesheetSerial   = 1;
7f561c08de6b Initial load
duke
parents:
diff changeset
   220
        _stepPatternSerial  = 1;
7f561c08de6b Initial load
duke
parents:
diff changeset
   221
        _helperClassSerial  = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
   222
        _attributeSetSerial = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
   223
        _multiDocument      = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   224
        _hasIdCall          = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   225
        _numberFieldIndexes = new int[] {
7f561c08de6b Initial load
duke
parents:
diff changeset
   226
            -1,         // LEVEL_SINGLE
7f561c08de6b Initial load
duke
parents:
diff changeset
   227
            -1,         // LEVEL_MULTIPLE
7f561c08de6b Initial load
duke
parents:
diff changeset
   228
            -1          // LEVEL_ANY
7f561c08de6b Initial load
duke
parents:
diff changeset
   229
        };
7f561c08de6b Initial load
duke
parents:
diff changeset
   230
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   231
7f561c08de6b Initial load
duke
parents:
diff changeset
   232
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   233
     * Defines an external SourceLoader to provide the compiler with documents
7f561c08de6b Initial load
duke
parents:
diff changeset
   234
     * referenced in xsl:include/import
7f561c08de6b Initial load
duke
parents:
diff changeset
   235
     * @param loader The SourceLoader to use for include/import
7f561c08de6b Initial load
duke
parents:
diff changeset
   236
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   237
    public void setSourceLoader(SourceLoader loader) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   238
        _loader = loader;
7f561c08de6b Initial load
duke
parents:
diff changeset
   239
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   240
7f561c08de6b Initial load
duke
parents:
diff changeset
   241
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   242
     * Set a flag indicating if templates are to be inlined or not. The
7f561c08de6b Initial load
duke
parents:
diff changeset
   243
     * default is to do inlining, but this causes problems when the
7f561c08de6b Initial load
duke
parents:
diff changeset
   244
     * stylesheets have a large number of templates (e.g. branch targets
7f561c08de6b Initial load
duke
parents:
diff changeset
   245
     * exceeding 64K or a length of a method exceeding 64K).
7f561c08de6b Initial load
duke
parents:
diff changeset
   246
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   247
    public void setTemplateInlining(boolean templateInlining) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   248
        _templateInlining = templateInlining;
7f561c08de6b Initial load
duke
parents:
diff changeset
   249
    }
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   250
     /**
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   251
     * Return the state of the template inlining feature.
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   252
     */
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   253
    public boolean getTemplateInlining() {
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   254
        return _templateInlining;
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   255
    }
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   256
7f561c08de6b Initial load
duke
parents:
diff changeset
   257
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   258
     * Set the parameters to use to locate the correct <?xml-stylesheet ...?>
7f561c08de6b Initial load
duke
parents:
diff changeset
   259
     * processing instruction in the case where the input document to the
7f561c08de6b Initial load
duke
parents:
diff changeset
   260
     * compiler (and parser) is an XML document.
7f561c08de6b Initial load
duke
parents:
diff changeset
   261
     * @param media The media attribute to be matched. May be null, in which
7f561c08de6b Initial load
duke
parents:
diff changeset
   262
     * case the prefered templates will be used (i.e. alternate = no).
7f561c08de6b Initial load
duke
parents:
diff changeset
   263
     * @param title The value of the title attribute to match. May be null.
7f561c08de6b Initial load
duke
parents:
diff changeset
   264
     * @param charset The value of the charset attribute to match. May be null.
7f561c08de6b Initial load
duke
parents:
diff changeset
   265
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   266
    public void setPIParameters(String media, String title, String charset) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   267
        _parser.setPIParameters(media, title, charset);
7f561c08de6b Initial load
duke
parents:
diff changeset
   268
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   269
7f561c08de6b Initial load
duke
parents:
diff changeset
   270
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   271
     * Compiles an XSL stylesheet pointed to by a URL
7f561c08de6b Initial load
duke
parents:
diff changeset
   272
     * @param url An URL containing the input XSL stylesheet
7f561c08de6b Initial load
duke
parents:
diff changeset
   273
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   274
    public boolean compile(URL url) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   275
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   276
            // Open input stream from URL and wrap inside InputSource
7f561c08de6b Initial load
duke
parents:
diff changeset
   277
            final InputStream stream = url.openStream();
7f561c08de6b Initial load
duke
parents:
diff changeset
   278
            final InputSource input = new InputSource(stream);
7f561c08de6b Initial load
duke
parents:
diff changeset
   279
            input.setSystemId(url.toString());
7f561c08de6b Initial load
duke
parents:
diff changeset
   280
            return compile(input, _className);
7f561c08de6b Initial load
duke
parents:
diff changeset
   281
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   282
        catch (IOException e) {
16953
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   283
            _parser.reportError(Constants.FATAL, new ErrorMsg(ErrorMsg.JAXP_COMPILE_ERR, e));
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   284
            return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   285
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   286
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   287
7f561c08de6b Initial load
duke
parents:
diff changeset
   288
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   289
     * Compiles an XSL stylesheet pointed to by a URL
7f561c08de6b Initial load
duke
parents:
diff changeset
   290
     * @param url An URL containing the input XSL stylesheet
7f561c08de6b Initial load
duke
parents:
diff changeset
   291
     * @param name The name to assign to the translet class
7f561c08de6b Initial load
duke
parents:
diff changeset
   292
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   293
    public boolean compile(URL url, String name) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   294
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   295
            // Open input stream from URL and wrap inside InputSource
7f561c08de6b Initial load
duke
parents:
diff changeset
   296
            final InputStream stream = url.openStream();
7f561c08de6b Initial load
duke
parents:
diff changeset
   297
            final InputSource input = new InputSource(stream);
7f561c08de6b Initial load
duke
parents:
diff changeset
   298
            input.setSystemId(url.toString());
7f561c08de6b Initial load
duke
parents:
diff changeset
   299
            return compile(input, name);
7f561c08de6b Initial load
duke
parents:
diff changeset
   300
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   301
        catch (IOException e) {
16953
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   302
            _parser.reportError(Constants.FATAL, new ErrorMsg(ErrorMsg.JAXP_COMPILE_ERR, e));
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   303
            return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   304
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   305
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   306
7f561c08de6b Initial load
duke
parents:
diff changeset
   307
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   308
     * Compiles an XSL stylesheet passed in through an InputStream
7f561c08de6b Initial load
duke
parents:
diff changeset
   309
     * @param stream An InputStream that will pass in the stylesheet contents
7f561c08de6b Initial load
duke
parents:
diff changeset
   310
     * @param name The name of the translet class to generate
7f561c08de6b Initial load
duke
parents:
diff changeset
   311
     * @return 'true' if the compilation was successful
7f561c08de6b Initial load
duke
parents:
diff changeset
   312
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   313
    public boolean compile(InputStream stream, String name) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   314
        final InputSource input = new InputSource(stream);
7f561c08de6b Initial load
duke
parents:
diff changeset
   315
        input.setSystemId(name); // We have nothing else!!!
7f561c08de6b Initial load
duke
parents:
diff changeset
   316
        return compile(input, name);
7f561c08de6b Initial load
duke
parents:
diff changeset
   317
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   318
7f561c08de6b Initial load
duke
parents:
diff changeset
   319
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   320
     * Compiles an XSL stylesheet passed in through an InputStream
7f561c08de6b Initial load
duke
parents:
diff changeset
   321
     * @param input An InputSource that will pass in the stylesheet contents
7f561c08de6b Initial load
duke
parents:
diff changeset
   322
     * @param name The name of the translet class to generate - can be null
7f561c08de6b Initial load
duke
parents:
diff changeset
   323
     * @return 'true' if the compilation was successful
7f561c08de6b Initial load
duke
parents:
diff changeset
   324
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   325
    public boolean compile(InputSource input, String name) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   326
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   327
            // Reset globals in case we're called by compile(Vector v);
7f561c08de6b Initial load
duke
parents:
diff changeset
   328
            reset();
7f561c08de6b Initial load
duke
parents:
diff changeset
   329
7f561c08de6b Initial load
duke
parents:
diff changeset
   330
            // The systemId may not be set, so we'll have to check the URL
7f561c08de6b Initial load
duke
parents:
diff changeset
   331
            String systemId = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   332
            if (input != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   333
                systemId = input.getSystemId();
7f561c08de6b Initial load
duke
parents:
diff changeset
   334
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   335
7f561c08de6b Initial load
duke
parents:
diff changeset
   336
            // Set the translet class name if not already set
7f561c08de6b Initial load
duke
parents:
diff changeset
   337
            if (_className == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   338
                if (name != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   339
                    setClassName(name);
7f561c08de6b Initial load
duke
parents:
diff changeset
   340
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   341
                else if (systemId != null && !systemId.equals("")) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   342
                    setClassName(Util.baseName(systemId));
7f561c08de6b Initial load
duke
parents:
diff changeset
   343
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   344
7f561c08de6b Initial load
duke
parents:
diff changeset
   345
                // Ensure we have a non-empty class name at this point
7f561c08de6b Initial load
duke
parents:
diff changeset
   346
                if (_className == null || _className.length() == 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   347
                    setClassName("GregorSamsa"); // default translet name
7f561c08de6b Initial load
duke
parents:
diff changeset
   348
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   349
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   350
7f561c08de6b Initial load
duke
parents:
diff changeset
   351
            // Get the root node of the abstract syntax tree
7f561c08de6b Initial load
duke
parents:
diff changeset
   352
            SyntaxTreeNode element = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   353
            if (_reader == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   354
                element = _parser.parse(input);
7f561c08de6b Initial load
duke
parents:
diff changeset
   355
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   356
            else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   357
                element = _parser.parse(_reader, input);
7f561c08de6b Initial load
duke
parents:
diff changeset
   358
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   359
7f561c08de6b Initial load
duke
parents:
diff changeset
   360
            // Compile the translet - this is where the work is done!
7f561c08de6b Initial load
duke
parents:
diff changeset
   361
            if ((!_parser.errorsFound()) && (element != null)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   362
                // Create a Stylesheet element from the root node
7f561c08de6b Initial load
duke
parents:
diff changeset
   363
                _stylesheet = _parser.makeStylesheet(element);
7f561c08de6b Initial load
duke
parents:
diff changeset
   364
                _stylesheet.setSourceLoader(_loader);
7f561c08de6b Initial load
duke
parents:
diff changeset
   365
                _stylesheet.setSystemId(systemId);
7f561c08de6b Initial load
duke
parents:
diff changeset
   366
                _stylesheet.setParentStylesheet(null);
7f561c08de6b Initial load
duke
parents:
diff changeset
   367
                _stylesheet.setTemplateInlining(_templateInlining);
7f561c08de6b Initial load
duke
parents:
diff changeset
   368
                _parser.setCurrentStylesheet(_stylesheet);
7f561c08de6b Initial load
duke
parents:
diff changeset
   369
7f561c08de6b Initial load
duke
parents:
diff changeset
   370
                // Create AST under the Stylesheet element (parse & type-check)
7f561c08de6b Initial load
duke
parents:
diff changeset
   371
                _parser.createAST(_stylesheet);
7f561c08de6b Initial load
duke
parents:
diff changeset
   372
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   373
            // Generate the bytecodes and output the translet class(es)
7f561c08de6b Initial load
duke
parents:
diff changeset
   374
            if ((!_parser.errorsFound()) && (_stylesheet != null)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   375
                _stylesheet.setCallsNodeset(_callsNodeset);
7f561c08de6b Initial load
duke
parents:
diff changeset
   376
                _stylesheet.setMultiDocument(_multiDocument);
7f561c08de6b Initial load
duke
parents:
diff changeset
   377
                _stylesheet.setHasIdCall(_hasIdCall);
7f561c08de6b Initial load
duke
parents:
diff changeset
   378
7f561c08de6b Initial load
duke
parents:
diff changeset
   379
                // Class synchronization is needed for BCEL
7f561c08de6b Initial load
duke
parents:
diff changeset
   380
                synchronized (getClass()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   381
                    _stylesheet.translate();
7f561c08de6b Initial load
duke
parents:
diff changeset
   382
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   383
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   384
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   385
        catch (Exception e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   386
            /*if (_debug)*/ e.printStackTrace();
16953
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   387
            _parser.reportError(Constants.FATAL, new ErrorMsg(ErrorMsg.JAXP_COMPILE_ERR, e));
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   388
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   389
        catch (Error e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   390
            if (_debug) e.printStackTrace();
16953
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   391
            _parser.reportError(Constants.FATAL, new ErrorMsg(ErrorMsg.JAXP_COMPILE_ERR, e));
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   392
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   393
        finally {
7f561c08de6b Initial load
duke
parents:
diff changeset
   394
            _reader = null; // reset this here to be sure it is not re-used
7f561c08de6b Initial load
duke
parents:
diff changeset
   395
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   396
        return !_parser.errorsFound();
7f561c08de6b Initial load
duke
parents:
diff changeset
   397
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   398
7f561c08de6b Initial load
duke
parents:
diff changeset
   399
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   400
     * Compiles a set of stylesheets pointed to by a Vector of URLs
7f561c08de6b Initial load
duke
parents:
diff changeset
   401
     * @param stylesheets A Vector containing URLs pointing to the stylesheets
7f561c08de6b Initial load
duke
parents:
diff changeset
   402
     * @return 'true' if the compilation was successful
7f561c08de6b Initial load
duke
parents:
diff changeset
   403
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   404
    public boolean compile(Vector stylesheets) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   405
        // Get the number of stylesheets (ie. URLs) in the vector
7f561c08de6b Initial load
duke
parents:
diff changeset
   406
        final int count = stylesheets.size();
7f561c08de6b Initial load
duke
parents:
diff changeset
   407
7f561c08de6b Initial load
duke
parents:
diff changeset
   408
        // Return straight away if the vector is empty
7f561c08de6b Initial load
duke
parents:
diff changeset
   409
        if (count == 0) return true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   410
7f561c08de6b Initial load
duke
parents:
diff changeset
   411
        // Special handling needed if the URL count is one, becuase the
7f561c08de6b Initial load
duke
parents:
diff changeset
   412
        // _className global must not be reset if it was set explicitly
7f561c08de6b Initial load
duke
parents:
diff changeset
   413
        if (count == 1) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   414
            final Object url = stylesheets.firstElement();
7f561c08de6b Initial load
duke
parents:
diff changeset
   415
            if (url instanceof URL)
7f561c08de6b Initial load
duke
parents:
diff changeset
   416
                return compile((URL)url);
7f561c08de6b Initial load
duke
parents:
diff changeset
   417
            else
7f561c08de6b Initial load
duke
parents:
diff changeset
   418
                return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   419
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   420
        else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   421
            // Traverse all elements in the vector and compile
7f561c08de6b Initial load
duke
parents:
diff changeset
   422
            final Enumeration urls = stylesheets.elements();
7f561c08de6b Initial load
duke
parents:
diff changeset
   423
            while (urls.hasMoreElements()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   424
                _className = null; // reset, so that new name will be computed
7f561c08de6b Initial load
duke
parents:
diff changeset
   425
                final Object url = urls.nextElement();
7f561c08de6b Initial load
duke
parents:
diff changeset
   426
                if (url instanceof URL) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   427
                    if (!compile((URL)url)) return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   428
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   429
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   430
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   431
        return true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   432
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   433
7f561c08de6b Initial load
duke
parents:
diff changeset
   434
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   435
     * Returns an array of bytecode arrays generated by a compilation.
7f561c08de6b Initial load
duke
parents:
diff changeset
   436
     * @return JVM bytecodes that represent translet class definition
7f561c08de6b Initial load
duke
parents:
diff changeset
   437
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   438
    public byte[][] getBytecodes() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   439
        final int count = _classes.size();
7f561c08de6b Initial load
duke
parents:
diff changeset
   440
        final byte[][] result = new byte[count][1];
7f561c08de6b Initial load
duke
parents:
diff changeset
   441
        for (int i = 0; i < count; i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
   442
            result[i] = (byte[])_classes.elementAt(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
   443
        return result;
7f561c08de6b Initial load
duke
parents:
diff changeset
   444
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   445
7f561c08de6b Initial load
duke
parents:
diff changeset
   446
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   447
     * Compiles a stylesheet pointed to by a URL. The result is put in a
7f561c08de6b Initial load
duke
parents:
diff changeset
   448
     * set of byte arrays. One byte array for each generated class.
7f561c08de6b Initial load
duke
parents:
diff changeset
   449
     * @param name The name of the translet class to generate
7f561c08de6b Initial load
duke
parents:
diff changeset
   450
     * @param input An InputSource that will pass in the stylesheet contents
7f561c08de6b Initial load
duke
parents:
diff changeset
   451
     * @param outputType The output type
7f561c08de6b Initial load
duke
parents:
diff changeset
   452
     * @return JVM bytecodes that represent translet class definition
7f561c08de6b Initial load
duke
parents:
diff changeset
   453
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   454
    public byte[][] compile(String name, InputSource input, int outputType) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   455
        _outputType = outputType;
7f561c08de6b Initial load
duke
parents:
diff changeset
   456
        if (compile(input, name))
7f561c08de6b Initial load
duke
parents:
diff changeset
   457
            return getBytecodes();
7f561c08de6b Initial load
duke
parents:
diff changeset
   458
        else
7f561c08de6b Initial load
duke
parents:
diff changeset
   459
            return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   460
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   461
7f561c08de6b Initial load
duke
parents:
diff changeset
   462
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   463
     * Compiles a stylesheet pointed to by a URL. The result is put in a
7f561c08de6b Initial load
duke
parents:
diff changeset
   464
     * set of byte arrays. One byte array for each generated class.
7f561c08de6b Initial load
duke
parents:
diff changeset
   465
     * @param name The name of the translet class to generate
7f561c08de6b Initial load
duke
parents:
diff changeset
   466
     * @param input An InputSource that will pass in the stylesheet contents
7f561c08de6b Initial load
duke
parents:
diff changeset
   467
     * @return JVM bytecodes that represent translet class definition
7f561c08de6b Initial load
duke
parents:
diff changeset
   468
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   469
    public byte[][] compile(String name, InputSource input) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   470
        return compile(name, input, BYTEARRAY_OUTPUT);
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
     * Set the XMLReader to use for parsing the next input stylesheet
7f561c08de6b Initial load
duke
parents:
diff changeset
   475
     * @param reader XMLReader (SAX2 parser) to use
7f561c08de6b Initial load
duke
parents:
diff changeset
   476
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   477
    public void setXMLReader(XMLReader reader) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   478
        _reader = reader;
7f561c08de6b Initial load
duke
parents:
diff changeset
   479
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   480
7f561c08de6b Initial load
duke
parents:
diff changeset
   481
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   482
     * Get the XMLReader to use for parsing the next input stylesheet
7f561c08de6b Initial load
duke
parents:
diff changeset
   483
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   484
    public XMLReader getXMLReader() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   485
        return _reader ;
7f561c08de6b Initial load
duke
parents:
diff changeset
   486
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   487
7f561c08de6b Initial load
duke
parents:
diff changeset
   488
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   489
     * Get a Vector containing all compile error messages
7f561c08de6b Initial load
duke
parents:
diff changeset
   490
     * @return A Vector containing all compile error messages
7f561c08de6b Initial load
duke
parents:
diff changeset
   491
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   492
    public Vector getErrors() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   493
        return _parser.getErrors();
7f561c08de6b Initial load
duke
parents:
diff changeset
   494
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   495
7f561c08de6b Initial load
duke
parents:
diff changeset
   496
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   497
     * Get a Vector containing all compile warning messages
7f561c08de6b Initial load
duke
parents:
diff changeset
   498
     * @return A Vector containing all compile error messages
7f561c08de6b Initial load
duke
parents:
diff changeset
   499
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   500
    public Vector getWarnings() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   501
        return _parser.getWarnings();
7f561c08de6b Initial load
duke
parents:
diff changeset
   502
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   503
7f561c08de6b Initial load
duke
parents:
diff changeset
   504
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   505
     * Print all compile error messages to standard output
7f561c08de6b Initial load
duke
parents:
diff changeset
   506
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   507
    public void printErrors() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   508
        _parser.printErrors();
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
     * Print all compile warning messages to standard output
7f561c08de6b Initial load
duke
parents:
diff changeset
   513
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   514
    public void printWarnings() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   515
        _parser.printWarnings();
7f561c08de6b Initial load
duke
parents:
diff changeset
   516
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   517
7f561c08de6b Initial load
duke
parents:
diff changeset
   518
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   519
     * This method is called by the XPathParser when it encounters a call
7f561c08de6b Initial load
duke
parents:
diff changeset
   520
     * to the document() function. Affects the DOM used by the translet.
7f561c08de6b Initial load
duke
parents:
diff changeset
   521
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   522
    protected void setMultiDocument(boolean flag) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   523
        _multiDocument = flag;
7f561c08de6b Initial load
duke
parents:
diff changeset
   524
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   525
7f561c08de6b Initial load
duke
parents:
diff changeset
   526
    public boolean isMultiDocument() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   527
        return _multiDocument;
7f561c08de6b Initial load
duke
parents:
diff changeset
   528
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   529
7f561c08de6b Initial load
duke
parents:
diff changeset
   530
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   531
     * This method is called by the XPathParser when it encounters a call
7f561c08de6b Initial load
duke
parents:
diff changeset
   532
     * to the nodeset() extension function. Implies multi document.
7f561c08de6b Initial load
duke
parents:
diff changeset
   533
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   534
    protected void setCallsNodeset(boolean flag) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   535
        if (flag) setMultiDocument(flag);
7f561c08de6b Initial load
duke
parents:
diff changeset
   536
        _callsNodeset = flag;
7f561c08de6b Initial load
duke
parents:
diff changeset
   537
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   538
7f561c08de6b Initial load
duke
parents:
diff changeset
   539
    public boolean callsNodeset() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   540
        return _callsNodeset;
7f561c08de6b Initial load
duke
parents:
diff changeset
   541
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   542
7f561c08de6b Initial load
duke
parents:
diff changeset
   543
    protected void setHasIdCall(boolean flag) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   544
        _hasIdCall = flag;
7f561c08de6b Initial load
duke
parents:
diff changeset
   545
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   546
7f561c08de6b Initial load
duke
parents:
diff changeset
   547
    public boolean hasIdCall() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   548
        return _hasIdCall;
7f561c08de6b Initial load
duke
parents:
diff changeset
   549
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   550
7f561c08de6b Initial load
duke
parents:
diff changeset
   551
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   552
     * Set the class name for the generated translet. This class name is
7f561c08de6b Initial load
duke
parents:
diff changeset
   553
     * overridden if multiple stylesheets are compiled in one go using the
7f561c08de6b Initial load
duke
parents:
diff changeset
   554
     * compile(Vector urls) method.
7f561c08de6b Initial load
duke
parents:
diff changeset
   555
     * @param className The name to assign to the translet class
7f561c08de6b Initial load
duke
parents:
diff changeset
   556
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   557
    public void setClassName(String className) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   558
        final String base  = Util.baseName(className);
7f561c08de6b Initial load
duke
parents:
diff changeset
   559
        final String noext = Util.noExtName(base);
7f561c08de6b Initial load
duke
parents:
diff changeset
   560
        String name  = Util.toJavaName(noext);
7f561c08de6b Initial load
duke
parents:
diff changeset
   561
7f561c08de6b Initial load
duke
parents:
diff changeset
   562
        if (_packageName == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   563
            _className = name;
7f561c08de6b Initial load
duke
parents:
diff changeset
   564
        else
7f561c08de6b Initial load
duke
parents:
diff changeset
   565
            _className = _packageName + '.' + name;
7f561c08de6b Initial load
duke
parents:
diff changeset
   566
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   567
7f561c08de6b Initial load
duke
parents:
diff changeset
   568
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   569
     * Get the class name for the generated translet.
7f561c08de6b Initial load
duke
parents:
diff changeset
   570
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   571
    public String getClassName() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   572
        return _className;
7f561c08de6b Initial load
duke
parents:
diff changeset
   573
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   574
7f561c08de6b Initial load
duke
parents:
diff changeset
   575
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   576
     * Convert for Java class name of local system file name.
7f561c08de6b Initial load
duke
parents:
diff changeset
   577
     * (Replace '.' with '/' on UNIX and replace '.' by '\' on Windows/DOS.)
7f561c08de6b Initial load
duke
parents:
diff changeset
   578
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   579
    private String classFileName(final String className) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   580
        return className.replace('.', File.separatorChar) + ".class";
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
     * Generate an output File object to send the translet to
7f561c08de6b Initial load
duke
parents:
diff changeset
   585
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   586
    private File getOutputFile(String className) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   587
        if (_destDir != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   588
            return new File(_destDir, classFileName(className));
7f561c08de6b Initial load
duke
parents:
diff changeset
   589
        else
7f561c08de6b Initial load
duke
parents:
diff changeset
   590
            return new File(classFileName(className));
7f561c08de6b Initial load
duke
parents:
diff changeset
   591
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   592
7f561c08de6b Initial load
duke
parents:
diff changeset
   593
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   594
     * Set the destination directory for the translet.
7f561c08de6b Initial load
duke
parents:
diff changeset
   595
     * The current working directory will be used by default.
7f561c08de6b Initial load
duke
parents:
diff changeset
   596
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   597
    public boolean setDestDirectory(String dstDirName) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   598
        final File dir = new File(dstDirName);
16953
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   599
        if (SecuritySupport.getFileExists(dir) || dir.mkdirs()) {
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   600
            _destDir = dir;
7f561c08de6b Initial load
duke
parents:
diff changeset
   601
            return true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   602
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   603
        else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   604
            _destDir = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   605
            return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   606
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   607
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   608
7f561c08de6b Initial load
duke
parents:
diff changeset
   609
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   610
     * Set an optional package name for the translet and auxiliary classes
7f561c08de6b Initial load
duke
parents:
diff changeset
   611
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   612
    public void setPackageName(String packageName) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   613
        _packageName = packageName;
7f561c08de6b Initial load
duke
parents:
diff changeset
   614
        if (_className != null) setClassName(_className);
7f561c08de6b Initial load
duke
parents:
diff changeset
   615
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   616
7f561c08de6b Initial load
duke
parents:
diff changeset
   617
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   618
     * Set the name of an optional JAR-file to dump the translet and
7f561c08de6b Initial load
duke
parents:
diff changeset
   619
     * auxiliary classes to
7f561c08de6b Initial load
duke
parents:
diff changeset
   620
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   621
    public void setJarFileName(String jarFileName) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   622
        final String JAR_EXT = ".jar";
7f561c08de6b Initial load
duke
parents:
diff changeset
   623
        if (jarFileName.endsWith(JAR_EXT))
7f561c08de6b Initial load
duke
parents:
diff changeset
   624
            _jarFileName = jarFileName;
7f561c08de6b Initial load
duke
parents:
diff changeset
   625
        else
7f561c08de6b Initial load
duke
parents:
diff changeset
   626
            _jarFileName = jarFileName + JAR_EXT;
7f561c08de6b Initial load
duke
parents:
diff changeset
   627
        _outputType = JAR_OUTPUT;
7f561c08de6b Initial load
duke
parents:
diff changeset
   628
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   629
7f561c08de6b Initial load
duke
parents:
diff changeset
   630
    public String getJarFileName() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   631
        return _jarFileName;
7f561c08de6b Initial load
duke
parents:
diff changeset
   632
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   633
7f561c08de6b Initial load
duke
parents:
diff changeset
   634
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   635
     * Set the top-level stylesheet
7f561c08de6b Initial load
duke
parents:
diff changeset
   636
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   637
    public void setStylesheet(Stylesheet stylesheet) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   638
        if (_stylesheet == null) _stylesheet = stylesheet;
7f561c08de6b Initial load
duke
parents:
diff changeset
   639
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   640
7f561c08de6b Initial load
duke
parents:
diff changeset
   641
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   642
     * Returns the top-level stylesheet
7f561c08de6b Initial load
duke
parents:
diff changeset
   643
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   644
    public Stylesheet getStylesheet() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   645
        return _stylesheet;
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
     * Registers an attribute and gives it a type so that it can be mapped to
7f561c08de6b Initial load
duke
parents:
diff changeset
   650
     * DOM attribute types at run-time.
7f561c08de6b Initial load
duke
parents:
diff changeset
   651
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   652
    public int registerAttribute(QName name) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   653
        Integer code = (Integer)_attributes.get(name.toString());
7f561c08de6b Initial load
duke
parents:
diff changeset
   654
        if (code == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   655
            code = new Integer(_nextGType++);
7f561c08de6b Initial load
duke
parents:
diff changeset
   656
            _attributes.put(name.toString(), code);
7f561c08de6b Initial load
duke
parents:
diff changeset
   657
            final String uri = name.getNamespace();
7f561c08de6b Initial load
duke
parents:
diff changeset
   658
            final String local = "@"+name.getLocalPart();
7f561c08de6b Initial load
duke
parents:
diff changeset
   659
            if ((uri != null) && (!uri.equals("")))
7f561c08de6b Initial load
duke
parents:
diff changeset
   660
                _namesIndex.addElement(uri+":"+local);
7f561c08de6b Initial load
duke
parents:
diff changeset
   661
            else
7f561c08de6b Initial load
duke
parents:
diff changeset
   662
                _namesIndex.addElement(local);
7f561c08de6b Initial load
duke
parents:
diff changeset
   663
            if (name.getLocalPart().equals("*")) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   664
                registerNamespace(name.getNamespace());
7f561c08de6b Initial load
duke
parents:
diff changeset
   665
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   666
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   667
        return code.intValue();
7f561c08de6b Initial load
duke
parents:
diff changeset
   668
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   669
7f561c08de6b Initial load
duke
parents:
diff changeset
   670
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   671
     * Registers an element and gives it a type so that it can be mapped to
7f561c08de6b Initial load
duke
parents:
diff changeset
   672
     * DOM element types at run-time.
7f561c08de6b Initial load
duke
parents:
diff changeset
   673
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   674
    public int registerElement(QName name) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   675
        // Register element (full QName)
7f561c08de6b Initial load
duke
parents:
diff changeset
   676
        Integer code = (Integer)_elements.get(name.toString());
7f561c08de6b Initial load
duke
parents:
diff changeset
   677
        if (code == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   678
            _elements.put(name.toString(), code = new Integer(_nextGType++));
7f561c08de6b Initial load
duke
parents:
diff changeset
   679
            _namesIndex.addElement(name.toString());
7f561c08de6b Initial load
duke
parents:
diff changeset
   680
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   681
        if (name.getLocalPart().equals("*")) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   682
            registerNamespace(name.getNamespace());
7f561c08de6b Initial load
duke
parents:
diff changeset
   683
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   684
        return code.intValue();
7f561c08de6b Initial load
duke
parents:
diff changeset
   685
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   686
7f561c08de6b Initial load
duke
parents:
diff changeset
   687
     /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   688
      * Registers a namespace prefix and gives it a type so that it can be mapped to
7f561c08de6b Initial load
duke
parents:
diff changeset
   689
      * DOM namespace types at run-time.
7f561c08de6b Initial load
duke
parents:
diff changeset
   690
      */
7f561c08de6b Initial load
duke
parents:
diff changeset
   691
7f561c08de6b Initial load
duke
parents:
diff changeset
   692
    public int registerNamespacePrefix(QName name) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   693
7f561c08de6b Initial load
duke
parents:
diff changeset
   694
    Integer code = (Integer)_namespacePrefixes.get(name.toString());
7f561c08de6b Initial load
duke
parents:
diff changeset
   695
    if (code == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   696
        code = new Integer(_nextGType++);
7f561c08de6b Initial load
duke
parents:
diff changeset
   697
        _namespacePrefixes.put(name.toString(), code);
7f561c08de6b Initial load
duke
parents:
diff changeset
   698
        final String uri = name.getNamespace();
7f561c08de6b Initial load
duke
parents:
diff changeset
   699
        if ((uri != null) && (!uri.equals(""))){
7f561c08de6b Initial load
duke
parents:
diff changeset
   700
            // namespace::ext2:ped2 will be made empty in TypedNamespaceIterator
7f561c08de6b Initial load
duke
parents:
diff changeset
   701
            _namesIndex.addElement("?");
7f561c08de6b Initial load
duke
parents:
diff changeset
   702
        } else{
7f561c08de6b Initial load
duke
parents:
diff changeset
   703
           _namesIndex.addElement("?"+name.getLocalPart());
7f561c08de6b Initial load
duke
parents:
diff changeset
   704
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   705
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   706
    return code.intValue();
7f561c08de6b Initial load
duke
parents:
diff changeset
   707
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   708
7f561c08de6b Initial load
duke
parents:
diff changeset
   709
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   710
     * Registers a namespace and gives it a type so that it can be mapped to
7f561c08de6b Initial load
duke
parents:
diff changeset
   711
     * DOM namespace types at run-time.
7f561c08de6b Initial load
duke
parents:
diff changeset
   712
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   713
    public int registerNamespace(String namespaceURI) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   714
        Integer code = (Integer)_namespaces.get(namespaceURI);
7f561c08de6b Initial load
duke
parents:
diff changeset
   715
        if (code == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   716
            code = new Integer(_nextNSType++);
7f561c08de6b Initial load
duke
parents:
diff changeset
   717
            _namespaces.put(namespaceURI,code);
7f561c08de6b Initial load
duke
parents:
diff changeset
   718
            _namespaceIndex.addElement(namespaceURI);
7f561c08de6b Initial load
duke
parents:
diff changeset
   719
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   720
        return code.intValue();
7f561c08de6b Initial load
duke
parents:
diff changeset
   721
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   722
7f561c08de6b Initial load
duke
parents:
diff changeset
   723
    public int nextModeSerial() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   724
        return _modeSerial++;
7f561c08de6b Initial load
duke
parents:
diff changeset
   725
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   726
7f561c08de6b Initial load
duke
parents:
diff changeset
   727
    public int nextStylesheetSerial() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   728
        return _stylesheetSerial++;
7f561c08de6b Initial load
duke
parents:
diff changeset
   729
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   730
7f561c08de6b Initial load
duke
parents:
diff changeset
   731
    public int nextStepPatternSerial() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   732
        return _stepPatternSerial++;
7f561c08de6b Initial load
duke
parents:
diff changeset
   733
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   734
7f561c08de6b Initial load
duke
parents:
diff changeset
   735
    public int[] getNumberFieldIndexes() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   736
        return _numberFieldIndexes;
7f561c08de6b Initial load
duke
parents:
diff changeset
   737
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   738
7f561c08de6b Initial load
duke
parents:
diff changeset
   739
    public int nextHelperClassSerial() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   740
        return _helperClassSerial++;
7f561c08de6b Initial load
duke
parents:
diff changeset
   741
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   742
7f561c08de6b Initial load
duke
parents:
diff changeset
   743
    public int nextAttributeSetSerial() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   744
        return _attributeSetSerial++;
7f561c08de6b Initial load
duke
parents:
diff changeset
   745
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   746
7f561c08de6b Initial load
duke
parents:
diff changeset
   747
    public Vector getNamesIndex() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   748
        return _namesIndex;
7f561c08de6b Initial load
duke
parents:
diff changeset
   749
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   750
7f561c08de6b Initial load
duke
parents:
diff changeset
   751
    public Vector getNamespaceIndex() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   752
        return _namespaceIndex;
7f561c08de6b Initial load
duke
parents:
diff changeset
   753
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   754
7f561c08de6b Initial load
duke
parents:
diff changeset
   755
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   756
     * Returns a unique name for every helper class needed to
7f561c08de6b Initial load
duke
parents:
diff changeset
   757
     * execute a translet.
7f561c08de6b Initial load
duke
parents:
diff changeset
   758
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   759
    public String getHelperClassName() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   760
        return getClassName() + '$' + _helperClassSerial++;
7f561c08de6b Initial load
duke
parents:
diff changeset
   761
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   762
7f561c08de6b Initial load
duke
parents:
diff changeset
   763
    public void dumpClass(JavaClass clazz) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   764
7f561c08de6b Initial load
duke
parents:
diff changeset
   765
        if (_outputType == FILE_OUTPUT ||
7f561c08de6b Initial load
duke
parents:
diff changeset
   766
            _outputType == BYTEARRAY_AND_FILE_OUTPUT)
7f561c08de6b Initial load
duke
parents:
diff changeset
   767
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   768
            File outFile = getOutputFile(clazz.getClassName());
7f561c08de6b Initial load
duke
parents:
diff changeset
   769
            String parentDir = outFile.getParent();
7f561c08de6b Initial load
duke
parents:
diff changeset
   770
            if (parentDir != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   771
                File parentFile = new File(parentDir);
16953
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   772
                if (!SecuritySupport.getFileExists(parentFile))
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   773
                    parentFile.mkdirs();
7f561c08de6b Initial load
duke
parents:
diff changeset
   774
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   775
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   776
7f561c08de6b Initial load
duke
parents:
diff changeset
   777
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   778
            switch (_outputType) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   779
            case FILE_OUTPUT:
7f561c08de6b Initial load
duke
parents:
diff changeset
   780
                clazz.dump(
7f561c08de6b Initial load
duke
parents:
diff changeset
   781
                    new BufferedOutputStream(
7f561c08de6b Initial load
duke
parents:
diff changeset
   782
                        new FileOutputStream(
7f561c08de6b Initial load
duke
parents:
diff changeset
   783
                            getOutputFile(clazz.getClassName()))));
7f561c08de6b Initial load
duke
parents:
diff changeset
   784
                break;
7f561c08de6b Initial load
duke
parents:
diff changeset
   785
            case JAR_OUTPUT:
7f561c08de6b Initial load
duke
parents:
diff changeset
   786
                _bcelClasses.addElement(clazz);
7f561c08de6b Initial load
duke
parents:
diff changeset
   787
                break;
7f561c08de6b Initial load
duke
parents:
diff changeset
   788
            case BYTEARRAY_OUTPUT:
7f561c08de6b Initial load
duke
parents:
diff changeset
   789
            case BYTEARRAY_AND_FILE_OUTPUT:
7f561c08de6b Initial load
duke
parents:
diff changeset
   790
            case BYTEARRAY_AND_JAR_OUTPUT:
7f561c08de6b Initial load
duke
parents:
diff changeset
   791
            case CLASSLOADER_OUTPUT:
7f561c08de6b Initial load
duke
parents:
diff changeset
   792
                ByteArrayOutputStream out = new ByteArrayOutputStream(2048);
7f561c08de6b Initial load
duke
parents:
diff changeset
   793
                clazz.dump(out);
7f561c08de6b Initial load
duke
parents:
diff changeset
   794
                _classes.addElement(out.toByteArray());
7f561c08de6b Initial load
duke
parents:
diff changeset
   795
7f561c08de6b Initial load
duke
parents:
diff changeset
   796
                if (_outputType == BYTEARRAY_AND_FILE_OUTPUT)
7f561c08de6b Initial load
duke
parents:
diff changeset
   797
                  clazz.dump(new BufferedOutputStream(
7f561c08de6b Initial load
duke
parents:
diff changeset
   798
                        new FileOutputStream(getOutputFile(clazz.getClassName()))));
7f561c08de6b Initial load
duke
parents:
diff changeset
   799
                else if (_outputType == BYTEARRAY_AND_JAR_OUTPUT)
7f561c08de6b Initial load
duke
parents:
diff changeset
   800
                  _bcelClasses.addElement(clazz);
7f561c08de6b Initial load
duke
parents:
diff changeset
   801
7f561c08de6b Initial load
duke
parents:
diff changeset
   802
                break;
7f561c08de6b Initial load
duke
parents:
diff changeset
   803
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   804
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   805
        catch (Exception e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   806
            e.printStackTrace();
7f561c08de6b Initial load
duke
parents:
diff changeset
   807
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   808
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   809
7f561c08de6b Initial load
duke
parents:
diff changeset
   810
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   811
     * File separators are converted to forward slashes for ZIP files.
7f561c08de6b Initial load
duke
parents:
diff changeset
   812
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   813
    private String entryName(File f) throws IOException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   814
        return f.getName().replace(File.separatorChar, '/');
7f561c08de6b Initial load
duke
parents:
diff changeset
   815
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   816
7f561c08de6b Initial load
duke
parents:
diff changeset
   817
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   818
     * Generate output JAR-file and packages
7f561c08de6b Initial load
duke
parents:
diff changeset
   819
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   820
    public void outputToJar() throws IOException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   821
        // create the manifest
7f561c08de6b Initial load
duke
parents:
diff changeset
   822
        final Manifest manifest = new Manifest();
7f561c08de6b Initial load
duke
parents:
diff changeset
   823
        final java.util.jar.Attributes atrs = manifest.getMainAttributes();
7f561c08de6b Initial load
duke
parents:
diff changeset
   824
        atrs.put(java.util.jar.Attributes.Name.MANIFEST_VERSION,"1.2");
7f561c08de6b Initial load
duke
parents:
diff changeset
   825
7f561c08de6b Initial load
duke
parents:
diff changeset
   826
        final Map map = manifest.getEntries();
7f561c08de6b Initial load
duke
parents:
diff changeset
   827
        // create manifest
7f561c08de6b Initial load
duke
parents:
diff changeset
   828
        Enumeration classes = _bcelClasses.elements();
7f561c08de6b Initial load
duke
parents:
diff changeset
   829
        final String now = (new Date()).toString();
7f561c08de6b Initial load
duke
parents:
diff changeset
   830
        final java.util.jar.Attributes.Name dateAttr =
7f561c08de6b Initial load
duke
parents:
diff changeset
   831
            new java.util.jar.Attributes.Name("Date");
7f561c08de6b Initial load
duke
parents:
diff changeset
   832
        while (classes.hasMoreElements()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   833
            final JavaClass clazz = (JavaClass)classes.nextElement();
7f561c08de6b Initial load
duke
parents:
diff changeset
   834
            final String className = clazz.getClassName().replace('.','/');
7f561c08de6b Initial load
duke
parents:
diff changeset
   835
            final java.util.jar.Attributes attr = new java.util.jar.Attributes();
7f561c08de6b Initial load
duke
parents:
diff changeset
   836
            attr.put(dateAttr, now);
7f561c08de6b Initial load
duke
parents:
diff changeset
   837
            map.put(className+".class", attr);
7f561c08de6b Initial load
duke
parents:
diff changeset
   838
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   839
7f561c08de6b Initial load
duke
parents:
diff changeset
   840
        final File jarFile = new File(_destDir, _jarFileName);
7f561c08de6b Initial load
duke
parents:
diff changeset
   841
        final JarOutputStream jos =
7f561c08de6b Initial load
duke
parents:
diff changeset
   842
            new JarOutputStream(new FileOutputStream(jarFile), manifest);
7f561c08de6b Initial load
duke
parents:
diff changeset
   843
        classes = _bcelClasses.elements();
7f561c08de6b Initial load
duke
parents:
diff changeset
   844
        while (classes.hasMoreElements()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   845
            final JavaClass clazz = (JavaClass)classes.nextElement();
7f561c08de6b Initial load
duke
parents:
diff changeset
   846
            final String className = clazz.getClassName().replace('.','/');
7f561c08de6b Initial load
duke
parents:
diff changeset
   847
            jos.putNextEntry(new JarEntry(className+".class"));
7f561c08de6b Initial load
duke
parents:
diff changeset
   848
            final ByteArrayOutputStream out = new ByteArrayOutputStream(2048);
7f561c08de6b Initial load
duke
parents:
diff changeset
   849
            clazz.dump(out); // dump() closes it's output stream
7f561c08de6b Initial load
duke
parents:
diff changeset
   850
            out.writeTo(jos);
7f561c08de6b Initial load
duke
parents:
diff changeset
   851
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   852
        jos.close();
7f561c08de6b Initial load
duke
parents:
diff changeset
   853
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   854
7f561c08de6b Initial load
duke
parents:
diff changeset
   855
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   856
     * Turn debugging messages on/off
7f561c08de6b Initial load
duke
parents:
diff changeset
   857
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   858
    public void setDebug(boolean debug) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   859
        _debug = debug;
7f561c08de6b Initial load
duke
parents:
diff changeset
   860
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   861
7f561c08de6b Initial load
duke
parents:
diff changeset
   862
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   863
     * Get current debugging message setting
7f561c08de6b Initial load
duke
parents:
diff changeset
   864
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   865
    public boolean debug() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   866
        return _debug;
7f561c08de6b Initial load
duke
parents:
diff changeset
   867
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   868
7f561c08de6b Initial load
duke
parents:
diff changeset
   869
7f561c08de6b Initial load
duke
parents:
diff changeset
   870
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   871
     * Retrieve a string representation of the character data to be stored
7f561c08de6b Initial load
duke
parents:
diff changeset
   872
     * in the translet as a <code>char[]</code>.  There may be more than
7f561c08de6b Initial load
duke
parents:
diff changeset
   873
     * one such array required.
7f561c08de6b Initial load
duke
parents:
diff changeset
   874
     * @param index The index of the <code>char[]</code>.  Zero-based.
7f561c08de6b Initial load
duke
parents:
diff changeset
   875
     * @return String The character data to be stored in the corresponding
7f561c08de6b Initial load
duke
parents:
diff changeset
   876
     *               <code>char[]</code>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   877
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   878
    public String getCharacterData(int index) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   879
        return ((StringBuffer) m_characterData.elementAt(index)).toString();
7f561c08de6b Initial load
duke
parents:
diff changeset
   880
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   881
7f561c08de6b Initial load
duke
parents:
diff changeset
   882
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   883
     * Get the number of char[] arrays, thus far, that will be created to
7f561c08de6b Initial load
duke
parents:
diff changeset
   884
     * store literal text in the stylesheet.
7f561c08de6b Initial load
duke
parents:
diff changeset
   885
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   886
    public int getCharacterDataCount() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   887
        return (m_characterData != null) ? m_characterData.size() : 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
   888
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   889
7f561c08de6b Initial load
duke
parents:
diff changeset
   890
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   891
     * Add literal text to char arrays that will be used to store character
7f561c08de6b Initial load
duke
parents:
diff changeset
   892
     * data in the stylesheet.
7f561c08de6b Initial load
duke
parents:
diff changeset
   893
     * @param newData String data to be added to char arrays.
7f561c08de6b Initial load
duke
parents:
diff changeset
   894
     *                Pre-condition:  <code>newData.length() &le; 21845</code>
7f561c08de6b Initial load
duke
parents:
diff changeset
   895
     * @return int offset at which character data will be stored
7f561c08de6b Initial load
duke
parents:
diff changeset
   896
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   897
    public int addCharacterData(String newData) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   898
        StringBuffer currData;
7f561c08de6b Initial load
duke
parents:
diff changeset
   899
        if (m_characterData == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   900
            m_characterData = new Vector();
7f561c08de6b Initial load
duke
parents:
diff changeset
   901
            currData = new StringBuffer();
7f561c08de6b Initial load
duke
parents:
diff changeset
   902
            m_characterData.addElement(currData);
7f561c08de6b Initial load
duke
parents:
diff changeset
   903
        } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   904
            currData = (StringBuffer) m_characterData
7f561c08de6b Initial load
duke
parents:
diff changeset
   905
                                           .elementAt(m_characterData.size()-1);
7f561c08de6b Initial load
duke
parents:
diff changeset
   906
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   907
7f561c08de6b Initial load
duke
parents:
diff changeset
   908
        // Character data could take up to three-times as much space when
7f561c08de6b Initial load
duke
parents:
diff changeset
   909
        // written to the class file as UTF-8.  The maximum size for a
7f561c08de6b Initial load
duke
parents:
diff changeset
   910
        // constant is 65535/3.  If we exceed that,
7f561c08de6b Initial load
duke
parents:
diff changeset
   911
        // (We really should use some "bin packing".)
7f561c08de6b Initial load
duke
parents:
diff changeset
   912
        if (newData.length() + currData.length() > 21845) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   913
            currData = new StringBuffer();
7f561c08de6b Initial load
duke
parents:
diff changeset
   914
            m_characterData.addElement(currData);
7f561c08de6b Initial load
duke
parents:
diff changeset
   915
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   916
7f561c08de6b Initial load
duke
parents:
diff changeset
   917
        int newDataOffset = currData.length();
7f561c08de6b Initial load
duke
parents:
diff changeset
   918
        currData.append(newData);
7f561c08de6b Initial load
duke
parents:
diff changeset
   919
7f561c08de6b Initial load
duke
parents:
diff changeset
   920
        return newDataOffset;
7f561c08de6b Initial load
duke
parents:
diff changeset
   921
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   922
}