KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > functions > ElementAvailable


1 package com.icl.saxon.functions;
2 import com.icl.saxon.*;
3 import com.icl.saxon.om.Namespace;
4 import com.icl.saxon.expr.*;
5 import com.icl.saxon.style.*;
6
7 import java.util.*;
8
9
10 public class ElementAvailable extends Function {
11
12     /**
13     * Function name (for diagnostics)
14     */

15
16     public String JavaDoc getName() {
17         return "element-available";
18     };
19
20     /**
21     * Determine the data type of the expression
22     * @return Value.BOOLEAN
23     */

24
25     public int getDataType() {
26         return Value.BOOLEAN;
27     }
28
29     /**
30     * Simplify and validate.
31     * This is a pure function so it can be simplified in advance if the arguments are known
32     */

33
34     public Expression simplify() throws XPathException {
35         checkArgumentCount(1, 1);
36         argument[0] = argument[0].simplify();
37         if (argument[0] instanceof Value) {
38             return evaluate(null);
39         }
40         return this;
41     }
42
43     /**
44     * Evaluate the function in a boolean context
45     */

46
47     public boolean evaluateAsBoolean(Context c) throws XPathException {
48         String JavaDoc qname = argument[0].evaluateAsString(c);
49         return getStaticContext().isElementAvailable(qname);
50     }
51
52     /**
53     * Evaluate in a general context
54     */

55
56     public Value evaluate(Context c) throws XPathException {
57         return new BooleanValue(evaluateAsBoolean(c));
58     }
59
60     /**
61     * Determine the dependencies
62     */

63
64     public int getDependencies() {
65         return argument[0].getDependencies();
66     }
67
68     /**
69     * Reduce the dependencies
70     */

71
72     public Expression reduce(int dep, Context c) throws XPathException {
73         ElementAvailable f = new ElementAvailable();
74         f.addArgument(argument[0].reduce(dep, c));
75         f.setStaticContext(getStaticContext());
76         return f.simplify();
77     }
78
79     /**
80     * Get the factory class for user extension elements
81     */

82
83     //private ExtensionElementFactory getFactory(String uri) throws XPathException {
84
// int lastSlash = uri.lastIndexOf('/');
85
// if (lastSlash<0 || lastSlash==uri.length()-1) {
86
// return null;
87
// }
88
// String factoryClass = uri.substring(lastSlash+1);
89
// ExtensionElementFactory factory;
90
//
91
// try {
92
// return (ExtensionElementFactory)Loader.getInstance(factoryClass);
93
// } catch (Exception err) {
94
// return null;
95
// }
96
///}
97

98 }
99
100
101
102 //
103
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
104
// you may not use this file except in compliance with the License. You may obtain a copy of the
105
// License at http://www.mozilla.org/MPL/
106
//
107
// Software distributed under the License is distributed on an "AS IS" basis,
108
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
109
// See the License for the specific language governing rights and limitations under the License.
110
//
111
// The Original Code is: all this file.
112
//
113
// The Initial Developer of the Original Code is
114
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
115
//
116
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
117
//
118
// Contributor(s): none.
119
//
120
Popular Tags