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