KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

32
33     public Expression simplify() throws XPathException {
34         checkArgumentCount(2, 2);
35         argument[0] = argument[0].simplify();
36         argument[1] = argument[1].simplify();
37
38         if (argument[0] instanceof Value && argument[1] instanceof Value) {
39             return evaluate(null);
40         }
41         
42         if (argument[1] instanceof Value &&
43                  ((Value)argument[1]).asString().equals("")) {
44             return new BooleanValue(true);
45         }
46
47         return this;
48     }
49
50     /**
51     * Evaluate the function in a boolean context
52     */

53
54     public boolean evaluateAsBoolean(Context c) throws XPathException {
55         String JavaDoc arg0 = argument[0].evaluateAsString(c);
56         String JavaDoc arg1 = argument[1].evaluateAsString(c);
57         return arg0.indexOf(arg1) >= 0;
58     }
59
60     /**
61     * Evaluate in a general context
62     */

63
64     public Value evaluate(Context c) throws XPathException {
65         return new BooleanValue(evaluateAsBoolean(c));
66     }
67
68     /**
69     * Determine the dependencies
70     */

71
72     public int getDependencies() {
73         return argument[0].getDependencies() | argument[1].getDependencies();
74     }
75
76     /**
77     * Reduce the dependencies
78     */

79
80     public Expression reduce(int dep, Context c) throws XPathException {
81         Contains f = new Contains();
82         f.addArgument(argument[0].reduce(dep, c));
83         f.addArgument(argument[1].reduce(dep, c));
84         f.setStaticContext(getStaticContext());
85         return f.simplify();
86     }
87
88 }
89
90
91
92 //
93
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
94
// you may not use this file except in compliance with the License. You may obtain a copy of the
95
// License at http://www.mozilla.org/MPL/
96
//
97
// Software distributed under the License is distributed on an "AS IS" basis,
98
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
99
// See the License for the specific language governing rights and limitations under the License.
100
//
101
// The Original Code is: all this file.
102
//
103
// The Initial Developer of the Original Code is
104
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
105
//
106
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
107
//
108
// Contributor(s): none.
109
//
110
Popular Tags