~vcs-imports/xena/trunk

« back to all changes in this revision

Viewing changes to ext/src/xalan-j_2_7_1/src/org/apache/xalan/xsltc/compiler/ElementAvailableCall.java

  • Committer: matthewoliver
  • Date: 2009-12-10 03:18:07 UTC
  • Revision ID: vcs-imports@canonical.com-20091210031807-l086qguzdlljtkl9
Merged Xena Testing into Xena Stable for the Xena 5 release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Licensed to the Apache Software Foundation (ASF) under one
 
3
 * or more contributor license agreements. See the NOTICE file
 
4
 * distributed with this work for additional information
 
5
 * regarding copyright ownership. The ASF licenses this file
 
6
 * to you under the Apache License, Version 2.0 (the  "License");
 
7
 * you may not use this file except in compliance with the License.
 
8
 * You may obtain a copy of the License at
 
9
 *
 
10
 *     http://www.apache.org/licenses/LICENSE-2.0
 
11
 *
 
12
 * Unless required by applicable law or agreed to in writing, software
 
13
 * distributed under the License is distributed on an "AS IS" BASIS,
 
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
15
 * See the License for the specific language governing permissions and
 
16
 * limitations under the License.
 
17
 */
 
18
/*
 
19
 * $Id: ElementAvailableCall.java,v 1.2 2009/12/10 03:18:18 matthewoliver Exp $
 
20
 */
 
21
 
 
22
package org.apache.xalan.xsltc.compiler;
 
23
 
 
24
import java.util.Vector;
 
25
 
 
26
import org.apache.bcel.generic.ConstantPoolGen;
 
27
import org.apache.bcel.generic.PUSH;
 
28
import org.apache.xalan.xsltc.compiler.util.ClassGenerator;
 
29
import org.apache.xalan.xsltc.compiler.util.ErrorMsg;
 
30
import org.apache.xalan.xsltc.compiler.util.MethodGenerator;
 
31
import org.apache.xalan.xsltc.compiler.util.Type;
 
32
import org.apache.xalan.xsltc.compiler.util.TypeCheckError;
 
33
 
 
34
/**
 
35
 * @author Jacek Ambroziak
 
36
 * @author Santiago Pericas-Geertsen
 
37
 */
 
38
final class ElementAvailableCall extends FunctionCall {
 
39
 
 
40
    public ElementAvailableCall(QName fname, Vector arguments) {
 
41
        super(fname, arguments);
 
42
    }
 
43
 
 
44
    /**
 
45
     * Force the argument to this function to be a literal string.
 
46
     */
 
47
    public Type typeCheck(SymbolTable stable) throws TypeCheckError {
 
48
        if (argument() instanceof LiteralExpr) {
 
49
            return _type = Type.Boolean;
 
50
        }
 
51
        ErrorMsg err = new ErrorMsg(ErrorMsg.NEED_LITERAL_ERR,
 
52
                                    "element-available", this);
 
53
        throw new TypeCheckError(err);
 
54
    }
 
55
 
 
56
    /**
 
57
     * Returns an object representing the compile-time evaluation 
 
58
     * of an expression. We are only using this for function-available
 
59
     * and element-available at this time.
 
60
     */
 
61
    public Object evaluateAtCompileTime() {
 
62
        return getResult() ? Boolean.TRUE : Boolean.FALSE;
 
63
    }
 
64
 
 
65
    /**
 
66
     * Returns the result that this function will return
 
67
     */
 
68
    public boolean getResult() {
 
69
        try {
 
70
            final LiteralExpr arg = (LiteralExpr) argument();
 
71
            final String qname = arg.getValue();
 
72
            final int index = qname.indexOf(':');
 
73
            final String localName = (index > 0) ? 
 
74
                qname.substring(index + 1) : qname;
 
75
            return getParser().elementSupported(arg.getNamespace(), 
 
76
                                                localName);
 
77
        }
 
78
        catch (ClassCastException e) {
 
79
            return false;
 
80
        }
 
81
    }
 
82
 
 
83
    /**
 
84
     * Calls to 'element-available' are resolved at compile time since 
 
85
     * the namespaces declared in the stylsheet are not available at run
 
86
     * time. Consequently, arguments to this function must be literals.
 
87
     */
 
88
    public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
 
89
        final ConstantPoolGen cpg = classGen.getConstantPool();
 
90
        final boolean result = getResult();
 
91
        methodGen.getInstructionList().append(new PUSH(cpg, result));
 
92
    }
 
93
}