jaxp/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/compiler/ApplyTemplates.java
author joehw
Wed, 09 Mar 2016 16:09:55 -0800
changeset 36486 b84e564d2358
parent 33349 975138b77cff
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) 2015, 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 java.util.Enumeration;
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
import java.util.Vector;
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
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.INVOKEINTERFACE;
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
import com.sun.org.apache.bcel.internal.generic.INVOKEVIRTUAL;
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
import com.sun.org.apache.bcel.internal.generic.InstructionList;
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.NodeSetType;
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.NodeType;
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ReferenceType;
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ResultTreeType;
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util;
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
import com.sun.org.apache.xml.internal.utils.XML11Char;
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
    41
import java.util.Iterator;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
 * @author Jacek Ambroziak
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
 * @author Santiago Pericas-Geertsen
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
final class ApplyTemplates extends Instruction {
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
    private Expression _select;
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
    private Type       _type = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
    private QName      _modeName;
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
    private String     _functionName;
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
    public void display(int indent) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
        indent(indent);
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
        Util.println("ApplyTemplates");
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
        indent(indent + IndentIncrement);
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
        Util.println("select " + _select.toString());
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
        if (_modeName != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
            indent(indent + IndentIncrement);
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
            Util.println("mode " + _modeName);
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
    public boolean hasWithParams() {
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
        return hasContents();
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
    public void parseContents(Parser parser) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
        final String select = getAttribute("select");
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
        final String mode   = getAttribute("mode");
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
        if (select.length() > 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
            _select = parser.parseExpression(this, "select", null);
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
        if (mode.length() > 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
            if (!XML11Char.isXML11ValidQName(mode)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
                ErrorMsg err = new ErrorMsg(ErrorMsg.INVALID_QNAME_ERR, mode, this);
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
                parser.reportError(Constants.ERROR, err);
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
            _modeName = parser.getQNameIgnoreDefaultNs(mode);
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
        // instantiate Mode if needed, cache (apply temp) function name
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
        _functionName =
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
            parser.getTopLevelStylesheet().getMode(_modeName).functionName();
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
        parseChildren(parser);// with-params
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
    public Type typeCheck(SymbolTable stable) throws TypeCheckError {
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
        if (_select != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
            _type = _select.typeCheck(stable);
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
            if (_type instanceof NodeType || _type instanceof ReferenceType) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
                _select = new CastExpr(_select, Type.NodeSet);
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
                _type = Type.NodeSet;
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
            if (_type instanceof NodeSetType||_type instanceof ResultTreeType) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
                typeCheckContents(stable); // with-params
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
                return Type.Void;
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
            throw new TypeCheckError(this);
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
        else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
            typeCheckContents(stable);          // with-params
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
            return Type.Void;
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
     * Translate call-template. A parameter frame is pushed only if
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
     * some template in the stylesheet uses parameters.
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
    public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
        boolean setStartNodeCalled = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
        final Stylesheet stylesheet = classGen.getStylesheet();
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
        final ConstantPoolGen cpg = classGen.getConstantPool();
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
        final InstructionList il = methodGen.getInstructionList();
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
        final int current = methodGen.getLocalIndex("current");
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
        // check if sorting nodes is required
36486
b84e564d2358 8150704: XALAN: ERROR: 'No more DTM IDs are available' when transforming with lots of temporary result trees
joehw
parents: 33349
diff changeset
   122
        final Vector<Sort> sortObjects = new Vector<>();
b84e564d2358 8150704: XALAN: ERROR: 'No more DTM IDs are available' when transforming with lots of temporary result trees
joehw
parents: 33349
diff changeset
   123
        for (final SyntaxTreeNode child : getContents()) {
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
            if (child instanceof Sort) {
36486
b84e564d2358 8150704: XALAN: ERROR: 'No more DTM IDs are available' when transforming with lots of temporary result trees
joehw
parents: 33349
diff changeset
   125
                sortObjects.addElement((Sort)child);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
        // Push a new parameter frame
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
        if (stylesheet.hasLocalParams() || hasContents()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
            il.append(classGen.loadTranslet());
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
            final int pushFrame = cpg.addMethodref(TRANSLET_CLASS,
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
                                                   PUSH_PARAM_FRAME,
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
                                                   PUSH_PARAM_FRAME_SIG);
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
            il.append(new INVOKEVIRTUAL(pushFrame));
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
            // translate with-params
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
            translateContents(classGen, methodGen);
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
        il.append(classGen.loadTranslet());
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
        // The 'select' expression is a result-tree
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
        if ((_type != null) && (_type instanceof ResultTreeType)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
            // <xsl:sort> cannot be applied to a result tree - issue warning
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
            if (sortObjects.size() > 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
                ErrorMsg err = new ErrorMsg(ErrorMsg.RESULT_TREE_SORT_ERR,this);
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
                getParser().reportError(WARNING, err);
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
            // Put the result tree (a DOM adapter) on the stack
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
            _select.translate(classGen, methodGen);
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
            // Get back the DOM and iterator (not just iterator!!!)
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
            _type.translateTo(classGen, methodGen, Type.NodeSet);
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
        else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
            il.append(methodGen.loadDOM());
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
            // compute node iterator for applyTemplates
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
            if (sortObjects.size() > 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
                Sort.translateSortIterator(classGen, methodGen,
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
                                           _select, sortObjects);
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
                int setStartNode = cpg.addInterfaceMethodref(NODE_ITERATOR,
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
                                                             SET_START_NODE,
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
                                                             "(I)"+
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
                                                             NODE_ITERATOR_SIG);
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
                il.append(methodGen.loadCurrentNode());
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
                il.append(new INVOKEINTERFACE(setStartNode,2));
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
                setStartNodeCalled = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
            else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
                if (_select == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
                    Mode.compileGetChildren(classGen, methodGen, current);
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
                else
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
                    _select.translate(classGen, methodGen);
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
        if (_select != null && !setStartNodeCalled) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
            _select.startIterator(classGen, methodGen);
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
7f561c08de6b Initial load
duke
parents:
diff changeset
   182
        //!!! need to instantiate all needed modes
7f561c08de6b Initial load
duke
parents:
diff changeset
   183
        final String className = classGen.getStylesheet().getClassName();
7f561c08de6b Initial load
duke
parents:
diff changeset
   184
        il.append(methodGen.loadHandler());
7f561c08de6b Initial load
duke
parents:
diff changeset
   185
        final String applyTemplatesSig = classGen.getApplyTemplatesSig();
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
        final int applyTemplates = cpg.addMethodref(className,
7f561c08de6b Initial load
duke
parents:
diff changeset
   187
                                                    _functionName,
7f561c08de6b Initial load
duke
parents:
diff changeset
   188
                                                    applyTemplatesSig);
7f561c08de6b Initial load
duke
parents:
diff changeset
   189
        il.append(new INVOKEVIRTUAL(applyTemplates));
7f561c08de6b Initial load
duke
parents:
diff changeset
   190
36486
b84e564d2358 8150704: XALAN: ERROR: 'No more DTM IDs are available' when transforming with lots of temporary result trees
joehw
parents: 33349
diff changeset
   191
        // unmap parameters to release temporary result trees
b84e564d2358 8150704: XALAN: ERROR: 'No more DTM IDs are available' when transforming with lots of temporary result trees
joehw
parents: 33349
diff changeset
   192
        for (final SyntaxTreeNode child : getContents()) {
b84e564d2358 8150704: XALAN: ERROR: 'No more DTM IDs are available' when transforming with lots of temporary result trees
joehw
parents: 33349
diff changeset
   193
            if (child instanceof WithParam) {
b84e564d2358 8150704: XALAN: ERROR: 'No more DTM IDs are available' when transforming with lots of temporary result trees
joehw
parents: 33349
diff changeset
   194
                ((WithParam)child).releaseResultTree(classGen, methodGen);
b84e564d2358 8150704: XALAN: ERROR: 'No more DTM IDs are available' when transforming with lots of temporary result trees
joehw
parents: 33349
diff changeset
   195
            }
b84e564d2358 8150704: XALAN: ERROR: 'No more DTM IDs are available' when transforming with lots of temporary result trees
joehw
parents: 33349
diff changeset
   196
        }
b84e564d2358 8150704: XALAN: ERROR: 'No more DTM IDs are available' when transforming with lots of temporary result trees
joehw
parents: 33349
diff changeset
   197
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   198
        // Pop parameter frame
7f561c08de6b Initial load
duke
parents:
diff changeset
   199
        if (stylesheet.hasLocalParams() || hasContents()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   200
            il.append(classGen.loadTranslet());
7f561c08de6b Initial load
duke
parents:
diff changeset
   201
            final int popFrame = cpg.addMethodref(TRANSLET_CLASS,
7f561c08de6b Initial load
duke
parents:
diff changeset
   202
                                                  POP_PARAM_FRAME,
7f561c08de6b Initial load
duke
parents:
diff changeset
   203
                                                  POP_PARAM_FRAME_SIG);
7f561c08de6b Initial load
duke
parents:
diff changeset
   204
            il.append(new INVOKEVIRTUAL(popFrame));
7f561c08de6b Initial load
duke
parents:
diff changeset
   205
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   206
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   207
}