KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > functions > StringLength


1 package net.sf.saxon.functions;
2 import net.sf.saxon.expr.*;
3 import net.sf.saxon.om.Item;
4 import net.sf.saxon.trans.XPathException;
5 import net.sf.saxon.value.AtomicValue;
6 import net.sf.saxon.value.IntegerValue;
7 import net.sf.saxon.value.StringValue;
8
9 /**
10  * Implement the XPath string-length() function
11  */

12
13 public class StringLength extends SystemFunction {
14
15     private boolean shortcut = false;
16                                 // if this is set we return 0 for a zero length string,
17
// 1 for any other. Used by the optimizer.
18
/**
19     * Simplify and validate.
20     * This is a pure function so it can be simplified in advance if the arguments are known
21     */

22
23      public Expression simplify(StaticContext env) throws XPathException {
24         //useContextItemAsDefault();
25
return simplifyArguments(env);
26     }
27
28     /**
29      * Determine the intrinsic dependencies of an expression, that is, those which are not derived
30      * from the dependencies of its subexpressions. For example, position() has an intrinsic dependency
31      * on the context position, while (position()+1) does not. The default implementation
32      * of the method returns 0, indicating "no dependencies".
33      *
34      * @return a set of bit-significant flags identifying the "intrinsic"
35      * dependencies. The flags are documented in class net.sf.saxon.value.StaticProperty
36      */

37
38     public int getIntrinsicDependencies() {
39         int d = super.getIntrinsicDependencies();
40         if (argument.length == 0) {
41             d |= StaticProperty.DEPENDS_ON_CONTEXT_ITEM;
42         }
43         return d;
44     }
45
46     /**
47     * Pre-evaluate a function at compile time. Functions that do not allow
48     * pre-evaluation, or that need access to context information, can override this method.
49     */

50
51     public Expression preEvaluate(StaticContext env) throws XPathException {
52         if (argument.length == 0) {
53             return this;
54         } else {
55             return ExpressionTool.eagerEvaluate(this, null);
56         }
57     }
58
59     /**
60     * setShortCut() - used by optimizer when we only need to know if the length is non-zero
61     */

62
63     public void setShortcut() {
64         shortcut = true;
65     }
66
67     /**
68     * Evaluate in a general context
69     */

70
71     public Item evaluateItem(XPathContext c) throws XPathException {
72         AtomicValue sv;
73         if (argument.length == 0) {
74             sv = StringValue.makeStringValue(c.getContextItem().getStringValueCS());
75         } else {
76             sv = (AtomicValue)argument[0].evaluateItem(c);
77         }
78         if (sv==null) {
79             return IntegerValue.ZERO;
80         }
81         CharSequence JavaDoc s = sv.getStringValueCS();
82
83         if (shortcut) {
84             return new IntegerValue((s.length()>0 ? 1 : 0));
85         } else {
86             return new IntegerValue(StringValue.getStringLength(s));
87         }
88     }
89
90 }
91
92
93
94 //
95
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
96
// you may not use this file except in compliance with the License. You may obtain a copy of the
97
// License at http://www.mozilla.org/MPL/
98
//
99
// Software distributed under the License is distributed on an "AS IS" basis,
100
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
101
// See the License for the specific language governing rights and limitations under the License.
102
//
103
// The Original Code is: all this file.
104
//
105
// The Initial Developer of the Original Code is Michael H. Kay.
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