1/*
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3 */
4/*
5 * Licensed to the Apache Software Foundation (ASF) under one or more
6 * contributor license agreements.  See the NOTICE file distributed with
7 * this work for additional information regarding copyright ownership.
8 * The ASF licenses this file to You under the Apache License, Version 2.0
9 * (the "License"); you may not use this file except in compliance with
10 * the License.  You may obtain a copy of the License at
11 *
12 *     http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20/*
21 * $Id: UnsupportedElement.java,v 1.2.4.1 2005/09/05 09:26:51 pvedula Exp $
22 */
23
24package com.sun.org.apache.xalan.internal.xsltc.compiler;
25
26import java.util.Vector;
27
28import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
29import com.sun.org.apache.bcel.internal.generic.INVOKESTATIC;
30import com.sun.org.apache.bcel.internal.generic.InstructionList;
31import com.sun.org.apache.bcel.internal.generic.PUSH;
32
33import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
34import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
35import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
36import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
37import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
38import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util;
39import java.util.List;
40
41/**
42 * @author Morten Jorgensen
43 */
44final class UnsupportedElement extends SyntaxTreeNode {
45
46    private Vector _fallbacks = null;
47    private ErrorMsg _message = null;
48    private boolean _isExtension = false;
49
50    /**
51     * Basic consutrcor - stores element uri/prefix/localname
52     */
53    public UnsupportedElement(String uri, String prefix, String local, boolean isExtension) {
54        super(uri, prefix, local);
55        _isExtension = isExtension;
56    }
57
58    /**
59     * There are different categories of unsupported elements (believe it
60     * or not): there are elements within the XSLT namespace (these would
61     * be elements that are not yet implemented), there are extensions of
62     * other XSLT processors and there are unrecognised extension elements
63     * of this XSLT processor. The error message passed to this method
64     * should describe the unsupported element itself and what category
65     * the element belongs in.
66     */
67    public void setErrorMessage(ErrorMsg message) {
68        _message = message;
69    }
70
71    /**
72     * Displays the contents of this element
73     */
74    public void display(int indent) {
75        indent(indent);
76        Util.println("Unsupported element = " + _qname.getNamespace() +
77                     ":" + _qname.getLocalPart());
78        displayContents(indent + IndentIncrement);
79    }
80
81
82    /**
83     * Scan and process all fallback children of the unsupported element.
84     */
85    private void processFallbacks(Parser parser) {
86
87        List<SyntaxTreeNode> children = getContents();
88        if (children != null) {
89            final int count = children.size();
90            for (int i = 0; i < count; i++) {
91                SyntaxTreeNode child = children.get(i);
92                if (child instanceof Fallback) {
93                    Fallback fallback = (Fallback)child;
94                    fallback.activate();
95                    fallback.parseContents(parser);
96                    if (_fallbacks == null) {
97                        _fallbacks = new Vector();
98                    }
99                    _fallbacks.addElement(child);
100                }
101            }
102        }
103    }
104
105    /**
106     * Find any fallback in the descendant nodes; then activate & parse it
107     */
108    public void parseContents(Parser parser) {
109        processFallbacks(parser);
110    }
111
112    /**
113     * Run type check on the fallback element (if any).
114     */
115    public Type typeCheck(SymbolTable stable) throws TypeCheckError {
116        if (_fallbacks != null) {
117            int count = _fallbacks.size();
118            for (int i = 0; i < count; i++) {
119                Fallback fallback = (Fallback)_fallbacks.elementAt(i);
120                fallback.typeCheck(stable);
121            }
122        }
123        return Type.Void;
124    }
125
126    /**
127     * Translate the fallback element (if any).
128     */
129    public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
130        if (_fallbacks != null) {
131            int count = _fallbacks.size();
132            for (int i = 0; i < count; i++) {
133                Fallback fallback = (Fallback)_fallbacks.elementAt(i);
134                fallback.translate(classGen, methodGen);
135            }
136        }
137        // We only go into the else block in forward-compatibility mode, when
138        // the unsupported element has no fallback.
139        else {
140            // If the unsupported element does not have any fallback child, then
141            // at runtime, a runtime error should be raised when the unsupported
142            // element is instantiated. Otherwise, no error is thrown.
143            ConstantPoolGen cpg = classGen.getConstantPool();
144            InstructionList il = methodGen.getInstructionList();
145
146            final int unsupportedElem = cpg.addMethodref(BASIS_LIBRARY_CLASS, "unsupported_ElementF",
147                                                         "(" + STRING_SIG + "Z)V");
148            il.append(new PUSH(cpg, getQName().toString()));
149            il.append(new PUSH(cpg, _isExtension));
150            il.append(new INVOKESTATIC(unsupportedElem));
151        }
152    }
153}
154