KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.icl.saxon.functions;
2 import com.icl.saxon.*;
3 import com.icl.saxon.om.NodeInfo;
4 import com.icl.saxon.om.NodeEnumeration;
5 import com.icl.saxon.expr.*;
6
7 public class LocalName extends Function {
8
9     /**
10     * Function name (for diagnostics)
11     */

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

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

29
30     public Expression simplify() throws XPathException {
31         int numArgs = checkArgumentCount(0, 1);
32         if (numArgs==1) {
33             argument[0] = argument[0].simplify();
34         }
35         return this;
36     }
37
38     /**
39     * Evaluate the function in a string context
40     */

41
42     public String evaluateAsString(Context c) throws XPathException {
43         if (getNumberOfArguments()==1) {
44             NodeEnumeration enum = argument[0].enumerate(c, true);
45             if (enum.hasMoreElements()) {
46                 return enum.nextElement().getLocalName();
47             } else {
48                 return "";
49             }
50         } else {
51             return c.getContextNodeInfo().getLocalName();
52         }
53     }
54
55     /**
56     * Evaluate in a general context
57     */

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

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

78
79     public Expression reduce(int dep, Context c) throws XPathException {
80         if (getNumberOfArguments()==1) {
81             LocalName f = new LocalName();
82             f.addArgument(argument[0].reduce(dep, c));
83             f.setStaticContext(getStaticContext());
84             return f.simplify();
85         } else {
86             if ((dep & Context.CONTEXT_NODE)!=0) {
87                 return evaluate(c);
88             } else {
89                 return this;
90             }
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