src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/compiler/Parser.java
author joehw
Fri, 19 Jul 2019 02:40:59 +0000
changeset 55742 6e1161923897
parent 48577 8dff65f1d611
permissions -rw-r--r--
8157830: Errors in XSLT stylesheet are not dispatched correctly to ErrorListener Reviewed-by: lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     1
/*
55742
6e1161923897 8157830: Errors in XSLT stylesheet are not dispatched correctly to ErrorListener
joehw
parents: 48577
diff changeset
     2
 * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     3
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
     4
/*
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
     5
 * Licensed to the Apache Software Foundation (ASF) under one or more
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
     6
 * contributor license agreements.  See the NOTICE file distributed with
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
     7
 * this work for additional information regarding copyright ownership.
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
     8
 * The ASF licenses this file to You under the Apache License, Version 2.0
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
     9
 * (the "License"); you may not use this file except in compliance with
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
    10
 * the License.  You may obtain a copy of the License at
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    11
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    12
 *     http://www.apache.org/licenses/LICENSE-2.0
7f561c08de6b Initial load
duke
parents:
diff changeset
    13
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    14
 * Unless required by applicable law or agreed to in writing, software
7f561c08de6b Initial load
duke
parents:
diff changeset
    15
 * distributed under the License is distributed on an "AS IS" BASIS,
7f561c08de6b Initial load
duke
parents:
diff changeset
    16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7f561c08de6b Initial load
duke
parents:
diff changeset
    17
 * See the License for the specific language governing permissions and
7f561c08de6b Initial load
duke
parents:
diff changeset
    18
 * limitations under the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    19
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    20
7f561c08de6b Initial load
duke
parents:
diff changeset
    21
package com.sun.org.apache.xalan.internal.xsltc.compiler;
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
17534
21dc0b2762da 8011653: Upgrade JDK8 to JAXP 1.5
joehw
parents: 16953
diff changeset
    23
import com.sun.java_cup.internal.runtime.Symbol;
21dc0b2762da 8011653: Upgrade JDK8 to JAXP 1.5
joehw
parents: 16953
diff changeset
    24
import com.sun.org.apache.xalan.internal.XalanConstants;
21dc0b2762da 8011653: Upgrade JDK8 to JAXP 1.5
joehw
parents: 16953
diff changeset
    25
import com.sun.org.apache.xalan.internal.utils.ObjectFactory;
20968
dde41f8b7b96 8014530: Better digital signature processing
joehw
parents: 18354
diff changeset
    26
import com.sun.org.apache.xalan.internal.utils.XMLSecurityManager;
17534
21dc0b2762da 8011653: Upgrade JDK8 to JAXP 1.5
joehw
parents: 16953
diff changeset
    27
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
21dc0b2762da 8011653: Upgrade JDK8 to JAXP 1.5
joehw
parents: 16953
diff changeset
    28
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodType;
21dc0b2762da 8011653: Upgrade JDK8 to JAXP 1.5
joehw
parents: 16953
diff changeset
    29
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
21dc0b2762da 8011653: Upgrade JDK8 to JAXP 1.5
joehw
parents: 16953
diff changeset
    30
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
21dc0b2762da 8011653: Upgrade JDK8 to JAXP 1.5
joehw
parents: 16953
diff changeset
    31
import com.sun.org.apache.xml.internal.serializer.utils.SystemIDResolver;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
import java.io.File;
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
import java.io.IOException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
import java.io.StringReader;
39907
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents: 35729
diff changeset
    35
import java.util.ArrayList;
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
    36
import java.util.HashMap;
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
    37
import java.util.Iterator;
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
    38
import java.util.List;
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
    39
import java.util.Map;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
import java.util.Properties;
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
import java.util.Stack;
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
import java.util.StringTokenizer;
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
import javax.xml.XMLConstants;
39907
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents: 35729
diff changeset
    44
import javax.xml.catalog.CatalogFeatures;
55742
6e1161923897 8157830: Errors in XSLT stylesheet are not dispatched correctly to ErrorListener
joehw
parents: 48577
diff changeset
    45
import jdk.xml.internal.ErrorHandlerProxy;
39907
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents: 35729
diff changeset
    46
import jdk.xml.internal.JdkXmlFeatures;
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents: 35729
diff changeset
    47
import jdk.xml.internal.JdkXmlUtils;
47312
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
    48
import jdk.xml.internal.SecuritySupport;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
import org.xml.sax.Attributes;
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
import org.xml.sax.ContentHandler;
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
import org.xml.sax.InputSource;
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
import org.xml.sax.Locator;
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
import org.xml.sax.SAXException;
18354
ce3ebc4a1aec 8016153: Property http://javax.xml.XMLConstants/property/accessExternalDTD is not recognized.
joehw
parents: 17534
diff changeset
    54
import org.xml.sax.SAXNotRecognizedException;
39907
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents: 35729
diff changeset
    55
import org.xml.sax.SAXNotSupportedException;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
import org.xml.sax.XMLReader;
17534
21dc0b2762da 8011653: Upgrade JDK8 to JAXP 1.5
joehw
parents: 16953
diff changeset
    57
import org.xml.sax.helpers.AttributesImpl;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
 * @author Jacek Ambroziak
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
 * @author Santiago Pericas-Geertsen
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
 * @author G. Todd Miller
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>
55742
6e1161923897 8157830: Errors in XSLT stylesheet are not dispatched correctly to ErrorListener
joehw
parents: 48577
diff changeset
    65
 * @LastModified: July 2019
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
public class Parser implements Constants, ContentHandler {
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
39909
00e4298ae168 8162598: XSLTC transformer swallows empty namespace declaration which is needed to undeclare default namespace
clanger
parents: 39907
diff changeset
    69
    private static final String XSL = "xsl";           // standard prefix
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
    private static final String TRANSLET = "translet"; // extension prefix
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
    private Locator _locator = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
39909
00e4298ae168 8162598: XSLTC transformer swallows empty namespace declaration which is needed to undeclare default namespace
clanger
parents: 39907
diff changeset
    74
    private XSLTC _xsltc;                  // Reference to the compiler object.
00e4298ae168 8162598: XSLTC transformer swallows empty namespace declaration which is needed to undeclare default namespace
clanger
parents: 39907
diff changeset
    75
    private XPathParser _xpathParser;      // Reference to the XPath parser.
00e4298ae168 8162598: XSLTC transformer swallows empty namespace declaration which is needed to undeclare default namespace
clanger
parents: 39907
diff changeset
    76
    private ArrayList<ErrorMsg> _errors;   // Contains all compilation errors
00e4298ae168 8162598: XSLTC transformer swallows empty namespace declaration which is needed to undeclare default namespace
clanger
parents: 39907
diff changeset
    77
    private ArrayList<ErrorMsg> _warnings; // Contains all compilation warnings
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
    79
    private Map<String, String>   _instructionClasses; // Maps instructions to classes
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
    80
    private Map<String, String[]> _instructionAttrs;  // reqd and opt attrs
39909
00e4298ae168 8162598: XSLTC transformer swallows empty namespace declaration which is needed to undeclare default namespace
clanger
parents: 39907
diff changeset
    81
    private Map<String, QName>    _qNames;
00e4298ae168 8162598: XSLTC transformer swallows empty namespace declaration which is needed to undeclare default namespace
clanger
parents: 39907
diff changeset
    82
    private Map<String, Map<String, QName>> _namespaces;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
    private QName       _useAttributeSets;
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
    private QName       _excludeResultPrefixes;
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
    private QName       _extensionElementPrefixes;
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
    86
    private Map<String, Object>   _variableScope;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
    private Stylesheet  _currentStylesheet;
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
    private SymbolTable _symbolTable; // Maps QNames to syntax-tree nodes
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
    private Output      _output;
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
    private Template    _template;    // Reference to the template being parsed.
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
    private boolean     _rootNamespaceDef; // Used for validity check
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
    private SyntaxTreeNode _root;
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
    private String _target;
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
    private int _currentImportPrecedence;
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
48577
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 48409
diff changeset
   100
    private boolean _overrideDefaultParser;
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   101
55742
6e1161923897 8157830: Errors in XSLT stylesheet are not dispatched correctly to ErrorListener
joehw
parents: 48577
diff changeset
   102
    // flag indicates whether there's an user's ErrorListener
6e1161923897 8157830: Errors in XSLT stylesheet are not dispatched correctly to ErrorListener
joehw
parents: 48577
diff changeset
   103
    private boolean _hasUserErrListener;
6e1161923897 8157830: Errors in XSLT stylesheet are not dispatched correctly to ErrorListener
joehw
parents: 48577
diff changeset
   104
6e1161923897 8157830: Errors in XSLT stylesheet are not dispatched correctly to ErrorListener
joehw
parents: 48577
diff changeset
   105
    public Parser(XSLTC xsltc, boolean useOverrideDefaultParser, boolean hasUserErrListener) {
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
        _xsltc = xsltc;
48577
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 48409
diff changeset
   107
        _overrideDefaultParser = useOverrideDefaultParser;
55742
6e1161923897 8157830: Errors in XSLT stylesheet are not dispatched correctly to ErrorListener
joehw
parents: 48577
diff changeset
   108
        _hasUserErrListener = hasUserErrListener;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
    public void init() {
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
   112
        _qNames              = new HashMap<>(512);
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
   113
        _namespaces          = new HashMap<>();
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
   114
        _instructionClasses  = new HashMap<>();
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
   115
        _instructionAttrs    = new HashMap<>();
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
   116
        _variableScope       = new HashMap<>();
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
        _template            = null;
39907
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents: 35729
diff changeset
   118
        _errors              = new ArrayList<>();
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents: 35729
diff changeset
   119
        _warnings            = new ArrayList<>();
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
        _symbolTable         = new SymbolTable();
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
        _xpathParser         = new XPathParser(this);
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
        _currentStylesheet   = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
        _output              = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
        _root                = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
        _rootNamespaceDef    = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
        _currentImportPrecedence = 1;
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
        initStdClasses();
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
        initInstructionAttrs();
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
        initExtClasses();
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
        initSymbolTable();
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
        _useAttributeSets =
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
            getQName(XSLT_URI, XSL, "use-attribute-sets");
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
        _excludeResultPrefixes =
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
            getQName(XSLT_URI, XSL, "exclude-result-prefixes");
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
        _extensionElementPrefixes =
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
            getQName(XSLT_URI, XSL, "extension-element-prefixes");
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
    public void setOutput(Output output) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
        if (_output != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
            if (_output.getImportPrecedence() <= output.getImportPrecedence()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
                output.mergeOutput(_output);
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
                _output.disable();
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
                _output = output;
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
            else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
                output.disable();
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
        else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
            _output = output;
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
    public Output getOutput() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
        return _output;
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
    public Properties getOutputProperties() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
        return getTopLevelStylesheet().getOutputProperties();
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
    public void addVariable(Variable var) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
        addVariableOrParam(var);
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
    public void addParameter(Param param) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
        addVariableOrParam(param);
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
    private void addVariableOrParam(VariableBase var) {
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
   174
        Object existing = _variableScope.get(var.getName().getStringRep());
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
        if (existing != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
            if (existing instanceof Stack) {
39909
00e4298ae168 8162598: XSLTC transformer swallows empty namespace declaration which is needed to undeclare default namespace
clanger
parents: 39907
diff changeset
   177
                @SuppressWarnings("unchecked")
00e4298ae168 8162598: XSLTC transformer swallows empty namespace declaration which is needed to undeclare default namespace
clanger
parents: 39907
diff changeset
   178
                Stack<VariableBase> stack = (Stack<VariableBase>)existing;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
                stack.push(var);
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
            else if (existing instanceof VariableBase) {
39909
00e4298ae168 8162598: XSLTC transformer swallows empty namespace declaration which is needed to undeclare default namespace
clanger
parents: 39907
diff changeset
   182
                Stack<VariableBase> stack = new Stack<>();
00e4298ae168 8162598: XSLTC transformer swallows empty namespace declaration which is needed to undeclare default namespace
clanger
parents: 39907
diff changeset
   183
                stack.push((VariableBase)existing);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   184
                stack.push(var);
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
   185
                _variableScope.put(var.getName().getStringRep(), stack);
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
        else {
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
   189
            _variableScope.put(var.getName().getStringRep(), var);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   190
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   191
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   192
7f561c08de6b Initial load
duke
parents:
diff changeset
   193
    public void removeVariable(QName name) {
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
   194
        Object existing = _variableScope.get(name.getStringRep());
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   195
        if (existing instanceof Stack) {
39909
00e4298ae168 8162598: XSLTC transformer swallows empty namespace declaration which is needed to undeclare default namespace
clanger
parents: 39907
diff changeset
   196
            @SuppressWarnings("unchecked")
00e4298ae168 8162598: XSLTC transformer swallows empty namespace declaration which is needed to undeclare default namespace
clanger
parents: 39907
diff changeset
   197
            Stack<VariableBase> stack = (Stack<VariableBase>)existing;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   198
            if (!stack.isEmpty()) stack.pop();
7f561c08de6b Initial load
duke
parents:
diff changeset
   199
            if (!stack.isEmpty()) return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   200
        }
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
   201
        _variableScope.remove(name.getStringRep());
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   202
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   203
7f561c08de6b Initial load
duke
parents:
diff changeset
   204
    public VariableBase lookupVariable(QName name) {
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
   205
        Object existing = _variableScope.get(name.getStringRep());
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   206
        if (existing instanceof VariableBase) {
39909
00e4298ae168 8162598: XSLTC transformer swallows empty namespace declaration which is needed to undeclare default namespace
clanger
parents: 39907
diff changeset
   207
            return (VariableBase)existing;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   208
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   209
        else if (existing instanceof Stack) {
39909
00e4298ae168 8162598: XSLTC transformer swallows empty namespace declaration which is needed to undeclare default namespace
clanger
parents: 39907
diff changeset
   210
            @SuppressWarnings("unchecked")
00e4298ae168 8162598: XSLTC transformer swallows empty namespace declaration which is needed to undeclare default namespace
clanger
parents: 39907
diff changeset
   211
            Stack<VariableBase> stack = (Stack<VariableBase>)existing;
00e4298ae168 8162598: XSLTC transformer swallows empty namespace declaration which is needed to undeclare default namespace
clanger
parents: 39907
diff changeset
   212
            return stack.peek();
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   213
        }
39909
00e4298ae168 8162598: XSLTC transformer swallows empty namespace declaration which is needed to undeclare default namespace
clanger
parents: 39907
diff changeset
   214
        return null;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   215
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   216
7f561c08de6b Initial load
duke
parents:
diff changeset
   217
    public void setXSLTC(XSLTC xsltc) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   218
        _xsltc = xsltc;
7f561c08de6b Initial load
duke
parents:
diff changeset
   219
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   220
7f561c08de6b Initial load
duke
parents:
diff changeset
   221
    public XSLTC getXSLTC() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   222
        return _xsltc;
7f561c08de6b Initial load
duke
parents:
diff changeset
   223
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   224
7f561c08de6b Initial load
duke
parents:
diff changeset
   225
    public int getCurrentImportPrecedence() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   226
        return _currentImportPrecedence;
7f561c08de6b Initial load
duke
parents:
diff changeset
   227
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   228
7f561c08de6b Initial load
duke
parents:
diff changeset
   229
    public int getNextImportPrecedence() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   230
        return ++_currentImportPrecedence;
7f561c08de6b Initial load
duke
parents:
diff changeset
   231
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   232
7f561c08de6b Initial load
duke
parents:
diff changeset
   233
    public void setCurrentStylesheet(Stylesheet stylesheet) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   234
        _currentStylesheet = stylesheet;
7f561c08de6b Initial load
duke
parents:
diff changeset
   235
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   236
7f561c08de6b Initial load
duke
parents:
diff changeset
   237
    public Stylesheet getCurrentStylesheet() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   238
        return _currentStylesheet;
7f561c08de6b Initial load
duke
parents:
diff changeset
   239
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   240
7f561c08de6b Initial load
duke
parents:
diff changeset
   241
    public Stylesheet getTopLevelStylesheet() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   242
        return _xsltc.getStylesheet();
7f561c08de6b Initial load
duke
parents:
diff changeset
   243
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   244
7f561c08de6b Initial load
duke
parents:
diff changeset
   245
    public QName getQNameSafe(final String stringRep) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   246
        // parse and retrieve namespace
7f561c08de6b Initial load
duke
parents:
diff changeset
   247
        final int colon = stringRep.lastIndexOf(':');
7f561c08de6b Initial load
duke
parents:
diff changeset
   248
        if (colon != -1) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   249
            final String prefix = stringRep.substring(0, colon);
7f561c08de6b Initial load
duke
parents:
diff changeset
   250
            final String localname = stringRep.substring(colon + 1);
7f561c08de6b Initial load
duke
parents:
diff changeset
   251
            String namespace = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   252
7f561c08de6b Initial load
duke
parents:
diff changeset
   253
            // Get the namespace uri from the symbol table
7f561c08de6b Initial load
duke
parents:
diff changeset
   254
            if (prefix.equals(XMLNS_PREFIX) == false) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   255
                namespace = _symbolTable.lookupNamespace(prefix);
7f561c08de6b Initial load
duke
parents:
diff changeset
   256
                if (namespace == null) namespace = EMPTYSTRING;
7f561c08de6b Initial load
duke
parents:
diff changeset
   257
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   258
            return getQName(namespace, prefix, localname);
7f561c08de6b Initial load
duke
parents:
diff changeset
   259
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   260
        else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   261
            final String uri = stringRep.equals(XMLNS_PREFIX) ? null
7f561c08de6b Initial load
duke
parents:
diff changeset
   262
                : _symbolTable.lookupNamespace(EMPTYSTRING);
7f561c08de6b Initial load
duke
parents:
diff changeset
   263
            return getQName(uri, null, stringRep);
7f561c08de6b Initial load
duke
parents:
diff changeset
   264
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   265
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   266
7f561c08de6b Initial load
duke
parents:
diff changeset
   267
    public QName getQName(final String stringRep) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   268
        return getQName(stringRep, true, false);
7f561c08de6b Initial load
duke
parents:
diff changeset
   269
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   270
7f561c08de6b Initial load
duke
parents:
diff changeset
   271
    public QName getQNameIgnoreDefaultNs(final String stringRep) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   272
        return getQName(stringRep, true, true);
7f561c08de6b Initial load
duke
parents:
diff changeset
   273
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   274
7f561c08de6b Initial load
duke
parents:
diff changeset
   275
    public QName getQName(final String stringRep, boolean reportError) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   276
        return getQName(stringRep, reportError, false);
7f561c08de6b Initial load
duke
parents:
diff changeset
   277
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   278
7f561c08de6b Initial load
duke
parents:
diff changeset
   279
    private QName getQName(final String stringRep, boolean reportError,
7f561c08de6b Initial load
duke
parents:
diff changeset
   280
        boolean ignoreDefaultNs)
7f561c08de6b Initial load
duke
parents:
diff changeset
   281
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   282
        // parse and retrieve namespace
7f561c08de6b Initial load
duke
parents:
diff changeset
   283
        final int colon = stringRep.lastIndexOf(':');
7f561c08de6b Initial load
duke
parents:
diff changeset
   284
        if (colon != -1) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   285
            final String prefix = stringRep.substring(0, colon);
7f561c08de6b Initial load
duke
parents:
diff changeset
   286
            final String localname = stringRep.substring(colon + 1);
7f561c08de6b Initial load
duke
parents:
diff changeset
   287
            String namespace = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   288
7f561c08de6b Initial load
duke
parents:
diff changeset
   289
            // Get the namespace uri from the symbol table
7f561c08de6b Initial load
duke
parents:
diff changeset
   290
            if (prefix.equals(XMLNS_PREFIX) == false) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   291
                namespace = _symbolTable.lookupNamespace(prefix);
7f561c08de6b Initial load
duke
parents:
diff changeset
   292
                if (namespace == null && reportError) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   293
                    final int line = getLineNumber();
7f561c08de6b Initial load
duke
parents:
diff changeset
   294
                    ErrorMsg err = new ErrorMsg(ErrorMsg.NAMESPACE_UNDEF_ERR,
7f561c08de6b Initial load
duke
parents:
diff changeset
   295
                                                line, prefix);
7f561c08de6b Initial load
duke
parents:
diff changeset
   296
                    reportError(ERROR, err);
7f561c08de6b Initial load
duke
parents:
diff changeset
   297
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   298
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   299
            return getQName(namespace, prefix, localname);
7f561c08de6b Initial load
duke
parents:
diff changeset
   300
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   301
        else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   302
            if (stringRep.equals(XMLNS_PREFIX)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   303
                ignoreDefaultNs = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   304
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   305
            final String defURI = ignoreDefaultNs ? null
7f561c08de6b Initial load
duke
parents:
diff changeset
   306
                                  : _symbolTable.lookupNamespace(EMPTYSTRING);
7f561c08de6b Initial load
duke
parents:
diff changeset
   307
            return getQName(defURI, null, stringRep);
7f561c08de6b Initial load
duke
parents:
diff changeset
   308
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   309
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   310
7f561c08de6b Initial load
duke
parents:
diff changeset
   311
    public QName getQName(String namespace, String prefix, String localname) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   312
        if (namespace == null || namespace.equals(EMPTYSTRING)) {
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
   313
            QName name = _qNames.get(localname);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   314
            if (name == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   315
                name = new QName(null, prefix, localname);
7f561c08de6b Initial load
duke
parents:
diff changeset
   316
                _qNames.put(localname, name);
7f561c08de6b Initial load
duke
parents:
diff changeset
   317
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   318
            return name;
7f561c08de6b Initial load
duke
parents:
diff changeset
   319
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   320
        else {
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
   321
            Map<String, QName> space = _namespaces.get(namespace);
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   322
            String lexicalQName =
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   323
                       (prefix == null || prefix.length() == 0)
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   324
                            ? localname
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   325
                            : (prefix + ':' + localname);
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   326
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   327
            if (space == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   328
                final QName name = new QName(namespace, prefix, localname);
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
   329
                _namespaces.put(namespace, space = new HashMap<>());
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   330
                space.put(lexicalQName, name);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   331
                return name;
7f561c08de6b Initial load
duke
parents:
diff changeset
   332
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   333
            else {
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
   334
                QName name = space.get(lexicalQName);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   335
                if (name == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   336
                    name = new QName(namespace, prefix, localname);
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   337
                    space.put(lexicalQName, name);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   338
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   339
                return name;
7f561c08de6b Initial load
duke
parents:
diff changeset
   340
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   341
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   342
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   343
7f561c08de6b Initial load
duke
parents:
diff changeset
   344
    public QName getQName(String scope, String name) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   345
        return getQName(scope + name);
7f561c08de6b Initial load
duke
parents:
diff changeset
   346
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   347
7f561c08de6b Initial load
duke
parents:
diff changeset
   348
    public QName getQName(QName scope, QName name) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   349
        return getQName(scope.toString() + name.toString());
7f561c08de6b Initial load
duke
parents:
diff changeset
   350
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   351
7f561c08de6b Initial load
duke
parents:
diff changeset
   352
    public QName getUseAttributeSets() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   353
        return _useAttributeSets;
7f561c08de6b Initial load
duke
parents:
diff changeset
   354
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   355
7f561c08de6b Initial load
duke
parents:
diff changeset
   356
    public QName getExtensionElementPrefixes() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   357
        return _extensionElementPrefixes;
7f561c08de6b Initial load
duke
parents:
diff changeset
   358
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   359
7f561c08de6b Initial load
duke
parents:
diff changeset
   360
    public QName getExcludeResultPrefixes() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   361
        return _excludeResultPrefixes;
7f561c08de6b Initial load
duke
parents:
diff changeset
   362
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   363
7f561c08de6b Initial load
duke
parents:
diff changeset
   364
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   365
     * Create an instance of the <code>Stylesheet</code> class,
7f561c08de6b Initial load
duke
parents:
diff changeset
   366
     * and then parse, typecheck and compile the instance.
7f561c08de6b Initial load
duke
parents:
diff changeset
   367
     * Must be called after <code>parse()</code>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   368
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   369
    public Stylesheet makeStylesheet(SyntaxTreeNode element)
7f561c08de6b Initial load
duke
parents:
diff changeset
   370
        throws CompilerException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   371
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   372
            Stylesheet stylesheet;
7f561c08de6b Initial load
duke
parents:
diff changeset
   373
7f561c08de6b Initial load
duke
parents:
diff changeset
   374
            if (element instanceof Stylesheet) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   375
                stylesheet = (Stylesheet)element;
7f561c08de6b Initial load
duke
parents:
diff changeset
   376
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   377
            else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   378
                stylesheet = new Stylesheet();
7f561c08de6b Initial load
duke
parents:
diff changeset
   379
                stylesheet.setSimplified();
7f561c08de6b Initial load
duke
parents:
diff changeset
   380
                stylesheet.addElement(element);
7f561c08de6b Initial load
duke
parents:
diff changeset
   381
                stylesheet.setAttributes((AttributesImpl) element.getAttributes());
7f561c08de6b Initial load
duke
parents:
diff changeset
   382
7f561c08de6b Initial load
duke
parents:
diff changeset
   383
                // Map the default NS if not already defined
7f561c08de6b Initial load
duke
parents:
diff changeset
   384
                if (element.lookupNamespace(EMPTYSTRING) == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   385
                    element.addPrefixMapping(EMPTYSTRING, EMPTYSTRING);
7f561c08de6b Initial load
duke
parents:
diff changeset
   386
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   387
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   388
            stylesheet.setParser(this);
7f561c08de6b Initial load
duke
parents:
diff changeset
   389
            return stylesheet;
7f561c08de6b Initial load
duke
parents:
diff changeset
   390
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   391
        catch (ClassCastException e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   392
            ErrorMsg err = new ErrorMsg(ErrorMsg.NOT_STYLESHEET_ERR, element);
7f561c08de6b Initial load
duke
parents:
diff changeset
   393
            throw new CompilerException(err.toString());
7f561c08de6b Initial load
duke
parents:
diff changeset
   394
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   395
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   396
7f561c08de6b Initial load
duke
parents:
diff changeset
   397
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   398
     * Instanciates a SAX2 parser and generate the AST from the input.
7f561c08de6b Initial load
duke
parents:
diff changeset
   399
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   400
    public void createAST(Stylesheet stylesheet) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   401
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   402
            if (stylesheet != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   403
                stylesheet.parseContents(this);
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
   404
                final Iterator<SyntaxTreeNode> elements = stylesheet.elements();
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
   405
                while (elements.hasNext()) {
39909
00e4298ae168 8162598: XSLTC transformer swallows empty namespace declaration which is needed to undeclare default namespace
clanger
parents: 39907
diff changeset
   406
                    SyntaxTreeNode child = elements.next();
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   407
                    if (child instanceof Text) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   408
                        final int l = getLineNumber();
7f561c08de6b Initial load
duke
parents:
diff changeset
   409
                        ErrorMsg err =
7f561c08de6b Initial load
duke
parents:
diff changeset
   410
                            new ErrorMsg(ErrorMsg.ILLEGAL_TEXT_NODE_ERR,l,null);
7f561c08de6b Initial load
duke
parents:
diff changeset
   411
                        reportError(ERROR, err);
7f561c08de6b Initial load
duke
parents:
diff changeset
   412
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   413
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   414
                if (!errorsFound()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   415
                    stylesheet.typeCheck(_symbolTable);
7f561c08de6b Initial load
duke
parents:
diff changeset
   416
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   417
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   418
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   419
        catch (TypeCheckError e) {
16953
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   420
            reportError(ERROR, new ErrorMsg(ErrorMsg.JAXP_COMPILE_ERR, e));
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   421
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   422
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   423
7f561c08de6b Initial load
duke
parents:
diff changeset
   424
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   425
     * Parses a stylesheet and builds the internal abstract syntax tree
7f561c08de6b Initial load
duke
parents:
diff changeset
   426
     * @param reader A SAX2 SAXReader (parser)
7f561c08de6b Initial load
duke
parents:
diff changeset
   427
     * @param input A SAX2 InputSource can be passed to a SAX reader
7f561c08de6b Initial load
duke
parents:
diff changeset
   428
     * @return The root of the abstract syntax tree
7f561c08de6b Initial load
duke
parents:
diff changeset
   429
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   430
    public SyntaxTreeNode parse(XMLReader reader, InputSource input) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   431
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   432
            // Parse the input document and build the abstract syntax tree
7f561c08de6b Initial load
duke
parents:
diff changeset
   433
            reader.setContentHandler(this);
55742
6e1161923897 8157830: Errors in XSLT stylesheet are not dispatched correctly to ErrorListener
joehw
parents: 48577
diff changeset
   434
            if (_hasUserErrListener) {
6e1161923897 8157830: Errors in XSLT stylesheet are not dispatched correctly to ErrorListener
joehw
parents: 48577
diff changeset
   435
                // Set a ErrorHandler proxy to pass any parsing error on to be handled
6e1161923897 8157830: Errors in XSLT stylesheet are not dispatched correctly to ErrorListener
joehw
parents: 48577
diff changeset
   436
                // by the user's ErrorListener
6e1161923897 8157830: Errors in XSLT stylesheet are not dispatched correctly to ErrorListener
joehw
parents: 48577
diff changeset
   437
                reader.setErrorHandler(new ErrorHandlerProxy());
6e1161923897 8157830: Errors in XSLT stylesheet are not dispatched correctly to ErrorListener
joehw
parents: 48577
diff changeset
   438
            }
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   439
            reader.parse(input);
7f561c08de6b Initial load
duke
parents:
diff changeset
   440
            // Find the start of the stylesheet within the tree
47712
bde0215f1f70 8181151: Fix lint warnings in JAXP repo: cast
joehw
parents: 47312
diff changeset
   441
            return getStylesheet(_root);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   442
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   443
        catch (IOException e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   444
            if (_xsltc.debug()) e.printStackTrace();
16953
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   445
            reportError(ERROR,new ErrorMsg(ErrorMsg.JAXP_COMPILE_ERR, e));
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   446
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   447
        catch (SAXException e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   448
            Throwable ex = e.getException();
7f561c08de6b Initial load
duke
parents:
diff changeset
   449
            if (_xsltc.debug()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   450
                e.printStackTrace();
7f561c08de6b Initial load
duke
parents:
diff changeset
   451
                if (ex != null) ex.printStackTrace();
7f561c08de6b Initial load
duke
parents:
diff changeset
   452
            }
16953
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   453
            reportError(ERROR, new ErrorMsg(ErrorMsg.JAXP_COMPILE_ERR, e));
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   454
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   455
        catch (CompilerException e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   456
            if (_xsltc.debug()) e.printStackTrace();
16953
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   457
            reportError(ERROR, new ErrorMsg(ErrorMsg.JAXP_COMPILE_ERR, e));
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   458
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   459
        catch (Exception e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   460
            if (_xsltc.debug()) e.printStackTrace();
16953
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   461
            reportError(ERROR, new ErrorMsg(ErrorMsg.JAXP_COMPILE_ERR, e));
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   462
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   463
        return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   464
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   465
7f561c08de6b Initial load
duke
parents:
diff changeset
   466
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   467
     * Parses a stylesheet and builds the internal abstract syntax tree
7f561c08de6b Initial load
duke
parents:
diff changeset
   468
     * @param input A SAX2 InputSource can be passed to a SAX reader
7f561c08de6b Initial load
duke
parents:
diff changeset
   469
     * @return The root of the abstract syntax tree
7f561c08de6b Initial load
duke
parents:
diff changeset
   470
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   471
    public SyntaxTreeNode parse(InputSource input) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   472
        try {
48577
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 48409
diff changeset
   473
            final XMLReader reader = JdkXmlUtils.getXMLReader(_overrideDefaultParser,
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 48409
diff changeset
   474
                    _xsltc.isSecureProcessing());
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   475
48577
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 48409
diff changeset
   476
            JdkXmlUtils.setXMLReaderPropertyIfSupport(reader, XMLConstants.ACCESS_EXTERNAL_DTD,
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 48409
diff changeset
   477
                    _xsltc.getProperty(XMLConstants.ACCESS_EXTERNAL_DTD), true);
39907
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents: 35729
diff changeset
   478
18354
ce3ebc4a1aec 8016153: Property http://javax.xml.XMLConstants/property/accessExternalDTD is not recognized.
joehw
parents: 17534
diff changeset
   479
39907
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents: 35729
diff changeset
   480
            boolean supportCatalog = true;
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents: 35729
diff changeset
   481
            boolean useCatalog = _xsltc.getFeature(JdkXmlFeatures.XmlFeature.USE_CATALOG);
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents: 35729
diff changeset
   482
            try {
48577
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 48409
diff changeset
   483
                reader.setFeature(JdkXmlUtils.USE_CATALOG, useCatalog);
39907
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents: 35729
diff changeset
   484
            }
48577
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 48409
diff changeset
   485
            catch (SAXNotRecognizedException | SAXNotSupportedException e) {
39907
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents: 35729
diff changeset
   486
                supportCatalog = false;
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents: 35729
diff changeset
   487
            }
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents: 35729
diff changeset
   488
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents: 35729
diff changeset
   489
            if (supportCatalog && useCatalog) {
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents: 35729
diff changeset
   490
                try {
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents: 35729
diff changeset
   491
                    CatalogFeatures cf = (CatalogFeatures)_xsltc.getProperty(JdkXmlFeatures.CATALOG_FEATURES);
48577
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 48409
diff changeset
   492
                        if (cf != null) {
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 48409
diff changeset
   493
                            for (CatalogFeatures.Feature f : CatalogFeatures.Feature.values()) {
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 48409
diff changeset
   494
                                reader.setProperty(f.getPropertyName(), cf.get(f));
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 48409
diff changeset
   495
                            }
39907
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents: 35729
diff changeset
   496
                        }
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents: 35729
diff changeset
   497
                } catch (SAXNotRecognizedException e) {
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents: 35729
diff changeset
   498
                    //shall not happen for internal settings
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents: 35729
diff changeset
   499
                }
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents: 35729
diff changeset
   500
            }
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents: 35729
diff changeset
   501
35729
49f1515c5f5c 8144593: Suppress not recognized property/feature warning messages from SAXParser
aefimov
parents: 33349
diff changeset
   502
            String lastProperty = "";
20968
dde41f8b7b96 8014530: Better digital signature processing
joehw
parents: 18354
diff changeset
   503
            try {
dde41f8b7b96 8014530: Better digital signature processing
joehw
parents: 18354
diff changeset
   504
                XMLSecurityManager securityManager =
dde41f8b7b96 8014530: Better digital signature processing
joehw
parents: 18354
diff changeset
   505
                        (XMLSecurityManager)_xsltc.getProperty(XalanConstants.SECURITY_MANAGER);
dde41f8b7b96 8014530: Better digital signature processing
joehw
parents: 18354
diff changeset
   506
                for (XMLSecurityManager.Limit limit : XMLSecurityManager.Limit.values()) {
35729
49f1515c5f5c 8144593: Suppress not recognized property/feature warning messages from SAXParser
aefimov
parents: 33349
diff changeset
   507
                    lastProperty = limit.apiProperty();
49f1515c5f5c 8144593: Suppress not recognized property/feature warning messages from SAXParser
aefimov
parents: 33349
diff changeset
   508
                    reader.setProperty(lastProperty, securityManager.getLimitValueAsString(limit));
20968
dde41f8b7b96 8014530: Better digital signature processing
joehw
parents: 18354
diff changeset
   509
                }
dde41f8b7b96 8014530: Better digital signature processing
joehw
parents: 18354
diff changeset
   510
                if (securityManager.printEntityCountInfo()) {
35729
49f1515c5f5c 8144593: Suppress not recognized property/feature warning messages from SAXParser
aefimov
parents: 33349
diff changeset
   511
                    lastProperty = XalanConstants.JDK_ENTITY_COUNT_INFO;
48577
8dff65f1d611 8186080: Transform XML interfaces
joehw
parents: 48409
diff changeset
   512
                    reader.setProperty(XalanConstants.JDK_ENTITY_COUNT_INFO, XalanConstants.JDK_YES);
20968
dde41f8b7b96 8014530: Better digital signature processing
joehw
parents: 18354
diff changeset
   513
                }
dde41f8b7b96 8014530: Better digital signature processing
joehw
parents: 18354
diff changeset
   514
            } catch (SAXException se) {
35729
49f1515c5f5c 8144593: Suppress not recognized property/feature warning messages from SAXParser
aefimov
parents: 33349
diff changeset
   515
                XMLSecurityManager.printWarning(reader.getClass().getName(), lastProperty, se);
20968
dde41f8b7b96 8014530: Better digital signature processing
joehw
parents: 18354
diff changeset
   516
            }
dde41f8b7b96 8014530: Better digital signature processing
joehw
parents: 18354
diff changeset
   517
42247
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 41507
diff changeset
   518
            // try setting other JDK-impl properties, ignore if not supported
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 41507
diff changeset
   519
            JdkXmlUtils.setXMLReaderPropertyIfSupport(reader, JdkXmlUtils.CDATA_CHUNK_SIZE,
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 41507
diff changeset
   520
                _xsltc.getProperty(JdkXmlUtils.CDATA_CHUNK_SIZE), false);
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 41507
diff changeset
   521
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   522
            return(parse(reader, input));
7f561c08de6b Initial load
duke
parents:
diff changeset
   523
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   524
        catch (SAXException e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   525
            reportError(ERROR, new ErrorMsg(e.getMessage()));
7f561c08de6b Initial load
duke
parents:
diff changeset
   526
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   527
        return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   528
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   529
7f561c08de6b Initial load
duke
parents:
diff changeset
   530
    public SyntaxTreeNode getDocumentRoot() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   531
        return _root;
7f561c08de6b Initial load
duke
parents:
diff changeset
   532
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   533
7f561c08de6b Initial load
duke
parents:
diff changeset
   534
    private String _PImedia = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   535
    private String _PItitle = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   536
    private String _PIcharset = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   537
7f561c08de6b Initial load
duke
parents:
diff changeset
   538
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   539
     * Set the parameters to use to locate the correct <?xml-stylesheet ...?>
7f561c08de6b Initial load
duke
parents:
diff changeset
   540
     * processing instruction in the case where the input document is an
7f561c08de6b Initial load
duke
parents:
diff changeset
   541
     * XML document with one or more references to a stylesheet.
7f561c08de6b Initial load
duke
parents:
diff changeset
   542
     * @param media The media attribute to be matched. May be null, in which
7f561c08de6b Initial load
duke
parents:
diff changeset
   543
     * case the prefered templates will be used (i.e. alternate = no).
7f561c08de6b Initial load
duke
parents:
diff changeset
   544
     * @param title The value of the title attribute to match. May be null.
7f561c08de6b Initial load
duke
parents:
diff changeset
   545
     * @param charset The value of the charset attribute to match. May be null.
7f561c08de6b Initial load
duke
parents:
diff changeset
   546
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   547
    protected void setPIParameters(String media, String title, String charset) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   548
        _PImedia = media;
7f561c08de6b Initial load
duke
parents:
diff changeset
   549
        _PItitle = title;
7f561c08de6b Initial load
duke
parents:
diff changeset
   550
        _PIcharset = charset;
7f561c08de6b Initial load
duke
parents:
diff changeset
   551
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   552
7f561c08de6b Initial load
duke
parents:
diff changeset
   553
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   554
     * Extracts the DOM for the stylesheet. In the case of an embedded
7f561c08de6b Initial load
duke
parents:
diff changeset
   555
     * stylesheet, it extracts the DOM subtree corresponding to the
7f561c08de6b Initial load
duke
parents:
diff changeset
   556
     * embedded stylesheet that has an 'id' attribute whose value is the
7f561c08de6b Initial load
duke
parents:
diff changeset
   557
     * same as the value declared in the <?xml-stylesheet...?> processing
7f561c08de6b Initial load
duke
parents:
diff changeset
   558
     * instruction (P.I.). In the xml-stylesheet P.I. the value is labeled
7f561c08de6b Initial load
duke
parents:
diff changeset
   559
     * as the 'href' data of the P.I. The extracted DOM representing the
7f561c08de6b Initial load
duke
parents:
diff changeset
   560
     * stylesheet is returned as an Element object.
7f561c08de6b Initial load
duke
parents:
diff changeset
   561
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   562
    private SyntaxTreeNode getStylesheet(SyntaxTreeNode root)
7f561c08de6b Initial load
duke
parents:
diff changeset
   563
        throws CompilerException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   564
7f561c08de6b Initial load
duke
parents:
diff changeset
   565
        // Assume that this is a pure XSL stylesheet if there is not
7f561c08de6b Initial load
duke
parents:
diff changeset
   566
        // <?xml-stylesheet ....?> processing instruction
7f561c08de6b Initial load
duke
parents:
diff changeset
   567
        if (_target == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   568
            if (!_rootNamespaceDef) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   569
                ErrorMsg msg = new ErrorMsg(ErrorMsg.MISSING_XSLT_URI_ERR);
7f561c08de6b Initial load
duke
parents:
diff changeset
   570
                throw new CompilerException(msg.toString());
7f561c08de6b Initial load
duke
parents:
diff changeset
   571
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   572
            return(root);
7f561c08de6b Initial load
duke
parents:
diff changeset
   573
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   574
7f561c08de6b Initial load
duke
parents:
diff changeset
   575
        // Find the xsl:stylesheet or xsl:transform with this reference
7f561c08de6b Initial load
duke
parents:
diff changeset
   576
        if (_target.charAt(0) == '#') {
7f561c08de6b Initial load
duke
parents:
diff changeset
   577
            SyntaxTreeNode element = findStylesheet(root, _target.substring(1));
7f561c08de6b Initial load
duke
parents:
diff changeset
   578
            if (element == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   579
                ErrorMsg msg = new ErrorMsg(ErrorMsg.MISSING_XSLT_TARGET_ERR,
7f561c08de6b Initial load
duke
parents:
diff changeset
   580
                                            _target, root);
7f561c08de6b Initial load
duke
parents:
diff changeset
   581
                throw new CompilerException(msg.toString());
7f561c08de6b Initial load
duke
parents:
diff changeset
   582
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   583
            return(element);
7f561c08de6b Initial load
duke
parents:
diff changeset
   584
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   585
        else {
17534
21dc0b2762da 8011653: Upgrade JDK8 to JAXP 1.5
joehw
parents: 16953
diff changeset
   586
            try {
21dc0b2762da 8011653: Upgrade JDK8 to JAXP 1.5
joehw
parents: 16953
diff changeset
   587
                String path = _target;
21dc0b2762da 8011653: Upgrade JDK8 to JAXP 1.5
joehw
parents: 16953
diff changeset
   588
                if (path.indexOf(":")==-1) {
21dc0b2762da 8011653: Upgrade JDK8 to JAXP 1.5
joehw
parents: 16953
diff changeset
   589
                    path = "file:" + path;
21dc0b2762da 8011653: Upgrade JDK8 to JAXP 1.5
joehw
parents: 16953
diff changeset
   590
                }
21dc0b2762da 8011653: Upgrade JDK8 to JAXP 1.5
joehw
parents: 16953
diff changeset
   591
                path = SystemIDResolver.getAbsoluteURI(path);
21dc0b2762da 8011653: Upgrade JDK8 to JAXP 1.5
joehw
parents: 16953
diff changeset
   592
                String accessError = SecuritySupport.checkAccess(path,
20968
dde41f8b7b96 8014530: Better digital signature processing
joehw
parents: 18354
diff changeset
   593
                        (String)_xsltc.getProperty(XMLConstants.ACCESS_EXTERNAL_STYLESHEET),
17534
21dc0b2762da 8011653: Upgrade JDK8 to JAXP 1.5
joehw
parents: 16953
diff changeset
   594
                        XalanConstants.ACCESS_EXTERNAL_ALL);
21dc0b2762da 8011653: Upgrade JDK8 to JAXP 1.5
joehw
parents: 16953
diff changeset
   595
                if (accessError != null) {
21dc0b2762da 8011653: Upgrade JDK8 to JAXP 1.5
joehw
parents: 16953
diff changeset
   596
                    ErrorMsg msg = new ErrorMsg(ErrorMsg.ACCESSING_XSLT_TARGET_ERR,
21dc0b2762da 8011653: Upgrade JDK8 to JAXP 1.5
joehw
parents: 16953
diff changeset
   597
                            SecuritySupport.sanitizePath(_target), accessError,
21dc0b2762da 8011653: Upgrade JDK8 to JAXP 1.5
joehw
parents: 16953
diff changeset
   598
                            root);
21dc0b2762da 8011653: Upgrade JDK8 to JAXP 1.5
joehw
parents: 16953
diff changeset
   599
                    throw new CompilerException(msg.toString());
21dc0b2762da 8011653: Upgrade JDK8 to JAXP 1.5
joehw
parents: 16953
diff changeset
   600
                }
21dc0b2762da 8011653: Upgrade JDK8 to JAXP 1.5
joehw
parents: 16953
diff changeset
   601
            } catch (IOException ex) {
21dc0b2762da 8011653: Upgrade JDK8 to JAXP 1.5
joehw
parents: 16953
diff changeset
   602
                throw new CompilerException(ex);
21dc0b2762da 8011653: Upgrade JDK8 to JAXP 1.5
joehw
parents: 16953
diff changeset
   603
            }
21dc0b2762da 8011653: Upgrade JDK8 to JAXP 1.5
joehw
parents: 16953
diff changeset
   604
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   605
            return(loadExternalStylesheet(_target));
7f561c08de6b Initial load
duke
parents:
diff changeset
   606
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   607
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   608
7f561c08de6b Initial load
duke
parents:
diff changeset
   609
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   610
     * Find a Stylesheet element with a specific ID attribute value.
7f561c08de6b Initial load
duke
parents:
diff changeset
   611
     * This method is used to find a Stylesheet node that is referred
7f561c08de6b Initial load
duke
parents:
diff changeset
   612
     * in a <?xml-stylesheet ... ?> processing instruction.
7f561c08de6b Initial load
duke
parents:
diff changeset
   613
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   614
    private SyntaxTreeNode findStylesheet(SyntaxTreeNode root, String href) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   615
7f561c08de6b Initial load
duke
parents:
diff changeset
   616
        if (root == null) return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   617
7f561c08de6b Initial load
duke
parents:
diff changeset
   618
        if (root instanceof Stylesheet) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   619
            String id = root.getAttribute("id");
7f561c08de6b Initial load
duke
parents:
diff changeset
   620
            if (id.equals(href)) return root;
7f561c08de6b Initial load
duke
parents:
diff changeset
   621
        }
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
   622
        List<SyntaxTreeNode> children = root.getContents();
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   623
        if (children != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   624
            final int count = children.size();
7f561c08de6b Initial load
duke
parents:
diff changeset
   625
            for (int i = 0; i < count; i++) {
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
   626
                SyntaxTreeNode child = children.get(i);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   627
                SyntaxTreeNode node = findStylesheet(child, href);
7f561c08de6b Initial load
duke
parents:
diff changeset
   628
                if (node != null) return node;
7f561c08de6b Initial load
duke
parents:
diff changeset
   629
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   630
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   631
        return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   632
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   633
7f561c08de6b Initial load
duke
parents:
diff changeset
   634
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   635
     * For embedded stylesheets: Load an external file with stylesheet
7f561c08de6b Initial load
duke
parents:
diff changeset
   636
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   637
    private SyntaxTreeNode loadExternalStylesheet(String location)
7f561c08de6b Initial load
duke
parents:
diff changeset
   638
        throws CompilerException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   639
7f561c08de6b Initial load
duke
parents:
diff changeset
   640
        InputSource source;
7f561c08de6b Initial load
duke
parents:
diff changeset
   641
7f561c08de6b Initial load
duke
parents:
diff changeset
   642
        // Check if the location is URL or a local file
7f561c08de6b Initial load
duke
parents:
diff changeset
   643
        if ((new File(location)).exists())
7f561c08de6b Initial load
duke
parents:
diff changeset
   644
            source = new InputSource("file:"+location);
7f561c08de6b Initial load
duke
parents:
diff changeset
   645
        else
7f561c08de6b Initial load
duke
parents:
diff changeset
   646
            source = new InputSource(location);
7f561c08de6b Initial load
duke
parents:
diff changeset
   647
47712
bde0215f1f70 8181151: Fix lint warnings in JAXP repo: cast
joehw
parents: 47312
diff changeset
   648
        SyntaxTreeNode external = parse(source);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   649
        return(external);
7f561c08de6b Initial load
duke
parents:
diff changeset
   650
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   651
7f561c08de6b Initial load
duke
parents:
diff changeset
   652
    private void initAttrTable(String elementName, String[] attrs) {
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
   653
        _instructionAttrs.put(getQName(XSLT_URI, XSL, elementName).getStringRep(),
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   654
                                attrs);
7f561c08de6b Initial load
duke
parents:
diff changeset
   655
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   656
7f561c08de6b Initial load
duke
parents:
diff changeset
   657
    private void initInstructionAttrs() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   658
        initAttrTable("template",
7f561c08de6b Initial load
duke
parents:
diff changeset
   659
            new String[] {"match", "name", "priority", "mode"});
7f561c08de6b Initial load
duke
parents:
diff changeset
   660
        initAttrTable("stylesheet",
7f561c08de6b Initial load
duke
parents:
diff changeset
   661
            new String[] {"id", "version", "extension-element-prefixes",
7f561c08de6b Initial load
duke
parents:
diff changeset
   662
                "exclude-result-prefixes"});
7f561c08de6b Initial load
duke
parents:
diff changeset
   663
        initAttrTable("transform",
7f561c08de6b Initial load
duke
parents:
diff changeset
   664
            new String[] {"id", "version", "extension-element-prefixes",
7f561c08de6b Initial load
duke
parents:
diff changeset
   665
                "exclude-result-prefixes"});
7f561c08de6b Initial load
duke
parents:
diff changeset
   666
        initAttrTable("text", new String[] {"disable-output-escaping"});
7f561c08de6b Initial load
duke
parents:
diff changeset
   667
        initAttrTable("if", new String[] {"test"});
7f561c08de6b Initial load
duke
parents:
diff changeset
   668
        initAttrTable("choose", new String[] {});
7f561c08de6b Initial load
duke
parents:
diff changeset
   669
        initAttrTable("when", new String[] {"test"});
7f561c08de6b Initial load
duke
parents:
diff changeset
   670
        initAttrTable("otherwise", new String[] {});
7f561c08de6b Initial load
duke
parents:
diff changeset
   671
        initAttrTable("for-each", new String[] {"select"});
7f561c08de6b Initial load
duke
parents:
diff changeset
   672
        initAttrTable("message", new String[] {"terminate"});
7f561c08de6b Initial load
duke
parents:
diff changeset
   673
        initAttrTable("number",
7f561c08de6b Initial load
duke
parents:
diff changeset
   674
            new String[] {"level", "count", "from", "value", "format", "lang",
7f561c08de6b Initial load
duke
parents:
diff changeset
   675
                "letter-value", "grouping-separator", "grouping-size"});
7f561c08de6b Initial load
duke
parents:
diff changeset
   676
                initAttrTable("comment", new String[] {});
7f561c08de6b Initial load
duke
parents:
diff changeset
   677
        initAttrTable("copy", new String[] {"use-attribute-sets"});
7f561c08de6b Initial load
duke
parents:
diff changeset
   678
        initAttrTable("copy-of", new String[] {"select"});
7f561c08de6b Initial load
duke
parents:
diff changeset
   679
        initAttrTable("param", new String[] {"name", "select"});
7f561c08de6b Initial load
duke
parents:
diff changeset
   680
        initAttrTable("with-param", new String[] {"name", "select"});
7f561c08de6b Initial load
duke
parents:
diff changeset
   681
        initAttrTable("variable", new String[] {"name", "select"});
7f561c08de6b Initial load
duke
parents:
diff changeset
   682
        initAttrTable("output",
7f561c08de6b Initial load
duke
parents:
diff changeset
   683
            new String[] {"method", "version", "encoding",
7f561c08de6b Initial load
duke
parents:
diff changeset
   684
                "omit-xml-declaration", "standalone", "doctype-public",
7f561c08de6b Initial load
duke
parents:
diff changeset
   685
                "doctype-system", "cdata-section-elements", "indent",
7f561c08de6b Initial load
duke
parents:
diff changeset
   686
                "media-type"});
7f561c08de6b Initial load
duke
parents:
diff changeset
   687
        initAttrTable("sort",
7f561c08de6b Initial load
duke
parents:
diff changeset
   688
           new String[] {"select", "order", "case-order", "lang", "data-type"});
7f561c08de6b Initial load
duke
parents:
diff changeset
   689
        initAttrTable("key", new String[] {"name", "match", "use"});
7f561c08de6b Initial load
duke
parents:
diff changeset
   690
        initAttrTable("fallback", new String[] {});
7f561c08de6b Initial load
duke
parents:
diff changeset
   691
        initAttrTable("attribute", new String[] {"name", "namespace"});
7f561c08de6b Initial load
duke
parents:
diff changeset
   692
        initAttrTable("attribute-set",
7f561c08de6b Initial load
duke
parents:
diff changeset
   693
            new String[] {"name", "use-attribute-sets"});
7f561c08de6b Initial load
duke
parents:
diff changeset
   694
        initAttrTable("value-of",
7f561c08de6b Initial load
duke
parents:
diff changeset
   695
            new String[] {"select", "disable-output-escaping"});
7f561c08de6b Initial load
duke
parents:
diff changeset
   696
        initAttrTable("element",
7f561c08de6b Initial load
duke
parents:
diff changeset
   697
            new String[] {"name", "namespace", "use-attribute-sets"});
7f561c08de6b Initial load
duke
parents:
diff changeset
   698
        initAttrTable("call-template", new String[] {"name"});
7f561c08de6b Initial load
duke
parents:
diff changeset
   699
        initAttrTable("apply-templates", new String[] {"select", "mode"});
7f561c08de6b Initial load
duke
parents:
diff changeset
   700
        initAttrTable("apply-imports", new String[] {});
7f561c08de6b Initial load
duke
parents:
diff changeset
   701
        initAttrTable("decimal-format",
7f561c08de6b Initial load
duke
parents:
diff changeset
   702
            new String[] {"name", "decimal-separator", "grouping-separator",
7f561c08de6b Initial load
duke
parents:
diff changeset
   703
                "infinity", "minus-sign", "NaN", "percent", "per-mille",
7f561c08de6b Initial load
duke
parents:
diff changeset
   704
                "zero-digit", "digit", "pattern-separator"});
7f561c08de6b Initial load
duke
parents:
diff changeset
   705
        initAttrTable("import", new String[] {"href"});
7f561c08de6b Initial load
duke
parents:
diff changeset
   706
        initAttrTable("include", new String[] {"href"});
7f561c08de6b Initial load
duke
parents:
diff changeset
   707
        initAttrTable("strip-space", new String[] {"elements"});
7f561c08de6b Initial load
duke
parents:
diff changeset
   708
        initAttrTable("preserve-space", new String[] {"elements"});
7f561c08de6b Initial load
duke
parents:
diff changeset
   709
        initAttrTable("processing-instruction", new String[] {"name"});
7f561c08de6b Initial load
duke
parents:
diff changeset
   710
        initAttrTable("namespace-alias",
7f561c08de6b Initial load
duke
parents:
diff changeset
   711
           new String[] {"stylesheet-prefix", "result-prefix"});
7f561c08de6b Initial load
duke
parents:
diff changeset
   712
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   713
7f561c08de6b Initial load
duke
parents:
diff changeset
   714
    /**
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
   715
     * Initialize the _instructionClasses map, which maps XSL element
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   716
     * names to Java classes in this package.
7f561c08de6b Initial load
duke
parents:
diff changeset
   717
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   718
    private void initStdClasses() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   719
        initStdClass("template", "Template");
7f561c08de6b Initial load
duke
parents:
diff changeset
   720
        initStdClass("stylesheet", "Stylesheet");
7f561c08de6b Initial load
duke
parents:
diff changeset
   721
        initStdClass("transform", "Stylesheet");
7f561c08de6b Initial load
duke
parents:
diff changeset
   722
        initStdClass("text", "Text");
7f561c08de6b Initial load
duke
parents:
diff changeset
   723
        initStdClass("if", "If");
7f561c08de6b Initial load
duke
parents:
diff changeset
   724
        initStdClass("choose", "Choose");
7f561c08de6b Initial load
duke
parents:
diff changeset
   725
        initStdClass("when", "When");
7f561c08de6b Initial load
duke
parents:
diff changeset
   726
        initStdClass("otherwise", "Otherwise");
7f561c08de6b Initial load
duke
parents:
diff changeset
   727
        initStdClass("for-each", "ForEach");
7f561c08de6b Initial load
duke
parents:
diff changeset
   728
        initStdClass("message", "Message");
7f561c08de6b Initial load
duke
parents:
diff changeset
   729
        initStdClass("number", "Number");
7f561c08de6b Initial load
duke
parents:
diff changeset
   730
        initStdClass("comment", "Comment");
7f561c08de6b Initial load
duke
parents:
diff changeset
   731
        initStdClass("copy", "Copy");
7f561c08de6b Initial load
duke
parents:
diff changeset
   732
        initStdClass("copy-of", "CopyOf");
7f561c08de6b Initial load
duke
parents:
diff changeset
   733
        initStdClass("param", "Param");
7f561c08de6b Initial load
duke
parents:
diff changeset
   734
        initStdClass("with-param", "WithParam");
7f561c08de6b Initial load
duke
parents:
diff changeset
   735
        initStdClass("variable", "Variable");
7f561c08de6b Initial load
duke
parents:
diff changeset
   736
        initStdClass("output", "Output");
7f561c08de6b Initial load
duke
parents:
diff changeset
   737
        initStdClass("sort", "Sort");
7f561c08de6b Initial load
duke
parents:
diff changeset
   738
        initStdClass("key", "Key");
7f561c08de6b Initial load
duke
parents:
diff changeset
   739
        initStdClass("fallback", "Fallback");
7f561c08de6b Initial load
duke
parents:
diff changeset
   740
        initStdClass("attribute", "XslAttribute");
7f561c08de6b Initial load
duke
parents:
diff changeset
   741
        initStdClass("attribute-set", "AttributeSet");
7f561c08de6b Initial load
duke
parents:
diff changeset
   742
        initStdClass("value-of", "ValueOf");
7f561c08de6b Initial load
duke
parents:
diff changeset
   743
        initStdClass("element", "XslElement");
7f561c08de6b Initial load
duke
parents:
diff changeset
   744
        initStdClass("call-template", "CallTemplate");
7f561c08de6b Initial load
duke
parents:
diff changeset
   745
        initStdClass("apply-templates", "ApplyTemplates");
7f561c08de6b Initial load
duke
parents:
diff changeset
   746
        initStdClass("apply-imports", "ApplyImports");
7f561c08de6b Initial load
duke
parents:
diff changeset
   747
        initStdClass("decimal-format", "DecimalFormatting");
7f561c08de6b Initial load
duke
parents:
diff changeset
   748
        initStdClass("import", "Import");
7f561c08de6b Initial load
duke
parents:
diff changeset
   749
        initStdClass("include", "Include");
7f561c08de6b Initial load
duke
parents:
diff changeset
   750
        initStdClass("strip-space", "Whitespace");
7f561c08de6b Initial load
duke
parents:
diff changeset
   751
        initStdClass("preserve-space", "Whitespace");
7f561c08de6b Initial load
duke
parents:
diff changeset
   752
        initStdClass("processing-instruction", "ProcessingInstruction");
7f561c08de6b Initial load
duke
parents:
diff changeset
   753
        initStdClass("namespace-alias", "NamespaceAlias");
7f561c08de6b Initial load
duke
parents:
diff changeset
   754
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   755
7f561c08de6b Initial load
duke
parents:
diff changeset
   756
    private void initStdClass(String elementName, String className) {
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
   757
        _instructionClasses.put(getQName(XSLT_URI, XSL, elementName).getStringRep(),
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   758
                                COMPILER_PACKAGE + '.' + className);
7f561c08de6b Initial load
duke
parents:
diff changeset
   759
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   760
7f561c08de6b Initial load
duke
parents:
diff changeset
   761
    public boolean elementSupported(String namespace, String localName) {
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
   762
        return(_instructionClasses.get(getQName(namespace, XSL, localName).getStringRep()) != null);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   763
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   764
7f561c08de6b Initial load
duke
parents:
diff changeset
   765
    public boolean functionSupported(String fname) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   766
        return(_symbolTable.lookupPrimop(fname) != null);
7f561c08de6b Initial load
duke
parents:
diff changeset
   767
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   768
7f561c08de6b Initial load
duke
parents:
diff changeset
   769
    private void initExtClasses() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   770
        initExtClass("output", "TransletOutput");
7f561c08de6b Initial load
duke
parents:
diff changeset
   771
        initExtClass(REDIRECT_URI, "write", "TransletOutput");
7f561c08de6b Initial load
duke
parents:
diff changeset
   772
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   773
7f561c08de6b Initial load
duke
parents:
diff changeset
   774
    private void initExtClass(String elementName, String className) {
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
   775
        _instructionClasses.put(getQName(TRANSLET_URI, TRANSLET, elementName).getStringRep(),
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   776
                                COMPILER_PACKAGE + '.' + className);
7f561c08de6b Initial load
duke
parents:
diff changeset
   777
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   778
7f561c08de6b Initial load
duke
parents:
diff changeset
   779
    private void initExtClass(String namespace, String elementName, String className) {
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
   780
        _instructionClasses.put(getQName(namespace, TRANSLET, elementName).getStringRep(),
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   781
                                COMPILER_PACKAGE + '.' + className);
7f561c08de6b Initial load
duke
parents:
diff changeset
   782
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   783
7f561c08de6b Initial load
duke
parents:
diff changeset
   784
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   785
     * Add primops and base functions to the symbol table.
7f561c08de6b Initial load
duke
parents:
diff changeset
   786
     */
