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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
use libc::{c_char, c_void};
use std::cell::RefCell;
use std::collections::{HashMap, HashSet};
use std::error::Error;
use std::ffi::{CStr, CString};
use std::hash::{Hash, Hasher};
use std::mem;
use std::ptr;
use std::rc::Rc;
use std::str;
use crate::bindings::*;
use crate::c_helpers::*;
use crate::tree::namespace::Namespace;
use crate::tree::nodetype::NodeType;
use crate::tree::{Document, DocumentRef, DocumentWeak};
use crate::xpath::Context;
pub static mut NODE_RC_MAX_GUARD: usize = 2;
pub fn set_node_rc_guard(value: usize) {
unsafe {
NODE_RC_MAX_GUARD = value;
}
}
type NodeRef = Rc<RefCell<_Node>>;
#[derive(Debug)]
struct _Node {
node_ptr: xmlNodePtr,
document: DocumentWeak,
unlinked: bool,
}
#[derive(Clone, Debug)]
pub struct Node(NodeRef);
impl Hash for Node {
fn hash<H: Hasher>(&self, state: &mut H) {
self.node_ptr().hash(state);
}
}
impl PartialEq for Node {
fn eq(&self, other: &Node) -> bool {
self.node_ptr() == other.node_ptr()
}
}
impl Eq for Node {}
impl Drop for _Node {
fn drop(&mut self) {
if self.unlinked {
let node_ptr = self.node_ptr;
if !node_ptr.is_null() {
unsafe {
xmlFreeNode(node_ptr);
}
}
}
}
}
impl Node {
pub fn new(name: &str, ns: Option<Namespace>, doc: &Document) -> Result<Self, ()> {
let c_name = CString::new(name).unwrap();
let ns_ptr = match ns {
None => ptr::null_mut(),
Some(ns) => ns.ns_ptr(),
};
unsafe {
let node = xmlNewDocNode(
doc.doc_ptr(),
ns_ptr,
c_name.as_bytes().as_ptr(),
ptr::null(),
);
if node.is_null() {
Err(())
} else {
Ok(Node::wrap(node, &doc.0))
}
}
}
pub fn node_ptr(&self) -> xmlNodePtr {
self.0.borrow().node_ptr
}
pub fn node_ptr_mut(&mut self) -> Result<xmlNodePtr, String> {
let weak_count = Rc::weak_count(&self.0);
let strong_count = Rc::strong_count(&self.0);
let guard_ok = unsafe { weak_count == 0 && strong_count <= NODE_RC_MAX_GUARD };
if guard_ok {
Ok(self.0.borrow_mut().node_ptr)
} else {
Err(format!(
"Can not mutably reference a shared Node {:?}! Rc: weak count: {:?}; strong count: {:?}",
self.get_name(),
weak_count,
strong_count,
))
}
}
pub(crate) fn wrap(node_ptr: xmlNodePtr, document: &DocumentRef) -> Node {
if let Some(node) = document.borrow().get_node(node_ptr) {
return node.clone();
}
let node = _Node {
node_ptr,
document: Rc::downgrade(&document),
unlinked: false,
};
let wrapped_node = Node(Rc::new(RefCell::new(node)));
document
.borrow_mut()
.insert_node(node_ptr, wrapped_node.clone());
wrapped_node
}
pub fn new_text(content: &str, doc: &Document) -> Result<Self, ()> {
let c_content = CString::new(content).unwrap();
unsafe {
let node = xmlNewDocText(doc.doc_ptr(), c_content.as_bytes().as_ptr());
if node.is_null() {
Err(())
} else {
Ok(Node::wrap(node, &doc.0))
}
}
}
pub fn mock(doc: &Document) -> Self {
Node::new("mock", None, &doc).unwrap()
}
pub fn null() -> Self {
Node(Rc::new(RefCell::new(_Node {
node_ptr: ptr::null_mut(),
document: Rc::downgrade(&Document::null_ref()),
unlinked: true,
})))
}
pub fn to_hashable(&self) -> usize {
unsafe { mem::transmute::<xmlNodePtr, usize>(self.node_ptr()) }
}
pub(crate) fn get_docref(&self) -> DocumentWeak {
self.0.borrow().document.clone()
}
pub fn get_next_sibling(&self) -> Option<Node> {
let ptr = xmlNextSibling(self.node_ptr());
self.ptr_as_option(ptr)
}
pub fn get_prev_sibling(&self) -> Option<Node> {
let ptr = xmlPrevSibling(self.node_ptr());
self.ptr_as_option(ptr)
}
pub fn get_first_child(&self) -> Option<Node> {
let ptr = xmlGetFirstChild(self.node_ptr());
self.ptr_as_option(ptr)
}
pub fn get_first_element_child(&self) -> Option<Node> {
match self.get_first_child() {
None => None,
Some(child) => {
let mut current_node = child;
while !current_node.is_element_node() {
if let Some(sibling) = current_node.get_next_sibling() {
current_node = sibling;
} else {
break;
}
}
if current_node.is_element_node() {
Some(current_node)
} else {
None
}
}
}
}
pub fn get_last_child(&self) -> Option<Node> {
let ptr = unsafe { xmlGetLastChild(self.node_ptr()) };
self.ptr_as_option(ptr)
}
pub fn get_child_nodes(&self) -> Vec<Node> {
let mut children = Vec::new();
if let Some(first_child) = self.get_first_child() {
children.push(first_child);
while let Some(sibling) = children.last().unwrap().get_next_sibling() {
children.push(sibling)
}
}
children
}
pub fn get_child_elements(&self) -> Vec<Node> {
self
.get_child_nodes()
.into_iter()
.filter(|n| n.get_type() == Some(NodeType::ElementNode))
.collect::<Vec<Node>>()
}
pub fn get_parent(&self) -> Option<Node> {
let ptr = xmlGetParent(self.node_ptr());
self.ptr_as_option(ptr)
}
pub fn get_type(&self) -> Option<NodeType> {
NodeType::from_int(xmlGetNodeType(self.node_ptr()))
}
pub fn add_prev_sibling(&mut self, new_sibling: &mut Node) -> Result<(), Box<Error>> {
new_sibling.set_linked();
unsafe {
if xmlAddPrevSibling(self.node_ptr_mut()?, new_sibling.node_ptr_mut()?).is_null() {
Err(From::from("add_prev_sibling returned NULL"))
} else {
Ok(())
}
}
}
pub fn add_next_sibling(&mut self, new_sibling: &mut Node) -> Result<(), Box<Error>> {
new_sibling.set_linked();
unsafe {
if xmlAddNextSibling(self.node_ptr_mut()?, new_sibling.node_ptr_mut()?).is_null() {
Err(From::from("add_next_sibling returned NULL"))
} else {
Ok(())
}
}
}
pub fn is_text_node(&self) -> bool {
self.get_type() == Some(NodeType::TextNode)
}
pub fn is_element_node(&self) -> bool {
self.get_type() == Some(NodeType::ElementNode)
}
pub fn is_null(&self) -> bool {
self.node_ptr().is_null()
}
pub fn get_name(&self) -> String {
let name_ptr = xmlNodeGetName(self.node_ptr());
if name_ptr.is_null() {
return String::new();
}
let c_string = unsafe { CStr::from_ptr(name_ptr) };
c_string.to_string_lossy().into_owned()
}
pub fn set_name(&mut self, name: &str) -> Result<(), Box<Error>> {
let c_name = CString::new(name).unwrap();
unsafe { xmlNodeSetName(self.node_ptr_mut()?, c_name.as_bytes().as_ptr()) }
Ok(())
}
pub fn get_content(&self) -> String {
let content_ptr = unsafe { xmlNodeGetContent(self.node_ptr()) };
if content_ptr.is_null() {
return String::new();
}
let c_string = unsafe { CStr::from_ptr(content_ptr as *const c_char) };
let rust_utf8 = c_string.to_string_lossy().into_owned();
unsafe {
libc::free(content_ptr as *mut c_void);
}
rust_utf8
}
pub fn set_content(&mut self, content: &str) -> Result<(), Box<Error>> {
let c_content = CString::new(content).unwrap();
unsafe { xmlNodeSetContent(self.node_ptr_mut()?, c_content.as_bytes().as_ptr()) }
Ok(())
}
pub fn get_property(&self, name: &str) -> Option<String> {
let c_name = CString::new(name).unwrap();
let value_ptr = unsafe { xmlGetProp(self.node_ptr(), c_name.as_bytes().as_ptr()) };
if value_ptr.is_null() {
return None;
}
let c_value_string = unsafe { CStr::from_ptr(value_ptr as *const c_char) };
let prop_str = c_value_string.to_string_lossy().into_owned();
unsafe {
libc::free(value_ptr as *mut c_void);
}
Some(prop_str)
}
pub fn get_property_ns(&self, name: &str, ns: &str) -> Option<String> {
let c_name = CString::new(name).unwrap();
let c_ns = CString::new(ns).unwrap();
let value_ptr = unsafe {
xmlGetNsProp(
self.node_ptr(),
c_name.as_bytes().as_ptr(),
c_ns.as_bytes().as_ptr(),
)
};
if value_ptr.is_null() {
return None;
}
let c_value_string = unsafe { CStr::from_ptr(value_ptr as *const c_char) };
let prop_str = c_value_string.to_string_lossy().into_owned();
unsafe {
libc::free(value_ptr as *mut c_void);
}
Some(prop_str)
}
pub fn get_property_node(&self, name: &str) -> Option<Node> {
let c_name = CString::new(name).unwrap();
unsafe {
let attr_node = xmlHasProp(self.node_ptr(), c_name.as_bytes().as_ptr());
self.ptr_as_option(attr_node as xmlNodePtr)
}
}
pub fn set_property(&mut self, name: &str, value: &str) -> Result<(), Box<Error>> {
let c_name = CString::new(name).unwrap();
let c_value = CString::new(value).unwrap();
unsafe {
xmlSetProp(
self.node_ptr_mut()?,
c_name.as_bytes().as_ptr(),
c_value.as_bytes().as_ptr(),
)
};
Ok(())
}
pub fn set_property_ns(
&mut self,
name: &str,
value: &str,
ns: &Namespace,
) -> Result<(), Box<Error>> {
let c_name = CString::new(name).unwrap();
let c_value = CString::new(value).unwrap();
unsafe {
xmlSetNsProp(
self.node_ptr_mut()?,
ns.ns_ptr(),
c_name.as_bytes().as_ptr(),
c_value.as_bytes().as_ptr(),
)
};
Ok(())
}
pub fn remove_property(&mut self, name: &str) -> Result<(), Box<Error>> {
let c_name = CString::new(name).unwrap();
unsafe {
let attr_node = xmlHasProp(self.node_ptr_mut()?, c_name.as_bytes().as_ptr());
if !attr_node.is_null() {
let remove_prop_status = xmlRemoveProp(attr_node);
if remove_prop_status == 0 {
Ok(())
} else {
Err(From::from(format!(
"libxml2 failed to remove property with status: {:?}",
remove_prop_status
)))
}
} else {
Ok(())
}
}
}
pub fn get_attribute(&self, name: &str) -> Option<String> {
self.get_property(name)
}
pub fn get_attribute_ns(&self, name: &str, ns: &str) -> Option<String> {
self.get_property_ns(name, ns)
}
pub fn get_attribute_node(&self, name: &str) -> Option<Node> {
self.get_property_node(name)
}
pub fn set_attribute(&mut self, name: &str, value: &str) -> Result<(), Box<Error>> {
self.set_property(name, value)
}
pub fn set_attribute_ns(
&mut self,
name: &str,
value: &str,
ns: &Namespace,
) -> Result<(), Box<Error>> {
self.set_property_ns(name, value, ns)
}
pub fn remove_attribute(&mut self, name: &str) -> Result<(), Box<Error>> {
self.remove_property(name)
}
pub fn get_properties(&self) -> HashMap<String, String> {
let mut attributes = HashMap::new();
let mut attr_names = Vec::new();
unsafe {
let mut current_prop = xmlGetFirstProperty(self.node_ptr());
while !current_prop.is_null() {
let name_ptr = xmlAttrName(current_prop);
let c_name_string = CStr::from_ptr(name_ptr);
let name = c_name_string.to_string_lossy().into_owned();
attr_names.push(name);
current_prop = xmlNextPropertySibling(current_prop);
}
}
for name in attr_names {
let value = self.get_property(&name).unwrap_or_default();
attributes.insert(name, value);
}
attributes
}
pub fn get_attributes(&self) -> HashMap<String, String> {
self.get_properties()
}
pub fn get_namespace(&self) -> Option<Namespace> {
let ns_ptr = xmlNodeNs(self.node_ptr());
if ns_ptr.is_null() {
None
} else {
Some(Namespace { ns_ptr })
}
}
pub fn get_namespaces(&self, doc: &Document) -> Vec<Namespace> {
let list_ptr_raw = unsafe { xmlGetNsList(doc.doc_ptr(), self.node_ptr()) };
if list_ptr_raw.is_null() {
Vec::new()
} else {
let mut namespaces = Vec::new();
let mut ptr_iter = list_ptr_raw as *mut xmlNsPtr;
unsafe {
while !ptr_iter.is_null() && !(*ptr_iter).is_null() {
namespaces.push(Namespace { ns_ptr: *ptr_iter });
ptr_iter = ptr_iter.add(1);
}
}
namespaces
}
}
pub fn get_namespace_declarations(&self) -> Vec<Namespace> {
if self.get_type() != Some(NodeType::ElementNode) {
return Vec::new();
}
let mut namespaces = Vec::new();
let mut ns_ptr = xmlNodeNsDeclarations(self.node_ptr());
while !ns_ptr.is_null() {
if !xmlNsPrefix(ns_ptr).is_null() || !xmlNsHref(ns_ptr).is_null() {
namespaces.push(Namespace { ns_ptr });
}
ns_ptr = xmlNextNsSibling(ns_ptr);
}
namespaces
}
pub fn set_namespace(&mut self, namespace: &Namespace) -> Result<(), Box<Error>> {
unsafe {
xmlSetNs(self.node_ptr_mut()?, namespace.ns_ptr());
}
Ok(())
}
pub fn lookup_namespace_prefix(&self, href: &str) -> Option<String> {
if href.is_empty() {
return None;
}
let c_href = CString::new(href).unwrap();
unsafe {
let ptr_mut = self.node_ptr();
let ns_ptr = xmlSearchNsByHref(xmlGetDoc(ptr_mut), ptr_mut, c_href.as_bytes().as_ptr());
if !ns_ptr.is_null() {
let ns = Namespace { ns_ptr };
let ns_prefix = ns.get_prefix();
Some(ns_prefix)
} else {
None
}
}
}
pub fn lookup_namespace_uri(&self, prefix: &str) -> Option<String> {
if prefix.is_empty() {
return None;
}
let c_prefix = CString::new(prefix).unwrap();
unsafe {
let ns_ptr = xmlSearchNs(
xmlGetDoc(self.node_ptr()),
self.node_ptr(),
c_prefix.as_bytes().as_ptr(),
);
if !ns_ptr.is_null() {
let ns = Namespace { ns_ptr };
let ns_prefix = ns.get_href();
if !ns_prefix.is_empty() {
Some(ns_prefix)
} else {
None
}
} else {
None
}
}
}
pub fn recursively_remove_namespaces(&mut self) -> Result<(), Box<Error>> {
xmlNodeRecursivelyRemoveNs(self.node_ptr_mut()?);
Ok(())
}
pub fn get_class_names(&self) -> HashSet<String> {
let mut set = HashSet::new();
if let Some(value) = self.get_property("class") {
for n in value.split(' ') {
set.insert(n.to_owned());
}
}
set
}
pub fn add_child(&mut self, child: &mut Node) -> Result<(), String> {
child.set_linked();
unsafe {
let new_child_ptr = xmlAddChild(self.node_ptr_mut()?, child.node_ptr_mut()?);
if new_child_ptr.is_null() {
Err("add_child encountered NULL pointer".to_string())
} else {
Ok(())
}
}
}
pub fn new_child(&mut self, ns: Option<Namespace>, name: &str) -> Result<Node, Box<Error>> {
let c_name = CString::new(name).unwrap();
let ns_ptr = match ns {
None => ptr::null_mut(),
Some(mut ns) => ns.ns_ptr_mut(),
};
unsafe {
let new_ptr = xmlNewChild(
self.node_ptr_mut()?,
ns_ptr,
c_name.as_bytes().as_ptr(),
ptr::null(),
);
Ok(Node::wrap(new_ptr, &self.get_docref().upgrade().unwrap()))
}
}
pub fn add_text_child(
&mut self,
ns: Option<Namespace>,
name: &str,
content: &str,
) -> Result<Node, Box<Error>> {
let c_name = CString::new(name).unwrap();
let c_content = CString::new(content).unwrap();
let ns_ptr = match ns {
None => ptr::null_mut(),
Some(mut ns) => ns.ns_ptr_mut(),
};
unsafe {
let new_ptr = xmlNewTextChild(
self.node_ptr_mut()?,
ns_ptr,
c_name.as_bytes().as_ptr(),
c_content.as_bytes().as_ptr(),
);
Ok(Node::wrap(new_ptr, &self.get_docref().upgrade().unwrap()))
}
}
pub fn append_text(&mut self, content: &str) -> Result<(), Box<Error>> {
let c_len = content.len() as i32;
if c_len > 0 {
let c_content = CString::new(content).unwrap();
unsafe {
xmlNodeAddContentLen(self.node_ptr_mut()?, c_content.as_bytes().as_ptr(), c_len);
}
}
Ok(())
}
pub fn unlink_node(&mut self) {
let node_type = self.get_type();
if node_type != Some(NodeType::DocumentNode)
&& node_type != Some(NodeType::DocumentFragNode)
&& !self.is_unlinked()
{
self.set_unlinked();
unsafe {
xmlUnlinkNode(self.node_ptr());
}
}
}
pub fn unlink(&mut self) {
self.unlink_node()
}
pub fn unbind_node(&mut self) {
self.unlink_node()
}
pub fn unbind(&mut self) {
self.unlink_node()
}
pub fn is_unlinked(&self) -> bool {
self.0.borrow().unlinked
}
fn ptr_as_option(&self, node_ptr: xmlNodePtr) -> Option<Node> {
if node_ptr.is_null() {
None
} else {
let doc_ref = self.get_docref().upgrade().unwrap();
let new_node = Node::wrap(node_ptr, &doc_ref);
Some(new_node)
}
}
fn set_linked(&mut self) {
self.0.borrow_mut().unlinked = false;
}
fn set_unlinked(&mut self) {
self.0.borrow_mut().unlinked = true;
}
pub fn findnodes(&self, xpath: &str) -> Result<Vec<Node>, ()> {
let mut context = Context::from_node(&self)?;
context.findnodes(xpath, Some(self))
}
}