@@ -119,7 +119,7 @@ private static int getNodePosition(final Node node) {
119
119
int position = 1 ;
120
120
Node siblingNode = node .getPreviousSibling ();
121
121
while (siblingNode != null ) {
122
- if (siblingNode != null && siblingNode .getNodeName ().equals (node .getNodeName ())) {
122
+ if (siblingNode .getNodeName ().equals (node .getNodeName ())) {
123
123
++position ;
124
124
}
125
125
siblingNode = siblingNode .getPreviousSibling ();
@@ -138,70 +138,83 @@ private static void getPathValues(final Node node, final List<String> values) {
138
138
139
139
final StringBuilder value = new StringBuilder ();
140
140
141
- if (node .getNodeType () == Node .ATTRIBUTE_NODE ) {
142
- // For an attribute node, if the node is in no namespace,
143
- // @local, where local is the local part of the node name.
144
- // Otherwise, @Q{uri}local, where uri is the namespace URI of
145
- // the node name, and local is the local part of the node name.
146
- value .append ('/' );
147
- if (node .getNamespaceURI () != null ) {
148
- value .append (String .format ("@Q{%s}" , node .getNamespaceURI ()));
149
- } else {
150
- value .append ('@' );
151
- }
152
- value .append (node .getLocalName ());
153
-
154
- // attributes have an owner element - not a parent node!
155
- parent = ((Attr ) node ).getOwnerElement ();
156
-
157
- } else if (node .getNodeType () == Node .TEXT_NODE ) {
158
- // For a text node: text()[position] where position is an integer
159
- // representing the position of the selected node among its text
160
- // node siblings
161
- final int nodePosition = getNodePosition (node );
162
- if (nodePosition > 0 ) {
163
- value .append (String .format ("/text()[%d]" , nodePosition ));
164
- }
165
- } else if (node .getNodeType () == Node .COMMENT_NODE ) {
166
- // For a comment node: comment()[position] where position is an
167
- // integer representing the position of the selected node among
168
- // its comment node siblings.
169
- final int nodePosition = getNodePosition (node );
170
- if (nodePosition > 0 ) {
171
- value .append (String .format ("/comment()[%d]" , nodePosition ));
172
- }
173
- } else if (node .getNodeType () == Node .PROCESSING_INSTRUCTION_NODE ) {
174
- // For a processing-instruction node: processing-instruction(local)[position]
175
- // where local is the name of the processing instruction node and position is
176
- // an integer representing the position of the selected node among its
177
- // like-named processing-instruction node siblings.
178
- final int nodePosition = getNodePosition (node );
179
- if (nodePosition > 0 ) {
180
- value .append (String .format ("/processing-instruction(%s)[%d]" , node .getNodeName (), nodePosition ));
181
- }
182
- } else if (node .getNodeType () == INode .NAMESPACE_NODE ) {
183
- // For a namespace node: If the namespace node has a name: namespace::prefix,
184
- // where prefix is the local part of the name of the namespace node
185
- // (which represents the namespace prefix). If the namespace node
186
- // has no name (that is, it represents the default namespace):
187
- // namespace::*[Q{http://www.w3.org/2005/xpath-functions}local-name()=""]
188
- if (node .getNamespaceURI () != null ) {
189
- value .append (String .format ("namespace::{%s}" , node .getLocalName ()));
190
- } else {
191
- value .append ("namespace::*[Q{http://www.w3.org/2005/xpath-functions}local-name()=\" \" ]" );
192
- }
193
- } else if (node .getLocalName () != null ) {
194
- // For an element node, Q{uri}local[position], where uri is the
195
- // namespace URI of the node name or the empty string if the
196
- // node is in no namespace, local is the local part of the node
197
- // name, and position is an integer representing the position
198
- // of the selected node among its like-named siblings.
199
- final int nodePosition = getNodePosition (node );
200
- value .append ((node .getOwnerDocument () != null && node .getOwnerDocument ().getDocumentElement () != null ) ? "/Q" : "Q" );
201
- value .append (((INode ) node ).getQName ().toURIQualifiedName ());
202
- if (nodePosition > 0 ) {
203
- value .append (String .format ("[%d]" , nodePosition ));
204
- }
141
+ switch (node .getNodeType ()) {
142
+ case Node .ATTRIBUTE_NODE :
143
+ // For an attribute node, if the node is in no namespace,
144
+ // @local, where local is the local part of the node name.
145
+ // Otherwise, @Q{uri}local, where uri is the namespace URI of
146
+ // the node name, and local is the local part of the node name.
147
+ value .append ('/' );
148
+ if (node .getNamespaceURI () != null ) {
149
+ value .append (String .format ("@Q{%s}" , node .getNamespaceURI ()));
150
+ } else {
151
+ value .append ('@' );
152
+ }
153
+ value .append (node .getLocalName ());
154
+
155
+ // attributes have an owner element - not a parent node!
156
+ parent = ((Attr ) node ).getOwnerElement ();
157
+ break ;
158
+
159
+ case Node .TEXT_NODE :
160
+ // For a text node: text()[position] where position is an integer
161
+ // representing the position of the selected node among its text
162
+ // node siblings
163
+ final int textNodePosition = getNodePosition (node );
164
+ if (textNodePosition > 0 ) {
165
+ value .append (String .format ("/text()[%d]" , textNodePosition ));
166
+ }
167
+ break ;
168
+
169
+ case Node .COMMENT_NODE :
170
+ // For a comment node: comment()[position] where position is an
171
+ // integer representing the position of the selected node among
172
+ // its comment node siblings.
173
+ final int commentNodePosition = getNodePosition (node );
174
+ if (commentNodePosition > 0 ) {
175
+ value .append (String .format ("/comment()[%d]" , commentNodePosition ));
176
+ }
177
+ break ;
178
+
179
+ case Node .PROCESSING_INSTRUCTION_NODE :
180
+ // For a processing-instruction node: processing-instruction(local)[position]
181
+ // where local is the name of the processing instruction node and position is
182
+ // an integer representing the position of the selected node among its
183
+ // like-named processing-instruction node siblings.
184
+ int processingInstructionNodePosition = getNodePosition (node );
185
+ if (processingInstructionNodePosition > 0 ) {
186
+ value .append (String .format ("/processing-instruction(%s)[%d]" , node .getNodeName (), processingInstructionNodePosition ));
187
+ }
188
+ break ;
189
+
190
+ case INode .NAMESPACE_NODE :
191
+ // For a namespace node: If the namespace node has a name: namespace::prefix,
192
+ // where prefix is the local part of the name of the namespace node
193
+ // (which represents the namespace prefix). If the namespace node
194
+ // has no name (that is, it represents the default namespace):
195
+ // namespace::*[Q{http://www.w3.org/2005/xpath-functions}local-name()=""]
196
+ if (node .getNamespaceURI () != null ) {
197
+ value .append (String .format ("namespace::{%s}" , node .getLocalName ()));
198
+ } else {
199
+ value .append ("namespace::*[Q{http://www.w3.org/2005/xpath-functions}local-name()=\" \" ]" );
200
+ }
201
+ break ;
202
+
203
+ default :
204
+ if (node .getLocalName () != null ) {
205
+ // For an element node, Q{uri}local[position], where uri is the
206
+ // namespace URI of the node name or the empty string if the
207
+ // node is in no namespace, local is the local part of the node
208
+ // name, and position is an integer representing the position
209
+ // of the selected node among its like-named siblings.
210
+ final int nodePosition = getNodePosition (node );
211
+ value .append ((node .getOwnerDocument () != null && node .getOwnerDocument ().getDocumentElement () != null ) ? "/Q" : "Q" );
212
+ value .append (((INode ) node ).getQName ().toURIQualifiedName ());
213
+ if (nodePosition > 0 ) {
214
+ value .append (String .format ("[%d]" , nodePosition ));
215
+ }
216
+ }
217
+ break ;
205
218
}
206
219
207
220
if (parent != null ) {
0 commit comments