KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

24
25     public int getDataType() {
26         return Value.STRING;
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         int numArgs = checkArgumentCount(0, 1);
36         if (numArgs==1) {
37             argument[0] = argument[0].simplify();
38             if (argument[0].getDataType()==Value.STRING) {
39                 return argument[0];
40             }
41             if (argument[0] instanceof Value) {
42                 return new StringValue(((Value)argument[0]).asString());
43             }
44         }
45         return this;
46     }
47
48     /**
49     * Evaluate the function in a string context
50     */

51
52     public String JavaDoc evaluateAsString(Context c) throws XPathException {
53         if (getNumberOfArguments()==1) {
54             return argument[0].evaluateAsString(c);
55         } else {
56             return (c.getContextNodeInfo()).getStringValue();
57         }
58     }
59
60     /**
61     * Evaluate in a general context
62     */

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

71
72     public int getDependencies() {
73         if (getNumberOfArguments()==1) {
74             return argument[0].getDependencies();
75         } else {
76             return Context.CONTEXT_NODE;
77         }
78     }
79
80     /**
81     * Reduce the dependencies
82     */

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