KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.*;
8
9
10
11 public class NameFn extends Function {
12
13     /**
14     * Function name (for diagnostics)
15     */

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

25
26     public int getDataType() {
27         return Value.STRING;
28     }
29
30     /**
31     * Simplify and validate.
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         }
39         return this;
40     }
41
42     /**
43     * Evaluate the function in a string context
44     */

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

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

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

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