src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/compiler/ApplyTemplates.java
author joehw
Wed, 18 Oct 2017 13:25:49 -0700
changeset 47359 e1a6c0168741
parent 47216 71c04702a3d5
child 48409 5ab69533994b
permissions -rw-r--r--
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked Reviewed-by: lancea, rriggs, mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     1
/*
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
     2
 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
     3
 * @LastModified: Oct 2017
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     4
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
     5
/*
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
     6
 * Licensed to the Apache Software Foundation (ASF) under one or more
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
     7
 * contributor license agreements.  See the NOTICE file distributed with
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
     8
 * this work for additional information regarding copyright ownership.
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
     9
 * 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
    10
 * (the "License"); you may not use this file except in compliance with
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
    11
 * the License.  You may obtain a copy of the License at
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    12
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    13
 *     http://www.apache.org/licenses/LICENSE-2.0
7f561c08de6b Initial load
duke
parents:
diff changeset
    14
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    15
 * Unless required by applicable law or agreed to in writing, software
7f561c08de6b Initial load
duke
parents:
diff changeset
    16
 * distributed under the License is distributed on an "AS IS" BASIS,
7f561c08de6b Initial load
duke
parents:
diff changeset
    17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7f561c08de6b Initial load
duke
parents:
diff changeset
    18
 * See the License for the specific language governing permissions and
7f561c08de6b Initial load
duke
parents:
diff changeset
    19
 * limitations under the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    20
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    21
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
package com.sun.org.apache.xalan.internal.xsltc.compiler;
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
import com.sun.org.apache.bcel.internal.generic.INVOKEINTERFACE;
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
import com.sun.org.apache.bcel.internal.generic.INVOKEVIRTUAL;
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
import com.sun.org.apache.bcel.internal.generic.InstructionList;
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.NodeSetType;
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.NodeType;
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ReferenceType;
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ResultTreeType;
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util;
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
import com.sun.org.apache.xml.internal.utils.XML11Char;
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    39
import java.util.ArrayList;
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    40
import java.util.List;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
 * @author Jacek Ambroziak
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
 * @author Santiago Pericas-Geertsen
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
final class ApplyTemplates extends Instruction {
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
    private Expression _select;
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
    private Type       _type = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
    private QName      _modeName;
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
    private String     _functionName;
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
    public void display(int indent) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
        indent(indent);
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
        Util.println("ApplyTemplates");
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
        indent(indent + IndentIncrement);
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
        Util.println("select " + _select.toString());
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
        if (_modeName != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
            indent(indent + IndentIncrement);
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
            Util.println("mode " + _modeName);
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
    public boolean hasWithParams() {
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
        return hasContents();
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
    public void parseContents(Parser parser) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
        final String select = getAttribute("select");
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
        final String mode   = getAttribute("mode");
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
        if (select.length() > 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
            _select = parser.parseExpression(this, "select", null);
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
        if (mode.length() > 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
            if (!XML11Char.isXML11ValidQName(mode)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
                ErrorMsg err = new ErrorMsg(ErrorMsg.INVALID_QNAME_ERR, mode, this);
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
                parser.reportError(Constants.ERROR, err);
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
            _modeName = parser.getQNameIgnoreDefaultNs(mode);
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
        // instantiate Mode if needed, cache (apply temp) function name
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
        _functionName =
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
            parser.getTopLevelStylesheet().getMode(_modeName).functionName();
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
        parseChildren(parser);// with-params
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
    public Type typeCheck(SymbolTable stable) throws TypeCheckError {
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
        if (_select != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
            _type = _select.typeCheck(stable);
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
            if (_type instanceof NodeType || _type instanceof ReferenceType) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
                _select = new CastExpr(_select, Type.NodeSet);
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
                _type = Type.NodeSet;
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
            if (_type instanceof NodeSetType||_type instanceof ResultTreeType) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
                typeCheckContents(stable); // with-params
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
                return Type.Void;
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
            throw new TypeCheckError(this);
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
        else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
            typeCheckContents(stable);          // with-params
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
            return Type.Void;
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
        }
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
     * Translate call-template. A parameter frame is pushed only if
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
     * some template in the stylesheet uses parameters.
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
    public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
        boolean setStartNodeCalled = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
        final Stylesheet stylesheet = classGen.getStylesheet();
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
        final ConstantPoolGen cpg = classGen.getConstantPool();
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
        final InstructionList il = methodGen.getInstructionList();
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
        final int current = methodGen.getLocalIndex("current");
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
        // check if sorting nodes is required
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   121
        final List<Sort> sortObjects = new ArrayList<>();
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
        for (final SyntaxTreeNode child : getContents()) {
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
            if (child instanceof Sort) {
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   124
                sortObjects.add((Sort)child);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
        // Push a new parameter frame
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
        if (stylesheet.hasLocalParams() || hasContents()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
            il.append(classGen.loadTranslet());
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
            final int pushFrame = cpg.addMethodref(TRANSLET_CLASS,
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
                                                   PUSH_PARAM_FRAME,
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
                                                   PUSH_PARAM_FRAME_SIG);
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
            il.append(new INVOKEVIRTUAL(pushFrame));
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
            // translate with-params
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
            translateContents(classGen, methodGen);
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
        il.append(classGen.loadTranslet());
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
        // The 'select' expression is a result-tree
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
        if ((_type != null) && (_type instanceof ResultTreeType)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
            // <xsl:sort> cannot be applied to a result tree - issue warning
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
            if (sortObjects.size() > 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
                ErrorMsg err = new ErrorMsg(ErrorMsg.RESULT_TREE_SORT_ERR,this);
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
                getParser().reportError(WARNING, err);
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
            // Put the result tree (a DOM adapter) on the stack
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
            _select.translate(classGen, methodGen);
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
            // Get back the DOM and iterator (not just iterator!!!)
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
            _type.translateTo(classGen, methodGen, Type.NodeSet);
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
        else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
            il.append(methodGen.loadDOM());
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
            // compute node iterator for applyTemplates
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
            if (sortObjects.size() > 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
                Sort.translateSortIterator(classGen, methodGen,
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
                                           _select, sortObjects);
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
                int setStartNode = cpg.addInterfaceMethodref(NODE_ITERATOR,
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
                                                             SET_START_NODE,
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
                                                             "(I)"+
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
                                                             NODE_ITERATOR_SIG);
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
                il.append(methodGen.loadCurrentNode());
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
                il.append(new INVOKEINTERFACE(setStartNode,2));
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
                setStartNodeCalled = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
            else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
                if (_select == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
                    Mode.compileGetChildren(classGen, methodGen, current);
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
                else
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
                    _select.translate(classGen, methodGen);
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
        if (_select != null && !setStartNodeCalled) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
            _select.startIterator(classGen, methodGen);
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
        //!!! need to instantiate all needed modes
7f561c08de6b Initial load
duke
parents:
diff changeset
   182
        final String className = classGen.getStylesheet().getClassName();
7f561c08de6b Initial load
duke
parents:
diff changeset
   183
        il.append(methodGen.loadHandler());
7f561c08de6b Initial load
duke
parents:
diff changeset
   184
        final String applyTemplatesSig = classGen.getApplyTemplatesSig();
7f561c08de6b Initial load
duke
parents:
diff changeset
   185
        final int applyTemplates = cpg.addMethodref(className,
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
                                                    _functionName,
7f561c08de6b Initial load
duke
parents:
diff changeset
   187
                                                    applyTemplatesSig);
7f561c08de6b Initial load
duke
parents:
diff changeset
   188
        il.append(new INVOKEVIRTUAL(applyTemplates));
7f561c08de6b Initial load
duke
parents:
diff changeset
   189
36486
b84e564d2358 8150704: XALAN: ERROR: 'No more DTM IDs are available' when transforming with lots of temporary result trees
joehw
parents: 33349
diff changeset
   190
        // 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
   191
        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
   192
            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
   193
                ((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
   194
            }
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
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   197
        // Pop parameter frame
7f561c08de6b Initial load
duke
parents:
diff changeset
   198
        if (stylesheet.hasLocalParams() || hasContents()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   199
            il.append(classGen.loadTranslet());
7f561c08de6b Initial load
duke
parents:
diff changeset
   200
            final int popFrame = cpg.addMethodref(TRANSLET_CLASS,
7f561c08de6b Initial load
duke
parents:
diff changeset
   201
                                                  POP_PARAM_FRAME,
7f561c08de6b Initial load
duke
parents:
diff changeset
   202
                                                  POP_PARAM_FRAME_SIG);
7f561c08de6b Initial load
duke
parents:
diff changeset
   203
            il.append(new INVOKEVIRTUAL(popFrame));
7f561c08de6b Initial load
duke
parents:
diff changeset
   204
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   205
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   206
}