| 1 |
/* |
| 2 |
www.sourceforge.net/projects/tinyxpath |
| 3 |
Copyright (c) 2002-2004 Yves Berquin ([email protected]) |
| 4 |
|
| 5 |
This software is provided 'as-is', without any express or implied |
| 6 |
warranty. In no event will the authors be held liable for any |
| 7 |
damages arising from the use of this software. |
| 8 |
|
| 9 |
Permission is granted to anyone to use this software for any |
| 10 |
purpose, including commercial applications, and to alter it and |
| 11 |
redistribute it freely, subject to the following restrictions: |
| 12 |
|
| 13 |
1. The origin of this software must not be misrepresented; you must |
| 14 |
not claim that you wrote the original software. If you use this |
| 15 |
software in a product, an acknowledgment in the product documentation |
| 16 |
would be appreciated but is not required. |
| 17 |
|
| 18 |
2. Altered source versions must be plainly marked as such, and |
| 19 |
must not be misrepresented as being the original software. |
| 20 |
|
| 21 |
3. This notice may not be removed or altered from any source |
| 22 |
distribution. |
| 23 |
*/ |
| 24 |
|
| 25 |
#include "node_set.h" |
| 26 |
|
| 27 |
#ifdef HAVE_CONFIG_H |
| 28 |
#include "config.h" |
| 29 |
#endif |
| 30 |
|
| 31 |
#ifdef HAVE_STDINT_H |
| 32 |
#include <stdint.h> // for intptr_t (32/64 bits void* pointer compatibility) |
| 33 |
#endif |
| 34 |
|
| 35 |
namespace TinyXPath |
| 36 |
{ |
| 37 |
|
| 38 |
/// Copy constructor |
| 39 |
node_set::node_set (const node_set & ns2) |
| 40 |
{ |
| 41 |
* this = ns2; |
| 42 |
} |
| 43 |
|
| 44 |
/// Assignation operator. Allows one to write expressions like ns_1 = ns_2; |
| 45 |
node_set & node_set::operator = (const node_set & ns2) |
| 46 |
{ |
| 47 |
u_nb_node = ns2 . u_nb_node; |
| 48 |
if (u_nb_node) |
| 49 |
{ |
| 50 |
vpp_node_set = new const void * [u_nb_node]; |
| 51 |
memcpy (vpp_node_set, ns2 . vpp_node_set, u_nb_node * sizeof (void *)); |
| 52 |
op_attrib = new bool [u_nb_node]; |
| 53 |
memcpy (op_attrib, ns2 . op_attrib, u_nb_node * sizeof (bool)); |
| 54 |
} |
| 55 |
else |
| 56 |
{ |
| 57 |
vpp_node_set = NULL; |
| 58 |
op_attrib = NULL; |
| 59 |
} |
| 60 |
return * this; |
| 61 |
} |
| 62 |
|
| 63 |
/// Copy all element children of a node to the node_set |
| 64 |
void node_set::v_copy_node_children ( |
| 65 |
const TiXmlNode * XNp_root) ///< The father of the nodes to be copied |
| 66 |
{ |
| 67 |
v_copy_node_children (XNp_root, NULL); |
| 68 |
} |
| 69 |
|
| 70 |
/// Copy all element children of a node to the node_set, if their name matches a given name |
| 71 |
void node_set::v_copy_node_children ( |
| 72 |
const TiXmlNode * XNp_root, ///< The father of the nodes to be copied |
| 73 |
const char * cp_lookup) ///< Lookup name (or NULL) |
| 74 |
{ |
| 75 |
const TiXmlNode * XNp_child; |
| 76 |
|
| 77 |
XNp_child = XNp_root -> FirstChildElement (); |
| 78 |
while (XNp_child) |
| 79 |
{ |
| 80 |
if ((! cp_lookup) || ! strcmp (XNp_child -> Value (), cp_lookup)) |
| 81 |
v_add_node_in_set (XNp_child); |
| 82 |
XNp_child = XNp_child -> NextSiblingElement (); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
/// Copy all nodes in the tree to the node_set |
| 87 |
void node_set::v_copy_selected_node_recursive ( |
| 88 |
const TiXmlNode * XNp_root) ///< The node to be copied |
| 89 |
{ |
| 90 |
v_copy_selected_node_recursive (XNp_root, NULL); |
| 91 |
} |
| 92 |
|
| 93 |
/// Copy all nodes in the tree to the node_set |
| 94 |
void node_set::v_copy_selected_node_recursive ( |
| 95 |
const TiXmlNode * XNp_root, ///< The node to be copied |
| 96 |
const char * cp_lookup) ///< Lookup name (or NULL) |
| 97 |
{ |
| 98 |
const TiXmlAttribute * XAp_attrib; |
| 99 |
const TiXmlNode * XNp_child; |
| 100 |
|
| 101 |
if ((! cp_lookup) || ! strcmp (XNp_root -> Value (), cp_lookup)) |
| 102 |
v_add_node_in_set (XNp_root); |
| •103 |
if (XNp_root -> Type () == TiXmlNode::TINYXML_ELEMENT) |
| 104 |
{ |
| 105 |
XAp_attrib = XNp_root -> ToElement () -> FirstAttribute (); |
| 106 |
while (XAp_attrib) |
| 107 |
{ |
| 108 |
v_add_attrib_in_set (XAp_attrib); |
| 109 |
XAp_attrib = XAp_attrib -> Next (); |
| 110 |
} |
| 111 |
} |
| 112 |
XNp_child = XNp_root -> FirstChild (); |
| 113 |
while (XNp_child) |
| 114 |
{ |
| 115 |
v_copy_selected_node_recursive (XNp_child, cp_lookup); |
| 116 |
XNp_child = XNp_child -> NextSiblingElement (); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
/// Copy all nodes in the tree to the node_set, excluding attributes |
| 121 |
void node_set::v_copy_selected_node_recursive_no_attrib ( |
| 122 |
const TiXmlNode * XNp_root, ///< Node whole children are to be copied |
| 123 |
const char * cp_lookup) ///< Lookup name or NULL |
| 124 |
{ |
| 125 |
const TiXmlElement * XEp_child; |
| 126 |
|
| 127 |
XEp_child = XNp_root -> FirstChildElement (); |
| 128 |
while (XEp_child) |
| 129 |
{ |
| 130 |
if ((! cp_lookup) || ! strcmp (XEp_child -> Value (), cp_lookup)) |
| 131 |
v_add_node_in_set (XEp_child); |
| 132 |
v_copy_selected_node_recursive_no_attrib (XEp_child, cp_lookup); |
| 133 |
XEp_child = XEp_child -> NextSiblingElement (); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
/// Return the string value aka concatenation of all text items |
| 138 |
TIXML_STRING node_set::S_get_string_value () const |
| 139 |
{ |
| 140 |
TIXML_STRING S_res; |
| 141 |
const TiXmlNode * XNp_node; |
| 142 |
unsigned u_node; |
| 143 |
|
| 144 |
S_res = ""; |
| 145 |
for (u_node = 0; u_node < u_nb_node; u_node++) |
| 146 |
{ |
| 147 |
if (! op_attrib [u_node]) |
| 148 |
{ |
| 149 |
XNp_node = (const TiXmlNode *) vpp_node_set [u_node]; |
| •150 |
if (XNp_node -> Type () == TiXmlNode::TINYXML_TEXT) |
| 151 |
S_res += XNp_node -> Value (); |
| 152 |
} |
| 153 |
} |
| 154 |
return S_res; |
| 155 |
} |
| 156 |
|
| 157 |
/// Checks if a node exist in the node set |
| 158 |
bool node_set::o_exist_in_set ( |
| 159 |
const TiXmlBase * XBp_member) ///< Check if a base exist in the node set |
| 160 |
{ |
| 161 |
unsigned u_node; |
| 162 |
|
| 163 |
for (u_node = 0; u_node < u_nb_node; u_node++) |
| 164 |
if (vpp_node_set [u_node] == XBp_member) |
| 165 |
return true; |
| 166 |
return false; |
| 167 |
} |
| 168 |
|
| 169 |
/// Adds a new node in the node set |
| 170 |
void node_set::v_add_base_in_set ( |
| 171 |
const TiXmlBase * XBp_member, ///< Base to add (node or attribute) |
| 172 |
bool o_attrib) ///< true if the base is an attribute, false if it's a node |
| 173 |
{ |
| 174 |
const void ** vpp_new_list; |
| 175 |
bool * op_new_list; |
| 176 |
|
| 177 |
if (o_exist_in_set (XBp_member)) |
| 178 |
return; |
| 179 |
vpp_new_list = new const void * [u_nb_node + 1]; |
| 180 |
op_new_list = new bool [u_nb_node + 1]; |
| 181 |
if (u_nb_node) |
| 182 |
{ |
| 183 |
memcpy (vpp_new_list, vpp_node_set, u_nb_node * sizeof (void *)); |
| 184 |
delete [] vpp_node_set; |
| 185 |
memcpy (op_new_list, op_attrib, u_nb_node * sizeof (bool)); |
| 186 |
delete [] op_attrib; |
| 187 |
} |
| 188 |
vpp_new_list [u_nb_node] = (const void *) XBp_member; |
| 189 |
vpp_node_set = vpp_new_list; |
| 190 |
op_new_list [u_nb_node] = o_attrib; |
| 191 |
op_attrib = op_new_list; |
| 192 |
u_nb_node++; |
| 193 |
} |
| 194 |
|
| 195 |
/// Populate the node set with all following nodes. |
| 196 |
/// \n Exerpt : \n |
| 197 |
/// the following axis contains all nodes in the same document as the context |
| 198 |
/// node that are after the context node in document order, excluding any |
| 199 |
/// descendants and excluding attribute nodes and namespace nodes |
| 200 |
void node_set::v_add_all_foll_node ( |
| 201 |
const TiXmlNode * XNp_node, ///< base node |
| 202 |
const TIXML_STRING & S_name) ///< lookup name (or "*") |
| 203 |
{ |
| 204 |
const TiXmlNode * XNp_ptr; |
| 205 |
const char * cp_lookup; |
| 206 |
|
| 207 |
if (S_name == "*") |
| 208 |
cp_lookup = NULL; |
| 209 |
else |
| 210 |
cp_lookup = S_name . c_str (); |
| 211 |
XNp_ptr = XNp_node -> NextSiblingElement (); |
| 212 |
while (XNp_ptr) |
| 213 |
{ |
| 214 |
v_add_node_in_set_if_name_or_star (XNp_ptr, S_name); |
| 215 |
v_copy_node_children (XNp_ptr, cp_lookup); |
| 216 |
XNp_ptr = XNp_ptr -> NextSiblingElement (); |
| 217 |
} |
| 218 |
XNp_ptr = XNp_node -> Parent (); |
| •219 |
if (XNp_ptr && XNp_ptr -> Type () == TiXmlNode::TINYXML_ELEMENT) |
| 220 |
v_add_all_foll_node (XNp_ptr, S_name); |
| 221 |
} |
| 222 |
|
| 223 |
/// Populate the node set with all preceding nodes. |
| 224 |
/// \n Exerpt : \n |
| 225 |
/// the preceding axis contains all nodes in the same document as the context |
| 226 |
/// node that are before the context node in document order, excluding any |
| 227 |
/// ancestors and excluding attribute nodes and namespace nodes |
| 228 |
void node_set::v_add_all_prec_node ( |
| 229 |
const TiXmlNode * XNp_node, ///< base node |
| 230 |
const TIXML_STRING & S_name) ///< lookup name (or "*") |
| 231 |
{ |
| 232 |
const TiXmlNode * XNp_ptr; |
| 233 |
const char * cp_lookup; |
| 234 |
|
| 235 |
if (S_name == "*") |
| 236 |
cp_lookup = NULL; |
| 237 |
else |
| 238 |
cp_lookup = S_name . c_str (); |
| 239 |
XNp_ptr = XNp_node -> PreviousSibling (); |
| 240 |
while (XNp_ptr) |
| 241 |
{ |
| •242 |
if (XNp_ptr -> Type () == TiXmlNode::TINYXML_ELEMENT) |
| 243 |
{ |
| 244 |
v_add_node_in_set_if_name_or_star (XNp_ptr, S_name); |
| 245 |
v_copy_node_children (XNp_ptr, cp_lookup); |
| 246 |
} |
| 247 |
XNp_ptr = XNp_ptr -> PreviousSibling (); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
/// Internal utility structure for the node set sorting |
| 252 |
struct ptr_and_flag |
| 253 |
{ |
| 254 |
const void * vp_ptr; |
| 255 |
bool o_flag; |
| 256 |
} ; |
| 257 |
|
| 258 |
/// Internal utility function for node set sorting |
| 259 |
int i_compare_ptr_and_flag ( |
| 260 |
const void * vp_1, ///< Ptr to first element to compare |
| 261 |
const void * vp_2) ///< Ptr to second element to compare |
| 262 |
{ |
| 263 |
const ptr_and_flag * pafp_1, * pafp_2; |
| 264 |
TiXmlNode * XNp_1, * XNp_2; |
| 265 |
|
| 266 |
pafp_1 = (const ptr_and_flag *) vp_1; |
| 267 |
pafp_2 = (const ptr_and_flag *) vp_2; |
| 268 |
if (pafp_1 -> o_flag || pafp_2 -> o_flag) |
| 269 |
// for now |
| 270 |
return 0; |
| 271 |
XNp_1 = (TiXmlNode *) pafp_1 -> vp_ptr; |
| 272 |
XNp_2 = (TiXmlNode *) pafp_2 -> vp_ptr; |
| 273 |
|
| 274 |
intptr_t n1 = reinterpret_cast<intptr_t>(XNp_1 -> GetUserData ()); |
| 275 |
intptr_t n2 = reinterpret_cast<intptr_t>(XNp_2 -> GetUserData ()); |
| 276 |
|
| 277 |
if( n1 == n2) |
| 278 |
return 0; |
| 279 |
|
| 280 |
if( n1 > n2) |
| 281 |
return 1; |
| 282 |
else |
| 283 |
return -1; |
| 284 |
} |
| 285 |
|
| 286 |
/// Sort the node set according to the document order. |
| 287 |
/// \n The document order must have been recorded already in the tiny xml user's value |
| 288 |
/// \n There's still a problem with the attributes. They aren't covered by the GetUserData / SetUserData |
| 289 |
/// yet. If two attributes come from the same element, we have to compute on the fly their relative position |
| 290 |
void node_set::v_document_sort () |
| 291 |
{ |
| 292 |
ptr_and_flag * pafp_list; |
| 293 |
unsigned u_node; |
| 294 |
|
| 295 |
if (u_nb_node < 2) |
| 296 |
return; |
| 297 |
|
| 298 |
// well, next problem is, if we want to make a clean library, we can't use static variables |
| 299 |
// the only way for us to sort the vpp_node_set / op_attrib is to make it the other way up |
| 300 |
pafp_list = new ptr_and_flag [u_nb_node]; |
| 301 |
for (u_node = 0; u_node < u_nb_node; u_node++) |
| 302 |
{ |
| 303 |
pafp_list [u_node] . vp_ptr = vpp_node_set [u_node]; |
| 304 |
pafp_list [u_node] . o_flag = op_attrib [u_node]; |
| 305 |
} |
| 306 |
qsort (pafp_list, u_nb_node, sizeof (ptr_and_flag), i_compare_ptr_and_flag); |
| 307 |
for (u_node = 0; u_node < u_nb_node; u_node++) |
| 308 |
{ |
| 309 |
vpp_node_set [u_node] = pafp_list [u_node] . vp_ptr; |
| 310 |
op_attrib [u_node] = pafp_list [u_node] . o_flag; |
| 311 |
} |
| 312 |
delete [] pafp_list; |
| 313 |
} |
| 314 |
|
| 315 |
/// Debug function to print the content of a node set to stdout |
| 316 |
void node_set::v_dump () |
| 317 |
{ |
| 318 |
unsigned u_node; |
| 319 |
const TiXmlAttribute * XAp_att; |
| 320 |
const TiXmlNode * XNp_node; |
| 321 |
|
| 322 |
printf ("-- start node set (%d items) --\n", u_nb_node); |
| 323 |
for (u_node = 0; u_node < u_nb_node; u_node++) |
| 324 |
{ |
| 325 |
if (op_attrib [u_node]) |
| 326 |
{ |
| 327 |
XAp_att = XAp_get_attribute_in_set (u_node); |
| 328 |
printf (" [%d] : Attribute : %s=%s\n", u_node, XAp_att -> Name (), XAp_att -> Value ()); |
| 329 |
} |
| 330 |
else |
| 331 |
{ |
| 332 |
XNp_node = XNp_get_node_in_set (u_node); |
| 333 |
printf (" [%d] : Node : %s\n", u_node, XNp_node -> Value ()); |
| 334 |
} |
| 335 |
} |
| 336 |
printf ("-- end node set --\n"); |
| 337 |
} |
| 338 |
|
| 339 |
} |