39909
00e4298ae168 8162598: XSLTC transformer swallows empty namespace declaration which is needed to undeclare default namespace
clanger
parents: 39907
diff changeset
   787
    @SuppressWarnings("unused")
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   788
    private void initSymbolTable() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   789
        MethodType I_V  = new MethodType(Type.Int, Type.Void);
7f561c08de6b Initial load
duke
parents:
diff changeset
   790
        MethodType I_R  = new MethodType(Type.Int, Type.Real);
7f561c08de6b Initial load
duke
parents:
diff changeset
   791
        MethodType I_S  = new MethodType(Type.Int, Type.String);
7f561c08de6b Initial load
duke
parents:
diff changeset
   792
        MethodType I_D  = new MethodType(Type.Int, Type.NodeSet);
7f561c08de6b Initial load
duke
parents:
diff changeset
   793
        MethodType R_I  = new MethodType(Type.Real, Type.Int);
7f561c08de6b Initial load
duke
parents:
diff changeset
   794
        MethodType R_V  = new MethodType(Type.Real, Type.Void);
7f561c08de6b Initial load
duke
parents:
diff changeset
   795
        MethodType R_R  = new MethodType(Type.Real, Type.Real);
7f561c08de6b Initial load
duke
parents:
diff changeset
   796
        MethodType R_D  = new MethodType(Type.Real, Type.NodeSet);
