KickJava   Java API By Example, From Geeks To Geeks.

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


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
7 public class StringLength extends Function {
8
9     /**
10     * Function name (for diagnostics)
11     */

12
13     public String JavaDoc getName() {
14         return "string-length";
15     };
16
17     /**
18     * Determine the data type of the expression
19     * @return Value.NUMBER
20     */

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

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

45
46     public double evaluateAsNumber(Context c) throws XPathException {
47         if (getNumberOfArguments()==1) {
48             return StringValue.getLength(
49                     argument[0].evaluateAsString(c));
50         } else {
51             return StringValue.getLength(
52                     (c.getContextNodeInfo()).getStringValue());
53         }
54     }
55
56     /**
57     * Evaluate in a general context
58     */

59
60     public Value evaluate(Context c) throws XPathException {
61         return new NumericValue(evaluateAsNumber(c));
62     }
63
64     /**
65     * Determine the dependencies
66     */

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

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