jaxp/src/com/sun/org/apache/xalan/internal/xsltc/compiler/SyntaxTreeNode.java
author joehw
Tue, 17 Apr 2012 11:17:59 -0700
changeset 12458 d601e4bba306
parent 12457 c348e06f0e82
permissions -rw-r--r--
7160380: Sync JDK8 with JAXP 1.4.5 Summary: bring JDK8 up to date to what we have in 7u4 Reviewed-by: lancea, 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: SyntaxTreeNode.java,v 1.6 2006/06/06 22:34:33 spericas 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.util.Enumeration;
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
import java.util.Hashtable;
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
import java.util.Vector;
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
import com.sun.org.apache.bcel.internal.generic.ANEWARRAY;
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
import com.sun.org.apache.bcel.internal.generic.BasicType;
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
import com.sun.org.apache.bcel.internal.generic.CHECKCAST;
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
import com.sun.org.apache.bcel.internal.generic.DUP_X1;
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
import com.sun.org.apache.bcel.internal.generic.GETFIELD;
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
import com.sun.org.apache.bcel.internal.generic.ICONST;
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
import com.sun.org.apache.bcel.internal.generic.INVOKEINTERFACE;
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
import com.sun.org.apache.bcel.internal.generic.INVOKESPECIAL;
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
import com.sun.org.apache.bcel.internal.generic.INVOKEVIRTUAL;
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
import com.sun.org.apache.bcel.internal.generic.InstructionList;
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
import com.sun.org.apache.bcel.internal.generic.NEW;
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
import com.sun.org.apache.bcel.internal.generic.NEWARRAY;
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
import com.sun.org.apache.bcel.internal.generic.PUSH;
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
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.MethodGenerator;
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
import com.sun.org.apache.xalan.internal.xsltc.DOM;
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util;
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
import org.xml.sax.Attributes;
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
import org.xml.sax.helpers.AttributesImpl;
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
 * @author Jacek Ambroziak
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
 * @author Santiago Pericas-Geertsen
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
 * @author G. Todd Miller
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
 * @author Morten Jorensen
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
 * @author Erwin Bolwidt <ejb@klomp.org>
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
 * @author John Howard <JohnH@schemasoft.com>
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
public abstract class SyntaxTreeNode implements Constants {
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
    // Reference to the AST parser
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
    private Parser _parser;
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
    // AST navigation pointers
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
    protected SyntaxTreeNode _parent;          // Parent node
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
    private Stylesheet       _stylesheet;      // Stylesheet ancestor node
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
    private Template         _template;        // Template ancestor node
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
    private final Vector _contents = new Vector(2); // Child nodes
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
    // Element description data
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
    protected QName _qname;                    // The element QName
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
    private int _line;                         // Source file line number
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
    protected AttributesImpl _attributes = null;   // Attributes of this element
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
    private   Hashtable _prefixMapping = null; // Namespace declarations
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
    // Sentinel - used to denote unrecognised syntaxt tree nodes.
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
    protected static final SyntaxTreeNode Dummy = new AbsolutePathPattern(null);
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
    // These two are used for indenting nodes in the AST (debug output)
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
    protected static final int IndentIncrement = 4;
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
    private static final char[] _spaces =
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
        "                                                       ".toCharArray();
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
     * Creates a new SyntaxTreeNode with a 'null' QName and no source file
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
     * line number reference.
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
    public SyntaxTreeNode() {
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
        _line = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
        _qname = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
     * Creates a new SyntaxTreeNode with a 'null' QName.
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
     * @param line Source file line number reference
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
    public SyntaxTreeNode(int line) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
        _line = line;
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
        _qname = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
     * Creates a new SyntaxTreeNode with no source file line number reference.
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
     * @param uri The element's namespace URI
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
     * @param prefix The element's namespace prefix
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
     * @param local The element's local name
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
    public SyntaxTreeNode(String uri, String prefix, String local) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
        _line = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
        setQName(uri, prefix, local);
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
     * Set the source file line number for this element
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
     * @param line The source file line number.
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
    protected final void setLineNumber(int line) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
        _line = line;
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
     * Get the source file line number for this element. If unavailable, lookup
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
     * in ancestors.
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
     * @return The source file line number.
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
    public final int getLineNumber() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
        if (_line > 0) return _line;
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
        SyntaxTreeNode parent = getParent();
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
        return (parent != null) ? parent.getLineNumber() : 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
     * Set the QName for the syntax tree node.
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
     * @param qname The QName for the syntax tree node
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
    protected void setQName(QName qname) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
        _qname = qname;
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
     * Set the QName for the SyntaxTreeNode
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
     * @param uri The element's namespace URI
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
     * @param prefix The element's namespace prefix
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
     * @param local The element's local name
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
    protected void setQName(String uri, String prefix, String localname) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
        _qname = new QName(uri, prefix, localname);
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
     * Set the QName for the SyntaxTreeNode
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
     * @param qname The QName for the syntax tree node
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
    protected QName getQName() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
        return(_qname);
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
     * Set the attributes for this SyntaxTreeNode.
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
     * @param attributes Attributes for the element. Must be passed in as an
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
     *                   implementation of org.xml.sax.Attributes.
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
    protected void setAttributes(AttributesImpl attributes) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
        _attributes = attributes;
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
     * Returns a value for an attribute from the source element.
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
     * @param qname The QName of the attribute to return.
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
     * @return The value of the attribute of name 'qname'.
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
    protected String getAttribute(String qname) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
        if (_attributes == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
            return EMPTYSTRING;
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   182
        final String value = _attributes.getValue(qname);
7f561c08de6b Initial load
duke
parents:
diff changeset
   183
        return (value == null || value.equals(EMPTYSTRING)) ?
7f561c08de6b Initial load
duke
parents:
diff changeset
   184
            EMPTYSTRING : value;
7f561c08de6b Initial load
duke
parents:
diff changeset
   185
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
7f561c08de6b Initial load
duke
parents:
diff changeset
   187
    protected String getAttribute(String prefix, String localName) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   188
        return getAttribute(prefix + ':' + localName);
7f561c08de6b Initial load
duke
parents:
diff changeset
   189
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   190
7f561c08de6b Initial load
duke
parents:
diff changeset
   191
    protected boolean hasAttribute(String qname) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   192
        return (_attributes != null && _attributes.getValue(qname) != null);
7f561c08de6b Initial load
duke
parents:
diff changeset
   193
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   194
7f561c08de6b Initial load
duke
parents:
diff changeset
   195
    protected void addAttribute(String qname, String value) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   196
        int index = _attributes.getIndex(qname);
7f561c08de6b Initial load
duke
parents:
diff changeset
   197
        if (index != -1) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   198
            _attributes.setAttribute(index, "", Util.getLocalName(qname),
7f561c08de6b Initial load
duke
parents:
diff changeset
   199
                    qname, "CDATA", value);
7f561c08de6b Initial load
duke
parents:
diff changeset
   200
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   201
        else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   202
            _attributes.addAttribute("", Util.getLocalName(qname), qname,
7f561c08de6b Initial load
duke
parents:
diff changeset
   203
                    "CDATA", value);
7f561c08de6b Initial load
duke
parents:
diff changeset
   204
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   205
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   206
7f561c08de6b Initial load
duke
parents:
diff changeset
   207
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   208
     * Returns a list of all attributes declared for the element represented by
7f561c08de6b Initial load
duke
parents:
diff changeset
   209
     * this syntax tree node.
7f561c08de6b Initial load
duke
parents:
diff changeset
   210
     * @return Attributes for this syntax tree node
7f561c08de6b Initial load
duke
parents:
diff changeset
   211
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   212
    protected Attributes getAttributes() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   213
        return(_attributes);
7f561c08de6b Initial load
duke
parents:
diff changeset
   214
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   215
7f561c08de6b Initial load
duke
parents:
diff changeset
   216
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   217
     * Sets the prefix mapping for the namespaces that were declared in this
7f561c08de6b Initial load
duke
parents:
diff changeset
   218
     * element. This does not include all prefix mappings in scope, so one
7f561c08de6b Initial load
duke
parents:
diff changeset
   219
     * may have to check ancestor elements to get all mappings that are in
7f561c08de6b Initial load
duke
parents:
diff changeset
   220
     * in scope. The prefixes must be passed in as a Hashtable that maps
7f561c08de6b Initial load
duke
parents:
diff changeset
   221
     * namespace prefixes (String objects) to namespace URIs (also String).
7f561c08de6b Initial load
duke
parents:
diff changeset
   222
     * @param mapping The Hashtable containing the mappings.
7f561c08de6b Initial load
duke
parents:
diff changeset
   223
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   224
    protected void setPrefixMapping(Hashtable mapping) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   225
        _prefixMapping = mapping;
7f561c08de6b Initial load
duke
parents:
diff changeset
   226
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   227
7f561c08de6b Initial load
duke
parents:
diff changeset
   228
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   229
     * Returns a Hashtable containing the prefix mappings that were declared
7f561c08de6b Initial load
duke
parents:
diff changeset
   230
     * for this element. This does not include all prefix mappings in scope,
7f561c08de6b Initial load
duke
parents:
diff changeset
   231
     * so one may have to check ancestor elements to get all mappings that are
7f561c08de6b Initial load
duke
parents:
diff changeset
   232
     * in in scope.
7f561c08de6b Initial load
duke
parents:
diff changeset
   233
     * @return Prefix mappings (for this element only).
7f561c08de6b Initial load
duke
parents:
diff changeset
   234
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   235
    protected Hashtable getPrefixMapping() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   236
        return _prefixMapping;
7f561c08de6b Initial load
duke
parents:
diff changeset
   237
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   238
7f561c08de6b Initial load
duke
parents:
diff changeset
   239
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   240
     * Adds a single prefix mapping to this syntax tree node.
7f561c08de6b Initial load
duke
parents:
diff changeset
   241
     * @param prefix Namespace prefix.
7f561c08de6b Initial load
duke
parents:
diff changeset
   242
     * @param uri Namespace URI.
7f561c08de6b Initial load
duke
parents:
diff changeset
   243
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   244
    protected void addPrefixMapping(String prefix, String uri) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   245
        if (_prefixMapping == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   246
            _prefixMapping = new Hashtable();
7f561c08de6b Initial load
duke
parents:
diff changeset
   247
        _prefixMapping.put(prefix, uri);
7f561c08de6b Initial load
duke
parents:
diff changeset
   248
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   249
7f561c08de6b Initial load
duke
parents:
diff changeset
   250
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   251
     * Returns any namespace URI that is in scope for a given prefix. This
7f561c08de6b Initial load
duke
parents:
diff changeset
   252
     * method checks namespace mappings for this element, and if necessary
7f561c08de6b Initial load
duke
parents:
diff changeset
   253
     * for ancestor elements as well (ie. if the prefix maps to an URI in this
7f561c08de6b Initial load
duke
parents:
diff changeset
   254
     * scope then you'll definately get the URI from this method).
7f561c08de6b Initial load
duke
parents:
diff changeset
   255
     * @param prefix Namespace prefix.
7f561c08de6b Initial load
duke
parents:
diff changeset
   256
     * @return Namespace URI.
7f561c08de6b Initial load
duke
parents:
diff changeset
   257
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   258
    protected String lookupNamespace(String prefix) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   259
        // Initialise the output (default is 'null' for undefined)
7f561c08de6b Initial load
duke
parents:
diff changeset
   260
        String uri = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   261
7f561c08de6b Initial load
duke
parents:
diff changeset
   262
        // First look up the prefix/uri mapping in our own hashtable...
7f561c08de6b Initial load
duke
parents:
diff changeset
   263
        if (_prefixMapping != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   264
            uri = (String)_prefixMapping.get(prefix);
7f561c08de6b Initial load
duke
parents:
diff changeset
   265
        // ... but if we can't find it there we ask our parent for the mapping
7f561c08de6b Initial load
duke
parents:
diff changeset
   266
        if ((uri == null) && (_parent != null)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   267
            uri = _parent.lookupNamespace(prefix);
7f561c08de6b Initial load
duke
parents:
diff changeset
   268
            if ((prefix == Constants.EMPTYSTRING) && (uri == null))
7f561c08de6b Initial load
duke
parents:
diff changeset
   269
                uri = Constants.EMPTYSTRING;
7f561c08de6b Initial load
duke
parents:
diff changeset
   270
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   271
        // ... and then we return whatever URI we've got.
7f561c08de6b Initial load
duke
parents:
diff changeset
   272
        return(uri);
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
     * Returns any namespace prefix that is mapped to a prefix in the current
7f561c08de6b Initial load
duke
parents:
diff changeset
   277
     * scope. This method checks namespace mappings for this element, and if
7f561c08de6b Initial load
duke
parents:
diff changeset
   278
     * necessary for ancestor elements as well (ie. if the URI is declared
7f561c08de6b Initial load
duke
parents:
diff changeset
   279
     * within the current scope then you'll definately get the prefix from
7f561c08de6b Initial load
duke
parents:
diff changeset
   280
     * this method). Note that this is a very slow method and consequentially
7f561c08de6b Initial load
duke
parents:
diff changeset
   281
     * it should only be used strictly when needed.
7f561c08de6b Initial load
duke
parents:
diff changeset
   282
     * @param uri Namespace URI.
7f561c08de6b Initial load
duke
parents:
diff changeset
   283
     * @return Namespace prefix.
7f561c08de6b Initial load
duke
parents:
diff changeset
   284
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   285
    protected String lookupPrefix(String uri) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   286
        // Initialise the output (default is 'null' for undefined)
7f561c08de6b Initial load
duke
parents:
diff changeset
   287
        String prefix = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   288
7f561c08de6b Initial load
duke
parents:
diff changeset
   289
        // First look up the prefix/uri mapping in our own hashtable...
7f561c08de6b Initial load
duke
parents:
diff changeset
   290
        if ((_prefixMapping != null) &&
7f561c08de6b Initial load
duke
parents:
diff changeset
   291
            (_prefixMapping.contains(uri))) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   292
            Enumeration prefixes = _prefixMapping.keys();
7f561c08de6b Initial load
duke
parents:
diff changeset
   293
            while (prefixes.hasMoreElements()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   294
                prefix = (String)prefixes.nextElement();
7f561c08de6b Initial load
duke
parents:
diff changeset
   295
                String mapsTo = (String)_prefixMapping.get(prefix);
7f561c08de6b Initial load
duke
parents:
diff changeset
   296
                if (mapsTo.equals(uri)) return(prefix);
7f561c08de6b Initial load
duke
parents:
diff changeset
   297
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   298
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   299
        // ... but if we can't find it there we ask our parent for the mapping
7f561c08de6b Initial load
duke
parents:
diff changeset
   300
        else if (_parent != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   301
            prefix = _parent.lookupPrefix(uri);
7f561c08de6b Initial load
duke
parents:
diff changeset
   302
            if ((uri == Constants.EMPTYSTRING) && (prefix == null))
7f561c08de6b Initial load
duke
parents:
diff changeset
   303
                prefix = Constants.EMPTYSTRING;
7f561c08de6b Initial load
duke
parents:
diff changeset
   304
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   305
        return(prefix);
7f561c08de6b Initial load
duke
parents:
diff changeset
   306
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   307
7f561c08de6b Initial load
duke
parents:
diff changeset
   308
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   309
     * Set this node's parser. The parser (the XSLT parser) gives this
7f561c08de6b Initial load
duke
parents:
diff changeset
   310
     * syntax tree node access to the symbol table and XPath parser.
7f561c08de6b Initial load
duke
parents:
diff changeset
   311
     * @param parser The XSLT parser.
7f561c08de6b Initial load
duke
parents:
diff changeset
   312
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   313
    protected void setParser(Parser parser) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   314
        _parser = parser;
7f561c08de6b Initial load
duke
parents:
diff changeset
   315
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   316
7f561c08de6b Initial load
duke
parents:
diff changeset
   317
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   318
     * Returns this node's XSLT parser.
7f561c08de6b Initial load
duke
parents:
diff changeset
   319
     * @return The XSLT parser.
7f561c08de6b Initial load
duke
parents:
diff changeset
   320
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   321
    public final Parser getParser() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   322
        return _parser;
7f561c08de6b Initial load
duke
parents:
diff changeset
   323
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   324
7f561c08de6b Initial load
duke
parents:
diff changeset
   325
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   326
     * Set this syntax tree node's parent node, if unset. For
7f561c08de6b Initial load
duke
parents:
diff changeset
   327
     * re-parenting just use <code>node._parent = newparent</code>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   328
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   329
     * @param parent The parent node.
7f561c08de6b Initial load
duke
parents:
diff changeset
   330
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   331
    protected void setParent(SyntaxTreeNode parent) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   332
        if (_parent == null) _parent = parent;
7f561c08de6b Initial load
duke
parents:
diff changeset
   333
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   334
7f561c08de6b Initial load
duke
parents:
diff changeset
   335
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   336
     * Returns this syntax tree node's parent node.
7f561c08de6b Initial load
duke
parents:
diff changeset
   337
     * @return The parent syntax tree node.
7f561c08de6b Initial load
duke
parents:
diff changeset
   338
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   339
    protected final SyntaxTreeNode getParent() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   340
        return _parent;
7f561c08de6b Initial load
duke
parents:
diff changeset
   341
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   342
7f561c08de6b Initial load
duke
parents:
diff changeset
   343
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   344
     * Returns 'true' if this syntax tree node is the Sentinal node.
7f561c08de6b Initial load
duke
parents:
diff changeset
   345
     * @return 'true' if this syntax tree node is the Sentinal node.
7f561c08de6b Initial load
duke
parents:
diff changeset
   346
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   347
    protected final boolean isDummy() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   348
        return this == Dummy;
7f561c08de6b Initial load
duke
parents:
diff changeset
   349
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   350
7f561c08de6b Initial load
duke
parents:
diff changeset
   351
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   352
     * Get the import precedence of this element. The import precedence equals
7f561c08de6b Initial load
duke
parents:
diff changeset
   353
     * the import precedence of the stylesheet in which this element occured.
7f561c08de6b Initial load
duke
parents:
diff changeset
   354
     * @return The import precedence of this syntax tree node.
7f561c08de6b Initial load
duke
parents:
diff changeset
   355
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   356
    protected int getImportPrecedence() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   357
        Stylesheet stylesheet = getStylesheet();
7f561c08de6b Initial load
duke
parents:
diff changeset
   358
        if (stylesheet == null) return Integer.MIN_VALUE;
7f561c08de6b Initial load
duke
parents:
diff changeset
   359
        return stylesheet.getImportPrecedence();
7f561c08de6b Initial load
duke
parents:
diff changeset
   360
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   361
7f561c08de6b Initial load
duke
parents:
diff changeset
   362
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   363
     * Get the Stylesheet node that represents the <xsl:stylesheet/> element
7f561c08de6b Initial load
duke
parents:
diff changeset
   364
     * that this node occured under.
7f561c08de6b Initial load
duke
parents:
diff changeset
   365
     * @return The Stylesheet ancestor node of this node.
7f561c08de6b Initial load
duke
parents:
diff changeset
   366
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   367
    public Stylesheet getStylesheet() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   368
        if (_stylesheet == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   369
            SyntaxTreeNode parent = this;
7f561c08de6b Initial load
duke
parents:
diff changeset
   370
            while (parent != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   371
                if (parent instanceof Stylesheet)
7f561c08de6b Initial load
duke
parents:
diff changeset
   372
                    return((Stylesheet)parent);
7f561c08de6b Initial load
duke
parents:
diff changeset
   373
                parent = parent.getParent();
7f561c08de6b Initial load
duke
parents:
diff changeset
   374
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   375
            _stylesheet = (Stylesheet)parent;
7f561c08de6b Initial load
duke
parents:
diff changeset
   376
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   377
        return(_stylesheet);
7f561c08de6b Initial load
duke
parents:
diff changeset
   378
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   379
7f561c08de6b Initial load
duke
parents:
diff changeset
   380
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   381
     * Get the Template node that represents the <xsl:template/> element
7f561c08de6b Initial load
duke
parents:
diff changeset
   382
     * that this node occured under. Note that this method will return 'null'
7f561c08de6b Initial load
duke
parents:
diff changeset
   383
     * for nodes that represent top-level elements.
7f561c08de6b Initial load
duke
parents:
diff changeset
   384
     * @return The Template ancestor node of this node or 'null'.
7f561c08de6b Initial load
duke
parents:
diff changeset
   385
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   386
    protected Template getTemplate() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   387
        if (_template == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   388
            SyntaxTreeNode parent = this;
7f561c08de6b Initial load
duke
parents:
diff changeset
   389
            while ((parent != null) && (!(parent instanceof Template)))
7f561c08de6b Initial load
duke
parents:
diff changeset
   390
                parent = parent.getParent();
7f561c08de6b Initial load
duke
parents:
diff changeset
   391
            _template = (Template)parent;
7f561c08de6b Initial load
duke
parents:
diff changeset
   392
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   393
        return(_template);
7f561c08de6b Initial load
duke
parents:
diff changeset
   394
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   395
7f561c08de6b Initial load
duke
parents:
diff changeset
   396
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   397
     * Returns a reference to the XSLTC (XSLT compiler) in use.
7f561c08de6b Initial load
duke
parents:
diff changeset
   398
     * @return XSLTC - XSLT compiler.
7f561c08de6b Initial load
duke
parents:
diff changeset
   399
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   400
    protected final XSLTC getXSLTC() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   401
        return _parser.getXSLTC();
7f561c08de6b Initial load
duke
parents:
diff changeset
   402
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   403
7f561c08de6b Initial load
duke
parents:
diff changeset
   404
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   405
     * Returns the XSLT parser's symbol table.
7f561c08de6b Initial load
duke
parents:
diff changeset
   406
     * @return Symbol table.
7f561c08de6b Initial load
duke
parents:
diff changeset
   407
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   408
    protected final SymbolTable getSymbolTable() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   409
        return (_parser == null) ? null : _parser.getSymbolTable();
7f561c08de6b Initial load
duke
parents:
diff changeset
   410
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   411
7f561c08de6b Initial load
duke
parents:
diff changeset
   412
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   413
     * Parse the contents of this syntax tree nodes (child nodes, XPath
7f561c08de6b Initial load
duke
parents:
diff changeset
   414
     * expressions, patterns and functions). The default behaviour is to parser
7f561c08de6b Initial load
duke
parents:
diff changeset
   415
     * the syntax tree node's children (since there are no common expressions,
7f561c08de6b Initial load
duke
parents:
diff changeset
   416
     * patterns, etc. that can be handled in this base class.
7f561c08de6b Initial load
duke
parents:
diff changeset
   417
     * @param parser reference to the XSLT parser
7f561c08de6b Initial load
duke
parents:
diff changeset
   418
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   419
    public void parseContents(Parser parser) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   420
        parseChildren(parser);
7f561c08de6b Initial load
duke
parents:
diff changeset
   421
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   422
7f561c08de6b Initial load
duke
parents:
diff changeset
   423
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   424
     * Parse all children of this syntax tree node. This method is normally
7f561c08de6b Initial load
duke
parents:
diff changeset
   425
     * called by the parseContents() method.
7f561c08de6b Initial load
duke
parents:
diff changeset
   426
     * @param parser reference to the XSLT parser
7f561c08de6b Initial load
duke
parents:
diff changeset
   427
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   428
    protected final void parseChildren(Parser parser) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   429
7f561c08de6b Initial load
duke
parents:
diff changeset
   430
        Vector locals = null;   // only create when needed
7f561c08de6b Initial load
duke
parents:
diff changeset
   431
7f561c08de6b Initial load
duke
parents:
diff changeset
   432
        final int count = _contents.size();
7f561c08de6b Initial load
duke
parents:
diff changeset
   433
        for (int i=0; i<count; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   434
            SyntaxTreeNode child = (SyntaxTreeNode)_contents.elementAt(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
   435
            parser.getSymbolTable().setCurrentNode(child);
7f561c08de6b Initial load
duke
parents:
diff changeset
   436
            child.parseContents(parser);
7f561c08de6b Initial load
duke
parents:
diff changeset
   437
            // if variable or parameter, add it to scope
7f561c08de6b Initial load
duke
parents:
diff changeset
   438
            final QName varOrParamName = updateScope(parser, child);
7f561c08de6b Initial load
duke
parents:
diff changeset
   439
            if (varOrParamName != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   440
                if (locals == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   441
                    locals = new Vector(2);
7f561c08de6b Initial load
duke
parents:
diff changeset
   442
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   443
                locals.addElement(varOrParamName);
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
        parser.getSymbolTable().setCurrentNode(this);
7f561c08de6b Initial load
duke
parents:
diff changeset
   448
7f561c08de6b Initial load
duke
parents:
diff changeset
   449
        // after the last element, remove any locals from scope
7f561c08de6b Initial load
duke
parents:
diff changeset
   450
        if (locals != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   451
            final int nLocals = locals.size();
7f561c08de6b Initial load
duke
parents:
diff changeset
   452
            for (int i = 0; i < nLocals; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   453
                parser.removeVariable((QName)locals.elementAt(i));
7f561c08de6b Initial load
duke
parents:
diff changeset
   454
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   455
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   456
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   457
7f561c08de6b Initial load
duke
parents:
diff changeset
   458
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   459
     * Add a node to the current scope and return name of a variable or
7f561c08de6b Initial load
duke
parents:
diff changeset
   460
     * parameter if the node represents a variable or a parameter.
7f561c08de6b Initial load
duke
parents:
diff changeset
   461
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   462
    protected QName updateScope(Parser parser, SyntaxTreeNode node) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   463
        if (node instanceof Variable) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   464
            final Variable var = (Variable)node;
7f561c08de6b Initial load
duke
parents:
diff changeset
   465
            parser.addVariable(var);
7f561c08de6b Initial load
duke
parents:
diff changeset
   466
            return var.getName();
7f561c08de6b Initial load
duke
parents:
diff changeset
   467
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   468
        else if (node instanceof Param) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   469
            final Param param = (Param)node;
7f561c08de6b Initial load
duke
parents:
diff changeset
   470
            parser.addParameter(param);
7f561c08de6b Initial load
duke
parents:
diff changeset
   471
            return param.getName();
7f561c08de6b Initial load
duke
parents:
diff changeset
   472
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   473
        else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   474
            return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   475
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   476
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   477
7f561c08de6b Initial load
duke
parents:
diff changeset
   478
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   479
     * Type check the children of this node. The type check phase may add
7f561c08de6b Initial load
duke
parents:
diff changeset
   480
     * coercions (CastExpr) to the AST.
7f561c08de6b Initial load
duke
parents:
diff changeset
   481
     * @param stable The compiler/parser's symbol table
7f561c08de6b Initial load
duke
parents:
diff changeset
   482
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   483
    public abstract Type typeCheck(SymbolTable stable) throws TypeCheckError;
7f561c08de6b Initial load
duke
parents:
diff changeset
   484
7f561c08de6b Initial load
duke
parents:
diff changeset
   485
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   486
     * Call typeCheck() on all child syntax tree nodes.
7f561c08de6b Initial load
duke
parents:
diff changeset
   487
     * @param stable The compiler/parser's symbol table
7f561c08de6b Initial load
duke
parents:
diff changeset
   488
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   489
    protected Type typeCheckContents(SymbolTable stable) throws TypeCheckError {
7f561c08de6b Initial load
duke
parents:
diff changeset
   490
        final int n = elementCount();
7f561c08de6b Initial load
duke
parents:
diff changeset
   491
        for (int i = 0; i < n; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   492
            SyntaxTreeNode item = (SyntaxTreeNode)_contents.elementAt(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
   493
            item.typeCheck(stable);
7f561c08de6b Initial load
duke
parents:
diff changeset
   494
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   495
        return Type.Void;
7f561c08de6b Initial load
duke
parents:
diff changeset
   496
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   497
7f561c08de6b Initial load
duke
parents:
diff changeset
   498
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   499
     * Translate this abstract syntax tree node into JVM bytecodes.
7f561c08de6b Initial load
duke
parents:
diff changeset
   500
     * @param classGen BCEL Java class generator
7f561c08de6b Initial load
duke
parents:
diff changeset
   501
     * @param methodGen BCEL Java method generator
7f561c08de6b Initial load
duke
parents:
diff changeset
   502
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   503
    public abstract void translate(ClassGenerator classGen,
7f561c08de6b Initial load
duke
parents:
diff changeset
   504
                                   MethodGenerator methodGen);
7f561c08de6b Initial load
duke
parents:
diff changeset
   505
7f561c08de6b Initial load
duke
parents:
diff changeset
   506
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   507
     * Call translate() on all child syntax tree nodes.
7f561c08de6b Initial load
duke
parents:
diff changeset
   508
     * @param classGen BCEL Java class generator
7f561c08de6b Initial load
duke
parents:
diff changeset
   509
     * @param methodGen BCEL Java method generator
7f561c08de6b Initial load
duke
parents:
diff changeset
   510
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   511
    protected void translateContents(ClassGenerator classGen,
7f561c08de6b Initial load
duke
parents:
diff changeset
   512
                                     MethodGenerator methodGen) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   513
        // Call translate() on all child nodes
7f561c08de6b Initial load
duke
parents:
diff changeset
   514
        final int n = elementCount();
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   515
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   516
        for (int i = 0; i < n; i++) {
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   517
            methodGen.markChunkStart();
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   518
            final SyntaxTreeNode item = (SyntaxTreeNode)_contents.elementAt(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
   519
            item.translate(classGen, methodGen);
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   520
            methodGen.markChunkEnd();
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   521
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   522
7f561c08de6b Initial load
duke
parents:
diff changeset
   523
        // After translation, unmap any registers for any variables/parameters
7f561c08de6b Initial load
duke
parents:
diff changeset
   524
        // that were declared in this scope. Performing this unmapping in the
7f561c08de6b Initial load
duke
parents:
diff changeset
   525
        // same AST scope as the declaration deals with the problems of
7f561c08de6b Initial load
duke
parents:
diff changeset
   526
        // references falling out-of-scope inside the for-each element.
7f561c08de6b Initial load
duke
parents:
diff changeset
   527
        // (the cause of which being 'lazy' register allocation for references)
7f561c08de6b Initial load
duke
parents:
diff changeset
   528
        for (int i = 0; i < n; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   529
            if( _contents.elementAt(i) instanceof VariableBase) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   530
                final VariableBase var = (VariableBase)_contents.elementAt(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
   531
                var.unmapRegister(methodGen);
7f561c08de6b Initial load
duke
parents:
diff changeset
   532
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   533
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   534
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   535
7f561c08de6b Initial load
duke
parents:
diff changeset
   536
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   537
     * Return true if the node represents a simple RTF.
7f561c08de6b Initial load
duke
parents:
diff changeset
   538
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   539
     * A node is a simple RTF if all children only produce Text value.
7f561c08de6b Initial load
duke
parents:
diff changeset
   540
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   541
     * @param node A node
7f561c08de6b Initial load
duke
parents:
diff changeset
   542
     * @return true if the node content can be considered as a simple RTF.
7f561c08de6b Initial load
duke
parents:
diff changeset
   543
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   544
    private boolean isSimpleRTF(SyntaxTreeNode node) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   545
7f561c08de6b Initial load
duke
parents:
diff changeset
   546
        Vector contents = node.getContents();
7f561c08de6b Initial load
duke
parents:
diff changeset
   547
        for (int i = 0; i < contents.size(); i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   548
            SyntaxTreeNode item = (SyntaxTreeNode)contents.elementAt(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
   549
            if (!isTextElement(item, false))
7f561c08de6b Initial load
duke
parents:
diff changeset
   550
                return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   551
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   552
7f561c08de6b Initial load
duke
parents:
diff changeset
   553
        return true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   554
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   555
7f561c08de6b Initial load
duke
parents:
diff changeset
   556
     /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   557
     * Return true if the node represents an adaptive RTF.
7f561c08de6b Initial load
duke
parents:
diff changeset
   558
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   559
     * A node is an adaptive RTF if each children is a Text element
7f561c08de6b Initial load
duke
parents:
diff changeset
   560
     * or it is <xsl:call-template> or <xsl:apply-templates>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   561
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   562
     * @param node A node
7f561c08de6b Initial load
duke
parents:
diff changeset
   563
     * @return true if the node content can be considered as an adaptive RTF.
7f561c08de6b Initial load
duke
parents:
diff changeset
   564
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   565
    private boolean isAdaptiveRTF(SyntaxTreeNode node) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   566
7f561c08de6b Initial load
duke
parents:
diff changeset
   567
        Vector contents = node.getContents();
7f561c08de6b Initial load
duke
parents:
diff changeset
   568
        for (int i = 0; i < contents.size(); i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   569
            SyntaxTreeNode item = (SyntaxTreeNode)contents.elementAt(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
   570
            if (!isTextElement(item, true))
7f561c08de6b Initial load
duke
parents:
diff changeset
   571
                return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   572
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   573
7f561c08de6b Initial load
duke
parents:
diff changeset
   574
        return true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   575
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   576
7f561c08de6b Initial load
duke
parents:
diff changeset
   577
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   578
     * Return true if the node only produces Text content.
7f561c08de6b Initial load
duke
parents:
diff changeset
   579
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   580
     * A node is a Text element if it is Text, xsl:value-of, xsl:number,
7f561c08de6b Initial load
duke
parents:
diff changeset
   581
     * or a combination of these nested in a control instruction (xsl:if or
7f561c08de6b Initial load
duke
parents:
diff changeset
   582
     * xsl:choose).
7f561c08de6b Initial load
duke
parents:
diff changeset
   583
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   584
     * If the doExtendedCheck flag is true, xsl:call-template and xsl:apply-templates
7f561c08de6b Initial load
duke
parents:
diff changeset
   585
     * are also considered as Text elements.
7f561c08de6b Initial load
duke
parents:
diff changeset
   586
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   587
     * @param node A node
7f561c08de6b Initial load
duke
parents:
diff changeset
   588
     * @param doExtendedCheck If this flag is true, <xsl:call-template> and
7f561c08de6b Initial load
duke
parents:
diff changeset
   589
     * <xsl:apply-templates> are also considered as Text elements.
7f561c08de6b Initial load
duke
parents:
diff changeset
   590
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   591
     * @return true if the node of Text type
7f561c08de6b Initial load
duke
parents:
diff changeset
   592
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   593
    private boolean isTextElement(SyntaxTreeNode node, boolean doExtendedCheck) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   594
        if (node instanceof ValueOf || node instanceof Number
7f561c08de6b Initial load
duke
parents:
diff changeset
   595
            || node instanceof Text)
7f561c08de6b Initial load
duke
parents:
diff changeset
   596
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   597
            return true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   598
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   599
        else if (node instanceof If) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   600
            return doExtendedCheck ? isAdaptiveRTF(node) : isSimpleRTF(node);
7f561c08de6b Initial load
duke
parents:
diff changeset
   601
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   602
        else if (node instanceof Choose) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   603
            Vector contents = node.getContents();
7f561c08de6b Initial load
duke
parents:
diff changeset
   604
            for (int i = 0; i < contents.size(); i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   605
                SyntaxTreeNode item = (SyntaxTreeNode)contents.elementAt(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
   606
                if (item instanceof Text ||
7f561c08de6b Initial load
duke
parents:
diff changeset
   607
                     ((item instanceof When || item instanceof Otherwise)
7f561c08de6b Initial load
duke
parents:
diff changeset
   608
                     && ((doExtendedCheck && isAdaptiveRTF(item))
7f561c08de6b Initial load
duke
parents:
diff changeset
   609
                         || (!doExtendedCheck && isSimpleRTF(item)))))
7f561c08de6b Initial load
duke
parents:
diff changeset
   610
                    continue;
7f561c08de6b Initial load
duke
parents:
diff changeset
   611
                else
7f561c08de6b Initial load
duke
parents:
diff changeset
   612
                    return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   613
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   614
            return true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   615
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   616
        else if (doExtendedCheck &&
7f561c08de6b Initial load
duke
parents:
diff changeset
   617
                  (node instanceof CallTemplate
7f561c08de6b Initial load
duke
parents:
diff changeset
   618
                   || node instanceof ApplyTemplates))
7f561c08de6b Initial load
duke
parents:
diff changeset
   619
            return true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   620
        else
7f561c08de6b Initial load
duke
parents:
diff changeset
   621
            return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   622
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   623
7f561c08de6b Initial load
duke
parents:
diff changeset
   624
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   625
     * Utility method used by parameters and variables to store result trees
7f561c08de6b Initial load
duke
parents:
diff changeset
   626
     * @param classGen BCEL Java class generator
7f561c08de6b Initial load
duke
parents:
diff changeset
   627
     * @param methodGen BCEL Java method generator
7f561c08de6b Initial load
duke
parents:
diff changeset
   628
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   629
    protected void compileResultTree(ClassGenerator classGen,
7f561c08de6b Initial load
duke
parents:
diff changeset
   630
                                     MethodGenerator methodGen)
7f561c08de6b Initial load
duke
parents:
diff changeset
   631
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   632
        final ConstantPoolGen cpg = classGen.getConstantPool();
7f561c08de6b Initial load
duke
parents:
diff changeset
   633
        final InstructionList il = methodGen.getInstructionList();
7f561c08de6b Initial load
duke
parents:
diff changeset
   634
        final Stylesheet stylesheet = classGen.getStylesheet();
7f561c08de6b Initial load
duke
parents:
diff changeset
   635
7f561c08de6b Initial load
duke
parents:
diff changeset
   636
        boolean isSimple = isSimpleRTF(this);
7f561c08de6b Initial load
duke
parents:
diff changeset
   637
        boolean isAdaptive = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   638
        if (!isSimple) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   639
            isAdaptive = isAdaptiveRTF(this);
7f561c08de6b Initial load
duke
parents:
diff changeset
   640
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   641
7f561c08de6b Initial load
duke
parents:
diff changeset
   642
        int rtfType = isSimple ? DOM.SIMPLE_RTF
7f561c08de6b Initial load
duke
parents:
diff changeset
   643
                               : (isAdaptive ? DOM.ADAPTIVE_RTF : DOM.TREE_RTF);
7f561c08de6b Initial load
duke
parents:
diff changeset
   644
7f561c08de6b Initial load
duke
parents:
diff changeset
   645
        // Save the current handler base on the stack
7f561c08de6b Initial load
duke
parents:
diff changeset
   646
        il.append(methodGen.loadHandler());
7f561c08de6b Initial load
duke
parents:
diff changeset
   647
7f561c08de6b Initial load
duke
parents:
diff changeset
   648
        final String DOM_CLASS = classGen.getDOMClass();
7f561c08de6b Initial load
duke
parents:
diff changeset
   649
7f561c08de6b Initial load
duke
parents:
diff changeset
   650
        // Create new instance of DOM class (with RTF_INITIAL_SIZE nodes)
7f561c08de6b Initial load
duke
parents:
diff changeset
   651
        //int index = cpg.addMethodref(DOM_IMPL, "<init>", "(I)V");
7f561c08de6b Initial load
duke
parents:
diff changeset
   652
        //il.append(new NEW(cpg.addClass(DOM_IMPL)));
7f561c08de6b Initial load
duke
parents:
diff changeset
   653
7f561c08de6b Initial load
duke
parents:
diff changeset
   654
        il.append(methodGen.loadDOM());
7f561c08de6b Initial load
duke
parents:
diff changeset
   655
        int index = cpg.addInterfaceMethodref(DOM_INTF,
7f561c08de6b Initial load
duke
parents:
diff changeset
   656
                                 "getResultTreeFrag",
7f561c08de6b Initial load
duke
parents:
diff changeset
   657
                                 "(IIZ)" + DOM_INTF_SIG);
7f561c08de6b Initial load
duke
parents:
diff changeset
   658
        il.append(new PUSH(cpg, RTF_INITIAL_SIZE));
7f561c08de6b Initial load
duke
parents:
diff changeset
   659
        il.append(new PUSH(cpg, rtfType));
7f561c08de6b Initial load
duke
parents:
diff changeset
   660
        il.append(new PUSH(cpg, stylesheet.callsNodeset()));
7f561c08de6b Initial load
duke
parents:
diff changeset
   661
        il.append(new INVOKEINTERFACE(index,4));
7f561c08de6b Initial load
duke
parents:
diff changeset
   662
7f561c08de6b Initial load
duke
parents:
diff changeset
   663
        il.append(DUP);
7f561c08de6b Initial load
duke
parents:
diff changeset
   664
7f561c08de6b Initial load
duke
parents:
diff changeset
   665
        // Overwrite old handler with DOM handler
7f561c08de6b Initial load
duke
parents:
diff changeset
   666
        index = cpg.addInterfaceMethodref(DOM_INTF,
7f561c08de6b Initial load
duke
parents:
diff changeset
   667
                                 "getOutputDomBuilder",
7f561c08de6b Initial load
duke
parents:
diff changeset
   668
                                 "()" + TRANSLET_OUTPUT_SIG);
7f561c08de6b Initial load
duke
parents:
diff changeset
   669
7f561c08de6b Initial load
duke
parents:
diff changeset
   670
        il.append(new INVOKEINTERFACE(index,1));
7f561c08de6b Initial load
duke
parents:
diff changeset
   671
        il.append(DUP);
7f561c08de6b Initial load
duke
parents:
diff changeset
   672
        il.append(methodGen.storeHandler());
7f561c08de6b Initial load
duke
parents:
diff changeset
   673
7f561c08de6b Initial load
duke
parents:
diff changeset
   674
        // Call startDocument on the new handler
7f561c08de6b Initial load
duke
parents:
diff changeset
   675
        il.append(methodGen.startDocument());
7f561c08de6b Initial load
duke
parents:
diff changeset
   676
7f561c08de6b Initial load
duke
parents:
diff changeset
   677
        // Instantiate result tree fragment
7f561c08de6b Initial load
duke
parents:
diff changeset
   678
        translateContents(classGen, methodGen);
7f561c08de6b Initial load
duke
parents:
diff changeset
   679
7f561c08de6b Initial load
duke
parents:
diff changeset
   680
        // Call endDocument on the new handler
7f561c08de6b Initial load
duke
parents:
diff changeset
   681
        il.append(methodGen.loadHandler());
7f561c08de6b Initial load
duke
parents:
diff changeset
   682
        il.append(methodGen.endDocument());
7f561c08de6b Initial load
duke
parents:
diff changeset
   683
7f561c08de6b Initial load
duke
parents:
diff changeset
   684
        // Check if we need to wrap the DOMImpl object in a DOMAdapter object.
7f561c08de6b Initial load
duke
parents:
diff changeset
   685
        // DOMAdapter is not needed if the RTF is a simple RTF and the nodeset()
7f561c08de6b Initial load
duke
parents:
diff changeset
   686
        // function is not used.
7f561c08de6b Initial load
duke
parents:
diff changeset
   687
        if (stylesheet.callsNodeset()
7f561c08de6b Initial load
duke
parents:
diff changeset
   688
            && !DOM_CLASS.equals(DOM_IMPL_CLASS)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   689
            // new com.sun.org.apache.xalan.internal.xsltc.dom.DOMAdapter(DOMImpl,String[]);
7f561c08de6b Initial load
duke
parents:
diff changeset
   690
            index = cpg.addMethodref(DOM_ADAPTER_CLASS,
7f561c08de6b Initial load
duke
parents:
diff changeset
   691
                                     "<init>",
7f561c08de6b Initial load
duke
parents:
diff changeset
   692
                                     "("+DOM_INTF_SIG+
7f561c08de6b Initial load
duke
parents:
diff changeset
   693
                                     "["+STRING_SIG+
7f561c08de6b Initial load
duke
parents:
diff changeset
   694
                                     "["+STRING_SIG+
7f561c08de6b Initial load
duke
parents:
diff changeset
   695
                                     "[I"+
7f561c08de6b Initial load
duke
parents:
diff changeset
   696
                                     "["+STRING_SIG+")V");
7f561c08de6b Initial load
duke
parents:
diff changeset
   697
            il.append(new NEW(cpg.addClass(DOM_ADAPTER_CLASS)));
7f561c08de6b Initial load
duke
parents:
diff changeset
   698
            il.append(new DUP_X1());
7f561c08de6b Initial load
duke
parents:
diff changeset
   699
            il.append(SWAP);
7f561c08de6b Initial load
duke
parents:
diff changeset
   700
7f561c08de6b Initial load
duke
parents:
diff changeset
   701
            /*
7f561c08de6b Initial load
duke
parents:
diff changeset
   702
             * Give the DOM adapter an empty type mapping if the nodeset
7f561c08de6b Initial load
duke
parents:
diff changeset
   703
             * extension function is never called.
7f561c08de6b Initial load
duke
parents:
diff changeset
   704
             */
7f561c08de6b Initial load
duke
parents:
diff changeset
   705
            if (!stylesheet.callsNodeset()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   706
                il.append(new ICONST(0));
7f561c08de6b Initial load
duke
parents:
diff changeset
   707
                il.append(new ANEWARRAY(cpg.addClass(STRING)));
7f561c08de6b Initial load
duke
parents:
diff changeset
   708
                il.append(DUP);
7f561c08de6b Initial load
duke
parents:
diff changeset
   709
                il.append(DUP);
7f561c08de6b Initial load
duke
parents:
diff changeset
   710
                il.append(new ICONST(0));
7f561c08de6b Initial load
duke
parents:
diff changeset
   711
                il.append(new NEWARRAY(BasicType.INT));
7f561c08de6b Initial load
duke
parents:
diff changeset
   712
                il.append(SWAP);
7f561c08de6b Initial load
duke
parents:
diff changeset
   713
                il.append(new INVOKESPECIAL(index));
7f561c08de6b Initial load
duke
parents:
diff changeset
   714
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   715
            else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   716
                // Push name arrays on the stack
7f561c08de6b Initial load
duke
parents:
diff changeset
   717
                il.append(ALOAD_0);
7f561c08de6b Initial load
duke
parents:
diff changeset
   718
                il.append(new GETFIELD(cpg.addFieldref(TRANSLET_CLASS,
7f561c08de6b Initial load
duke
parents:
diff changeset
   719
                                           NAMES_INDEX,
7f561c08de6b Initial load
duke
parents:
diff changeset
   720
                                           NAMES_INDEX_SIG)));
7f561c08de6b Initial load
duke
parents:
diff changeset
   721
                il.append(ALOAD_0);
7f561c08de6b Initial load
duke
parents:
diff changeset
   722
                il.append(new GETFIELD(cpg.addFieldref(TRANSLET_CLASS,
7f561c08de6b Initial load
duke
parents:
diff changeset
   723
                                           URIS_INDEX,
7f561c08de6b Initial load
duke
parents:
diff changeset
   724
                                           URIS_INDEX_SIG)));
7f561c08de6b Initial load
duke
parents:
diff changeset
   725
                il.append(ALOAD_0);
7f561c08de6b Initial load
duke
parents:
diff changeset
   726
                il.append(new GETFIELD(cpg.addFieldref(TRANSLET_CLASS,
7f561c08de6b Initial load
duke
parents:
diff changeset
   727
                                           TYPES_INDEX,
7f561c08de6b Initial load
duke
parents:
diff changeset
   728
                                           TYPES_INDEX_SIG)));
7f561c08de6b Initial load
duke
parents:
diff changeset
   729
                il.append(ALOAD_0);
7f561c08de6b Initial load
duke
parents:
diff changeset
   730
                il.append(new GETFIELD(cpg.addFieldref(TRANSLET_CLASS,
7f561c08de6b Initial load
duke
parents:
diff changeset
   731
                                           NAMESPACE_INDEX,
7f561c08de6b Initial load
duke
parents:
diff changeset
   732
                                           NAMESPACE_INDEX_SIG)));
7f561c08de6b Initial load
duke
parents:
diff changeset
   733
7f561c08de6b Initial load
duke
parents:
diff changeset
   734
                // Initialized DOM adapter
7f561c08de6b Initial load
duke
parents:
diff changeset
   735
                il.append(new INVOKESPECIAL(index));
7f561c08de6b Initial load
duke
parents:
diff changeset
   736
7f561c08de6b Initial load
duke
parents:
diff changeset
   737
                // Add DOM adapter to MultiDOM class by calling addDOMAdapter()
7f561c08de6b Initial load
duke
parents:
diff changeset
   738
                il.append(DUP);
7f561c08de6b Initial load
duke
parents:
diff changeset
   739
                il.append(methodGen.loadDOM());
7f561c08de6b Initial load
duke
parents:
diff changeset
   740
                il.append(new CHECKCAST(cpg.addClass(classGen.getDOMClass())));
7f561c08de6b Initial load
duke
parents:
diff changeset
   741
                il.append(SWAP);
7f561c08de6b Initial load
duke
parents:
diff changeset
   742
                index = cpg.addMethodref(MULTI_DOM_CLASS,
7f561c08de6b Initial load
duke
parents:
diff changeset
   743
                                         "addDOMAdapter",
7f561c08de6b Initial load
duke
parents:
diff changeset
   744
                                         "(" + DOM_ADAPTER_SIG + ")I");
7f561c08de6b Initial load
duke
parents:
diff changeset
   745
                il.append(new INVOKEVIRTUAL(index));
7f561c08de6b Initial load
duke
parents:
diff changeset
   746
                il.append(POP);         // ignore mask returned by addDOMAdapter
7f561c08de6b Initial load
duke
parents:
diff changeset
   747
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   748
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   749
7f561c08de6b Initial load
duke
parents:
diff changeset
   750
        // Restore old handler base from stack
7f561c08de6b Initial load
duke
parents:
diff changeset
   751
        il.append(SWAP);
7f561c08de6b Initial load
duke
parents:
diff changeset
   752
        il.append(methodGen.storeHandler());
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 true if this expression/instruction depends on the context. By
7f561c08de6b Initial load
duke
parents:
diff changeset
   757
     * default, every expression/instruction depends on the context unless it
7f561c08de6b Initial load
duke
parents:
diff changeset
   758
     * overrides this method. Currently used to determine if result trees are
7f561c08de6b Initial load
duke
parents:
diff changeset
   759
     * compiled using procedures or little DOMs (result tree fragments).
7f561c08de6b Initial load
duke
parents:
diff changeset
   760
     * @return 'true' if this node depends on the context.
7f561c08de6b Initial load
duke
parents:
diff changeset
   761
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   762
    protected boolean contextDependent() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   763
        return true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   764
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   765
7f561c08de6b Initial load
duke
parents:
diff changeset
   766
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   767
     * Return true if any of the expressions/instructions in the contents of
7f561c08de6b Initial load
duke
parents:
diff changeset
   768
     * this node is context dependent.
7f561c08de6b Initial load
duke
parents:
diff changeset
   769
     * @return 'true' if the contents of this node is context dependent.
7f561c08de6b Initial load
duke
parents:
diff changeset
   770
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   771
    protected boolean dependentContents() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   772
        final int n = elementCount();
7f561c08de6b Initial load
duke
parents:
diff changeset
   773
        for (int i = 0; i < n; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   774
            final SyntaxTreeNode item = (SyntaxTreeNode)_contents.elementAt(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
   775
            if (item.contextDependent()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   776
                return true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   777
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   778
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   779
        return false;
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
     * Adds a child node to this syntax tree node.
7f561c08de6b Initial load
duke
parents:
diff changeset
   784
     * @param element is the new child node.
7f561c08de6b Initial load
duke
parents:
diff changeset
   785
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   786
    protected final void addElement(SyntaxTreeNode element) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   787
        _contents.addElement(element);
7f561c08de6b Initial load
duke
parents:
diff changeset
   788
        element.setParent(this);
7f561c08de6b Initial load
duke
parents:
diff changeset
   789
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   790
7f561c08de6b Initial load
duke
parents:
diff changeset
   791
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   792
     * Inserts the first child node of this syntax tree node. The existing
7f561c08de6b Initial load
duke
parents:
diff changeset
   793
     * children are shifted back one position.
7f561c08de6b Initial load
duke
parents:
diff changeset
   794
     * @param element is the new child node.
7f561c08de6b Initial load
duke
parents:
diff changeset
   795
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   796
    protected final void setFirstElement(SyntaxTreeNode element) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   797
        _contents.insertElementAt(element,0);
7f561c08de6b Initial load
duke
parents:
diff changeset
   798
        element.setParent(this);
7f561c08de6b Initial load
duke
parents:
diff changeset
   799
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   800
7f561c08de6b Initial load
duke
parents:
diff changeset
   801
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   802
     * Removed a child node of this syntax tree node.
7f561c08de6b Initial load
duke
parents:
diff changeset
   803
     * @param element is the child node to remove.
7f561c08de6b Initial load
duke
parents:
diff changeset
   804
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   805
    protected final void removeElement(SyntaxTreeNode element) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   806
        _contents.remove(element);
7f561c08de6b Initial load
duke
parents:
diff changeset
   807
        element.setParent(null);
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
     * Returns a Vector containing all the child nodes of this node.
7f561c08de6b Initial load
duke
parents:
diff changeset
   812
     * @return A Vector containing all the child nodes of this node.
7f561c08de6b Initial load
duke
parents:
diff changeset
   813
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   814
    protected final Vector getContents() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   815
        return _contents;
7f561c08de6b Initial load
duke
parents:
diff changeset
   816
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   817
7f561c08de6b Initial load
duke
parents:
diff changeset
   818
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   819
     * Tells you if this node has any child nodes.
7f561c08de6b Initial load
duke
parents:
diff changeset
   820
     * @return 'true' if this node has any children.
7f561c08de6b Initial load
duke
parents:
diff changeset
   821
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   822
    protected final boolean hasContents() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   823
        return elementCount() > 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
   824
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   825
7f561c08de6b Initial load
duke
parents:
diff changeset
   826
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   827
     * Returns the number of children this node has.
7f561c08de6b Initial load
duke
parents:
diff changeset
   828
     * @return Number of child nodes.
7f561c08de6b Initial load
duke
parents:
diff changeset
   829
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   830
    protected final int elementCount() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   831
        return _contents.size();
7f561c08de6b Initial load
duke
parents:
diff changeset
   832
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   833
7f561c08de6b Initial load
duke
parents:
diff changeset
   834
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   835
     * Returns an Enumeration of all child nodes of this node.
7f561c08de6b Initial load
duke
parents:
diff changeset
   836
     * @return An Enumeration of all child nodes of this node.
7f561c08de6b Initial load
duke
parents:
diff changeset
   837
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   838
    protected final Enumeration elements() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   839
        return _contents.elements();
7f561c08de6b Initial load
duke
parents:
diff changeset
   840
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   841
7f561c08de6b Initial load
duke
parents:
diff changeset
   842
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   843
     * Returns a child node at a given position.
7f561c08de6b Initial load
duke
parents:
diff changeset
   844
     * @param pos The child node's position.
7f561c08de6b Initial load
duke
parents:
diff changeset
   845
     * @return The child node.
7f561c08de6b Initial load
duke
parents:
diff changeset
   846
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   847
    protected final Object elementAt(int pos) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   848
        return _contents.elementAt(pos);
7f561c08de6b Initial load
duke
parents:
diff changeset
   849
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   850
7f561c08de6b Initial load
duke
parents:
diff changeset
   851
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   852
     * Returns this element's last child
7f561c08de6b Initial load
duke
parents:
diff changeset
   853
     * @return The child node.
7f561c08de6b Initial load
duke
parents:
diff changeset
   854
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   855
    protected final SyntaxTreeNode lastChild() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   856
        if (_contents.size() == 0) return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   857
        return (SyntaxTreeNode)_contents.lastElement();
7f561c08de6b Initial load
duke
parents:
diff changeset
   858
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   859
7f561c08de6b Initial load
duke
parents:
diff changeset
   860
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   861
     * Displays the contents of this syntax tree node (to stdout).
7f561c08de6b Initial load
duke
parents:
diff changeset
   862
     * This method is intended for debugging _only_, and should be overridden
7f561c08de6b Initial load
duke
parents:
diff changeset
   863
     * by all syntax tree node implementations.
7f561c08de6b Initial load
duke
parents:
diff changeset
   864
     * @param indent Indentation level for syntax tree levels.
7f561c08de6b Initial load
duke
parents:
diff changeset
   865
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   866
    public void display(int indent) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   867
        displayContents(indent);
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
     * Displays the contents of this syntax tree node (to stdout).
7f561c08de6b Initial load
duke
parents:
diff changeset
   872
     * This method is intended for debugging _only_ !!!
7f561c08de6b Initial load
duke
parents:
diff changeset
   873
     * @param indent Indentation level for syntax tree levels.
7f561c08de6b Initial load
duke
parents:
diff changeset
   874
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   875
    protected void displayContents(int indent) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   876
        final int n = elementCount();
7f561c08de6b Initial load
duke
parents:
diff changeset
   877
        for (int i = 0; i < n; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   878
            SyntaxTreeNode item = (SyntaxTreeNode)_contents.elementAt(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
   879
            item.display(indent);
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
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   884
     * Set the indentation level for debug output.
7f561c08de6b Initial load
duke
parents:
diff changeset
   885
     * @param indent Indentation level for syntax tree levels.
7f561c08de6b Initial load
duke
parents:
diff changeset
   886
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   887
    protected final void indent(int indent) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   888
        System.out.print(new String(_spaces, 0, indent));
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
     * Report an error to the parser.
7f561c08de6b Initial load
duke
parents:
diff changeset
   893
     * @param element The element in which the error occured (normally 'this'
7f561c08de6b Initial load
duke
parents:
diff changeset
   894
     * but it could also be an expression/pattern/etc.)
7f561c08de6b Initial load
duke
parents:
diff changeset
   895
     * @param parser The XSLT parser to report the error to.
7f561c08de6b Initial load
duke
parents:
diff changeset
   896
     * @param error The error code (from util/ErrorMsg).
7f561c08de6b Initial load
duke
parents:
diff changeset
   897
     * @param message Any additional error message.
7f561c08de6b Initial load
duke
parents:
diff changeset
   898
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   899
    protected void reportError(SyntaxTreeNode element, Parser parser,
7f561c08de6b Initial load
duke
parents:
diff changeset
   900
                               String errorCode, String message) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   901
        final ErrorMsg error = new ErrorMsg(errorCode, message, element);
7f561c08de6b Initial load
duke
parents:
diff changeset
   902
        parser.reportError(Constants.ERROR, error);
7f561c08de6b Initial load
duke
parents:
diff changeset
   903
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   904
7f561c08de6b Initial load
duke
parents:
diff changeset
   905
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   906
     * Report a recoverable error to the parser.
7f561c08de6b Initial load
duke
parents:
diff changeset
   907
     * @param element The element in which the error occured (normally 'this'
7f561c08de6b Initial load
duke
parents:
diff changeset
   908
     * but it could also be an expression/pattern/etc.)
7f561c08de6b Initial load
duke
parents:
diff changeset
   909
     * @param parser The XSLT parser to report the error to.
7f561c08de6b Initial load
duke
parents:
diff changeset
   910
     * @param error The error code (from util/ErrorMsg).
7f561c08de6b Initial load
duke
parents:
diff changeset
   911
     * @param message Any additional error message.
7f561c08de6b Initial load
duke
parents:
diff changeset
   912
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   913
    protected  void reportWarning(SyntaxTreeNode element, Parser parser,
7f561c08de6b Initial load
duke
parents:
diff changeset
   914
                                  String errorCode, String message) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   915
        final ErrorMsg error = new ErrorMsg(errorCode, message, element);
7f561c08de6b Initial load
duke
parents:
diff changeset
   916
        parser.reportError(Constants.WARNING, error);
7f561c08de6b Initial load
duke
parents:
diff changeset
   917
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   918
7f561c08de6b Initial load
duke
parents:
diff changeset
   919
}