KickJava   Java API By Example, From Geeks To Geeks.

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


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 SubstringBefore extends Function {
10
11     public String JavaDoc getName() {
12         return "substring-before";
13     };
14
15     /**
16     * Determine the data type of the expression
17     * @return Value.STRING
18     */

19
20     public int getDataType() {
21         return Value.STRING;
22     }
23
24     /**
25     * Simplify and validate
26     */

27
28     public Expression simplify() throws XPathException {
29         int numArgs = checkArgumentCount(2,2);
30         argument[0] = argument[0].simplify();
31         argument[1] = argument[1].simplify();
32         boolean fixed = (argument[0] instanceof Value) && (argument[1] instanceof Value);
33
34         if (fixed) {
35             return evaluate(null);
36         }
37         return this;
38     }
39
40     /**
41     * Evaluate the function in a string context
42     */

43
44     public String JavaDoc evaluateAsString(Context context) throws XPathException {
45
46         String JavaDoc s = argument[0].evaluateAsString(context);
47         String JavaDoc a = argument[1].evaluateAsString(context);
48         return before(s, a);
49     }
50
51     /**
52     * Evaluate in a general context
53     */

54
55     public Value evaluate(Context c) throws XPathException {
56         return new StringValue(evaluateAsString(c));
57     }
58     
59
60     /**
61     * Get dependencies
62     */

63
64     public int getDependencies() {
65         int dep = argument[0].getDependencies() | argument[1].getDependencies();
66         return dep;
67     }
68
69     /**
70     * Remove dependencies
71     */

72
73     public Expression reduce(int dep, Context c) throws XPathException {
74         SubstringBefore f = new SubstringBefore();
75         f.addArgument(argument[0].reduce(dep, c));
76         f.addArgument(argument[1].reduce(dep, c));
77         f.setStaticContext(getStaticContext());
78         return f.simplify();
79     }
80
81     /**
82     * Here is the algorithm
83     */

84
85     private static String JavaDoc before(String JavaDoc s1, String JavaDoc s2) {
86         int i = s1.indexOf(s2);
87         if (i<0) return "";
88         return s1.substring(0, i);
89     }
90     
91 }
92
93
94
95 //
96
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
97
// you may not use this file except in compliance with the License. You may obtain a copy of the
98
// License at http://www.mozilla.org/MPL/
99
//
100
// Software distributed under the License is distributed on an "AS IS" basis,
101
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
102
// See the License for the specific language governing rights and limitations under the License.
103
//
104
// The Original Code is: all this file.
105
//
106
// The Initial Developer of the Original Code is
107
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
108
//
109
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
110
//
111
// Contributor(s): none.
112
//
113
Popular Tags