7f561c08de6b Initial load
duke
parents:
diff changeset
   797
        MethodType R_O  = new MethodType(Type.Real, Type.Reference);
7f561c08de6b Initial load
duke
parents:
diff changeset
   798
        MethodType I_I  = new MethodType(Type.Int, Type.Int);
7f561c08de6b Initial load
duke
parents:
diff changeset
   799
        MethodType D_O  = new MethodType(Type.NodeSet, Type.Reference);
7f561c08de6b Initial load
duke
parents:
diff changeset
   800
        MethodType D_V  = new MethodType(Type.NodeSet, Type.Void);
7f561c08de6b Initial load
duke
parents:
diff changeset
   801
        MethodType D_S  = new MethodType(Type.NodeSet, Type.String);
7f561c08de6b Initial load
duke
parents:
diff changeset
   802
        MethodType D_D  = new MethodType(Type.NodeSet, Type.NodeSet);
7f561c08de6b Initial load
duke
parents:
diff changeset
   803
        MethodType A_V  = new MethodType(Type.Node, Type.Void);
7f561c08de6b Initial load
duke
parents:
diff changeset
   804
        MethodType S_V  = new MethodType(Type.String, Type.Void);
7f561c08de6b Initial load
duke
parents:
diff changeset
   805
        MethodType S_S  = new MethodType(Type.String, Type.String);
