KickJava   Java API By Example, From Geeks To Geeks.

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


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

15
16     public String JavaDoc getName() {
17         return "starts-with";
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(2, 2);
36         argument[0] = argument[0].simplify();
37         argument[1] = argument[1].simplify();
38
39         if (argument[0] instanceof Value && argument[1] instanceof Value) {
40             return evaluate(null);
41         }
42         
43         if (argument[1] instanceof Value &&
44                  ((Value)argument[1]).asString().equals("")) {
45             return new BooleanValue(true);
46         }
47
48         return this;
49     }
50
51     /**
52     * Evaluate the function in a boolean context
53     */

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

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

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

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