src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/compiler/Mode.java
author joehw
Wed, 18 Oct 2017 13:25:49 -0700
changeset 47359 e1a6c0168741
parent 47216 71c04702a3d5
child 47712 bde0215f1f70
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
 * $Id: Mode.java,v 1.2.4.1 2005/09/19 05:18:11 pvedula Exp $
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
package com.sun.org.apache.xalan.internal.xsltc.compiler;
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
import com.sun.org.apache.bcel.internal.generic.BranchHandle;
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
import com.sun.org.apache.bcel.internal.generic.DUP;
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
import com.sun.org.apache.bcel.internal.generic.GOTO_W;
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
import com.sun.org.apache.bcel.internal.generic.IFLT;
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
import com.sun.org.apache.bcel.internal.generic.ILOAD;
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
import com.sun.org.apache.bcel.internal.generic.INVOKEINTERFACE;
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
import com.sun.org.apache.bcel.internal.generic.INVOKEVIRTUAL;
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
import com.sun.org.apache.bcel.internal.generic.ISTORE;
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
    36
import com.sun.org.apache.bcel.internal.generic.Instruction;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
import com.sun.org.apache.bcel.internal.generic.InstructionHandle;
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
import com.sun.org.apache.bcel.internal.generic.InstructionList;
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
import com.sun.org.apache.bcel.internal.generic.LocalVariableGen;
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
import com.sun.org.apache.bcel.internal.generic.SWITCH;
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
import com.sun.org.apache.bcel.internal.generic.TargetLostException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
import com.sun.org.apache.bcel.internal.util.InstructionFinder;
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
import com.sun.org.apache.xalan.internal.xsltc.DOM;
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.NamedMethodGenerator;
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util;
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
import com.sun.org.apache.xml.internal.dtm.Axis;
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
import com.sun.org.apache.xml.internal.dtm.DTM;
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    50
import java.util.ArrayList;
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
    51
import java.util.HashMap;
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
    52
import java.util.Iterator;
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    53
import java.util.List;
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
    54
import java.util.Map;
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
    55
