1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63 package org.jaxen.function;
64
65 import java.util.List;
66
67 import org.jaxen.Context;
68 import org.jaxen.Function;
69 import org.jaxen.FunctionCallException;
70 import org.jaxen.Navigator;
71
72 /***
73 * <p><b>4.1</b> <code><i>string</i> name(<i>node-set?</i>)</code> </p>
74 *
75 *
76 * <blockquote src="http://www.w3.org/TR/xpath">
77 * The <b>name</b> function returns a string containing a QName representing
78 * the expanded-name of the node in the argument node-set that is first in document order.
79 * The QName must represent the expanded-name with respect to the namespace declarations
80 * in effect on the node whose expanded-name is being represented. Typically, this will
81 * be the QName that occurred in the XML source....If
82 * the argument node-set is empty or the first node has
83 * no expanded-name, an empty string is returned.
84 * If the argument it omitted, it defaults to a node-set with the context node as its only member.
85 * </blockquote>
86 *
87 * @author bob mcwhirter (bob @ werken.com)
88 *
89 * @see <a href="http://www.w3.org/TR/xpath#function-name" target="_top">Section 4.1 of the XPath Specification</a>
90 */
91 public class NameFunction implements Function
92 {
93
94
95 /***
96 * Create a new <code>NameFunction</code> object.
97 */
98 public NameFunction() {}
99
100 /***
101 * Returns the name of the specified node or the name of the context node if
102 * no arguments are provided.
103 *
104 * @param context the context at the point in the
105 * expression where the function is called
106 * @param args a <code>List</code> containing zero or one items
107 *
108 * @return a <code>String</code> containing the name
109 *
110 * @throws FunctionCallException if <code>args</code> has more than one item
111 */
112 public Object call(Context context,
113 List args) throws FunctionCallException
114 {
115 if ( args.size() == 0 )
116 {
117 return evaluate( context.getNodeSet(),
118 context.getNavigator() );
119 }
120
121 if ( args.size() == 1 )
122 {
123 return evaluate( args,
124 context.getNavigator() );
125 }
126
127 throw new FunctionCallException( "name() requires zero or one argument." );
128 }
129
130 /***
131 * Returns the name of <code>list.get(0)</code>
132 *
133 * @param list a list of nodes
134 * @param nav the <code>Navigator</code> used to retrieve the name
135 *
136 * @return the name of <code>list.get(0)</code>
137 *
138 * @throws FunctionCallException if <code>list.get(0)</code> is not a node
139 */
140 public static String evaluate(List list,
141 Navigator nav) throws FunctionCallException
142 {
143 if ( ! list.isEmpty() )
144 {
145 Object first = list.get(0);
146
147 if (first instanceof List)
148 {
149 return evaluate( (List) first,
150 nav );
151 }
152 else if ( nav.isElement( first ) )
153 {
154 return nav.getElementQName( first );
155 }
156 else if ( nav.isAttribute( first ) )
157 {
158 return nav.getAttributeQName( first );
159 }
160 else if ( nav.isProcessingInstruction( first ) )
161 {
162 return nav.getProcessingInstructionTarget( first );
163 }
164 else if ( nav.isNamespace( first ) )
165 {
166 return nav.getNamespacePrefix( first );
167 }
168 else if ( nav.isDocument( first ) )
169 {
170 return "";
171 }
172 else if ( nav.isComment( first ) )
173 {
174 return "";
175 }
176 else if ( nav.isText( first ) )
177 {
178 return "";
179 }
180 else {
181 throw new FunctionCallException("The argument to the name function must be a node-set");
182 }
183 }
184
185 return "";
186
187 }
188
189 }