7f561c08de6b Initial load
duke
parents:
diff changeset
   806
        MethodType S_A  = new MethodType(Type.String, Type.Node);
7f561c08de6b Initial load
duke
parents:
diff changeset
   807
        MethodType S_D  = new MethodType(Type.String, Type.NodeSet);
7f561c08de6b Initial load
duke
parents:
diff changeset
   808
        MethodType S_O  = new MethodType(Type.String, Type.Reference);
7f561c08de6b Initial load
duke
parents:
diff changeset
   809
        MethodType B_O  = new MethodType(Type.Boolean, Type.Reference);
7f561c08de6b Initial load
duke
parents:
diff changeset
   810
        MethodType B_V  = new MethodType(Type.Boolean, Type.Void);
7f561c08de6b Initial load
duke
parents:
diff changeset
   811
        MethodType B_B  = new MethodType(Type.Boolean, Type.Boolean);
7f561c08de6b Initial load
duke
parents:
diff changeset
   812
        MethodType B_S  = new MethodType(Type.Boolean, Type.String);
7f561c08de6b Initial load
duke
parents:
diff changeset
   813
        MethodType D_X  = new MethodType(Type.NodeSet, Type.Object);
7f561c08de6b Initial load
duke
parents:
diff changeset
   814
        MethodType R_RR = new MethodType(Type.Real, Type.Real, Type.Real);