import java.util.Set;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
 * Mode gathers all the templates belonging to a given mode;
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
 * it is responsible for generating an appropriate
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
 * applyTemplates + (mode name) method in the translet.
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
 * @author Jacek Ambroziak
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
 * @author Santiago Pericas-Geertsen
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
 * @author Morten Jorgensen
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
 * @author Erwin Bolwidt <ejb@klomp.org>
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
 * @author G. Todd Miller
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
final class Mode implements Constants {
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
     * The name of this mode as defined in the stylesheet.
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
    private final QName _name;
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
     * A reference to the stylesheet object that owns this mode.
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
    private final Stylesheet _stylesheet;
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
     * The name of the method in which this mode is compiled.
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
    private final String _methodName;
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
     * A vector of all the templates in this mode.
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
     */
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    87
    private List<Template> _templates;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
     * Group for patterns with node()-type kernel and child axis.
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
     */
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    92
    private List<LocationPathPattern> _childNodeGroup = null;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
     * Test sequence for patterns with node()-type kernel and child axis.
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
    private TestSeq _childNodeTestSeq = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
     * Group for patterns with node()-type kernel and attribute axis.
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
     */
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   102
    private List<LocationPathPattern> _attribNodeGroup = null;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
     * Test sequence for patterns with node()-type kernel and attribute axis.
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
    private TestSeq _attribNodeTestSeq = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
     * Group for patterns with id() or key()-type kernel.
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
     */
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   112
    private List<LocationPathPattern> _idxGroup = null;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
     * Test sequence for patterns with id() or key()-type kernel.
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
    private TestSeq _idxTestSeq = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
     * Group for patterns with any other kernel type.
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
     */
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   122
    private List<LocationPathPattern>[] _patternGroups;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
     * Test sequence for patterns with any other kernel type.
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
    private TestSeq[] _testSeq;
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
     * A mapping between templates and test sequences.
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
     */
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
   133
    private Map<Template, Object> _neededTemplates = new HashMap<>();
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
     * A mapping between named templates and Mode objects.
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
     */
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
   138
    private Map<Template, Mode> _namedTemplates = new HashMap<>();
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
     * A mapping between templates and instruction handles.
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
     */
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
   143
    private Map<Template, InstructionHandle> _templateIHs = new HashMap<>();
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
     * A mapping between templates and instruction lists.
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
     */
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
   148
    private Map<Template, InstructionList> _templateILs = new HashMap<>();
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
     * A reference to the pattern matching the root node.
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
    private LocationPathPattern _rootPattern = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
     * Stores ranges of template precendences for the compilation
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
   157
     * of apply-imports.
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
     */
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
   159
    private Map<Integer, Integer> _importLevels = null;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
     * A mapping between key names and keys.
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
     */
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
   164
    private Map<String, Key> _keys = null;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
     * Variable index for the current node used in code generation.
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
    private int _currentIndex;
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
     * Creates a new Mode.
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
     * @param name A textual representation of the mode's QName
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
     * @param stylesheet The Stylesheet in which the mode occured
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
     * @param suffix A suffix to append to the method name for this mode
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
     *               (normally a sequence number - still in a String).
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
     */
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   179
    @SuppressWarnings({"rawtypes", "unchecked"})
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
    public Mode(QName name, Stylesheet stylesheet, String suffix) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
        _name = name;
7f561c08de6b Initial load
duke
parents:
diff changeset
   182
        _stylesheet = stylesheet;
7f561c08de6b Initial load
duke
parents:
diff changeset
   183
        _methodName = APPLY_TEMPLATES + suffix;
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   184
        _templates = new ArrayList<>();
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   185
        _patternGroups = (List<LocationPathPattern>[])new ArrayList[32];
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   187
7f561c08de6b Initial load
duke
parents:
diff changeset
   188
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   189
     * Returns the name of the method (_not_ function) that will be
7f561c08de6b Initial load
duke
parents:
diff changeset
   190
     * compiled for this mode. Normally takes the form 'applyTemplates()'
7f561c08de6b Initial load
duke
parents:
diff changeset
   191
     * or * 'applyTemplates2()'.
7f561c08de6b Initial load
duke
parents:
diff changeset
   192
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   193
     * @return Method name for this mode
7f561c08de6b Initial load
duke
parents:
diff changeset
   194
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   195
    public String functionName() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   196
        return _methodName;
7f561c08de6b Initial load
duke
parents:
diff changeset
   197
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   198
7f561c08de6b Initial load
duke
parents:
diff changeset
   199
    public String functionName(int min, int max) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   200
        if (_importLevels == null) {
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
   201
            _importLevels = new HashMap<>();
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   202
        }
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
   203
        _importLevels.put(max, min);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   204
        return _methodName + '_' + max;
7f561c08de6b Initial load
duke
parents:
diff changeset
   205
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   206
7f561c08de6b Initial load
duke
parents:
diff changeset
   207
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   208
     * Shortcut to get the class compiled for this mode (will be inlined).
7f561c08de6b Initial load
duke
parents:
diff changeset
   209
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   210
    private String getClassName() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   211
        return _stylesheet.getClassName();
7f561c08de6b Initial load
duke
parents:
diff changeset
   212
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   213
7f561c08de6b Initial load
duke
parents:
diff changeset
   214
    public Stylesheet getStylesheet() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   215
        return _stylesheet;
7f561c08de6b Initial load
duke
parents:
diff changeset
   216
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   217
7f561c08de6b Initial load
duke
parents:
diff changeset
   218
    public void addTemplate(Template template) {
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   219
        _templates.add(template);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   220
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   221
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   222
    private List<Template> quicksort(List<Template> templates, int p, int r) {
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   223
        if (p < r) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   224
            final int q = partition(templates, p, r);
7f561c08de6b Initial load
duke
parents:
diff changeset
   225
            quicksort(templates, p, q);
7f561c08de6b Initial load
duke
parents:
diff changeset
   226
            quicksort(templates, q + 1, r);
7f561c08de6b Initial load
duke
parents:
diff changeset
   227
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   228
        return templates;
7f561c08de6b Initial load
duke
parents:
diff changeset
   229
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   230
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   231
    private int partition(List<Template> templates, int p, int r) {
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   232
        final Template x = (Template)templates.get(p);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   233
        int i = p - 1;
7f561c08de6b Initial load
duke
parents:
diff changeset
   234
        int j = r + 1;
7f561c08de6b Initial load
duke
parents:
diff changeset
   235
        while (true) {
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   236
            while (x.compareTo((Template)templates.get(--j)) > 0);
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   237
            while (x.compareTo((Template)templates.get(++i)) < 0);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   238
            if (i < j) {
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   239
                templates.set(j, templates.set(i, templates.get(j)));
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   240
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   241
            else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   242
                return j;
7f561c08de6b Initial load
duke
parents:
diff changeset
   243
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   244
        }
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
     * Process all the test patterns in this mode
7f561c08de6b Initial load
duke
parents:
diff changeset
   249
     */
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
   250
    public void processPatterns(Map<String, Key> keys) {
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   251
        _keys = keys;
7f561c08de6b Initial load
duke
parents:
diff changeset
   252
        _templates = quicksort(_templates, 0, _templates.size() - 1);
7f561c08de6b Initial load
duke
parents:
diff changeset
   253
7f561c08de6b Initial load
duke
parents:
diff changeset
   254
        // Traverse all templates
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   255
        for (Template template : _templates) {
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   256
            /*
7f561c08de6b Initial load
duke
parents:
diff changeset
   257
             * Add this template to a table of named templates if it has a name.
7f561c08de6b Initial load
duke
parents:
diff changeset
   258
             * If there are multiple templates with the same name, all but one
7f561c08de6b Initial load
duke
parents:
diff changeset
   259
             * (the one with highest priority) will be disabled.
7f561c08de6b Initial load
duke
parents:
diff changeset
   260
             */
7f561c08de6b Initial load
duke
parents:
diff changeset
   261
            if (template.isNamed() && !template.disabled()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   262
                _namedTemplates.put(template, this);
7f561c08de6b Initial load
duke
parents:
diff changeset
   263
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   264
7f561c08de6b Initial load
duke
parents:
diff changeset
   265
            // Add this template to a test sequence if it has a pattern
7f561c08de6b Initial load
duke
parents:
diff changeset
   266
            final Pattern pattern = template.getPattern();
7f561c08de6b Initial load
duke
parents:
diff changeset
   267
            if (pattern != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   268
                flattenAlternative(pattern, template, keys);
7f561c08de6b Initial load
duke
parents:
diff changeset
   269
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   270
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   271
        prepareTestSequences();
7f561c08de6b Initial load
duke
parents:
diff changeset
   272
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   273
7f561c08de6b Initial load
duke
parents:
diff changeset
   274
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   275
     * This method will break up alternative patterns (ie. unions of patterns,
7f561c08de6b Initial load
duke
parents:
diff changeset
   276
     * such as match="A/B | C/B") and add the basic patterns to their
7f561c08de6b Initial load
duke
parents:
diff changeset
   277
     * respective pattern groups.
7f561c08de6b Initial load
duke
parents:
diff changeset
   278
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   279
    private void flattenAlternative(Pattern pattern,
7f561c08de6b Initial load
duke
parents:
diff changeset
   280
                                    Template template,
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
   281
                                    Map<String, Key> keys) {
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   282
        // Patterns on type id() and key() are special since they do not have
7f561c08de6b Initial load
duke
parents:
diff changeset
   283
        // any kernel node type (it can be anything as long as the node is in
7f561c08de6b Initial load
duke
parents:
diff changeset
   284
        // the id's or key's index).
7f561c08de6b Initial load
duke
parents:
diff changeset
   285
        if (pattern instanceof IdKeyPattern) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   286
            final IdKeyPattern idkey = (IdKeyPattern)pattern;
7f561c08de6b Initial load
duke
parents:
diff changeset
   287
            idkey.setTemplate(template);
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   288
            if (_idxGroup == null) _idxGroup = new ArrayList<>();
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   289
            _idxGroup.add((IdKeyPattern)pattern);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   290
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   291
        // Alternative patterns are broken up and re-processed recursively
7f561c08de6b Initial load
duke
parents:
diff changeset
   292
        else if (pattern instanceof AlternativePattern) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   293
            final AlternativePattern alt = (AlternativePattern)pattern;
7f561c08de6b Initial load
duke
parents:
diff changeset
   294
            flattenAlternative(alt.getLeft(), template, keys);
7f561c08de6b Initial load
duke
parents:
diff changeset
   295
            flattenAlternative(alt.getRight(), template, keys);
7f561c08de6b Initial load
duke
parents:
diff changeset
   296
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   297
        // Finally we have a pattern that can be added to a test sequence!
7f561c08de6b Initial load
duke
parents:
diff changeset
   298
        else if (pattern instanceof LocationPathPattern) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   299
            final LocationPathPattern lpp = (LocationPathPattern)pattern;
7f561c08de6b Initial load
duke
parents:
diff changeset
   300
            lpp.setTemplate(template);
7f561c08de6b Initial load
duke
parents:
diff changeset
   301
            addPatternToGroup(lpp);
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
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   306
     * Group patterns by NodeTests of their last Step
7f561c08de6b Initial load
duke
parents:
diff changeset
   307
     * Keep them sorted by priority within group
7f561c08de6b Initial load
duke
parents:
diff changeset
   308
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   309
    private void addPatternToGroup(final LocationPathPattern lpp) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   310
        // id() and key()-type patterns do not have a kernel type
7f561c08de6b Initial load
duke
parents:
diff changeset
   311
        if (lpp instanceof IdKeyPattern) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   312
            addPattern(-1, lpp);
7f561c08de6b Initial load
duke
parents:
diff changeset
   313
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   314
        // Otherwise get the kernel pattern from the LPP
7f561c08de6b Initial load
duke
parents:
diff changeset
   315
        else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   316
            // kernel pattern is the last (maybe only) Step
7f561c08de6b Initial load
duke
parents:
diff changeset
   317
            final StepPattern kernel = lpp.getKernelPattern();
7f561c08de6b Initial load
duke
parents:
diff changeset
   318
            if (kernel != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   319
                addPattern(kernel.getNodeType(), lpp);
7f561c08de6b Initial load
duke
parents:
diff changeset
   320
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   321
            else if (_rootPattern == null ||
7f561c08de6b Initial load
duke
parents:
diff changeset
   322
                     lpp.noSmallerThan(_rootPattern)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   323
                _rootPattern = lpp;
7f561c08de6b Initial load
duke
parents:
diff changeset
   324
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   325
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   326
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   327
7f561c08de6b Initial load
duke
parents:
diff changeset
   328
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   329
     * Adds a pattern to a pattern group
7f561c08de6b Initial load
duke
parents:
diff changeset
   330
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   331
    private void addPattern(int kernelType, LocationPathPattern pattern) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   332
        // Make sure the array of pattern groups is long enough
7f561c08de6b Initial load
duke
parents:
diff changeset
   333
        final int oldLength = _patternGroups.length;
7f561c08de6b Initial load
duke
parents:
diff changeset
   334
        if (kernelType >= oldLength) {
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   335
            @SuppressWarnings({"rawtypes", "unchecked"})
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   336
            List<LocationPathPattern>[] newGroups =
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   337
                    (List<LocationPathPattern>[])new ArrayList[kernelType * 2];
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   338
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   339
            System.arraycopy(_patternGroups, 0, newGroups, 0, oldLength);
7f561c08de6b Initial load
duke
parents:
diff changeset
   340
            _patternGroups = newGroups;
7f561c08de6b Initial load
duke
parents:
diff changeset
   341
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   342
7f561c08de6b Initial load
duke
parents:
diff changeset
   343
        // Find the vector to put this pattern into
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   344
        List<LocationPathPattern> patterns;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   345
7f561c08de6b Initial load
duke
parents:
diff changeset
   346
        if (kernelType == DOM.NO_TYPE) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   347
            if (pattern.getAxis() == Axis.ATTRIBUTE) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   348
                patterns = (_attribNodeGroup == null) ?
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   349
                    (_attribNodeGroup = new ArrayList<>(2)) : _attribNodeGroup;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   350
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   351
            else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   352
                patterns = (_childNodeGroup == null) ?
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   353
                    (_childNodeGroup = new ArrayList<>(2)) : _childNodeGroup;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   354
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   355
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   356
        else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   357
            patterns = (_patternGroups[kernelType] == null) ?
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   358
                (_patternGroups[kernelType] = new ArrayList<>(2)) :
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   359
                _patternGroups[kernelType];
7f561c08de6b Initial load
duke
parents:
diff changeset
   360
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   361
7f561c08de6b Initial load
duke
parents:
diff changeset
   362
        if (patterns.size() == 0) {
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   363
            patterns.add(pattern);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   364
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   365
        else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   366
            boolean inserted = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   367
            for (int i = 0; i < patterns.size(); i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   368
                final LocationPathPattern lppToCompare =
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   369
                    (LocationPathPattern)patterns.get(i);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   370
7f561c08de6b Initial load
duke
parents:
diff changeset
   371
                if (pattern.noSmallerThan(lppToCompare)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   372
                    inserted = true;
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   373
                    patterns.add(i, pattern);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   374
                    break;
7f561c08de6b Initial load
duke
parents:
diff changeset
   375
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   376
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   377
            if (inserted == false) {
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   378
                patterns.add(pattern);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   379
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   380
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   381
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   382
7f561c08de6b Initial load
duke
parents:
diff changeset
   383
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   384
     * Complete test sequences of a given type by adding all patterns
7f561c08de6b Initial load
duke
parents:
diff changeset
   385
     * from a given group.
7f561c08de6b Initial load
duke
parents:
diff changeset
   386
     */
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   387
    private void completeTestSequences(int nodeType, List<LocationPathPattern> patterns) {
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   388
        if (patterns != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   389
            if (_patternGroups[nodeType] == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   390
                _patternGroups[nodeType] = patterns;
7f561c08de6b Initial load
duke
parents:
diff changeset
   391
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   392
            else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   393
                final int m = patterns.size();
7f561c08de6b Initial load
duke
parents:
diff changeset
   394
                for (int j = 0; j < m; j++) {
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   395
                    addPattern(nodeType, patterns.get(j));
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   396
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   397
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   398
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   399
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   400
7f561c08de6b Initial load
duke
parents:
diff changeset
   401
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   402
     * Build test sequences. The first step is to complete the test sequences
7f561c08de6b Initial load
duke
parents:
diff changeset
   403
     * by including patterns of "*" and "node()" kernel to all element test
7f561c08de6b Initial load
duke
parents:
diff changeset
   404
     * sequences, and of "@*" to all attribute test sequences.
7f561c08de6b Initial load
duke
parents:
diff changeset
   405
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   406
    private void prepareTestSequences() {
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   407
        final List<LocationPathPattern> starGroup = _patternGroups[DTM.ELEMENT_NODE];
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   408
        final List<LocationPathPattern> atStarGroup = _patternGroups[DTM.ATTRIBUTE_NODE];
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   409
7f561c08de6b Initial load
duke
parents:
diff changeset
   410
        // Complete test sequence for "text()" with "child::node()"
7f561c08de6b Initial load
duke
parents:
diff changeset
   411
        completeTestSequences(DTM.TEXT_NODE, _childNodeGroup);
7f561c08de6b Initial load
duke
parents:
diff changeset
   412
7f561c08de6b Initial load
duke
parents:
diff changeset
   413
        // Complete test sequence for "*" with "child::node()"
7f561c08de6b Initial load
duke
parents:
diff changeset
   414
        completeTestSequences(DTM.ELEMENT_NODE, _childNodeGroup);
7f561c08de6b Initial load
duke
parents:
diff changeset
   415
7f561c08de6b Initial load
duke
parents:
diff changeset
   416
        // Complete test sequence for "pi()" with "child::node()"
7f561c08de6b Initial load
duke
parents:
diff changeset
   417
        completeTestSequences(DTM.PROCESSING_INSTRUCTION_NODE, _childNodeGroup);
7f561c08de6b Initial load
duke
parents:
diff changeset
   418
7f561c08de6b Initial load
duke
parents:
diff changeset
   419
        // Complete test sequence for "comment()" with "child::node()"
7f561c08de6b Initial load
duke
parents:
diff changeset
   420
        completeTestSequences(DTM.COMMENT_NODE, _childNodeGroup);
7f561c08de6b Initial load
duke
parents:
diff changeset
   421
7f561c08de6b Initial load
duke
parents:
diff changeset
   422
        // Complete test sequence for "@*" with "attribute::node()"
7f561c08de6b Initial load
duke
parents:
diff changeset
   423
        completeTestSequences(DTM.ATTRIBUTE_NODE, _attribNodeGroup);
7f561c08de6b Initial load
duke
parents:
diff changeset
   424
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   425
        final List<String> names = _stylesheet.getXSLTC().getNamesIndex();
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   426
        if (starGroup != null || atStarGroup != null ||
7f561c08de6b Initial load
duke
parents:
diff changeset
   427
            _childNodeGroup != null || _attribNodeGroup != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   428
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   429
            final int n = _patternGroups.length;
7f561c08de6b Initial load
duke
parents:
diff changeset
   430
7f561c08de6b Initial load
duke
parents:
diff changeset
   431
            // Complete test sequence for user-defined types
7f561c08de6b Initial load
duke
parents:
diff changeset
   432
            for (int i = DTM.NTYPES; i < n; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   433
                if (_patternGroups[i] == null) continue;
7f561c08de6b Initial load
duke
parents:
diff changeset
   434
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   435
                final String name = names.get(i - DTM.NTYPES);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   436
7f561c08de6b Initial load
duke
parents:
diff changeset
   437
                if (isAttributeName(name)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   438
                    // If an attribute then copy "@*" to its test sequence
7f561c08de6b Initial load
duke
parents:
diff changeset
   439
                    completeTestSequences(i, atStarGroup);
7f561c08de6b Initial load
duke
parents:
diff changeset
   440
7f561c08de6b Initial load
duke
parents:
diff changeset
   441
                    // And also copy "attribute::node()" to its test sequence
7f561c08de6b Initial load
duke
parents:
diff changeset
   442
                    completeTestSequences(i, _attribNodeGroup);
7f561c08de6b Initial load
duke
parents:
diff changeset
   443
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   444
                else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   445
                    // If an element then copy "*" to its test sequence
7f561c08de6b Initial load
duke
parents:
diff changeset
   446
                    completeTestSequences(i, starGroup);
7f561c08de6b Initial load
duke
parents:
diff changeset
   447
7f561c08de6b Initial load
duke
parents:
diff changeset
   448
                    // And also copy "child::node()" to its test sequence
7f561c08de6b Initial load
duke
parents:
diff changeset
   449
                    completeTestSequences(i, _childNodeGroup);
7f561c08de6b Initial load
duke
parents:
diff changeset
   450
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   451
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   452
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   453
7f561c08de6b Initial load
duke
parents:
diff changeset
   454
        _testSeq = new TestSeq[DTM.NTYPES + names.size()];
7f561c08de6b Initial load
duke
parents:
diff changeset
   455
7f561c08de6b Initial load
duke
parents:
diff changeset
   456
        final int n = _patternGroups.length;
7f561c08de6b Initial load
duke
parents:
diff changeset
   457
        for (int i = 0; i < n; i++) {
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   458
            final List<LocationPathPattern> patterns = _patternGroups[i];
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   459
            if (patterns != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   460
                final TestSeq testSeq = new TestSeq(patterns, i, this);
7f561c08de6b Initial load
duke
parents:
diff changeset
   461
// System.out.println("testSeq[" + i + "] = " + testSeq);
7f561c08de6b Initial load
duke
parents:
diff changeset
   462
                testSeq.reduce();
7f561c08de6b Initial load
duke
parents:
diff changeset
   463
                _testSeq[i] = testSeq;
7f561c08de6b Initial load
duke
parents:
diff changeset
   464
                testSeq.findTemplates(_neededTemplates);
7f561c08de6b Initial load
duke
parents:
diff changeset
   465
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   466
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   467
7f561c08de6b Initial load
duke
parents:
diff changeset
   468
        if (_childNodeGroup != null && _childNodeGroup.size() > 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   469
            _childNodeTestSeq = new TestSeq(_childNodeGroup, -1, this);
7f561c08de6b Initial load
duke
parents:
diff changeset
   470
            _childNodeTestSeq.reduce();
7f561c08de6b Initial load
duke
parents:
diff changeset
   471
            _childNodeTestSeq.findTemplates(_neededTemplates);
7f561c08de6b Initial load
duke
parents:
diff changeset
   472
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   473
7f561c08de6b Initial load
duke
parents:
diff changeset
   474
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
   475
        if (_attribNodeGroup != null && _attribNodeGroup.size() > 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   476
            _attribNodeTestSeq = new TestSeq(_attribNodeGroup, -1, this);
7f561c08de6b Initial load
duke
parents:
diff changeset
   477
            _attribNodeTestSeq.reduce();
7f561c08de6b Initial load
duke
parents:
diff changeset
   478
            _attribNodeTestSeq.findTemplates(_neededTemplates);
7f561c08de6b Initial load
duke
parents:
diff changeset
   479
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   480
*/
7f561c08de6b Initial load
duke
parents:
diff changeset
   481
7f561c08de6b Initial load
duke
parents:
diff changeset
   482
        if (_idxGroup != null && _idxGroup.size() > 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   483
            _idxTestSeq = new TestSeq(_idxGroup, this);
7f561c08de6b Initial load
duke
parents:
diff changeset
   484
            _idxTestSeq.reduce();
7f561c08de6b Initial load
duke
parents:
diff changeset
   485
            _idxTestSeq.findTemplates(_neededTemplates);
7f561c08de6b Initial load
duke
parents:
diff changeset
   486
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   487
7f561c08de6b Initial load
duke
parents:
diff changeset
   488
        if (_rootPattern != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   489
            // doesn't matter what is 'put', only key matters
7f561c08de6b Initial load
duke
parents:
diff changeset
   490
            _neededTemplates.put(_rootPattern.getTemplate(), this);
7f561c08de6b Initial load
duke
parents:
diff changeset
   491
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   492
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   493
7f561c08de6b Initial load
duke
parents:
diff changeset
   494
    private void compileNamedTemplate(Template template,
7f561c08de6b Initial load
duke
parents:
diff changeset
   495
                                      ClassGenerator classGen) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   496
        final ConstantPoolGen cpg = classGen.getConstantPool();
7f561c08de6b Initial load
duke
parents:
diff changeset
   497
        final InstructionList il = new InstructionList();
7f561c08de6b Initial load
duke
parents:
diff changeset
   498
        String methodName = Util.escape(template.getName().toString());
7f561c08de6b Initial load
duke
parents:
diff changeset
   499
7f561c08de6b Initial load
duke
parents:
diff changeset
   500
        int numParams = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
   501
        if (template.isSimpleNamedTemplate()) {
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   502
            List<Param> parameters = template.getParameters();
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   503
            numParams = parameters.size();
7f561c08de6b Initial load
duke
parents:
diff changeset
   504
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   505
7f561c08de6b Initial load
duke
parents:
diff changeset
   506
        // Initialize the types and names arrays for the NamedMethodGenerator.
7f561c08de6b Initial load
duke
parents:
diff changeset
   507
        com.sun.org.apache.bcel.internal.generic.Type[] types =
7f561c08de6b Initial load
duke
parents:
diff changeset
   508
            new com.sun.org.apache.bcel.internal.generic.Type[4 + numParams];
7f561c08de6b Initial load
duke
parents:
diff changeset
   509
        String[] names = new String[4 + numParams];
7f561c08de6b Initial load
duke
parents:
diff changeset
   510
        types[0] = Util.getJCRefType(DOM_INTF_SIG);
7f561c08de6b Initial load
duke
parents:
diff changeset
   511
        types[1] = Util.getJCRefType(NODE_ITERATOR_SIG);
7f561c08de6b Initial load
duke
parents:
diff changeset
   512
        types[2] = Util.getJCRefType(TRANSLET_OUTPUT_SIG);
7f561c08de6b Initial load
duke
parents:
diff changeset
   513
        types[3] = com.sun.org.apache.bcel.internal.generic.Type.INT;
7f561c08de6b Initial load
duke
parents:
diff changeset
   514
        names[0] = DOCUMENT_PNAME;
7f561c08de6b Initial load
duke
parents:
diff changeset
   515
        names[1] = ITERATOR_PNAME;
7f561c08de6b Initial load
duke
parents:
diff changeset
   516
        names[2] = TRANSLET_OUTPUT_PNAME;
7f561c08de6b Initial load
duke
parents:
diff changeset
   517
        names[3] = NODE_PNAME;
7f561c08de6b Initial load
duke
parents:
diff changeset
   518
7f561c08de6b Initial load
duke
parents:
diff changeset
   519
        // For simple named templates, the signature of the generated method
7f561c08de6b Initial load
duke
parents:
diff changeset
   520
        // is not fixed. It depends on the number of parameters declared in the
7f561c08de6b Initial load
duke
parents:
diff changeset
   521
        // template.
7f561c08de6b Initial load
duke
parents:
diff changeset
   522
        for (int i = 4; i < 4 + numParams; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   523
            types[i] = Util.getJCRefType(OBJECT_SIG);
7f561c08de6b Initial load
duke
parents:
diff changeset
   524
            names[i] = "param" + String.valueOf(i-4);
7f561c08de6b Initial load
duke
parents:
diff changeset
   525
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   526
7f561c08de6b Initial load
duke
parents:
diff changeset
   527
        NamedMethodGenerator methodGen =
7f561c08de6b Initial load
duke
parents:
diff changeset
   528
                new NamedMethodGenerator(ACC_PUBLIC,
7f561c08de6b Initial load
duke
parents:
diff changeset
   529
                                     com.sun.org.apache.bcel.internal.generic.Type.VOID,
7f561c08de6b Initial load
duke
parents:
diff changeset
   530
                                     types, names, methodName,
7f561c08de6b Initial load
duke
parents:
diff changeset
   531
                                     getClassName(), il, cpg);
7f561c08de6b Initial load
duke
parents:
diff changeset
   532
7f561c08de6b Initial load
duke
parents:
diff changeset
   533
        il.append(template.compile(classGen, methodGen));
7f561c08de6b Initial load
duke
parents:
diff changeset
   534
        il.append(RETURN);
7f561c08de6b Initial load
duke
parents:
diff changeset
   535
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   536
        classGen.addMethod(methodGen);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   537
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   538
7f561c08de6b Initial load
duke
parents:
diff changeset
   539
    private void compileTemplates(ClassGenerator classGen,
7f561c08de6b Initial load
duke
parents:
diff changeset
   540
                                  MethodGenerator methodGen,
7f561c08de6b Initial load
duke
parents:
diff changeset
   541
                                  InstructionHandle next)
7f561c08de6b Initial load
duke
parents:
diff changeset
   542
    {
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
   543
        Set<Template> templates = _namedTemplates.keySet();
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
   544
        for (Template template : templates) {
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   545
            compileNamedTemplate(template, classGen);
7f561c08de6b Initial load
duke
parents:
diff changeset
   546
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   547
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
   548
        templates = _neededTemplates.keySet();
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
   549
        for (Template template : templates) {
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   550
            if (template.hasContents()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   551
                // !!! TODO templates both named and matched
7f561c08de6b Initial load
duke
parents:
diff changeset
   552
                InstructionList til = template.compile(classGen, methodGen);
7f561c08de6b Initial load
duke
parents:
diff changeset
   553
                til.append(new GOTO_W(next));
7f561c08de6b Initial load
duke
parents:
diff changeset
   554
                _templateILs.put(template, til);
7f561c08de6b Initial load
duke
parents:
diff changeset
   555
                _templateIHs.put(template, til.getStart());
7f561c08de6b Initial load
duke
parents:
diff changeset
   556
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   557
            else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   558
                // empty template
7f561c08de6b Initial load
duke
parents:
diff changeset
   559
                _templateIHs.put(template, next);
7f561c08de6b Initial load
duke
parents:
diff changeset
   560
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   561
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   562
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   563
7f561c08de6b Initial load
duke
parents:
diff changeset
   564
    private void appendTemplateCode(InstructionList body) {
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
   565
        for (Template template : _neededTemplates.keySet()) {
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
   566
            final InstructionList iList = _templateILs.get(template);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   567
            if (iList != null) {
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
   568
                body.append(iList);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   569
            }
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
   570
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   571
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   572
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   573
7f561c08de6b Initial load
duke
parents:
diff changeset
   574
    private void appendTestSequences(InstructionList body) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   575
        final int n = _testSeq.length;
7f561c08de6b Initial load
duke
parents:
diff changeset
   576
        for (int i = 0; i < n; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   577
            final TestSeq testSeq = _testSeq[i];
7f561c08de6b Initial load
duke
parents:
diff changeset
   578
            if (testSeq != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   579
                InstructionList il = testSeq.getInstructionList();
7f561c08de6b Initial load
duke
parents:
diff changeset
   580
                if (il != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   581
                    body.append(il);
7f561c08de6b Initial load
duke
parents:
diff changeset
   582
                // else trivial TestSeq
7f561c08de6b Initial load
duke
parents:
diff changeset
   583
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   584
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   585
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   586
7f561c08de6b Initial load
duke
parents:
diff changeset
   587
    public static void compileGetChildren(ClassGenerator classGen,
7f561c08de6b Initial load
duke
parents:
diff changeset
   588
                                          MethodGenerator methodGen,
7f561c08de6b Initial load
duke
parents:
diff changeset
   589
                                          int node) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   590
        final ConstantPoolGen cpg = classGen.getConstantPool();
7f561c08de6b Initial load
duke
parents:
diff changeset
   591
        final InstructionList il = methodGen.getInstructionList();
7f561c08de6b Initial load
duke
parents:
diff changeset
   592
        final int git = cpg.addInterfaceMethodref(DOM_INTF,
7f561c08de6b Initial load
duke
parents:
diff changeset
   593
                                                  GET_CHILDREN,
7f561c08de6b Initial load
duke
parents:
diff changeset
   594
                                                  GET_CHILDREN_SIG);
7f561c08de6b Initial load
duke
parents:
diff changeset
   595
        il.append(methodGen.loadDOM());
7f561c08de6b Initial load
duke
parents:
diff changeset
   596
        il.append(new ILOAD(node));
7f561c08de6b Initial load
duke
parents:
diff changeset
   597
        il.append(new INVOKEINTERFACE(git, 2));
7f561c08de6b Initial load
duke
parents:
diff changeset
   598
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   599
7f561c08de6b Initial load
duke
parents:
diff changeset
   600
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   601
     * Compiles the default handling for DOM elements: traverse all children
7f561c08de6b Initial load
duke
parents:
diff changeset
   602
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   603
    private InstructionList compileDefaultRecursion(ClassGenerator classGen,
7f561c08de6b Initial load
duke
parents:
diff changeset
   604
                                                    MethodGenerator methodGen,
7f561c08de6b Initial load
duke
parents:
diff changeset
   605
                                                    InstructionHandle next) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   606
        final ConstantPoolGen cpg = classGen.getConstantPool();
7f561c08de6b Initial load
duke
parents:
diff changeset
   607
        final InstructionList il = new InstructionList();
7f561c08de6b Initial load
duke
parents:
diff changeset
   608
        final String applyTemplatesSig = classGen.getApplyTemplatesSig();
7f561c08de6b Initial load
duke
parents:
diff changeset
   609
        final int git = cpg.addInterfaceMethodref(DOM_INTF,
7f561c08de6b Initial load
duke
parents:
diff changeset
   610
                                                  GET_CHILDREN,
7f561c08de6b Initial load
duke
parents:
diff changeset
   611
                                                  GET_CHILDREN_SIG);
7f561c08de6b Initial load
duke
parents:
diff changeset
   612
        final int applyTemplates = cpg.addMethodref(getClassName(),
7f561c08de6b Initial load
duke
parents:
diff changeset
   613
                                                    functionName(),
7f561c08de6b Initial load
duke
parents:
diff changeset
   614
                                                    applyTemplatesSig);
7f561c08de6b Initial load
duke
parents:
diff changeset
   615
        il.append(classGen.loadTranslet());
7f561c08de6b Initial load
duke
parents:
diff changeset
   616
        il.append(methodGen.loadDOM());
7f561c08de6b Initial load
duke
parents:
diff changeset
   617
7f561c08de6b Initial load
duke
parents:
diff changeset
   618
        il.append(methodGen.loadDOM());
7f561c08de6b Initial load
duke
parents:
diff changeset
   619
        il.append(new ILOAD(_currentIndex));
7f561c08de6b Initial load
duke
parents:
diff changeset
   620
        il.append(new INVOKEINTERFACE(git, 2));
7f561c08de6b Initial load
duke
parents:
diff changeset
   621
        il.append(methodGen.loadHandler());
7f561c08de6b Initial load
duke
parents:
diff changeset
   622
        il.append(new INVOKEVIRTUAL(applyTemplates));
7f561c08de6b Initial load
duke
parents:
diff changeset
   623
        il.append(new GOTO_W(next));
7f561c08de6b Initial load
duke
parents:
diff changeset
   624
        return il;
7f561c08de6b Initial load
duke
parents:
diff changeset
   625
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   626
7f561c08de6b Initial load
duke
parents:
diff changeset
   627
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   628
     * Compiles the default action for DOM text nodes and attribute nodes:
7f561c08de6b Initial load
duke
parents:
diff changeset
   629
     * output the node's text value
7f561c08de6b Initial load
duke
parents:
diff changeset
   630
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   631
    private InstructionList compileDefaultText(ClassGenerator classGen,
7f561c08de6b Initial load
duke
parents:
diff changeset
   632
                                               MethodGenerator methodGen,
7f561c08de6b Initial load
duke
parents:
diff changeset
   633
                                               InstructionHandle next) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   634
        final ConstantPoolGen cpg = classGen.getConstantPool();
7f561c08de6b Initial load
duke
parents:
diff changeset
   635
        final InstructionList il = new InstructionList();
7f561c08de6b Initial load
duke
parents:
diff changeset
   636
7f561c08de6b Initial load
duke
parents:
diff changeset
   637
        final int chars = cpg.addInterfaceMethodref(DOM_INTF,
7f561c08de6b Initial load
duke
parents:
diff changeset
   638
                                                    CHARACTERS,
7f561c08de6b Initial load
duke
parents:
diff changeset
   639
                                                    CHARACTERS_SIG);
7f561c08de6b Initial load
duke
parents:
diff changeset
   640
        il.append(methodGen.loadDOM());
7f561c08de6b Initial load
duke
parents:
diff changeset
   641
        il.append(new ILOAD(_currentIndex));
7f561c08de6b Initial load
duke
parents:
diff changeset
   642
        il.append(methodGen.loadHandler());
7f561c08de6b Initial load
duke
parents:
diff changeset
   643
        il.append(new INVOKEINTERFACE(chars, 3));
7f561c08de6b Initial load
duke
parents:
diff changeset
   644
        il.append(new GOTO_W(next));
7f561c08de6b Initial load
duke
parents:
diff changeset
   645
        return il;
7f561c08de6b Initial load
duke
parents:
diff changeset
   646
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   647
7f561c08de6b Initial load
duke
parents:
diff changeset
   648
    private InstructionList compileNamespaces(ClassGenerator classGen,
7f561c08de6b Initial load
duke
parents:
diff changeset
   649
                                              MethodGenerator methodGen,
7f561c08de6b Initial load
duke
parents:
diff changeset
   650
                                              boolean[] isNamespace,
7f561c08de6b Initial load
duke
parents:
diff changeset
   651
                                              boolean[] isAttribute,
7f561c08de6b Initial load
duke
parents:
diff changeset
   652
                                              boolean attrFlag,
7f561c08de6b Initial load
duke
parents:
diff changeset
   653
                                              InstructionHandle defaultTarget) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   654
        final XSLTC xsltc = classGen.getParser().getXSLTC();
7f561c08de6b Initial load
duke
parents:
diff changeset
   655
        final ConstantPoolGen cpg = classGen.getConstantPool();
7f561c08de6b Initial load
duke
parents:
diff changeset
   656
7f561c08de6b Initial load
duke
parents:
diff changeset
   657
        // Append switch() statement - namespace test dispatch loop
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   658
        final List<String> namespaces = xsltc.getNamespaceIndex();
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   659
        final List<String> names = xsltc.getNamesIndex();
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   660
        final int namespaceCount = namespaces.size() + 1;
7f561c08de6b Initial load
duke
parents:
diff changeset
   661
        final int namesCount = names.size();
7f561c08de6b Initial load
duke
parents:
diff changeset
   662
7f561c08de6b Initial load
duke
parents:
diff changeset
   663
        final InstructionList il = new InstructionList();
7f561c08de6b Initial load
duke
parents:
diff changeset
   664
        final int[] types = new int[namespaceCount];
7f561c08de6b Initial load
duke
parents:
diff changeset
   665
        final InstructionHandle[] targets = new InstructionHandle[types.length];
7f561c08de6b Initial load
duke
parents:
diff changeset
   666
7f561c08de6b Initial load
duke
parents:
diff changeset
   667
        if (namespaceCount > 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   668
            boolean compiled = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   669
7f561c08de6b Initial load
duke
parents:
diff changeset
   670
            // Initialize targets for namespace() switch statement
7f561c08de6b Initial load
duke
parents:
diff changeset
   671
            for (int i = 0; i < namespaceCount; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   672
                targets[i] = defaultTarget;
7f561c08de6b Initial load
duke
parents:
diff changeset
   673
                types[i] = i;
7f561c08de6b Initial load
duke
parents:
diff changeset
   674
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   675
7f561c08de6b Initial load
duke
parents:
diff changeset
   676
            // Add test sequences for known namespace types
7f561c08de6b Initial load
duke
parents:
diff changeset
   677
            for (int i = DTM.NTYPES; i < (DTM.NTYPES+namesCount); i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   678
                if ((isNamespace[i]) && (isAttribute[i] == attrFlag)) {
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   679
                    String name = names.get(i-DTM.NTYPES);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   680
                    String namespace = name.substring(0,name.lastIndexOf(':'));
7f561c08de6b Initial load
duke
parents:
diff changeset
   681
                    final int type = xsltc.registerNamespace(namespace);
7f561c08de6b Initial load
duke
parents:
diff changeset
   682
7f561c08de6b Initial load
duke
parents:
diff changeset
   683
                    if ((i < _testSeq.length) &&
7f561c08de6b Initial load
duke
parents:
diff changeset
   684
                        (_testSeq[i] != null)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   685
                        targets[type] =
7f561c08de6b Initial load
duke
parents:
diff changeset
   686
                            (_testSeq[i]).compile(classGen,
7f561c08de6b Initial load
duke
parents:
diff changeset
   687
                                                       methodGen,
7f561c08de6b Initial load
duke
parents:
diff changeset
   688
                                                       defaultTarget);
7f561c08de6b Initial load
duke
parents:
diff changeset
   689
                        compiled = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   690
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   691
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   692
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   693
7f561c08de6b Initial load
duke
parents:
diff changeset
   694
            // Return "null" if no test sequences were compiled
7f561c08de6b Initial load
duke
parents:
diff changeset
   695
            if (!compiled) return(null);
7f561c08de6b Initial load
duke
parents:
diff changeset
   696
7f561c08de6b Initial load
duke
parents:
diff changeset
   697
            // Append first code in applyTemplates() - get type of current node
7f561c08de6b Initial load
duke
parents:
diff changeset
   698
            final int getNS = cpg.addInterfaceMethodref(DOM_INTF,
7f561c08de6b Initial load
duke
parents:
diff changeset
   699
                                                        "getNamespaceType",
7f561c08de6b Initial load
duke
parents:
diff changeset
   700
                                                        "(I)I");
7f561c08de6b Initial load
duke
parents:
diff changeset
   701
            il.append(methodGen.loadDOM());
7f561c08de6b Initial load
duke
parents:
diff changeset
   702
            il.append(new ILOAD(_currentIndex));
7f561c08de6b Initial load
duke
parents:
diff changeset
   703
            il.append(new INVOKEINTERFACE(getNS, 2));
7f561c08de6b Initial load
duke
parents:
diff changeset
   704
            il.append(new SWITCH(types, targets, defaultTarget));
7f561c08de6b Initial load
duke
parents:
diff changeset
   705
            return(il);
7f561c08de6b Initial load
duke
parents:
diff changeset
   706
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   707
        else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   708
            return(null);
7f561c08de6b Initial load
duke
parents:
diff changeset
   709
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   710
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   711
7f561c08de6b Initial load
duke
parents:
diff changeset
   712
   /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   713
     * Compiles the applyTemplates() method and adds it to the translet.
7f561c08de6b Initial load
duke
parents:
diff changeset
   714
     * This is the main dispatch method.
7f561c08de6b Initial load
duke
parents:
diff changeset
   715
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   716
    public void compileApplyTemplates(ClassGenerator classGen) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   717
        final XSLTC xsltc = classGen.getParser().getXSLTC();
7f561c08de6b Initial load
duke
parents:
diff changeset
   718
        final ConstantPoolGen cpg = classGen.getConstantPool();
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   719
        final List<String> names = xsltc.getNamesIndex();
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   720
7f561c08de6b Initial load
duke
parents:
diff changeset
   721
        // Create the applyTemplates() method
7f561c08de6b Initial load
duke
parents:
diff changeset
   722
        final com.sun.org.apache.bcel.internal.generic.Type[] argTypes =
7f561c08de6b Initial load
duke
parents:
diff changeset
   723
            new com.sun.org.apache.bcel.internal.generic.Type[3];
7f561c08de6b Initial load
duke
parents:
diff changeset
   724
        argTypes[0] = Util.getJCRefType(DOM_INTF_SIG);
7f561c08de6b Initial load
duke
parents:
diff changeset
   725
        argTypes[1] = Util.getJCRefType(NODE_ITERATOR_SIG);
7f561c08de6b Initial load
duke
parents:
diff changeset
   726
        argTypes[2] = Util.getJCRefType(TRANSLET_OUTPUT_SIG);
7f561c08de6b Initial load
duke
parents:
diff changeset
   727
7f561c08de6b Initial load
duke
parents:
diff changeset
   728
        final String[] argNames = new String[3];
7f561c08de6b Initial load
duke
parents:
diff changeset
   729
        argNames[0] = DOCUMENT_PNAME;
7f561c08de6b Initial load
duke
parents:
diff changeset
   730
        argNames[1] = ITERATOR_PNAME;
7f561c08de6b Initial load
duke
parents:
diff changeset
   731
        argNames[2] = TRANSLET_OUTPUT_PNAME;
7f561c08de6b Initial load
duke
parents:
diff changeset
   732
7f561c08de6b Initial load
duke
parents:
diff changeset
   733
        final InstructionList mainIL = new InstructionList();
7f561c08de6b Initial load
duke
parents:
diff changeset
   734
        final MethodGenerator methodGen =
7f561c08de6b Initial load
duke
parents:
diff changeset
   735
            new MethodGenerator(ACC_PUBLIC | ACC_FINAL,
7f561c08de6b Initial load
duke
parents:
diff changeset
   736
                                com.sun.org.apache.bcel.internal.generic.Type.VOID,
7f561c08de6b Initial load
duke
parents:
diff changeset
   737
                                argTypes, argNames, functionName(),
7f561c08de6b Initial load
duke
parents:
diff changeset
   738
                                getClassName(), mainIL,
7f561c08de6b Initial load
duke
parents:
diff changeset
   739
                                classGen.getConstantPool());
7f561c08de6b Initial load
duke
parents:
diff changeset
   740
        methodGen.addException("com.sun.org.apache.xalan.internal.xsltc.TransletException");
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   741
        // Insert an extra NOP just to keep "current" from appearing as if it
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   742
        // has a value before the start of the loop.
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   743
        mainIL.append(NOP);
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   744
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   745
7f561c08de6b Initial load
duke
parents:
diff changeset
   746
        // Create a local variable to hold the current node
7f561c08de6b Initial load
duke
parents:
diff changeset
   747
        final LocalVariableGen current;
7f561c08de6b Initial load
duke
parents:
diff changeset
   748
        current = methodGen.addLocalVariable2("current",
7f561c08de6b Initial load
duke
parents:
diff changeset
   749
                                              com.sun.org.apache.bcel.internal.generic.Type.INT,
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   750
                                              null);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   751
        _currentIndex = current.getIndex();
7f561c08de6b Initial load
duke
parents:
diff changeset
   752
7f561c08de6b Initial load
duke
parents:
diff changeset
   753
        // Create the "body" instruction list that will eventually hold the
7f561c08de6b Initial load
duke
parents:
diff changeset
   754
        // code for the entire method (other ILs will be appended).
7f561c08de6b Initial load
duke
parents:
diff changeset
   755
        final InstructionList body = new InstructionList();
7f561c08de6b Initial load
duke
parents:
diff changeset
   756
        body.append(NOP);
7f561c08de6b Initial load
duke
parents:
diff changeset
   757
7f561c08de6b Initial load
duke
parents:
diff changeset
   758
        // Create an instruction list that contains the default next-node
7f561c08de6b Initial load
duke
parents:
diff changeset
   759
        // iteration
7f561c08de6b Initial load
duke
parents:
diff changeset
   760
        final InstructionList ilLoop = new InstructionList();
7f561c08de6b Initial load
duke
parents:
diff changeset
   761
        ilLoop.append(methodGen.loadIterator());
7f561c08de6b Initial load
duke
parents:
diff changeset
   762
        ilLoop.append(methodGen.nextNode());
7f561c08de6b Initial load
duke
parents:
diff changeset
   763
        ilLoop.append(DUP);
7f561c08de6b Initial load
duke
parents:
diff changeset
   764
        ilLoop.append(new ISTORE(_currentIndex));
7f561c08de6b Initial load
duke
parents:
diff changeset
   765
7f561c08de6b Initial load
duke
parents:
diff changeset
   766
        // The body of this code can get very large - large than can be handled
7f561c08de6b Initial load
duke
parents:
diff changeset
   767
        // by a single IFNE(body.getStart()) instruction - need workaround:
7f561c08de6b Initial load
duke
parents:
diff changeset
   768
        final BranchHandle ifeq = ilLoop.append(new IFLT(null));
7f561c08de6b Initial load
duke
parents:
diff changeset
   769
        final BranchHandle loop = ilLoop.append(new GOTO_W(null));
7f561c08de6b Initial load
duke
parents:
diff changeset
   770
        ifeq.setTarget(ilLoop.append(RETURN));  // applyTemplates() ends here!
7f561c08de6b Initial load
duke
parents:
diff changeset
   771
        final InstructionHandle ihLoop = ilLoop.getStart();
7f561c08de6b Initial load
duke
parents:
diff changeset
   772
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   773
        current.setStart(mainIL.append(new GOTO_W(ihLoop)));
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   774
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   775
        // Live range of "current" ends at end of loop
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   776
        current.setEnd(loop);
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   777
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   778
        // Compile default handling of elements (traverse children)
7f561c08de6b Initial load
duke
parents:
diff changeset
   779
        InstructionList ilRecurse =
7f561c08de6b Initial load
duke
parents:
diff changeset
   780
            compileDefaultRecursion(classGen, methodGen, ihLoop);
7f561c08de6b Initial load
duke
parents:
diff changeset
   781
        InstructionHandle ihRecurse = ilRecurse.getStart();
7f561c08de6b Initial load
duke
parents:
diff changeset
   782
7f561c08de6b Initial load
duke
parents:
diff changeset
   783
        // Compile default handling of text/attribute nodes (output text)
7f561c08de6b Initial load
duke
parents:
diff changeset
   784
        InstructionList ilText =
7f561c08de6b Initial load
duke
parents:
diff changeset
   785
            compileDefaultText(classGen, methodGen, ihLoop);
7f561c08de6b Initial load
duke
parents:
diff changeset
   786
        InstructionHandle ihText = ilText.getStart();
7f561c08de6b Initial load
duke
parents:
diff changeset
   787
7f561c08de6b Initial load
duke
parents:
diff changeset
   788
        // Distinguish attribute/element/namespace tests for further processing
7f561c08de6b Initial load
duke
parents:
diff changeset
   789
        final int[] types = new int[DTM.NTYPES + names.size()];
7f561c08de6b Initial load
duke
parents:
diff changeset
   790
        for (int i = 0; i < types.length; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   791
            types[i] = i;
7f561c08de6b Initial load
duke
parents:
diff changeset
   792
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   793
7f561c08de6b Initial load
duke
parents:
diff changeset
   794
        // Initialize isAttribute[] and isNamespace[] arrays
7f561c08de6b Initial load
duke
parents:
diff changeset
   795
        final boolean[] isAttribute = new boolean[types.length];
7f561c08de6b Initial load
duke
parents:
diff changeset
   796
        final boolean[] isNamespace = new boolean[types.length];
7f561c08de6b Initial load
duke
parents:
diff changeset
   797
        for (int i = 0; i < names.size(); i++) {
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   798
            final String name = names.get(i);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   799
            isAttribute[i + DTM.NTYPES] = isAttributeName(name);
7f561c08de6b Initial load
duke
parents:
diff changeset
   800
            isNamespace[i + DTM.NTYPES] = isNamespaceName(name);
7f561c08de6b Initial load
duke
parents:
diff changeset
   801
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   802
7f561c08de6b Initial load
duke
parents:
diff changeset
   803
        // Compile all templates - regardless of pattern type
7f561c08de6b Initial load
duke
parents:
diff changeset
   804
        compileTemplates(classGen, methodGen, ihLoop);
7f561c08de6b Initial load
duke
parents:
diff changeset
   805
7f561c08de6b Initial load
duke
parents:
diff changeset
   806
        // Handle template with explicit "*" pattern
7f561c08de6b Initial load
duke
parents:
diff changeset
   807
        final TestSeq elemTest = _testSeq[DTM.ELEMENT_NODE];
7f561c08de6b Initial load
duke
parents:
diff changeset
   808
        InstructionHandle ihElem = ihRecurse;
7f561c08de6b Initial load
duke
parents:
diff changeset
   809
        if (elemTest != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   810
            ihElem = elemTest.compile(classGen, methodGen, ihRecurse);
7f561c08de6b Initial load
duke
parents:
diff changeset
   811
7f561c08de6b Initial load
duke
parents:
diff changeset
   812
        // Handle template with explicit "@*" pattern
7f561c08de6b Initial load
duke
parents:
diff changeset
   813
        final TestSeq attrTest = _testSeq[DTM.ATTRIBUTE_NODE];
7f561c08de6b Initial load
duke
parents:
diff changeset
   814
        InstructionHandle ihAttr = ihText;
7f561c08de6b Initial load
duke
parents:
diff changeset
   815
        if (attrTest != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   816
            ihAttr = attrTest.compile(classGen, methodGen, ihAttr);
7f561c08de6b Initial load
duke
parents:
diff changeset
   817
7f561c08de6b Initial load
duke
parents:
diff changeset
   818
        // Do tests for id() and key() patterns first
7f561c08de6b Initial load
duke
parents:
diff changeset
   819
        InstructionList ilKey = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   820
        if (_idxTestSeq != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   821
            loop.setTarget(_idxTestSeq.compile(classGen, methodGen, body.getStart()));
7f561c08de6b Initial load
duke
parents:
diff changeset
   822
            ilKey = _idxTestSeq.getInstructionList();
7f561c08de6b Initial load
duke
parents:
diff changeset
   823
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   824
        else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   825
            loop.setTarget(body.getStart());
7f561c08de6b Initial load
duke
parents:
diff changeset
   826
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   827
7f561c08de6b Initial load
duke
parents:
diff changeset
   828
        // If there is a match on node() we need to replace ihElem
7f561c08de6b Initial load
duke
parents:
diff changeset
   829
        // and ihText if the priority of node() is higher
7f561c08de6b Initial load
duke
parents:
diff changeset
   830
        if (_childNodeTestSeq != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   831
            // Compare priorities of node() and "*"
7f561c08de6b Initial load
duke
parents:
diff changeset
   832
            double nodePrio = _childNodeTestSeq.getPriority();
7f561c08de6b Initial load
duke
parents:
diff changeset
   833
            int    nodePos  = _childNodeTestSeq.getPosition();
7f561c08de6b Initial load
duke
parents:
diff changeset
   834
            double elemPrio = (0 - Double.MAX_VALUE);
7f561c08de6b Initial load
duke
parents:
diff changeset
   835
            int    elemPos  = Integer.MIN_VALUE;
7f561c08de6b Initial load
duke
parents:
diff changeset
   836
7f561c08de6b Initial load
duke
parents:
diff changeset
   837
            if (elemTest != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   838
                elemPrio = elemTest.getPriority();
7f561c08de6b Initial load
duke
parents:
diff changeset
   839
                elemPos  = elemTest.getPosition();
7f561c08de6b Initial load
duke
parents:
diff changeset
   840
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   841
            if (elemPrio == Double.NaN || elemPrio < nodePrio ||
7f561c08de6b Initial load
duke
parents:
diff changeset
   842
                (elemPrio == nodePrio && elemPos < nodePos))
7f561c08de6b Initial load
duke
parents:
diff changeset
   843
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
   844
                ihElem = _childNodeTestSeq.compile(classGen, methodGen, ihLoop);
7f561c08de6b Initial load
duke
parents:
diff changeset
   845
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   846
7f561c08de6b Initial load
duke
parents:
diff changeset
   847
            // Compare priorities of node() and text()
7f561c08de6b Initial load
duke
parents:
diff changeset
   848
            final TestSeq textTest = _testSeq[DTM.TEXT_NODE];
7f561c08de6b Initial load
duke
parents:
diff changeset
   849
            double textPrio = (0 - Double.MAX_VALUE);
7f561c08de6b Initial load
duke
parents:
diff changeset
   850
            int    textPos  = Integer.MIN_VALUE;
7f561c08de6b Initial load
duke
parents:
diff changeset
   851
7f561c08de6b Initial load
duke
parents:
diff changeset
   852
            if (textTest != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   853
                textPrio = textTest.getPriority();
7f561c08de6b Initial load
duke
parents:
diff changeset
   854
                textPos  = textTest.getPosition();
7f561c08de6b Initial load
duke
parents:
diff changeset
   855
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   856
            if (textPrio == Double.NaN || textPrio < nodePrio ||
7f561c08de6b Initial load
duke
parents:
diff changeset
   857
                (textPrio == nodePrio && textPos < nodePos))
7f561c08de6b Initial load
duke
parents:
diff changeset
   858
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
   859
                ihText = _childNodeTestSeq.compile(classGen, methodGen, ihLoop);
7f561c08de6b Initial load
duke
parents:
diff changeset
   860
                _testSeq[DTM.TEXT_NODE] = _childNodeTestSeq;
7f561c08de6b Initial load
duke
parents:
diff changeset
   861
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   862
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   863
7f561c08de6b Initial load
duke
parents:
diff changeset
   864
        // Handle templates with "ns:*" pattern
7f561c08de6b Initial load
duke
parents:
diff changeset
   865
        InstructionHandle elemNamespaceHandle = ihElem;
7f561c08de6b Initial load
duke
parents:
diff changeset
   866
        InstructionList nsElem = compileNamespaces(classGen, methodGen,
7f561c08de6b Initial load
duke
parents:
diff changeset
   867
                                                   isNamespace, isAttribute,
7f561c08de6b Initial load
duke
parents:
diff changeset
   868
                                                   false, ihElem);
7f561c08de6b Initial load
duke
parents:
diff changeset
   869
        if (nsElem != null) elemNamespaceHandle = nsElem.getStart();
7f561c08de6b Initial load
duke
parents:
diff changeset
   870
7f561c08de6b Initial load
duke
parents:
diff changeset
   871
        // Handle templates with "ns:@*" pattern
7f561c08de6b Initial load
duke
parents:
diff changeset
   872
        InstructionHandle attrNamespaceHandle = ihAttr;
7f561c08de6b Initial load
duke
parents:
diff changeset
   873
        InstructionList nsAttr = compileNamespaces(classGen, methodGen,
7f561c08de6b Initial load
duke
parents:
diff changeset
   874
                                                   isNamespace, isAttribute,
7f561c08de6b Initial load
duke
parents:
diff changeset
   875
                                                   true, ihAttr);
7f561c08de6b Initial load
duke
parents:
diff changeset
   876
        if (nsAttr != null) attrNamespaceHandle = nsAttr.getStart();
7f561c08de6b Initial load
duke
parents:
diff changeset
   877
7f561c08de6b Initial load
duke
parents:
diff changeset
   878
        // Handle templates with "ns:elem" or "ns:@attr" pattern
7f561c08de6b Initial load
duke
parents:
diff changeset
   879
        final InstructionHandle[] targets = new InstructionHandle[types.length];
7f561c08de6b Initial load
duke
parents:
diff changeset
   880
        for (int i = DTM.NTYPES; i < targets.length; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   881
            final TestSeq testSeq = _testSeq[i];
7f561c08de6b Initial load
duke
parents:
diff changeset
   882
            // Jump straight to namespace tests ?
7f561c08de6b Initial load
duke
parents:
diff changeset
   883
            if (isNamespace[i]) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   884
                if (isAttribute[i])
7f561c08de6b Initial load
duke
parents:
diff changeset
   885
                    targets[i] = attrNamespaceHandle;
7f561c08de6b Initial load
duke
parents:
diff changeset
   886
                else
7f561c08de6b Initial load
duke
parents:
diff changeset
   887
                    targets[i] = elemNamespaceHandle;
7f561c08de6b Initial load
duke
parents:
diff changeset
   888
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   889
            // Test first, then jump to namespace tests
7f561c08de6b Initial load
duke
parents:
diff changeset
   890
            else if (testSeq != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   891
                if (isAttribute[i])
7f561c08de6b Initial load
duke
parents:
diff changeset
   892
                    targets[i] = testSeq.compile(classGen, methodGen,
7f561c08de6b Initial load
duke
parents:
diff changeset
   893
                                                 attrNamespaceHandle);
7f561c08de6b Initial load
duke
parents:
diff changeset
   894
                else
7f561c08de6b Initial load
duke
parents:
diff changeset
   895
                    targets[i] = testSeq.compile(classGen, methodGen,
7f561c08de6b Initial load
duke
parents:
diff changeset
   896
                                                 elemNamespaceHandle);
7f561c08de6b Initial load
duke
parents:
diff changeset
   897
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   898
            else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   899
                targets[i] = ihLoop;
7f561c08de6b Initial load
duke
parents:
diff changeset
   900
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   901
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   902
7f561c08de6b Initial load
duke
parents:
diff changeset
   903
7f561c08de6b Initial load
duke
parents:
diff changeset
   904
        // Handle pattern with match on root node - default: traverse children
7f561c08de6b Initial load
duke
parents:
diff changeset
   905
        targets[DTM.ROOT_NODE] = _rootPattern != null
7f561c08de6b Initial load
duke
parents:
diff changeset
   906
            ? getTemplateInstructionHandle(_rootPattern.getTemplate())
7f561c08de6b Initial load
duke
parents:
diff changeset
   907
            : ihRecurse;
7f561c08de6b Initial load
duke
parents:
diff changeset
   908
7f561c08de6b Initial load
duke
parents:
diff changeset
   909
        // Handle pattern with match on root node - default: traverse children
7f561c08de6b Initial load
duke
parents:
diff changeset
   910
        targets[DTM.DOCUMENT_NODE] = _rootPattern != null
7f561c08de6b Initial load
duke
parents:
diff changeset
   911
            ? getTemplateInstructionHandle(_rootPattern.getTemplate())
7f561c08de6b Initial load
duke
parents:
diff changeset
   912
            : ihRecurse;
7f561c08de6b Initial load
duke
parents:
diff changeset
   913
7f561c08de6b Initial load
duke
parents:
diff changeset
   914
        // Handle any pattern with match on text nodes - default: output text
7f561c08de6b Initial load
duke
parents:
diff changeset
   915
        targets[DTM.TEXT_NODE] = _testSeq[DTM.TEXT_NODE] != null
7f561c08de6b Initial load
duke
parents:
diff changeset
   916
            ? _testSeq[DTM.TEXT_NODE].compile(classGen, methodGen, ihText)
7f561c08de6b Initial load
duke
parents:
diff changeset
   917
            : ihText;
7f561c08de6b Initial load
duke
parents:
diff changeset
   918
7f561c08de6b Initial load
duke
parents:
diff changeset
   919
        // This DOM-type is not in use - default: process next node
7f561c08de6b Initial load
duke
parents:
diff changeset
   920
        targets[DTM.NAMESPACE_NODE] = ihLoop;
7f561c08de6b Initial load
duke
parents:
diff changeset
   921
7f561c08de6b Initial load
duke
parents:
diff changeset
   922
        // Match unknown element in DOM - default: check for namespace match
7f561c08de6b Initial load
duke
parents:
diff changeset
   923
        targets[DTM.ELEMENT_NODE] = elemNamespaceHandle;
7f561c08de6b Initial load
duke
parents:
diff changeset
   924
7f561c08de6b Initial load
duke
parents:
diff changeset
   925
        // Match unknown attribute in DOM - default: check for namespace match
7f561c08de6b Initial load
duke
parents:
diff changeset
   926
        targets[DTM.ATTRIBUTE_NODE] = attrNamespaceHandle;
7f561c08de6b Initial load
duke
parents:
diff changeset
   927
7f561c08de6b Initial load
duke
parents:
diff changeset
   928
        // Match on processing instruction - default: process next node
7f561c08de6b Initial load
duke
parents:
diff changeset
   929
        InstructionHandle ihPI = ihLoop;
7f561c08de6b Initial load
duke
parents:
diff changeset
   930
        if (_childNodeTestSeq != null) ihPI = ihElem;
7f561c08de6b Initial load
duke
parents:
diff changeset
   931
        if (_testSeq[DTM.PROCESSING_INSTRUCTION_NODE] != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   932
            targets[DTM.PROCESSING_INSTRUCTION_NODE] =
7f561c08de6b Initial load
duke
parents:
diff changeset
   933
                _testSeq[DTM.PROCESSING_INSTRUCTION_NODE].
7f561c08de6b Initial load
duke
parents:
diff changeset
   934
                compile(classGen, methodGen, ihPI);
7f561c08de6b Initial load
duke
parents:
diff changeset
   935
        else
7f561c08de6b Initial load
duke
parents:
diff changeset
   936
            targets[DTM.PROCESSING_INSTRUCTION_NODE] = ihPI;
7f561c08de6b Initial load
duke
parents:
diff changeset
   937
7f561c08de6b Initial load
duke
parents:
diff changeset
   938
        // Match on comments - default: process next node
7f561c08de6b Initial load
duke
parents:
diff changeset
   939
        InstructionHandle ihComment = ihLoop;
7f561c08de6b Initial load
duke
parents:
diff changeset
   940
        if (_childNodeTestSeq != null) ihComment = ihElem;
7f561c08de6b Initial load
duke
parents:
diff changeset
   941
        targets[DTM.COMMENT_NODE] = _testSeq[DTM.COMMENT_NODE] != null
7f561c08de6b Initial load
duke
parents:
diff changeset
   942
            ? _testSeq[DTM.COMMENT_NODE].compile(classGen, methodGen, ihComment)
7f561c08de6b Initial load
duke
parents:
diff changeset
   943
            : ihComment;
7f561c08de6b Initial load
duke
parents:
diff changeset
   944
7f561c08de6b Initial load
duke
parents:
diff changeset
   945
            // This DOM-type is not in use - default: process next node
7f561c08de6b Initial load
duke
parents:
diff changeset
   946
        targets[DTM.CDATA_SECTION_NODE] = ihLoop;
7f561c08de6b Initial load
duke
parents:
diff changeset
   947
7f561c08de6b Initial load
duke
parents:
diff changeset
   948
        // This DOM-type is not in use - default: process next node
7f561c08de6b Initial load
duke
parents:
diff changeset
   949
        targets[DTM.DOCUMENT_FRAGMENT_NODE] = ihLoop;
7f561c08de6b Initial load
duke
parents:
diff changeset
   950
7f561c08de6b Initial load
duke
parents:
diff changeset
   951
        // This DOM-type is not in use - default: process next node
7f561c08de6b Initial load
duke
parents:
diff changeset
   952
        targets[DTM.DOCUMENT_TYPE_NODE] = ihLoop;
7f561c08de6b Initial load
duke
parents:
diff changeset
   953
7f561c08de6b Initial load
duke
parents:
diff changeset
   954
        // This DOM-type is not in use - default: process next node
7f561c08de6b Initial load
duke
parents:
diff changeset
   955
        targets[DTM.ENTITY_NODE] = ihLoop;
7f561c08de6b Initial load
duke
parents:
diff changeset
   956
7f561c08de6b Initial load
duke
parents:
diff changeset
   957
        // This DOM-type is not in use - default: process next node
7f561c08de6b Initial load
duke
parents:
diff changeset
   958
        targets[DTM.ENTITY_REFERENCE_NODE] = ihLoop;
7f561c08de6b Initial load
duke
parents:
diff changeset
   959
7f561c08de6b Initial load
duke
parents:
diff changeset
   960
        // This DOM-type is not in use - default: process next node
7f561c08de6b Initial load
duke
parents:
diff changeset
   961
        targets[DTM.NOTATION_NODE] = ihLoop;
7f561c08de6b Initial load
duke
parents:
diff changeset
   962
7f561c08de6b Initial load
duke
parents:
diff changeset
   963
7f561c08de6b Initial load
duke
parents:
diff changeset
   964
        // Now compile test sequences for various match patterns:
7f561c08de6b Initial load
duke
parents:
diff changeset
   965
        for (int i = DTM.NTYPES; i < targets.length; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   966
            final TestSeq testSeq = _testSeq[i];
7f561c08de6b Initial load
duke
parents:
diff changeset
   967
            // Jump straight to namespace tests ?
7f561c08de6b Initial load
duke
parents:
diff changeset
   968
            if ((testSeq == null) || (isNamespace[i])) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   969
                if (isAttribute[i])
7f561c08de6b Initial load
duke
parents:
diff changeset
   970
                    targets[i] = attrNamespaceHandle;
7f561c08de6b Initial load
duke
parents:
diff changeset
   971
                else
7f561c08de6b Initial load
duke
parents:
diff changeset
   972
                    targets[i] = elemNamespaceHandle;
7f561c08de6b Initial load
duke
parents:
diff changeset
   973
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   974
            // Match on node type
7f561c08de6b Initial load
duke
parents:
diff changeset
   975
            else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   976
                if (isAttribute[i])
7f561c08de6b Initial load
duke
parents:
diff changeset
   977
                    targets[i] = testSeq.compile(classGen, methodGen,
7f561c08de6b Initial load
duke
parents:
diff changeset
   978
                                                 attrNamespaceHandle);
7f561c08de6b Initial load
duke
parents:
diff changeset
   979
                else
7f561c08de6b Initial load
duke
parents:
diff changeset
   980
                    targets[i] = testSeq.compile(classGen, methodGen,
7f561c08de6b Initial load
duke
parents:
diff changeset
   981
                                                 elemNamespaceHandle);
7f561c08de6b Initial load
duke
parents:
diff changeset
   982
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   983
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   984
7f561c08de6b Initial load
duke
parents:
diff changeset
   985
        if (ilKey != null) body.insert(ilKey);
7f561c08de6b Initial load
duke
parents:
diff changeset
   986
7f561c08de6b Initial load
duke
parents:
diff changeset
   987
        // Append first code in applyTemplates() - get type of current node
7f561c08de6b Initial load
duke
parents:
diff changeset
   988
        final int getType = cpg.addInterfaceMethodref(DOM_INTF,
7f561c08de6b Initial load
duke
parents:
diff changeset
   989
                                                      "getExpandedTypeID",
7f561c08de6b Initial load
duke
parents:
diff changeset
   990
                                                      "(I)I");
7f561c08de6b Initial load
duke
parents:
diff changeset
   991
        body.append(methodGen.loadDOM());
7f561c08de6b Initial load
duke
parents:
diff changeset
   992
        body.append(new ILOAD(_currentIndex));
7f561c08de6b Initial load
duke
parents:
diff changeset
   993
        body.append(new INVOKEINTERFACE(getType, 2));
7f561c08de6b Initial load
duke
parents:
diff changeset
   994
7f561c08de6b Initial load
duke
parents:
diff changeset
   995
        // Append switch() statement - main dispatch loop in applyTemplates()
7f561c08de6b Initial load
duke
parents:
diff changeset
   996
        InstructionHandle disp = body.append(new SWITCH(types, targets, ihLoop));
7f561c08de6b Initial load
duke
parents:
diff changeset
   997
7f561c08de6b Initial load
duke
parents:
diff changeset
   998
        // Append all the "case:" statements
7f561c08de6b Initial load
duke
parents:
diff changeset
   999
        appendTestSequences(body);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1000
        // Append the actual template code
7f561c08de6b Initial load
duke
parents:
diff changeset
  1001
        appendTemplateCode(body);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1002
7f561c08de6b Initial load
duke
parents:
diff changeset
  1003
        // Append NS:* node tests (if any)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1004
        if (nsElem != null) body.append(nsElem);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1005
        // Append NS:@* node tests (if any)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1006
        if (nsAttr != null) body.append(nsAttr);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1007
7f561c08de6b Initial load
duke
parents:
diff changeset
  1008
        // Append default action for element and root nodes
7f561c08de6b Initial load
duke
parents:
diff changeset
  1009
        body.append(ilRecurse);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1010
        // Append default action for text and attribute nodes
7f561c08de6b Initial load
duke
parents:
diff changeset
  1011
        body.append(ilText);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1012
7f561c08de6b Initial load
duke
parents:
diff changeset
  1013
        // putting together constituent instruction lists
7f561c08de6b Initial load
duke
parents:
diff changeset
  1014
        mainIL.append(body);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1015
        // fall through to ilLoop
7f561c08de6b Initial load
duke
parents:
diff changeset
  1016
        mainIL.append(ilLoop);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1017
7f561c08de6b Initial load
duke
parents:
diff changeset
  1018
        peepHoleOptimization(methodGen);
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
  1019
        classGen.addMethod(methodGen);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1020
7f561c08de6b Initial load
duke
parents:
diff changeset
  1021
        // Compile method(s) for <xsl:apply-imports/> for this mode
7f561c08de6b Initial load
duke
parents:
diff changeset
  1022
        if (_importLevels != null) {
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
  1023
            for (Map.Entry<Integer, Integer> entry : _importLevels.entrySet()) {
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
  1024
                compileApplyImports(classGen, entry.getValue(), entry.getKey());
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1025
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1026
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1027
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1028
7f561c08de6b Initial load
duke
parents:
diff changeset
  1029
    private void compileTemplateCalls(ClassGenerator classGen,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1030
                                      MethodGenerator methodGen,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1031
                                      InstructionHandle next, int min, int max){
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
  1032
        _neededTemplates.keySet().stream().forEach((template) -> {
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1033
            final int prec = template.getImportPrecedence();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1034
            if ((prec >= min) && (prec < max)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1035
                if (template.hasContents()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1036
                    InstructionList til = template.compile(classGen, methodGen);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1037
                    til.append(new GOTO_W(next));
7f561c08de6b Initial load
duke
parents:
diff changeset
  1038
                    _templateILs.put(template, til);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1039
                    _templateIHs.put(template, til.getStart());
7f561c08de6b Initial load
duke
parents:
diff changeset
  1040
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1041
                else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1042
                    // empty template
7f561c08de6b Initial load
duke
parents:
diff changeset
  1043
                    _templateIHs.put(template, next);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1044
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1045
            }
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
  1046
        });
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1047
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1048
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
  1049
    @SuppressWarnings({"rawtypes", "unchecked"})
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1050
    public void compileApplyImports(ClassGenerator classGen, int min, int max) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1051
        final XSLTC xsltc = classGen.getParser().getXSLTC();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1052
        final ConstantPoolGen cpg = classGen.getConstantPool();
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
  1053
        final List<String> names = xsltc.getNamesIndex();
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1054
7f561c08de6b Initial load
duke
parents:
diff changeset
  1055
        // Clear some datastructures
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
  1056
        _namedTemplates = new HashMap<>();
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
  1057
        _neededTemplates = new HashMap<>();
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
  1058
        _templateIHs = new HashMap<>();
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
  1059
        _templateILs = new HashMap<>();
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
  1060
        _patternGroups = (List<LocationPathPattern>[])new ArrayList[32];
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1061
        _rootPattern = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1062
7f561c08de6b Initial load
duke
parents:
diff changeset
  1063
        // IMPORTANT: Save orignal & complete set of templates!!!!
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
  1064
        List<Template> oldTemplates = _templates;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1065
7f561c08de6b Initial load
duke
parents:
diff changeset
  1066
        // Gather templates that are within the scope of this import
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
  1067
        _templates = new ArrayList<>();
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
  1068
        for (Template template : oldTemplates) {
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1069
            final int prec = template.getImportPrecedence();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1070
            if ((prec >= min) && (prec < max)) addTemplate(template);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1071
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1072
7f561c08de6b Initial load
duke
parents:
diff changeset
  1073
        // Process all patterns from those templates
7f561c08de6b Initial load
duke
parents:
diff changeset
  1074
        processPatterns(_keys);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1075
7f561c08de6b Initial load
duke
parents:
diff changeset
  1076
        // Create the applyTemplates() method
7f561c08de6b Initial load
duke
parents:
diff changeset
  1077
        final com.sun.org.apache.bcel.internal.generic.Type[] argTypes =
7f561c08de6b Initial load
duke
parents:
diff changeset
  1078
            new com.sun.org.apache.bcel.internal.generic.Type[4];
7f561c08de6b Initial load
duke
parents:
diff changeset
  1079
        argTypes[0] = Util.getJCRefType(DOM_INTF_SIG);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1080
        argTypes[1] = Util.getJCRefType(NODE_ITERATOR_SIG);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1081
        argTypes[2] = Util.getJCRefType(TRANSLET_OUTPUT_SIG);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1082
        argTypes[3] = com.sun.org.apache.bcel.internal.generic.Type.INT;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1083
7f561c08de6b Initial load
duke
parents:
diff changeset
  1084
        final String[] argNames = new String[4];
7f561c08de6b Initial load
duke
parents:
diff changeset
  1085
        argNames[0] = DOCUMENT_PNAME;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1086
        argNames[1] = ITERATOR_PNAME;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1087
        argNames[2] = TRANSLET_OUTPUT_PNAME;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1088
        argNames[3] = NODE_PNAME;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1089
7f561c08de6b Initial load
duke
parents:
diff changeset
  1090
        final InstructionList mainIL = new InstructionList();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1091
        final MethodGenerator methodGen =
7f561c08de6b Initial load
duke
parents:
diff changeset
  1092
            new MethodGenerator(ACC_PUBLIC | ACC_FINAL,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1093
                                com.sun.org.apache.bcel.internal.generic.Type.VOID,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1094
                                argTypes, argNames, functionName()+'_'+max,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1095
                                getClassName(), mainIL,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1096
                                classGen.getConstantPool());
7f561c08de6b Initial load
duke
parents:
diff changeset
  1097
        methodGen.addException("com.sun.org.apache.xalan.internal.xsltc.TransletException");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1098
7f561c08de6b Initial load
duke
parents:
diff changeset
  1099
        // Create the local variable to hold the current node
7f561c08de6b Initial load
duke
parents:
diff changeset
  1100
        final LocalVariableGen current;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1101
        current = methodGen.addLocalVariable2("current",
7f561c08de6b Initial load
duke
parents:
diff changeset
  1102
                                              com.sun.org.apache.bcel.internal.generic.Type.INT,
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
  1103
                                              null);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1104
        _currentIndex = current.getIndex();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1105
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
  1106
        mainIL.append(new ILOAD(methodGen.getLocalIndex(NODE_PNAME)));
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
  1107
        current.setStart(mainIL.append(new ISTORE(_currentIndex)));
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1108
7f561c08de6b Initial load
duke
parents:
diff changeset
  1109
        // Create the "body" instruction list that will eventually hold the
7f561c08de6b Initial load
duke
parents:
diff changeset
  1110
        // code for the entire method (other ILs will be appended).
7f561c08de6b Initial load
duke
parents:
diff changeset
  1111
        final InstructionList body = new InstructionList();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1112
        body.append(NOP);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1113
7f561c08de6b Initial load
duke
parents:
diff changeset
  1114
        // Create an instruction list that contains the default next-node
7f561c08de6b Initial load
duke
parents:
diff changeset
  1115
        // iteration
7f561c08de6b Initial load
duke
parents:
diff changeset
  1116
        final InstructionList ilLoop = new InstructionList();
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
  1117
        ilLoop.append(RETURN);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1118
        final InstructionHandle ihLoop = ilLoop.getStart();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1119
7f561c08de6b Initial load
duke
parents:
diff changeset
  1120
        // Compile default handling of elements (traverse children)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1121
        InstructionList ilRecurse =
7f561c08de6b Initial load
duke
parents:
diff changeset
  1122
            compileDefaultRecursion(classGen, methodGen, ihLoop);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1123
        InstructionHandle ihRecurse = ilRecurse.getStart();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1124
7f561c08de6b Initial load
duke
parents:
diff changeset
  1125
        // Compile default handling of text/attribute nodes (output text)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1126
        InstructionList ilText =
7f561c08de6b Initial load
duke
parents:
diff changeset
  1127
            compileDefaultText(classGen, methodGen, ihLoop);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1128
        InstructionHandle ihText = ilText.getStart();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1129
7f561c08de6b Initial load
duke
parents:
diff changeset
  1130
        // Distinguish attribute/element/namespace tests for further processing
7f561c08de6b Initial load
duke
parents:
diff changeset
  1131
        final int[] types = new int[DTM.NTYPES + names.size()];
7f561c08de6b Initial load
duke
parents:
diff changeset
  1132
        for (int i = 0; i < types.length; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1133
            types[i] = i;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1134
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1135
7f561c08de6b Initial load
duke
parents:
diff changeset
  1136
        final boolean[] isAttribute = new boolean[types.length];
7f561c08de6b Initial load
duke
parents:
diff changeset
  1137
        final boolean[] isNamespace = new boolean[types.length];
7f561c08de6b Initial load
duke
parents:
diff changeset
  1138
        for (int i = 0; i < names.size(); i++) {
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
  1139
            final String name = (String)names.get(i);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1140
            isAttribute[i+DTM.NTYPES] = isAttributeName(name);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1141
            isNamespace[i+DTM.NTYPES] = isNamespaceName(name);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1142
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1143
7f561c08de6b Initial load
duke
parents:
diff changeset
  1144
        // Compile all templates - regardless of pattern type
7f561c08de6b Initial load
duke
parents:
diff changeset
  1145
        compileTemplateCalls(classGen, methodGen, ihLoop, min, max);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1146
7f561c08de6b Initial load
duke
parents:
diff changeset
  1147
        // Handle template with explicit "*" pattern
7f561c08de6b Initial load
duke
parents:
diff changeset
  1148
        final TestSeq elemTest = _testSeq[DTM.ELEMENT_NODE];
7f561c08de6b Initial load
duke
parents:
diff changeset
  1149
        InstructionHandle ihElem = ihRecurse;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1150
        if (elemTest != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1151
            ihElem = elemTest.compile(classGen, methodGen, ihLoop);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1152
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1153
7f561c08de6b Initial load
duke
parents:
diff changeset
  1154
        // Handle template with explicit "@*" pattern
7f561c08de6b Initial load
duke
parents:
diff changeset
  1155
        final TestSeq attrTest = _testSeq[DTM.ATTRIBUTE_NODE];
7f561c08de6b Initial load
duke
parents:
diff changeset
  1156
        InstructionHandle ihAttr = ihLoop;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1157
        if (attrTest != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1158
            ihAttr = attrTest.compile(classGen, methodGen, ihAttr);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1159
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1160
7f561c08de6b Initial load
duke
parents:
diff changeset
  1161
        // Do tests for id() and key() patterns first
7f561c08de6b Initial load
duke
parents:
diff changeset
  1162
        InstructionList ilKey = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1163
        if (_idxTestSeq != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1164
            ilKey = _idxTestSeq.getInstructionList();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1165
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1166
7f561c08de6b Initial load
duke
parents:
diff changeset
  1167
        // If there is a match on node() we need to replace ihElem
7f561c08de6b Initial load
duke
parents:
diff changeset
  1168
        // and ihText if the priority of node() is higher
7f561c08de6b Initial load
duke
parents:
diff changeset
  1169
        if (_childNodeTestSeq != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1170
            // Compare priorities of node() and "*"
7f561c08de6b Initial load
duke
parents:
diff changeset
  1171
            double nodePrio = _childNodeTestSeq.getPriority();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1172
            int    nodePos  = _childNodeTestSeq.getPosition();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1173
            double elemPrio = (0 - Double.MAX_VALUE);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1174
            int    elemPos  = Integer.MIN_VALUE;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1175
7f561c08de6b Initial load
duke
parents:
diff changeset
  1176
            if (elemTest != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1177
                elemPrio = elemTest.getPriority();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1178
                elemPos  = elemTest.getPosition();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1179
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1180
7f561c08de6b Initial load
duke
parents:
diff changeset
  1181
            if (elemPrio == Double.NaN || elemPrio < nodePrio ||
7f561c08de6b Initial load
duke
parents:
diff changeset
  1182
                (elemPrio == nodePrio && elemPos < nodePos))
7f561c08de6b Initial load
duke
parents:
diff changeset
  1183
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1184
                ihElem = _childNodeTestSeq.compile(classGen, methodGen, ihLoop);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1185
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1186
7f561c08de6b Initial load
duke
parents:
diff changeset
  1187
            // Compare priorities of node() and text()
7f561c08de6b Initial load
duke
parents:
diff changeset
  1188
            final TestSeq textTest = _testSeq[DTM.TEXT_NODE];
7f561c08de6b Initial load
duke
parents:
diff changeset
  1189
            double textPrio = (0 - Double.MAX_VALUE);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1190
            int    textPos  = Integer.MIN_VALUE;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1191
7f561c08de6b Initial load
duke
parents:
diff changeset
  1192
            if (textTest != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1193
                textPrio = textTest.getPriority();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1194
                textPos  = textTest.getPosition();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1195
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1196
7f561c08de6b Initial load
duke
parents:
diff changeset
  1197
            if (textPrio == Double.NaN || textPrio < nodePrio ||
7f561c08de6b Initial load
duke
parents:
diff changeset
  1198
                (textPrio == nodePrio && textPos < nodePos))
7f561c08de6b Initial load
duke
parents:
diff changeset
  1199
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1200
                ihText = _childNodeTestSeq.compile(classGen, methodGen, ihLoop);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1201
                _testSeq[DTM.TEXT_NODE] = _childNodeTestSeq;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1202
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1203
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1204
7f561c08de6b Initial load
duke
parents:
diff changeset
  1205
        // Handle templates with "ns:*" pattern
7f561c08de6b Initial load
duke
parents:
diff changeset
  1206
        InstructionHandle elemNamespaceHandle = ihElem;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1207
        InstructionList nsElem = compileNamespaces(classGen, methodGen,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1208
                                                   isNamespace, isAttribute,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1209
                                                   false, ihElem);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1210
        if (nsElem != null) elemNamespaceHandle = nsElem.getStart();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1211
7f561c08de6b Initial load
duke
parents:
diff changeset
  1212
        // Handle templates with "ns:@*" pattern
7f561c08de6b Initial load
duke
parents:
diff changeset
  1213
        InstructionList nsAttr = compileNamespaces(classGen, methodGen,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1214
                                                   isNamespace, isAttribute,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1215
                                                   true, ihAttr);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1216
        InstructionHandle attrNamespaceHandle = ihAttr;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1217
        if (nsAttr != null) attrNamespaceHandle = nsAttr.getStart();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1218
7f561c08de6b Initial load
duke
parents:
diff changeset
  1219
        // Handle templates with "ns:elem" or "ns:@attr" pattern
7f561c08de6b Initial load
duke
parents:
diff changeset
  1220
        final InstructionHandle[] targets = new InstructionHandle[types.length];
7f561c08de6b Initial load
duke
parents:
diff changeset
  1221
        for (int i = DTM.NTYPES; i < targets.length; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1222
            final TestSeq testSeq = _testSeq[i];
7f561c08de6b Initial load
duke
parents:
diff changeset
  1223
            // Jump straight to namespace tests ?
7f561c08de6b Initial load
duke
parents:
diff changeset
  1224
            if (isNamespace[i]) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1225
                if (isAttribute[i])
7f561c08de6b Initial load
duke
parents:
diff changeset
  1226
                    targets[i] = attrNamespaceHandle;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1227
                else
7f561c08de6b Initial load
duke
parents:
diff changeset
  1228
                    targets[i] = elemNamespaceHandle;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1229
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1230
            // Test first, then jump to namespace tests
7f561c08de6b Initial load
duke
parents:
diff changeset
  1231
            else if (testSeq != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1232
                if (isAttribute[i])
7f561c08de6b Initial load
duke
parents:
diff changeset
  1233
                    targets[i] = testSeq.compile(classGen, methodGen,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1234
                                                 attrNamespaceHandle);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1235
                else
7f561c08de6b Initial load
duke
parents:
diff changeset
  1236
                    targets[i] = testSeq.compile(classGen, methodGen,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1237
                                                 elemNamespaceHandle);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1238
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1239
            else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1240
                targets[i] = ihLoop;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1241
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1242
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1243
7f561c08de6b Initial load
duke
parents:
diff changeset
  1244
        // Handle pattern with match on root node - default: traverse children
7f561c08de6b Initial load
duke
parents:
diff changeset
  1245
        targets[DTM.ROOT_NODE] = _rootPattern != null
7f561c08de6b Initial load
duke
parents:
diff changeset
  1246
            ? getTemplateInstructionHandle(_rootPattern.getTemplate())
7f561c08de6b Initial load
duke
parents:
diff changeset
  1247
            : ihRecurse;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1248
        // Handle pattern with match on root node - default: traverse children
7f561c08de6b Initial load
duke
parents:
diff changeset
  1249
        targets[DTM.DOCUMENT_NODE] = _rootPattern != null
7f561c08de6b Initial load
duke
parents:
diff changeset
  1250
            ? getTemplateInstructionHandle(_rootPattern.getTemplate())
7f561c08de6b Initial load
duke
parents:
diff changeset
  1251
            : ihRecurse;    // %HZ%:  Was ihLoop in XSLTC_DTM branch
7f561c08de6b Initial load
duke
parents:
diff changeset
  1252
7f561c08de6b Initial load
duke
parents:
diff changeset
  1253
        // Handle any pattern with match on text nodes - default: loop
7f561c08de6b Initial load
duke
parents:
diff changeset
  1254
        targets[DTM.TEXT_NODE] = _testSeq[DTM.TEXT_NODE] != null
7f561c08de6b Initial load
duke
parents:
diff changeset
  1255
            ? _testSeq[DTM.TEXT_NODE].compile(classGen, methodGen, ihText)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1256
            : ihText;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1257
7f561c08de6b Initial load
duke
parents:
diff changeset
  1258
        // This DOM-type is not in use - default: process next node
7f561c08de6b Initial load
duke
parents:
diff changeset
  1259
        targets[DTM.NAMESPACE_NODE] = ihLoop;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1260
7f561c08de6b Initial load
duke
parents:
diff changeset
  1261
        // Match unknown element in DOM - default: check for namespace match
7f561c08de6b Initial load
duke
parents:
diff changeset
  1262
        targets[DTM.ELEMENT_NODE] = elemNamespaceHandle;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1263
7f561c08de6b Initial load
duke
parents:
diff changeset
  1264
        // Match unknown attribute in DOM - default: check for namespace match
7f561c08de6b Initial load
duke
parents:
diff changeset
  1265
        targets[DTM.ATTRIBUTE_NODE] = attrNamespaceHandle;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1266
7f561c08de6b Initial load
duke
parents:
diff changeset
  1267
        // Match on processing instruction - default: loop
7f561c08de6b Initial load
duke
parents:
diff changeset
  1268
        InstructionHandle ihPI = ihLoop;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1269
        if (_childNodeTestSeq != null) ihPI = ihElem;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1270
        if (_testSeq[DTM.PROCESSING_INSTRUCTION_NODE] != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1271
            targets[DTM.PROCESSING_INSTRUCTION_NODE] =
7f561c08de6b Initial load
duke
parents:
diff changeset
  1272
                _testSeq[DTM.PROCESSING_INSTRUCTION_NODE].
7f561c08de6b Initial load
duke
parents:
diff changeset
  1273
                compile(classGen, methodGen, ihPI);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1274
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1275
        else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1276
            targets[DTM.PROCESSING_INSTRUCTION_NODE] = ihPI;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1277
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1278
7f561c08de6b Initial load
duke
parents:
diff changeset
  1279
        // Match on comments - default: process next node
7f561c08de6b Initial load
duke
parents:
diff changeset
  1280
        InstructionHandle ihComment = ihLoop;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1281
        if (_childNodeTestSeq != null) ihComment = ihElem;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1282
        targets[DTM.COMMENT_NODE] = _testSeq[DTM.COMMENT_NODE] != null
7f561c08de6b Initial load
duke
parents:
diff changeset
  1283
            ? _testSeq[DTM.COMMENT_NODE].compile(classGen, methodGen, ihComment)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1284
            : ihComment;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1285
7f561c08de6b Initial load
duke
parents:
diff changeset
  1286
                // This DOM-type is not in use - default: process next node
7f561c08de6b Initial load
duke
parents:
diff changeset
  1287
        targets[DTM.CDATA_SECTION_NODE] = ihLoop;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1288
7f561c08de6b Initial load
duke
parents:
diff changeset
  1289
        // This DOM-type is not in use - default: process next node
7f561c08de6b Initial load
duke
parents:
diff changeset
  1290
        targets[DTM.DOCUMENT_FRAGMENT_NODE] = ihLoop;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1291
7f561c08de6b Initial load
duke
parents:
diff changeset
  1292
        // This DOM-type is not in use - default: process next node
7f561c08de6b Initial load
duke
parents:
diff changeset
  1293
        targets[DTM.DOCUMENT_TYPE_NODE] = ihLoop;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1294
7f561c08de6b Initial load
duke
parents:
diff changeset
  1295
        // This DOM-type is not in use - default: process next node
7f561c08de6b Initial load
duke
parents:
diff changeset
  1296
        targets[DTM.ENTITY_NODE] = ihLoop;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1297
7f561c08de6b Initial load
duke
parents:
diff changeset
  1298
        // This DOM-type is not in use - default: process next node
7f561c08de6b Initial load
duke
parents:
diff changeset
  1299
        targets[DTM.ENTITY_REFERENCE_NODE] = ihLoop;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1300
7f561c08de6b Initial load
duke
parents:
diff changeset
  1301
        // This DOM-type is not in use - default: process next node
7f561c08de6b Initial load
duke
parents:
diff changeset
  1302
        targets[DTM.NOTATION_NODE] = ihLoop;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1303
7f561c08de6b Initial load
duke
parents:
diff changeset
  1304
7f561c08de6b Initial load
duke
parents:
diff changeset
  1305
7f561c08de6b Initial load
duke
parents:
diff changeset
  1306
        // Now compile test sequences for various match patterns:
7f561c08de6b Initial load
duke
parents:
diff changeset
  1307
        for (int i = DTM.NTYPES; i < targets.length; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1308
            final TestSeq testSeq = _testSeq[i];
7f561c08de6b Initial load
duke
parents:
diff changeset
  1309
            // Jump straight to namespace tests ?
7f561c08de6b Initial load
duke
parents:
diff changeset
  1310
            if ((testSeq == null) || (isNamespace[i])) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1311
                if (isAttribute[i])
7f561c08de6b Initial load
duke
parents:
diff changeset
  1312
                    targets[i] = attrNamespaceHandle;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1313
                else
7f561c08de6b Initial load
duke
parents:
diff changeset
  1314
                    targets[i] = elemNamespaceHandle;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1315
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1316
            // Match on node type
7f561c08de6b Initial load
duke
parents:
diff changeset
  1317
            else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1318
                if (isAttribute[i])
7f561c08de6b Initial load
duke
parents:
diff changeset
  1319
                    targets[i] = testSeq.compile(classGen, methodGen,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1320
                                                 attrNamespaceHandle);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1321
                else
7f561c08de6b Initial load
duke
parents:
diff changeset
  1322
                    targets[i] = testSeq.compile(classGen, methodGen,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1323
                                                 elemNamespaceHandle);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1324
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1325
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1326
7f561c08de6b Initial load
duke
parents:
diff changeset
  1327
        if (ilKey != null) body.insert(ilKey);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1328
7f561c08de6b Initial load
duke
parents:
diff changeset
  1329
        // Append first code in applyTemplates() - get type of current node
7f561c08de6b Initial load
duke
parents:
diff changeset
  1330
        final int getType = cpg.addInterfaceMethodref(DOM_INTF,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1331
                                                      "getExpandedTypeID",
7f561c08de6b Initial load
duke
parents:
diff changeset
  1332
                                                      "(I)I");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1333
        body.append(methodGen.loadDOM());
7f561c08de6b Initial load
duke
parents:
diff changeset
  1334
        body.append(new ILOAD(_currentIndex));
7f561c08de6b Initial load
duke
parents:
diff changeset
  1335
        body.append(new INVOKEINTERFACE(getType, 2));
7f561c08de6b Initial load
duke
parents:
diff changeset
  1336
7f561c08de6b Initial load
duke
parents:
diff changeset
  1337
        // Append switch() statement - main dispatch loop in applyTemplates()
7f561c08de6b Initial load
duke
parents:
diff changeset
  1338
        InstructionHandle disp = body.append(new SWITCH(types,targets,ihLoop));
7f561c08de6b Initial load
duke
parents:
diff changeset
  1339
7f561c08de6b Initial load
duke
parents:
diff changeset
  1340
        // Append all the "case:" statements
7f561c08de6b Initial load
duke
parents:
diff changeset
  1341
        appendTestSequences(body);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1342
        // Append the actual template code
7f561c08de6b Initial load
duke
parents:
diff changeset
  1343
        appendTemplateCode(body);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1344
7f561c08de6b Initial load
duke
parents:
diff changeset
  1345
        // Append NS:* node tests (if any)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1346
        if (nsElem != null) body.append(nsElem);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1347
        // Append NS:@* node tests (if any)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1348
        if (nsAttr != null) body.append(nsAttr);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1349
7f561c08de6b Initial load
duke
parents:
diff changeset
  1350
        // Append default action for element and root nodes
7f561c08de6b Initial load
duke
parents:
diff changeset
  1351
        body.append(ilRecurse);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1352
        // Append default action for text and attribute nodes
7f561c08de6b Initial load
duke
parents:
diff changeset
  1353
        body.append(ilText);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1354
7f561c08de6b Initial load
duke
parents:
diff changeset
  1355
        // putting together constituent instruction lists
7f561c08de6b Initial load
duke
parents:
diff changeset
  1356
        mainIL.append(body);
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
  1357
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
  1358
        // Mark the end of the live range for the "current" variable
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
  1359
        current.setEnd(body.getEnd());
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
  1360
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1361
        // fall through to ilLoop
7f561c08de6b Initial load
duke
parents:
diff changeset
  1362
        mainIL.append(ilLoop);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1363
7f561c08de6b Initial load
duke
parents:
diff changeset
  1364
        peepHoleOptimization(methodGen);
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
  1365
        classGen.addMethod(methodGen);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1366
7f561c08de6b Initial load
duke
parents:
diff changeset
  1367
        // Restore original (complete) set of templates for this transformation
7f561c08de6b Initial load
duke
parents:
diff changeset
  1368
        _templates = oldTemplates;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1369
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1370
7f561c08de6b Initial load
duke
parents:
diff changeset
  1371
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1372
      * Peephole optimization.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1373
      */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1374
    private void peepHoleOptimization(MethodGenerator methodGen) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1375
        InstructionList il = methodGen.getInstructionList();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1376
        InstructionFinder find = new InstructionFinder(il);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1377
        InstructionHandle ih;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1378
        String pattern;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1379
7f561c08de6b Initial load
duke
parents:
diff changeset
  1380
        // LoadInstruction, POP => (removed)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1381
        // pattern = "LoadInstruction POP";
7f561c08de6b Initial load
duke
parents:
diff changeset
  1382
        // changed to lower case - changing to all lower case although only the instruction with capital I
7f561c08de6b Initial load
duke
parents:
diff changeset
  1383
        // is creating a problem in the Turkish locale
7f561c08de6b Initial load
duke
parents:
diff changeset
  1384
        pattern = "loadinstruction pop";
7f561c08de6b Initial load
duke
parents:
diff changeset
  1385
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
  1386
        for (Iterator<InstructionHandle[]> iter = find.search(pattern); iter.hasNext();) {
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
  1387
            InstructionHandle[] match = iter.next();
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1388
            try {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1389
                if (!match[0].hasTargeters() && !match[1].hasTargeters()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1390
                    il.delete(match[0], match[1]);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1391
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1392
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1393
            catch (TargetLostException e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1394
                // TODO: move target down into the list
7f561c08de6b Initial load
duke
parents:
diff changeset
  1395
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1396
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1397
7f561c08de6b Initial load
duke
parents:
diff changeset
  1398
        // ILOAD_N, ILOAD_N, SWAP, ISTORE_N => ILOAD_N
7f561c08de6b Initial load
duke
parents:
diff changeset
  1399
        // pattern = "ILOAD ILOAD SWAP ISTORE";
7f561c08de6b Initial load
duke
parents:
diff changeset
  1400
        // changed to lower case - changing to all lower case although only the instruction with capital I
7f561c08de6b Initial load
duke
parents:
diff changeset
  1401
        // is creating a problem in the Turkish locale
7f561c08de6b Initial load
duke
parents:
diff changeset
  1402
        pattern = "iload iload swap istore";
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
  1403
        for (Iterator<InstructionHandle[]> iter = find.search(pattern); iter.hasNext();) {
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
  1404
            InstructionHandle[] match = iter.next();
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1405
            try {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1406
                com.sun.org.apache.bcel.internal.generic.ILOAD iload1 =
7f561c08de6b Initial load
duke
parents:
diff changeset
  1407
                    (com.sun.org.apache.bcel.internal.generic.ILOAD) match[0].getInstruction();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1408
                com.sun.org.apache.bcel.internal.generic.ILOAD iload2 =
7f561c08de6b Initial load
duke
parents:
diff changeset
  1409
                    (com.sun.org.apache.bcel.internal.generic.ILOAD) match[1].getInstruction();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1410
                com.sun.org.apache.bcel.internal.generic.ISTORE istore =
7f561c08de6b Initial load
duke
parents:
diff changeset
  1411
                    (com.sun.org.apache.bcel.internal.generic.ISTORE) match[3].getInstruction();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1412
7f561c08de6b Initial load
duke
parents:
diff changeset
  1413
                if (!match[1].hasTargeters() &&
7f561c08de6b Initial load
duke
parents:
diff changeset
  1414
                    !match[2].hasTargeters() &&
7f561c08de6b Initial load
duke
parents:
diff changeset
  1415
                    !match[3].hasTargeters() &&
7f561c08de6b Initial load
duke
parents:
diff changeset
  1416
                    iload1.getIndex() == iload2.getIndex() &&
7f561c08de6b Initial load
duke
parents:
diff changeset
  1417
                    iload2.getIndex() == istore.getIndex())
7f561c08de6b Initial load
duke
parents:
diff changeset
  1418
                {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1419
                    il.delete(match[1], match[3]);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1420
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1421
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1422
            catch (TargetLostException e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1423
                // TODO: move target down into the list
7f561c08de6b Initial load
duke
parents:
diff changeset
  1424
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1425
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1426
7f561c08de6b Initial load
duke
parents:
diff changeset
  1427
        // LoadInstruction_N, LoadInstruction_M, SWAP => LoadInstruction_M, LoadInstruction_N
7f561c08de6b Initial load
duke
parents:
diff changeset
  1428
        // pattern = "LoadInstruction LoadInstruction SWAP";
7f561c08de6b Initial load
duke
parents:
diff changeset
  1429
        // changed to lower case - changing to all lower case although only the instruction with capital I
7f561c08de6b Initial load
duke
parents:
diff changeset
  1430
        // is creating a problem in the Turkish locale
7f561c08de6b Initial load
duke
parents:
diff changeset
  1431
        pattern = "loadinstruction loadinstruction swap";
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
  1432
        for (Iterator<InstructionHandle[]> iter = find.search(pattern); iter.hasNext();) {
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
  1433
            InstructionHandle[] match = iter.next();
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1434
            try {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1435
                if (!match[0].hasTargeters() &&
7f561c08de6b Initial load
duke
parents:
diff changeset
  1436
                    !match[1].hasTargeters() &&
7f561c08de6b Initial load
duke
parents:
diff changeset
  1437
                    !match[2].hasTargeters())
7f561c08de6b Initial load
duke
parents:
diff changeset
  1438
                {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1439
                    Instruction load_m = match[1].getInstruction();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1440
                    il.insert(match[0], load_m);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1441
                    il.delete(match[1], match[2]);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1442
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1443
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1444
            catch (TargetLostException e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1445
                // TODO: move target down into the list
7f561c08de6b Initial load
duke
parents:
diff changeset
  1446
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1447
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1448
7f561c08de6b Initial load
duke
parents:
diff changeset
  1449
        // ALOAD_N ALOAD_N => ALOAD_N DUP
7f561c08de6b Initial load
duke
parents:
diff changeset
  1450
        // pattern = "ALOAD ALOAD";
7f561c08de6b Initial load
duke
parents:
diff changeset
  1451
        // changed to lower case - changing to all lower case although only the instruction with capital I
7f561c08de6b Initial load
duke
parents:
diff changeset
  1452
        // is creating a problem in the Turkish locale
7f561c08de6b Initial load
duke
parents:
diff changeset
  1453
        pattern = "aload aload";
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
  1454
        for (Iterator<InstructionHandle[]> iter = find.search(pattern); iter.hasNext();) {
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
  1455
            InstructionHandle[] match = iter.next();
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1456
            try {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1457
                if (!match[1].hasTargeters()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1458
                    com.sun.org.apache.bcel.internal.generic.ALOAD aload1 =
7f561c08de6b Initial load
duke
parents:
diff changeset
  1459
                        (com.sun.org.apache.bcel.internal.generic.ALOAD) match[0].getInstruction();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1460
                    com.sun.org.apache.bcel.internal.generic.ALOAD aload2 =
7f561c08de6b Initial load
duke
parents:
diff changeset
  1461
                        (com.sun.org.apache.bcel.internal.generic.ALOAD) match[1].getInstruction();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1462
7f561c08de6b Initial load
duke
parents:
diff changeset
  1463
                    if (aload1.getIndex() == aload2.getIndex()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1464
                        il.insert(match[1], new DUP());
7f561c08de6b Initial load
duke
parents:
diff changeset
  1465
                        il.delete(match[1]);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1466
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1467
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1468
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1469
            catch (TargetLostException e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1470
                // TODO: move target down into the list
7f561c08de6b Initial load
duke
parents:
diff changeset
  1471
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1472
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1473
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1474
7f561c08de6b Initial load
duke
parents:
diff changeset
  1475
    public InstructionHandle getTemplateInstructionHandle(Template template) {
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
  1476
        return _templateIHs.get(template);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1477
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1478
7f561c08de6b Initial load
duke
parents:
diff changeset
  1479
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1480
     * Auxiliary method to determine if a qname is an attribute.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1481
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1482
    private static boolean isAttributeName(String qname) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1483
        final int col = qname.lastIndexOf(':') + 1;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1484
        return (qname.charAt(col) == '@');
7f561c08de6b Initial load
duke
parents:
diff changeset
  1485
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1486
7f561c08de6b Initial load
duke
parents:
diff changeset
  1487
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1488
     * Auxiliary method to determine if a qname is a namespace
7f561c08de6b Initial load
duke
parents:
diff changeset
  1489
     * qualified "*".
7f561c08de6b Initial load
duke
parents:
diff changeset
  1490
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1491
    private static boolean isNamespaceName(String qname) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1492
        final int col = qname.lastIndexOf(':');
7f561c08de6b Initial load
duke
parents:
diff changeset
  1493
        return (col > -1 && qname.charAt(qname.length()-1) == '*');
7f561c08de6b Initial load
duke
parents:
diff changeset
  1494
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1495
}