KickJava   Java API By Example, From Geeks To Geeks.

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


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 SubstringAfter extends Function {
10
11     public String JavaDoc getName() {
12         return "substring-after";
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 after(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         SubstringAfter f = new SubstringAfter();
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     * Return those characters in the input string s1 that come after the first appearance of
83     * another string s2. If s2 is not present in s1, return the empty string.
84     */

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