7f561c08de6b Initial load
duke
parents:
diff changeset
   815
        MethodType I_II = new MethodType(Type.Int, Type.Int, Type.Int);
7f561c08de6b Initial load
duke
parents:
diff changeset
   816
        MethodType B_RR = new MethodType(Type.Boolean, Type.Real, Type.Real);
7f561c08de6b Initial load
duke
parents:
diff changeset
   817
        MethodType B_II = new MethodType(Type.Boolean, Type.Int, Type.Int);
7f561c08de6b Initial load
duke
parents:
diff changeset
   818
        MethodType S_SS = new MethodType(Type.String, Type.String, Type.String);
7f561c08de6b Initial load
duke
parents:
diff changeset
   819
        MethodType S_DS = new MethodType(Type.String, Type.Real, Type.String);
7f561c08de6b Initial load
duke
parents:
diff changeset
   820
        MethodType S_SR = new MethodType(Type.String, Type.String, Type.Real);
7f561c08de6b Initial load
duke
parents:
diff changeset
   821
        MethodType O_SO = new MethodType(Type.Reference, Type.String, Type.Reference);
7f561c08de6b Initial load
duke
parents:
diff changeset
   822
7f561c08de6b Initial load
duke
parents:
diff changeset
   823
        MethodType D_SS =
7f561c08de6b Initial load
duke
parents:
diff changeset
   824
            new MethodType(Type.NodeSet, Type.String, Type.String);
7f561c08de6b Initial load
duke
parents:
diff changeset
   825
        MethodType D_SD =
7f561c08de6b Initial load
duke
parents:
diff changeset
   826
            new MethodType(Type.NodeSet, Type.String, Type.NodeSet);
7f561c08de6b Initial load
duke
parents:
diff changeset
   827
        MethodType B_BB =
7f561c08de6b Initial load
duke
parents:
diff changeset
   828
            new MethodType(Type.Boolean, Type.Boolean, Type.Boolean);
7f561c08de6b Initial load
duke
parents:
diff changeset
   829
        MethodType B_SS =
7f561c08de6b Initial load
duke
parents:
diff changeset
   830
            new MethodType(Type.Boolean, Type.String, Type.String);
7f561c08de6b Initial load
duke
parents:
diff changeset
   831
        MethodType S_SD =
7f561c08de6b Initial load
duke
parents:
diff changeset
   832
            new MethodType(Type.String, Type.String, Type.NodeSet);
7f561c08de6b Initial load
duke
parents:
diff changeset
   833
        MethodType S_DSS =
7f561c08de6b Initial load
duke
parents:
diff changeset
   834
            new MethodType(Type.String, Type.Real, Type.String, Type.String);
7f561c08de6b Initial load
duke
parents:
diff changeset
   835
        MethodType S_SRR =
7f561c08de6b Initial load
duke
parents:
diff changeset
   836
            new MethodType(Type.String, Type.String, Type.Real, Type.Real);
7f561c08de6b Initial load
duke
parents:
diff changeset
   837
        MethodType S_SSS =
7f561c08de6b Initial load
duke
parents:
diff changeset
   838
            new MethodType(Type.String, Type.String, Type.String, Type.String);
7f561c08de6b Initial load
duke
parents:
diff changeset
   839
7f561c08de6b Initial load
duke
parents:
diff changeset
   840
        /*
7f561c08de6b Initial load
duke
parents:
diff changeset
   841
         * Standard functions: implemented but not in this table concat().
7f561c08de6b Initial load
duke
parents:
diff changeset
   842
         * When adding a new function make sure to uncomment
7f561c08de6b Initial load
duke
parents:
diff changeset
   843
         * the corresponding line in <tt>FunctionAvailableCall</tt>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   844
         */
7f561c08de6b Initial load
duke
parents:
diff changeset
   845
7f561c08de6b Initial load
duke
parents:
diff changeset
   846
        // The following functions are inlined
7f561c08de6b Initial load
duke
parents:
diff changeset
   847
7f561c08de6b Initial load
duke
parents:
diff changeset
   848
        _symbolTable.addPrimop("current", A_V);
7f561c08de6b Initial load
duke
parents:
diff changeset
   849
        _symbolTable.addPrimop("last", I_V);
7f561c08de6b Initial load
duke
parents:
diff changeset
   850
        _symbolTable.addPrimop("position", I_V);
7f561c08de6b Initial load
duke
parents:
diff changeset
   851
        _symbolTable.addPrimop("true", B_V);
7f561c08de6b Initial load
duke
parents:
diff changeset
   852
        _symbolTable.addPrimop("false", B_V);
7f561c08de6b Initial load
duke
parents:
diff changeset
   853
        _symbolTable.addPrimop("not", B_B);
7f561c08de6b Initial load
duke
parents:
diff changeset
   854
        _symbolTable.addPrimop("name", S_V);
