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