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