7f561c08de6b Initial load
duke
parents:
diff changeset
   855
        _symbolTable.addPrimop("name", S_A);
7f561c08de6b Initial load
duke
parents:
diff changeset
   856
        _symbolTable.addPrimop("generate-id", S_V);
7f561c08de6b Initial load
duke
parents:
diff changeset
   857
        _symbolTable.addPrimop("generate-id", S_A);
7f561c08de6b Initial load
duke
parents:
diff changeset
   858
        _symbolTable.addPrimop("ceiling", R_R);
7f561c08de6b Initial load
duke
parents:
diff changeset
   859
        _symbolTable.addPrimop("floor", R_R);
7f561c08de6b Initial load
duke
parents:
diff changeset
   860
        _symbolTable.addPrimop("round", R_R);
7f561c08de6b Initial load
duke
parents:
diff changeset
   861
        _symbolTable.addPrimop("contains", B_SS);
7f561c08de6b Initial load
duke
parents:
diff changeset
   862
        _symbolTable.addPrimop("number", R_O);
7f561c08de6b Initial load
duke
parents:
diff changeset
   863
        _symbolTable.addPrimop("number", R_V);
7f561c08de6b Initial load
duke
parents:
diff changeset
   864
        _symbolTable.addPrimop("boolean", B_O);
7f561c08de6b Initial load
duke
parents:
diff changeset
   865
        _symbolTable.addPrimop("string", S_O);
7f561c08de6b Initial load
duke
parents:
diff changeset
   866
        _symbolTable.addPrimop("string", S_V);
7f561c08de6b Initial load
duke
parents:
diff changeset
   867
        _symbolTable.addPrimop("translate", S_SSS);
7f561c08de6b Initial load
duke
parents:
diff changeset
   868
        _symbolTable.addPrimop("string-length", I_V);
7f561c08de6b Initial load
duke
parents:
diff changeset
   869
        _symbolTable.addPrimop("string-length", I_S);
7f561c08de6b Initial load
duke
parents:
diff changeset
   870
        _symbolTable.addPrimop("starts-with", B_SS);
7f561c08de6b Initial load
duke
parents:
diff changeset
   871
        _symbolTable.addPrimop("format-number", S_DS);
7f561c08de6b Initial load
duke
parents:
diff changeset
   872
        _symbolTable.addPrimop("format-number", S_DSS);
7f561c08de6b Initial load
duke
parents:
diff changeset
   873
        _symbolTable.addPrimop("unparsed-entity-uri", S_S);
7f561c08de6b Initial load
duke
parents:
diff changeset
   874
        _symbolTable.addPrimop("key", D_SS);
7f561c08de6b Initial load
duke
parents:
diff changeset
   875
        _symbolTable.addPrimop("key", D_SD);
7f561c08de6b Initial load
duke
parents:
diff changeset
   876
        _symbolTable.addPrimop("id", D_S);
7f561c08de6b Initial load
duke
parents:
diff changeset
   877
        _symbolTable.addPrimop("id", D_D);
7f561c08de6b Initial load
duke
parents:
diff changeset
   878
        _symbolTable.addPrimop("namespace-uri", S_V);
7f561c08de6b Initial load
duke
parents:
diff changeset
   879
        _symbolTable.addPrimop("function-available", B_S);
7f561c08de6b Initial load
duke
parents:
diff changeset
   880
        _symbolTable.addPrimop("element-available", B_S);
7f561c08de6b Initial load
duke
parents:
diff changeset
   881
        _symbolTable.addPrimop("document", D_S);
7f561c08de6b Initial load
duke
parents:
diff changeset
   882
        _symbolTable.addPrimop("document", D_V);
7f561c08de6b Initial load
duke
parents:
diff changeset
   883
7f561c08de6b Initial load
duke
parents:
diff changeset
   884
        // The following functions are implemented in the basis library
7f561c08de6b Initial load
duke
parents:
diff changeset
   885
        _symbolTable.addPrimop("count", I_D);
7f561c08de6b Initial load
duke
parents:
diff changeset
   886
        _symbolTable.addPrimop("sum", R_D);
7f561c08de6b Initial load
duke
parents:
diff changeset
   887
        _symbolTable.addPrimop("local-name", S_V);
7f561c08de6b Initial load
duke
parents:
diff changeset
   888
        _symbolTable.addPrimop("local-name", S_D);
7f561c08de6b Initial load
duke
parents:
diff changeset
   889
        _symbolTable.addPrimop("namespace-uri", S_V);
7f561c08de6b Initial load
duke
parents:
diff changeset
   890
        _symbolTable.addPrimop("namespace-uri", S_D);
7f561c08de6b Initial load
duke
parents:
diff changeset
   891
        _symbolTable.addPrimop("substring", S_SR);
7f561c08de6b Initial load
duke
parents:
diff changeset
   892
        _symbolTable.addPrimop("substring", S_SRR);
7f561c08de6b Initial load
duke
parents:
diff changeset
   893
        _symbolTable.addPrimop("substring-after", S_SS);
7f561c08de6b Initial load
duke
parents:
diff changeset
   894
        _symbolTable.addPrimop("substring-before", S_SS);
7f561c08de6b Initial load
duke
parents:
diff changeset
   895
        _symbolTable.addPrimop("normalize-space", S_V);
7f561c08de6b Initial load
duke
parents:
diff changeset
   896
        _symbolTable.addPrimop("normalize-space", S_S);
7f561c08de6b Initial load
duke
parents:
diff changeset
   897
        _symbolTable.addPrimop("system-property", S_S);
7f561c08de6b Initial load
duke
parents:
diff changeset
   898
7f561c08de6b Initial load
duke
parents:
diff changeset
   899
        // Extensions
7f561c08de6b Initial load
duke
parents:
diff changeset
   900
        _symbolTable.addPrimop("nodeset", D_O);
7f561c08de6b Initial load
duke
parents:
diff changeset
   901
        _symbolTable.addPrimop("objectType", S_O);
7f561c08de6b Initial load
duke
parents:
diff changeset
   902
        _symbolTable.addPrimop("cast", O_SO);
7f561c08de6b Initial load
duke
parents:
diff changeset
   903
7f561c08de6b Initial load
duke
parents:
diff changeset
   904
        // Operators +, -, *, /, % defined on real types.
7f561c08de6b Initial load
duke
parents:
diff changeset
   905
        _symbolTable.addPrimop("+", R_RR);
7f561c08de6b Initial load
duke
parents:
diff changeset
   906
        _symbolTable.addPrimop("-", R_RR);
7f561c08de6b Initial load
duke
parents:
diff changeset
   907
        _symbolTable.addPrimop("*", R_RR);
7f561c08de6b Initial load
duke
parents:
diff changeset
   908
        _symbolTable.addPrimop("/", R_RR);
7f561c08de6b Initial load
duke
parents:
diff changeset
   909
        _symbolTable.addPrimop("%", R_RR);
7f561c08de6b Initial load
duke
parents:
diff changeset
   910
7f561c08de6b Initial load
duke
parents:
diff changeset
   911
        // Operators +, -, * defined on integer types.
7f561c08de6b Initial load
duke
parents:
diff changeset
   912
        // Operators / and % are not  defined on integers (may cause exception)
7f561c08de6b Initial load
duke
parents:
diff changeset
   913
        _symbolTable.addPrimop("+", I_II);
7f561c08de6b Initial load
duke
parents:
diff changeset
   914
        _symbolTable.addPrimop("-", I_II);
7f561c08de6b Initial load
duke
parents:
diff changeset
   915
        _symbolTable.addPrimop("*", I_II);
7f561c08de6b Initial load
duke
parents:
diff changeset
   916
7f561c08de6b Initial load
duke
parents:
diff changeset
   917
         // Operators <, <= >, >= defined on real types.
7f561c08de6b Initial load
duke
parents:
diff changeset
   918
        _symbolTable.addPrimop("<",  B_RR);
7f561c08de6b Initial load
duke
parents:
diff changeset
   919
        _symbolTable.addPrimop("<=", B_RR);
7f561c08de6b Initial load
duke
parents:
diff changeset
   920
        _symbolTable.addPrimop(">",  B_RR);
7f561c08de6b Initial load
duke
parents:
diff changeset
   921
        _symbolTable.addPrimop(">=", B_RR);
7f561c08de6b Initial load
duke
parents:
diff changeset
   922
7f561c08de6b Initial load
duke
parents:
diff changeset
   923
        // Operators <, <= >, >= defined on int types.
7f561c08de6b Initial load
duke
parents:
diff changeset
   924
        _symbolTable.addPrimop("<",  B_II);
7f561c08de6b Initial load
duke
parents:
diff changeset
   925
        _symbolTable.addPrimop("<=", B_II);
7f561c08de6b Initial load
duke
parents:
diff changeset
   926
        _symbolTable.addPrimop(">",  B_II);
7f561c08de6b Initial load
duke
parents:
diff changeset
   927
        _symbolTable.addPrimop(">=", B_II);
7f561c08de6b Initial load
duke
parents:
diff changeset
   928
7f561c08de6b Initial load
duke
parents:
diff changeset
   929
        // Operators <, <= >, >= defined on boolean types.
7f561c08de6b Initial load
duke
parents:
diff changeset
   930
        _symbolTable.addPrimop("<",  B_BB);
7f561c08de6b Initial load
duke
parents:
diff changeset
   931
        _symbolTable.addPrimop("<=", B_BB);
7f561c08de6b Initial load
duke
parents:
diff changeset
   932
        _symbolTable.addPrimop(">",  B_BB);
7f561c08de6b Initial load
duke
parents:
diff changeset
   933
        _symbolTable.addPrimop(">=", B_BB);
7f561c08de6b Initial load
duke
parents:
diff changeset
   934
7f561c08de6b Initial load
duke
parents:
diff changeset
   935
        // Operators 'and' and 'or'.
7f561c08de6b Initial load
duke
parents:
diff changeset
   936
        _symbolTable.addPrimop("or", B_BB);
7f561c08de6b Initial load
duke
parents:
diff changeset
   937
        _symbolTable.addPrimop("and", B_BB);
7f561c08de6b Initial load
duke
parents:
diff changeset
   938
7f561c08de6b Initial load
duke
parents:
diff changeset
   939
        // Unary minus.
7f561c08de6b Initial load
duke
parents:
diff changeset
   940
        _symbolTable.addPrimop("u-", R_R);
7f561c08de6b Initial load
duke
parents:
diff changeset
   941
        _symbolTable.addPrimop("u-", I_I);
7f561c08de6b Initial load
duke
parents:
diff changeset
   942
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   943
7f561c08de6b Initial load
duke
parents:
diff changeset
   944
    public SymbolTable getSymbolTable() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   945
        return _symbolTable;
7f561c08de6b Initial load
duke
parents:
diff changeset
   946
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   947
7f561c08de6b Initial load
duke
parents:
diff changeset
   948
    public Template getTemplate() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   949
        return _template;
7f561c08de6b Initial load
duke
parents:
diff changeset
   950
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   951
7f561c08de6b Initial load
duke
parents:
diff changeset
   952
    public void setTemplate(Template template) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   953
        _template = template;
7f561c08de6b Initial load
duke
parents:
diff changeset
   954
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   955
7f561c08de6b Initial load
duke
parents:
diff changeset
   956
    private int _templateIndex = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
   957
7f561c08de6b Initial load
duke
parents:
diff changeset
   958
    public int getTemplateIndex() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   959
        return(_templateIndex++);
7f561c08de6b Initial load
duke
parents:
diff changeset
   960
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   961
7f561c08de6b Initial load
duke
parents:
diff changeset
   962
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   963
     * Creates a new node in the abstract syntax tree. This node can be
7f561c08de6b Initial load
duke
parents:
diff changeset
   964
     *  o) a supported XSLT 1.0 element
7f561c08de6b Initial load
duke
parents:
diff changeset
   965
     *  o) an unsupported XSLT element (post 1.0)
7f561c08de6b Initial load
duke
parents:
diff changeset
   966
     *  o) a supported XSLT extension
7f561c08de6b Initial load
duke
parents:
diff changeset
   967
     *  o) an unsupported XSLT extension
7f561c08de6b Initial load
duke
parents:
diff changeset
   968
     *  o) a literal result element (not an XSLT element and not an extension)
7f561c08de6b Initial load
duke
parents:
diff changeset
   969
     * Unsupported elements do not directly generate an error. We have to wait
7f561c08de6b Initial load
duke
parents:
diff changeset
   970
     * until we have received all child elements of an unsupported element to
7f561c08de6b Initial load
duke
parents:
diff changeset
   971
     * see if any <xsl:fallback> elements exist.
7f561c08de6b Initial load
duke
parents:
diff changeset
   972
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   973
7f561c08de6b Initial load
duke
parents:
diff changeset
   974
    private boolean versionIsOne = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   975
7f561c08de6b Initial load
duke
parents:
diff changeset
   976
    public SyntaxTreeNode makeInstance(String uri, String prefix,
7f561c08de6b Initial load
duke
parents:
diff changeset
   977
        String local, Attributes attributes)
7f561c08de6b Initial load
duke
parents:
diff changeset
   978
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   979
        SyntaxTreeNode node = null;
39909
00e4298ae168 8162598: XSLTC transformer swallows empty namespace declaration which is needed to undeclare default namespace
clanger
parents: 39907
diff changeset
   980
        QName qname = getQName(uri, prefix, local);
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
   981
        String className = _instructionClasses.get(qname.getStringRep());
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   982
7f561c08de6b Initial load
duke
parents:
diff changeset
   983
        if (className != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   984
            try {
39909
00e4298ae168 8162598: XSLTC transformer swallows empty namespace declaration which is needed to undeclare default namespace
clanger
parents: 39907
diff changeset
   985
                final Class<?> clazz = ObjectFactory.findProviderClass(className, true);
45853
bfa06be36a17 8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents: 42247
diff changeset
   986
                node = (SyntaxTreeNode)clazz.getDeclaredConstructor().newInstance();
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   987
                node.setQName(qname);
7f561c08de6b Initial load
duke
parents:
diff changeset
   988
                node.setParser(this);
7f561c08de6b Initial load
duke
parents:
diff changeset
   989
                if (_locator != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   990
                    node.setLineNumber(getLineNumber());
7f561c08de6b Initial load
duke
parents:
diff changeset
   991
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   992
                if (node instanceof Stylesheet) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   993
                    _xsltc.setStylesheet((Stylesheet)node);
7f561c08de6b Initial load
duke
parents:
diff changeset
   994
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   995
                checkForSuperfluousAttributes(node, attributes);
7f561c08de6b Initial load
duke
parents:
diff changeset
   996
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   997
            catch (ClassNotFoundException e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   998
                ErrorMsg err = new ErrorMsg(ErrorMsg.CLASS_NOT_FOUND_ERR, node);
7f561c08de6b Initial load
duke
parents:
diff changeset
   999
                reportError(ERROR, err);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1000
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1001
            catch (Exception e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1002
                ErrorMsg err = new ErrorMsg(ErrorMsg.INTERNAL_ERR,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1003
                                            e.getMessage(), node);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1004
                reportError(FATAL, err);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1005
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1006
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1007
        else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1008
            if (uri != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1009
                // Check if the element belongs in our namespace
7f561c08de6b Initial load
duke
parents:
diff changeset
  1010
                if (uri.equals(XSLT_URI)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1011
                    node = new UnsupportedElement(uri, prefix, local, false);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1012
                    UnsupportedElement element = (UnsupportedElement)node;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1013
                    ErrorMsg msg = new ErrorMsg(ErrorMsg.UNSUPPORTED_XSL_ERR,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1014
                                                getLineNumber(),local);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1015
                    element.setErrorMessage(msg);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1016
                    if (versionIsOne) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1017
                        reportError(UNSUPPORTED,msg);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1018
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1019
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1020
                // Check if this is an XSLTC extension element
7f561c08de6b Initial load
duke
parents:
diff changeset
  1021
                else if (uri.equals(TRANSLET_URI)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1022
                    node = new UnsupportedElement(uri, prefix, local, true);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1023
                    UnsupportedElement element = (UnsupportedElement)node;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1024
                    ErrorMsg msg = new ErrorMsg(ErrorMsg.UNSUPPORTED_EXT_ERR,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1025
                                                getLineNumber(),local);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1026
                    element.setErrorMessage(msg);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1027
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1028
                // Check if this is an extension of some other XSLT processor
7f561c08de6b Initial load
duke
parents:
diff changeset
  1029
                else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1030
                    Stylesheet sheet = _xsltc.getStylesheet();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1031
                    if ((sheet != null) && (sheet.isExtension(uri))) {
39909
00e4298ae168 8162598: XSLTC transformer swallows empty namespace declaration which is needed to undeclare default namespace
clanger
parents: 39907
diff changeset
  1032
                        if (sheet != _parentStack.peek()) {
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1033
                            node = new UnsupportedElement(uri, prefix, local, true);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1034
                            UnsupportedElement elem = (UnsupportedElement)node;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1035
                            ErrorMsg msg =
7f561c08de6b Initial load
duke
parents:
diff changeset
  1036
                                new ErrorMsg(ErrorMsg.UNSUPPORTED_EXT_ERR,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1037
                                             getLineNumber(),
7f561c08de6b Initial load
duke
parents:
diff changeset
  1038
                                             prefix+":"+local);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1039
                            elem.setErrorMessage(msg);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1040
                        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1041
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1042
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1043
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1044
            if (node == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1045
                node = new LiteralElement();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1046
                node.setLineNumber(getLineNumber());
7f561c08de6b Initial load
duke
parents:
diff changeset
  1047
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1048
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1049
        if ((node != null) && (node instanceof LiteralElement)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1050
            ((LiteralElement)node).setQName(qname);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1051
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1052
        return(node);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1053
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1054
7f561c08de6b Initial load
duke
parents:
diff changeset
  1055
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1056
     * checks the list of attributes against a list of allowed attributes
7f561c08de6b Initial load
duke
parents:
diff changeset
  1057
     * for a particular element node.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1058
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1059
    private void checkForSuperfluousAttributes(SyntaxTreeNode node,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1060
        Attributes attrs)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1061
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1062
        QName qname = node.getQName();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1063
        boolean isStylesheet = (node instanceof Stylesheet);
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
  1064
        String[] legal = _instructionAttrs.get(qname.getStringRep());
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1065
        if (versionIsOne && legal != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1066
            int j;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1067
            final int n = attrs.getLength();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1068
7f561c08de6b Initial load
duke
parents:
diff changeset
  1069
            for (int i = 0; i < n; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1070
                final String attrQName = attrs.getQName(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1071
7f561c08de6b Initial load
duke
parents:
diff changeset
  1072
                if (isStylesheet && attrQName.equals("version")) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1073
                    versionIsOne = attrs.getValue(i).equals("1.0");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1074
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1075
7f561c08de6b Initial load
duke
parents:
diff changeset
  1076
                // Ignore if special or if it has a prefix
7f561c08de6b Initial load
duke
parents:
diff changeset
  1077
                if (attrQName.startsWith("xml") ||
7f561c08de6b Initial load
duke
parents:
diff changeset
  1078
                    attrQName.indexOf(':') > 0) continue;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1079
7f561c08de6b Initial load
duke
parents:
diff changeset
  1080
                for (j = 0; j < legal.length; j++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1081
                    if (attrQName.equalsIgnoreCase(legal[j])) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1082
                        break;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1083
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1084
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1085
                if (j == legal.length) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1086
                    final ErrorMsg err =
7f561c08de6b Initial load
duke
parents:
diff changeset
  1087
                        new ErrorMsg(ErrorMsg.ILLEGAL_ATTRIBUTE_ERR,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1088
                                attrQName, node);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1089
                    // Workaround for the TCK failure ErrorListener.errorTests.error001..
7f561c08de6b Initial load
duke
parents:
diff changeset
  1090
                    err.setWarningError(true);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1091
                    reportError(WARNING, err);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1092
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1093
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1094
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1095
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1096
7f561c08de6b Initial load
duke
parents:
diff changeset
  1097
7f561c08de6b Initial load
duke
parents:
diff changeset
  1098
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1099
     * Parse an XPath expression:
7f561c08de6b Initial load
duke
parents:
diff changeset
  1100
     *  @param parent - XSL element where the expression occured
7f561c08de6b Initial load
duke
parents:
diff changeset
  1101
     *  @param exp    - textual representation of the expression
7f561c08de6b Initial load
duke
parents:
diff changeset
  1102
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1103
    public Expression parseExpression(SyntaxTreeNode parent, String exp) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1104
        return (Expression)parseTopLevel(parent, "<EXPRESSION>"+exp, null);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1105
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1106
7f561c08de6b Initial load
duke
parents:
diff changeset
  1107
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1108
     * Parse an XPath expression:
7f561c08de6b Initial load
duke
parents:
diff changeset
  1109
     *  @param parent - XSL element where the expression occured
7f561c08de6b Initial load
duke
parents:
diff changeset
  1110
     *  @param attr   - name of this element's attribute to get expression from
7f561c08de6b Initial load
duke
parents:
diff changeset
  1111
     *  @param def    - default expression (if the attribute was not found)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1112
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1113
    public Expression parseExpression(SyntaxTreeNode parent,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1114
                                      String attr, String def) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1115
        // Get the textual representation of the expression (if any)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1116
        String exp = parent.getAttribute(attr);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1117
        // Use the default expression if none was found
7f561c08de6b Initial load
duke
parents:
diff changeset
  1118
        if ((exp.length() == 0) && (def != null)) exp = def;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1119
        // Invoke the XPath parser
7f561c08de6b Initial load
duke
parents:
diff changeset
  1120
        return (Expression)parseTopLevel(parent, "<EXPRESSION>"+exp, exp);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1121
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1122
7f561c08de6b Initial load
duke
parents:
diff changeset
  1123
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1124
     * Parse an XPath pattern:
7f561c08de6b Initial load
duke
parents:
diff changeset
  1125
     *  @param parent  - XSL element where the pattern occured
7f561c08de6b Initial load
duke
parents:
diff changeset
  1126
     *  @param pattern - textual representation of the pattern
7f561c08de6b Initial load
duke
parents:
diff changeset
  1127
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1128
    public Pattern parsePattern(SyntaxTreeNode parent, String pattern) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1129
        return (Pattern)parseTopLevel(parent, "<PATTERN>"+pattern, pattern);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1130
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1131
7f561c08de6b Initial load
duke
parents:
diff changeset
  1132
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1133
     * Parse an XPath pattern:
7f561c08de6b Initial load
duke
parents:
diff changeset
  1134
     *  @param parent - XSL element where the pattern occured
7f561c08de6b Initial load
duke
parents:
diff changeset
  1135
     *  @param attr   - name of this element's attribute to get pattern from
7f561c08de6b Initial load
duke
parents:
diff changeset
  1136
     *  @param def    - default pattern (if the attribute was not found)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1137
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1138
    public Pattern parsePattern(SyntaxTreeNode parent,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1139
                                String attr, String def) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1140
        // Get the textual representation of the pattern (if any)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1141
        String pattern = parent.getAttribute(attr);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1142
        // Use the default pattern if none was found
7f561c08de6b Initial load
duke
parents:
diff changeset
  1143
        if ((pattern.length() == 0) && (def != null)) pattern = def;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1144
        // Invoke the XPath parser
7f561c08de6b Initial load
duke
parents:
diff changeset
  1145
        return (Pattern)parseTopLevel(parent, "<PATTERN>"+pattern, pattern);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1146
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1147
7f561c08de6b Initial load
duke
parents:
diff changeset
  1148
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1149
     * Parse an XPath expression or pattern using the generated XPathParser
7f561c08de6b Initial load
duke
parents:
diff changeset
  1150
     * The method will return a Dummy node if the XPath parser fails.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1151
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1152
    private SyntaxTreeNode parseTopLevel(SyntaxTreeNode parent, String text,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1153
                                         String expression) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1154
        int line = getLineNumber();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1155
7f561c08de6b Initial load
duke
parents:
diff changeset
  1156
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1157
            _xpathParser.setScanner(new XPathLexer(new StringReader(text)));
7f561c08de6b Initial load
duke
parents:
diff changeset
  1158
            Symbol result = _xpathParser.parse(expression, line);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1159
            if (result != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1160
                final SyntaxTreeNode node = (SyntaxTreeNode)result.value;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1161
                if (node != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1162
                    node.setParser(this);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1163
                    node.setParent(parent);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1164
                    node.setLineNumber(line);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1165
                    return node;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1166
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1167
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1168
            reportError(ERROR, new ErrorMsg(ErrorMsg.XPATH_PARSER_ERR,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1169
                                            expression, parent));
7f561c08de6b Initial load
duke
parents:
diff changeset
  1170
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1171
        catch (Exception e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1172
            if (_xsltc.debug()) e.printStackTrace();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1173
            reportError(ERROR, new ErrorMsg(ErrorMsg.XPATH_PARSER_ERR,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1174
                                            expression, parent));
7f561c08de6b Initial load
duke
parents:
diff changeset
  1175
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1176
7f561c08de6b Initial load
duke
parents:
diff changeset
  1177
        // Return a dummy pattern (which is an expression)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1178
        SyntaxTreeNode.Dummy.setParser(this);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1179
        return SyntaxTreeNode.Dummy;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1180
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1181
7f561c08de6b Initial load
duke
parents:
diff changeset
  1182
    /************************ ERROR HANDLING SECTION ************************/
7f561c08de6b Initial load
duke
parents:
diff changeset
  1183
7f561c08de6b Initial load
duke
parents:
diff changeset
  1184
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1185
     * Returns true if there were any errors during compilation
7f561c08de6b Initial load
duke
parents:
diff changeset
  1186
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1187
    public boolean errorsFound() {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1188
        return _errors.size() > 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1189
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1190
7f561c08de6b Initial load
duke
parents:
diff changeset
  1191
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1192
     * Prints all compile-time errors
7f561c08de6b Initial load
duke
parents:
diff changeset
  1193
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1194
    public void printErrors() {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1195
        final int size = _errors.size();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1196
        if (size > 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1197
            System.err.println(new ErrorMsg(ErrorMsg.COMPILER_ERROR_KEY));
7f561c08de6b Initial load
duke
parents:
diff changeset
  1198
            for (int i = 0; i < size; i++) {
39907
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents: 35729
diff changeset
  1199
                System.err.println("  " + _errors.get(i));
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1200
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1201
        }
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
     * Prints all compile-time warnings
7f561c08de6b Initial load
duke
parents:
diff changeset
  1206
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1207
    public void printWarnings() {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1208
        final int size = _warnings.size();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1209
        if (size > 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1210
            System.err.println(new ErrorMsg(ErrorMsg.COMPILER_WARNING_KEY));
7f561c08de6b Initial load
duke
parents:
diff changeset
  1211
            for (int i = 0; i < size; i++) {
39907
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents: 35729
diff changeset
  1212
                System.err.println("  " + _warnings.get(i));
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1213
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1214
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1215
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1216
7f561c08de6b Initial load
duke
parents:
diff changeset
  1217
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1218
     * Common error/warning message handler
7f561c08de6b Initial load
duke
parents:
diff changeset
  1219
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1220
    public void reportError(final int category, final ErrorMsg error) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1221
        switch (category) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1222
        case Constants.INTERNAL:
7f561c08de6b Initial load
duke
parents:
diff changeset
  1223
            // Unexpected internal errors, such as null-ptr exceptions, etc.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1224
            // Immediately terminates compilation, no translet produced
39907
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents: 35729
diff changeset
  1225
            _errors.add(error);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1226
            break;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1227
        case Constants.UNSUPPORTED:
7f561c08de6b Initial load
duke
parents:
diff changeset
  1228
            // XSLT elements that are not implemented and unsupported ext.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1229
            // Immediately terminates compilation, no translet produced
39907
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents: 35729
diff changeset
  1230
            _errors.add(error);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1231
            break;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1232
        case Constants.FATAL:
7f561c08de6b Initial load
duke
parents:
diff changeset
  1233
            // Fatal error in the stylesheet input (parsing or content)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1234
            // Immediately terminates compilation, no translet produced
39907
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents: 35729
diff changeset
  1235
            _errors.add(error);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1236
            break;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1237
        case Constants.ERROR:
7f561c08de6b Initial load
duke
parents:
diff changeset
  1238
            // Other error in the stylesheet input (parsing or content)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1239
            // Does not terminate compilation, no translet produced
39907
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents: 35729
diff changeset
  1240
            _errors.add(error);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1241
            break;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1242
        case Constants.WARNING:
7f561c08de6b Initial load
duke
parents:
diff changeset
  1243
            // Other error in the stylesheet input (content errors only)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1244
            // Does not terminate compilation, a translet is produced
39907
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents: 35729
diff changeset
  1245
            _warnings.add(error);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1246
            break;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1247
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1248
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1249
39907
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents: 35729
diff changeset
  1250
    public ArrayList<ErrorMsg> getErrors() {
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1251
        return _errors;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1252
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1253
39907
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents: 35729
diff changeset
  1254
    public ArrayList<ErrorMsg> getWarnings() {
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1255
        return _warnings;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1256
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1257
7f561c08de6b Initial load
duke
parents:
diff changeset
  1258
    /************************ SAX2 ContentHandler INTERFACE *****************/
7f561c08de6b Initial load
duke
parents:
diff changeset
  1259
39909
00e4298ae168 8162598: XSLTC transformer swallows empty namespace declaration which is needed to undeclare default namespace
clanger
parents: 39907
diff changeset
  1260
    private Stack<SyntaxTreeNode> _parentStack = null;
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
  1261
    private Map<String, String> _prefixMapping = null;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1262
7f561c08de6b Initial load
duke
parents:
diff changeset
  1263
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1264
     * SAX2: Receive notification of the beginning of a document.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1265
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1266
    public void startDocument() {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1267
        _root = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1268
        _target = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1269
        _prefixMapping = null;
39909
00e4298ae168 8162598: XSLTC transformer swallows empty namespace declaration which is needed to undeclare default namespace
clanger
parents: 39907
diff changeset
  1270
        _parentStack = new Stack<>();
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1271
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1272
7f561c08de6b Initial load
duke
parents:
diff changeset
  1273
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1274
     * SAX2: Receive notification of the end of a document.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1275
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1276
    public void endDocument() { }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1277
7f561c08de6b Initial load
duke
parents:
diff changeset
  1278
7f561c08de6b Initial load
duke
parents:
diff changeset
  1279
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1280
     * SAX2: Begin the scope of a prefix-URI Namespace mapping.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1281
     *       This has to be passed on to the symbol table!
7f561c08de6b Initial load
duke
parents:
diff changeset
  1282
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1283
    public void startPrefixMapping(String prefix, String uri) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1284
        if (_prefixMapping == null) {
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
  1285
            _prefixMapping = new HashMap<>();
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1286
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1287
        _prefixMapping.put(prefix, uri);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1288
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1289
7f561c08de6b Initial load
duke
parents:
diff changeset
  1290
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1291
     * SAX2: End the scope of a prefix-URI Namespace mapping.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1292
     *       This has to be passed on to the symbol table!
7f561c08de6b Initial load
duke
parents:
diff changeset
  1293
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1294
    public void endPrefixMapping(String prefix) { }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1295
7f561c08de6b Initial load
duke
parents:
diff changeset
  1296
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1297
     * SAX2: Receive notification of the beginning of an element.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1298
     *       The parser may re-use the attribute list that we're passed so
7f561c08de6b Initial load
duke
parents:
diff changeset
  1299
     *       we clone the attributes in our own Attributes implementation
7f561c08de6b Initial load
duke
parents:
diff changeset
  1300
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1301
    public void startElement(String uri, String localname,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1302
                             String qname, Attributes attributes)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1303
        throws SAXException {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1304
        final int col = qname.lastIndexOf(':');
7f561c08de6b Initial load
duke
parents:
diff changeset
  1305
        final String prefix = (col == -1) ? null : qname.substring(0, col);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1306
7f561c08de6b Initial load
duke
parents:
diff changeset
  1307
        SyntaxTreeNode element = makeInstance(uri, prefix,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1308
                                        localname, attributes);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1309
        if (element == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1310
            ErrorMsg err = new ErrorMsg(ErrorMsg.ELEMENT_PARSE_ERR,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1311
                                        prefix+':'+localname);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1312
            throw new SAXException(err.toString());
7f561c08de6b Initial load
duke
parents:
diff changeset
  1313
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1314
7f561c08de6b Initial load
duke
parents:
diff changeset
  1315
        // If this is the root element of the XML document we need to make sure
7f561c08de6b Initial load
duke
parents:
diff changeset
  1316
        // that it contains a definition of the XSL namespace URI
7f561c08de6b Initial load
duke
parents:
diff changeset
  1317
        if (_root == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1318
            if ((_prefixMapping == null) ||
7f561c08de6b Initial load
duke
parents:
diff changeset
  1319
                (_prefixMapping.containsValue(Constants.XSLT_URI) == false))
7f561c08de6b Initial load
duke
parents:
diff changeset
  1320
                _rootNamespaceDef = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1321
            else
7f561c08de6b Initial load
duke
parents:
diff changeset
  1322
                _rootNamespaceDef = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1323
            _root = element;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1324
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1325
        else {
39909
00e4298ae168 8162598: XSLTC transformer swallows empty namespace declaration which is needed to undeclare default namespace
clanger
parents: 39907
diff changeset
  1326
            SyntaxTreeNode parent = _parentStack.peek();
42247
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 41507
diff changeset
  1327
41507
eee8855a6cc9 8058152: JDK accepts XSLT stylesheet having import element erroneously placed
joehw
parents: 39909
diff changeset
  1328
            if (element.getClass().isAssignableFrom(Import.class) &&
eee8855a6cc9 8058152: JDK accepts XSLT stylesheet having import element erroneously placed
joehw
parents: 39909
diff changeset
  1329
                    parent.notTypeOf(Import.class)) {
eee8855a6cc9 8058152: JDK accepts XSLT stylesheet having import element erroneously placed
joehw
parents: 39909
diff changeset
  1330
                ErrorMsg err = new ErrorMsg(ErrorMsg.IMPORT_PRECEDE_OTHERS_ERR,
eee8855a6cc9 8058152: JDK accepts XSLT stylesheet having import element erroneously placed
joehw
parents: 39909
diff changeset
  1331
                                            prefix+':'+localname);
eee8855a6cc9 8058152: JDK accepts XSLT stylesheet having import element erroneously placed
joehw
parents: 39909
diff changeset
  1332
                throw new SAXException(err.toString());
eee8855a6cc9 8058152: JDK accepts XSLT stylesheet having import element erroneously placed
joehw
parents: 39909
diff changeset
  1333
            }
42247
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 41507
diff changeset
  1334
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1335
            parent.addElement(element);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1336
            element.setParent(parent);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1337
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1338
        element.setAttributes(new AttributesImpl(attributes));
7f561c08de6b Initial load
duke
parents:
diff changeset
  1339
        element.setPrefixMapping(_prefixMapping);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1340
7f561c08de6b Initial load
duke
parents:
diff changeset
  1341
        if (element instanceof Stylesheet) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1342
            // Extension elements and excluded elements have to be
7f561c08de6b Initial load
duke
parents:
diff changeset
  1343
            // handled at this point in order to correctly generate
7f561c08de6b Initial load
duke
parents:
diff changeset
  1344
            // Fallback elements from <xsl:fallback>s.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1345
            getSymbolTable().setCurrentNode(element);
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
  1346
            ((Stylesheet)element).declareExtensionPrefixes(this);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1347
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1348
7f561c08de6b Initial load
duke
parents:
diff changeset
  1349
        _prefixMapping = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1350
        _parentStack.push(element);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1351
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1352
7f561c08de6b Initial load
duke
parents:
diff changeset
  1353
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1354
     * SAX2: Receive notification of the end of an element.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1355
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1356
    public void endElement(String uri, String localname, String qname) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1357
        _parentStack.pop();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1358
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1359
7f561c08de6b Initial load
duke
parents:
diff changeset
  1360
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1361
     * SAX2: Receive notification of character data.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1362
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1363
    public void characters(char[] ch, int start, int length) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1364
        String string = new String(ch, start, length);
39909
00e4298ae168 8162598: XSLTC transformer swallows empty namespace declaration which is needed to undeclare default namespace
clanger
parents: 39907
diff changeset
  1365
        SyntaxTreeNode parent = _parentStack.peek();
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1366
7f561c08de6b Initial load
duke
parents:
diff changeset
  1367
        if (string.length() == 0) return;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1368
7f561c08de6b Initial load
duke
parents:
diff changeset
  1369
        // If this text occurs within an <xsl:text> element we append it
7f561c08de6b Initial load
duke
parents:
diff changeset
  1370
        // as-is to the existing text element
7f561c08de6b Initial load
duke
parents:
diff changeset
  1371
        if (parent instanceof Text) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1372
            ((Text)parent).setText(string);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1373
            return;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1374
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1375
7f561c08de6b Initial load
duke
parents:
diff changeset
  1376
        // Ignore text nodes that occur directly under <xsl:stylesheet>
7f561c08de6b Initial load
duke
parents:
diff changeset
  1377
        if (parent instanceof Stylesheet) return;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1378
7f561c08de6b Initial load
duke
parents:
diff changeset
  1379
        SyntaxTreeNode bro = parent.lastChild();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1380
        if ((bro != null) && (bro instanceof Text)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1381
            Text text = (Text)bro;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1382
            if (!text.isTextElement()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1383
                if ((length > 1) || ( ((int)ch[0]) < 0x100)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1384
                    text.setText(string);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1385
                    return;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1386
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1387
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1388
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1389
7f561c08de6b Initial load
duke
parents:
diff changeset
  1390
        // Add it as a regular text node otherwise
7f561c08de6b Initial load
duke
parents:
diff changeset
  1391
        parent.addElement(new Text(string));
7f561c08de6b Initial load
duke
parents:
diff changeset
  1392
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1393
7f561c08de6b Initial load
duke
parents:
diff changeset
  1394
    private String getTokenValue(String token) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1395
        final int start = token.indexOf('"');
7f561c08de6b Initial load
duke
parents:
diff changeset
  1396
        final int stop = token.lastIndexOf('"');
7f561c08de6b Initial load
duke
parents:
diff changeset
  1397
        return token.substring(start+1, stop);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1398
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1399
7f561c08de6b Initial load
duke
parents:
diff changeset
  1400
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1401
     * SAX2: Receive notification of a processing instruction.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1402
     *       These require special handling for stylesheet PIs.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1403
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1404
    public void processingInstruction(String name, String value) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1405
        // We only handle the <?xml-stylesheet ...?> PI
7f561c08de6b Initial load
duke
parents:
diff changeset
  1406
        if ((_target == null) && (name.equals("xml-stylesheet"))) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1407
7f561c08de6b Initial load
duke
parents:
diff changeset
  1408
            String href = null;    // URI of stylesheet found
7f561c08de6b Initial load
duke
parents:
diff changeset
  1409
            String media = null;   // Media of stylesheet found
7f561c08de6b Initial load
duke
parents:
diff changeset
  1410
            String title = null;   // Title of stylesheet found
7f561c08de6b Initial load
duke
parents:
diff changeset
  1411
            String charset = null; // Charset of stylesheet found
7f561c08de6b Initial load
duke
parents:
diff changeset
  1412
7f561c08de6b Initial load
duke
parents:
diff changeset
  1413
            // Get the attributes from the processing instruction
7f561c08de6b Initial load
duke
parents:
diff changeset
  1414
            StringTokenizer tokens = new StringTokenizer(value);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1415
            while (tokens.hasMoreElements()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1416
                String token = (String)tokens.nextElement();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1417
                if (token.startsWith("href"))
7f561c08de6b Initial load
duke
parents:
diff changeset
  1418
                    href = getTokenValue(token);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1419
                else if (token.startsWith("media"))
7f561c08de6b Initial load
duke
parents:
diff changeset
  1420
                    media = getTokenValue(token);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1421
                else if (token.startsWith("title"))
7f561c08de6b Initial load
duke
parents:
diff changeset
  1422
                    title = getTokenValue(token);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1423
                else if (token.startsWith("charset"))
7f561c08de6b Initial load
duke
parents:
diff changeset
  1424
                    charset = getTokenValue(token);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1425
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1426
7f561c08de6b Initial load
duke
parents:
diff changeset
  1427
            // Set the target to this PI's href if the parameters are
7f561c08de6b Initial load
duke
parents:
diff changeset
  1428
            // null or match the corresponding attributes of this PI.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1429
            if ( ((_PImedia == null) || (_PImedia.equals(media))) &&
7f561c08de6b Initial load
duke
parents:
diff changeset
  1430
                 ((_PItitle == null) || (_PImedia.equals(title))) &&
7f561c08de6b Initial load
duke
parents:
diff changeset
  1431
                 ((_PIcharset == null) || (_PImedia.equals(charset))) ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1432
                _target = href;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1433
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1434
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1435
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1436
7f561c08de6b Initial load
duke
parents:
diff changeset
  1437
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1438
     * IGNORED - all ignorable whitespace is ignored
7f561c08de6b Initial load
duke
parents:
diff changeset
  1439
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1440
    public void ignorableWhitespace(char[] ch, int start, int length) { }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1441
7f561c08de6b Initial load
duke
parents:
diff changeset
  1442
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1443
     * IGNORED - we do not have to do anything with skipped entities
7f561c08de6b Initial load
duke
parents:
diff changeset
  1444
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1445
    public void skippedEntity(String name) { }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1446
7f561c08de6b Initial load
duke
parents:
diff changeset
  1447
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1448
     * Store the document locator to later retrieve line numbers of all
7f561c08de6b Initial load
duke
parents:
diff changeset
  1449
     * elements from the stylesheet
7f561c08de6b Initial load
duke
parents:
diff changeset
  1450
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1451
    public void setDocumentLocator(Locator locator) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1452
        _locator = locator;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1453
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1454
7f561c08de6b Initial load
duke
parents:
diff changeset
  1455
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1456
     * Get the line number, or zero
7f561c08de6b Initial load
duke
parents:
diff changeset
  1457
     * if there is no _locator.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1458
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1459
    private int getLineNumber() {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1460
        int line = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1461
        if (_locator != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1462
                line = _locator.getLineNumber();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1463
        return line;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1464
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1465
7f561c08de6b Initial load
duke
parents:
diff changeset
  1466
}