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
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
//! The tree functionality
use c_signatures::*;

use libc;
use libc::{c_void, c_int};
use std::ffi::{CString, CStr};
use std::hash::{Hash, Hasher};
use std::ptr;
use std::str;
use std::collections::{HashSet, HashMap};
use std::mem;
use global::*;

/// An xml node
#[derive(Clone)]
pub struct Node {
  /// libxml's xmlNodePtr
  pub node_ptr: *mut c_void,
}

impl Hash for Node {
  /// Generates a hash value from the `node_ptr` value.
  fn hash<H: Hasher>(&self, state: &mut H) {
    self.node_ptr.hash(state);
  }
}

impl PartialEq for Node {
  /// Two nodes are considered equal, if they point to the same xmlNode.
  fn eq(&self, other: &Node) -> bool {
    self.node_ptr == other.node_ptr
  }
}

impl Eq for Node {}

impl Drop for Node {
  /// Free node if it isn't bound in some document
  fn drop(&mut self) {
    // TODO: How do we drop unbound nodes?
    // unsafe {
    //   if self.node_ptr {
    //     xmlFreeNode(self.node_ptr);
    //   }
    // }
  }
}

/// A libxml2 Document
#[derive(Debug)]
pub struct Document {
  /// libxml's `DocumentPtr`
  pub doc_ptr: *mut c_void,
}


impl Drop for Document {
  ///Free document when it goes out of scope
  fn drop(&mut self) {
    unsafe {
      xmlFreeDoc(self.doc_ptr);
    }
    _libxml_global_drop();
  }
}

impl Document {
  /// Creates a new empty libxml2 document
  pub fn new() -> Result<Self, ()> {
    _libxml_global_init();
    unsafe {
      let c_version = CString::new("1.0").unwrap();
      let libxml_doc = xmlNewDoc(c_version.as_ptr());
      if libxml_doc.is_null() {
        Err(())
      } else {
        Ok(Document { doc_ptr: libxml_doc })
      }
    }
  }
  /// Creates a new `Document` from an existing libxml2 pointer
  pub fn new_ptr(doc_ptr: *mut c_void) -> Self {
    _libxml_global_init();
    Document { doc_ptr: doc_ptr }
  }
  /// Write document to `filename`
  pub fn save_file(&self, filename: &str) -> Result<c_int, ()> {
    let c_filename = CString::new(filename).unwrap();
    unsafe {
      let retval = xmlSaveFile(c_filename.as_ptr(), self.doc_ptr);
      if retval < 0 {
        return Err(());
      }
      Ok(retval)
    }
  }
  /// Get the root element of the document
  pub fn get_root_element(&self) -> Node {
    unsafe {
      let node_ptr = xmlDocGetRootElement(self.doc_ptr);
      if node_ptr.is_null() {
        Node {
          node_ptr: self.doc_ptr
        }
      } else {
        Node {
          node_ptr : node_ptr,
        }
      }
    }
  }

  /// Sets the root element of the document
  pub fn set_root_element(&mut self, root: &Node) {
    unsafe {
      xmlDocSetRootElement(self.doc_ptr, root.node_ptr);
      // root.node_is_inserted = true;
    }
  }

  /// Serializes the `Document`
  pub fn to_string(&self, format: bool) -> String {
    unsafe {
      // allocate a buffer to dump into
      let mut receiver = ptr::null_mut();
      let size: c_int = 0;
      let c_utf8 = CString::new("UTF-8").unwrap();

      if !format {
        xmlDocDumpMemoryEnc(self.doc_ptr, &mut receiver, &size, c_utf8.as_ptr(), 1);
      } else {
        let current_indent = getIndentTreeOutput();
        setIndentTreeOutput(1);
        xmlDocDumpFormatMemoryEnc(self.doc_ptr, &mut receiver, &size, c_utf8.as_ptr(), 1);
        setIndentTreeOutput(current_indent);
      }

      let c_string = CStr::from_ptr(receiver);
      let node_string = str::from_utf8(c_string.to_bytes()).unwrap().to_owned();
      libc::free(receiver as *mut c_void);

      node_string
    }
  }

  /// Serializes a `Node` owned by this `Document
  pub fn node_to_string(&self, node: &Node) -> String {
    unsafe {
      // allocate a buffer to dump into
      let buf = xmlBufferCreate();

      // dump the node
      xmlNodeDump(buf,
                  self.doc_ptr,
                  node.node_ptr,
                  1, // level of indentation
                  0 /* disable formatting */);
      let result_ptr = xmlBufferContent(buf);
      let c_string = CStr::from_ptr(result_ptr);
      let node_string = str::from_utf8(c_string.to_bytes()).unwrap().to_owned();
      xmlBufferFree(buf);

      node_string
    }
  }

  /// Creates a node for an XML processing instruction
  pub fn create_processing_instruction(&mut self, name: &str, content: &str) -> Result<Node, ()> {
    unsafe {
      let c_name = CString::new(name).unwrap();
      let c_content = CString::new(content).unwrap();

      let node_ptr = xmlNewDocPI(self.doc_ptr, c_name.as_ptr(), c_content.as_ptr());
      if node_ptr.is_null() {
        Err(())
      } else {
        Ok(Node { node_ptr: node_ptr })
      }
    }
  }

  /// Cast the document as a libxml Node
  pub fn as_node(&self) -> Node {
    // TODO: Memory management? Could be a major pain...
    Node {node_ptr: self.doc_ptr}
  }

}



// The helper functions for trees
fn ptr_as_node_opt(ptr: *mut c_void) -> Option<Node> {
  if ptr.is_null() {
    None
  } else {
    Some(Node {
        node_ptr : ptr,
    })
  }
}

/// Types of xml nodes
#[derive(Debug, PartialEq)]
#[allow(missing_docs)]
pub enum NodeType {
  ElementNode,
  AttributeNode,
  TextNode,
  CDataSectionNode,
  EntityRefNode,
  EntityNode,
  PiNode,
  CommentNode,
  DocumentNode,
  DocumentTypeNode,
  DocumentFragNode,
  NotationNode,
  HtmlDocumentNode,
  DTDNode,
  ElementDecl,
  AttributeDecl,
  EntityDecl,
  NamespaceDecl,
  XIncludeStart,
  XIncludeEnd,
  DOCBDocumentNode,
}

impl NodeType {
  /// converts an integer from libxml's `enum NodeType`
  /// to an instance of our `NodeType`
  pub fn from_c_int(i: c_int) -> Option<NodeType> {
    match i {
      1 => Some(NodeType::ElementNode),
      2 => Some(NodeType::AttributeNode),
      3 => Some(NodeType::TextNode),
      4 => Some(NodeType::CDataSectionNode),
      5 => Some(NodeType::EntityRefNode),
      6 => Some(NodeType::EntityNode),
      7 => Some(NodeType::PiNode),
      8 => Some(NodeType::CommentNode),
      9 => Some(NodeType::DocumentNode),
      10 => Some(NodeType::DocumentTypeNode),
      11 => Some(NodeType::DocumentFragNode),
      12 => Some(NodeType::NotationNode),
      13 => Some(NodeType::HtmlDocumentNode),
      14 => Some(NodeType::DTDNode),
      15 => Some(NodeType::ElementDecl),
      16 => Some(NodeType::AttributeDecl),
      17 => Some(NodeType::EntityDecl),
      18 => Some(NodeType::NamespaceDecl),
      19 => Some(NodeType::XIncludeStart),
      20 => Some(NodeType::XIncludeEnd),
      21 => Some(NodeType::DOCBDocumentNode),
      _ => None,
    }
  }
}

impl Node {
  /// Create a new node, bound to a given document.
  pub fn new(name: &str, ns: Option<Namespace>, doc: &Document) -> Result<Self, ()> {
    // We will only allow to work with document-bound nodes for now, to avoid the problems of memory management.

    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_ptr(), ptr::null());
      if node.is_null() {
        Err(())
      } else {
        Ok(Node { node_ptr: node })
      }
    }
  }

  /// Create a new text node, bound to a given document
  pub fn new_text(content: &str, doc: &Document) -> Result<Self, ()> {
    // We will only allow to work with document-bound nodes for now, to avoid the problems of memory management.
    let c_content = CString::new(content).unwrap();
    unsafe {
      let node = xmlNewDocText(doc.doc_ptr, c_content.as_ptr());
      if node.is_null() {
        Err(())
      } else {
        Ok(Node { node_ptr: node })
      }
    }
  }
  /// Create a mock node, used for a placeholder argument
  pub fn mock() -> Self {
    let doc = Document::new().unwrap();
    Node::new("mock", None, &doc).unwrap()
  }

  /// For some reason `libc::c_void` isn't hashable and cannot be made hashable
  pub fn to_hashable(&self) -> usize {
    unsafe { mem::transmute::<*mut libc::c_void, usize>(self.node_ptr) }
  }

  /// Returns the next sibling if it exists
  pub fn get_next_sibling(&self) -> Option<Node> {
    let ptr = unsafe { xmlNextSibling(self.node_ptr) };
    ptr_as_node_opt(ptr)
  }

  /// Returns the previous sibling if it exists
  pub fn get_prev_sibling(&self) -> Option<Node> {
    let ptr = unsafe { xmlPrevSibling(self.node_ptr) };
    ptr_as_node_opt(ptr)
  }

  /// Returns the first child if it exists
  pub fn get_first_child(&self) -> Option<Node> {
    let ptr = unsafe { xmlGetFirstChild(self.node_ptr) };
    ptr_as_node_opt(ptr)
  }

  /// Returns the last child if it exists
  pub fn get_last_child(&self) -> Option<Node> {
    let ptr = unsafe { xmlGetLastChild(self.node_ptr) };
    ptr_as_node_opt(ptr)
  }

  /// Returns all child nodes of the given node as a vector
  pub fn get_child_nodes(&self) -> Vec<Node> {
    let mut children = Vec::new();
    if let Some(node) = self.get_first_child() {
      children.push(node.clone());
      let mut current_node = node;
      while let Some(sibling) = current_node.get_next_sibling() {
        current_node = sibling.clone();
        children.push(sibling)
      }
    }
    children
  }

  /// Returns all child elements of the given node as a vector
  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>>()
  }

  /// Returns the parent if it exists
  pub fn get_parent(&self) -> Option<Node> {
    let ptr = unsafe { xmlGetParent(self.node_ptr) };
    ptr_as_node_opt(ptr)
  }

  /// Get the node type
  pub fn get_type(&self) -> Option<NodeType> {
    NodeType::from_c_int(unsafe { xmlGetNodeType(self.node_ptr) })
  }


  /// Add a previous sibling
  pub fn add_prev_sibling(&self, new_sibling: Node) -> Option<Node> {
    // TODO: Think of using a Result type, the libxml2 call returns NULL on error, or the child node on success
    unsafe {
      if xmlAddPrevSibling(self.node_ptr, new_sibling.node_ptr).is_null() {
        None
      } else {
        Some(new_sibling)
      }
    }
  }

  /// Add a next sibling
  pub fn add_next_sibling(&self, new_sibling: Node) -> Option<Node> {
    // TODO: Think of using a Result type, the libxml2 call returns NULL on error, or the child node on success
    unsafe {
      if xmlAddNextSibling(self.node_ptr, new_sibling.node_ptr).is_null() {
        None
      } else {
        Some(new_sibling)
      }
    }
  }

  /// Returns true iff it is a text node
  pub fn is_text_node(&self) -> bool {
    match self.get_type() {
      Some(NodeType::TextNode) => true,
      _ => false,
    }
  }

  /// Returns the name of the node (empty string if name pointer is `NULL`)
  pub fn get_name(&self) -> String {
    let name_ptr = unsafe { xmlNodeGetName(self.node_ptr) };
    if name_ptr.is_null() {
      return String::new();
    }  //empty string
    let c_string = unsafe { CStr::from_ptr(name_ptr) };
    str::from_utf8(c_string.to_bytes()).unwrap().to_owned()
  }

  /// Returns the content of the node
  /// (empty string if content pointer is `NULL`)
  pub fn get_content(&self) -> String {
    let content_ptr = unsafe { xmlNodeGetContentPointer(self.node_ptr) };
    if content_ptr.is_null() {
      return String::new();
    }  //empty string
    let c_string = unsafe { CStr::from_ptr(content_ptr) };
    str::from_utf8(c_string.to_bytes()).unwrap().to_owned()
  }

  /// Sets the text content of this `Node`
  pub fn set_content(&mut self, content: &str) {
    let c_content = CString::new(content).unwrap();
    unsafe { xmlNodeSetContent(self.node_ptr, c_content.as_ptr()) }
  }

  /// Returns the value of property `name`
  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_ptr()) };
    if value_ptr.is_null() {
      return None;
    }
    let c_value_string = unsafe { CStr::from_ptr(value_ptr) };
    let prop_str = str::from_utf8(c_value_string.to_bytes()).unwrap().to_owned();
    unsafe {
      libc::free(value_ptr as *mut c_void);
    }
    Some(prop_str)
  }

  /// Returns the value of property `name` in namespace `ns`
  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_ptr(), c_ns.as_ptr()) };
    if value_ptr.is_null() {
      return None;
    }
    let c_value_string = unsafe { CStr::from_ptr(value_ptr) };
    let prop_str = str::from_utf8(c_value_string.to_bytes()).unwrap().to_owned();
    unsafe {
      libc::free(value_ptr as *mut c_void);
    }
    Some(prop_str)
  }

  /// Return an attribute as a `Node` struct of type AttributeNode
  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_ptr());
      if attr_node.is_null() {
        None
      } else {
        Some(Node{node_ptr: attr_node})
      }
    }
  }

  /// Sets the value of property `name` to `value`
  pub fn set_property(&mut self, name: &str, value: &str) {
    let c_name = CString::new(name).unwrap();
    let c_value = CString::new(value).unwrap();
    unsafe { xmlSetProp(self.node_ptr, c_name.as_ptr(), c_value.as_ptr()) };
  }
  /// Sets a namespaced attribute
  pub fn set_property_ns(&mut self, name: &str, value: &str, ns: Namespace) {
    let c_name = CString::new(name).unwrap();
    let c_value = CString::new(value).unwrap();
    unsafe { xmlSetNsProp(self.node_ptr, ns.ns_ptr, c_name.as_ptr(), c_value.as_ptr()) };
  }

  /// Removes the property of given `name`
  pub fn remove_property(&mut self, name: &str) {
    // TODO: Should we make the API return a Result type here?
    // Current behaviour on failures: silently return (noop)
    let c_name = CString::new(name).unwrap();
    unsafe {
      let attr_node = xmlHasProp(self.node_ptr, c_name.as_ptr());
      if !attr_node.is_null() {
        xmlRemoveProp(attr_node);
      }
    }
  }

  /// Alias for get_property
  pub fn get_attribute(&self, name: &str) -> Option<String> {
    self.get_property(name)
  }
  /// Alias for get_property_ns
  pub fn get_attribute_ns(&self, name: &str, ns: &str) -> Option<String> {
    self.get_property_ns(name, ns)
  }

  /// Alias for get_property_node
  pub fn get_attribute_node(&self, name: &str) -> Option<Node> {
    self.get_property_node(name)

  }

  /// Alias for set_property
  pub fn set_attribute(&mut self, name: &str, value: &str) {
    self.set_property(name, value)
  }
  /// Alias for set_property_ns
  pub fn set_attribute_ns(&mut self, name: &str, value: &str, ns: Namespace) {
    self.set_property_ns(name, value, ns)
  }

  /// Alias for remove_property
  pub fn remove_attribute(&mut self, name: &str) {
    self.remove_property(name)
  }

  /// Get a copy of the attributes of this node
  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 = str::from_utf8(c_name_string.to_bytes()).unwrap().to_owned();
        attr_names.push(name);
        current_prop = xmlNextPropertySibling(current_prop);
      }
    }

    for name in attr_names {
      let value = self.get_property(&name).unwrap_or(String::new());
      attributes.insert(name, value);
    }

    attributes
  }

  /// Alias for `get_properties`
  pub fn get_attributes(&self) -> HashMap<String, String> {
    self.get_properties()
  }

  /// Gets the active namespace associated of this node
  pub fn get_namespace(&self) -> Option<Namespace> {
    unsafe {
      let ns_ptr = xmlNodeNs(self.node_ptr);
      if ns_ptr.is_null() {
        None
      } else {
        Some(Namespace { ns_ptr: ns_ptr })
      }
    }
  }

  /// Gets a list of namespaces associated with this node
  pub fn get_namespaces(&self, doc: &Document) -> Vec<Namespace> {
    let mut ns_found = Vec::new();
    unsafe {
      let ns_ptr_list = xmlGetNsList(doc.doc_ptr, self.node_ptr);
      if !ns_ptr_list.is_null() {
        for index in 0.. {
          let ns_ptr = *ns_ptr_list.offset(index);
          if !ns_ptr.is_null() {
            ns_found.push(Namespace{ ns_ptr: ns_ptr});
          } else {
            break;
          }
        }
      }
    }
    ns_found
  }

  /// Get a list of namespaces declared with this node
  pub fn get_namespace_declarations(&self) -> Vec<Namespace> {
    let mut declarations = Vec::new();
    if self.get_type() != Some(NodeType::ElementNode) {
      return declarations; // only element nodes can have declarations
    }

    unsafe {
      let mut ns = xmlNodeNsDeclarations(self.node_ptr);
      while !ns.is_null() {
        if !xmlNsPrefix(ns).is_null() || !xmlNsHref(ns).is_null() {
          let ns_copy = xmlCopyNamespace(ns);
          if !ns_copy.is_null() {
            declarations.push(Namespace{ns_ptr: ns_copy});
          }
          ns = xmlNextNsSibling(ns);
        }
      }
    }
    declarations
  }

  /// Sets a `Namespace` for the node
  pub fn set_namespace(&mut self, namespace: Namespace) {
    unsafe {
      xmlSetNs(self.node_ptr, namespace.ns_ptr);
    }
  }

  /// Looks up the prefix of a namespace from its URI, basedo around a given `Node`
  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 ns_ptr = xmlSearchNsByHref( xmlGetDoc(self.node_ptr), self.node_ptr, c_href.as_ptr() );
      if !ns_ptr.is_null() {
        let ns = Namespace { ns_ptr: ns_ptr };
        let ns_prefix = ns.get_prefix();
        if !ns_prefix.is_empty() {
          Some(ns_prefix)
        } else {
          None
        }
      } else {
        None
      }
    }
  }

  /// Looks up the uri of a namespace from its prefix, basedo around a given `Node`
  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_ptr() );
      if !ns_ptr.is_null() {
        let ns = Namespace { ns_ptr: ns_ptr };
        let ns_prefix = ns.get_href();
        if !ns_prefix.is_empty() {
          Some(ns_prefix)
        } else {
          None
        }
      } else {
        None
      }
    }
  }

  /// Get a set of class names from this node's attributes
  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
  }

  /// Creates a new `Node` as child to the self `Node`
  pub fn add_child(&mut self, child: Node) -> Result<Node, ()> {
    unsafe {
      if xmlAddChild(self.node_ptr, child.node_ptr).is_null() {
        Err(())
      } else {
        Ok(child)
      }
    }
  }

  /// Creates a new `Node` as child to the self `Node`
  pub fn new_child(&mut self, ns: Option<Namespace>, name: &str) -> Result<Node, ()> {
    let c_name = CString::new(name).unwrap();
    let ns_ptr = match ns {
      None => ptr::null_mut(),
      Some(ns) => ns.ns_ptr,
    };
    unsafe {
      let new_ptr = xmlNewChild(self.node_ptr, ns_ptr, c_name.as_ptr(), ptr::null());
      Ok(Node { node_ptr: new_ptr })
    }
  }

  /// Adds a new text child, to this `Node`
  pub fn add_text_child(&mut self, ns: Option<Namespace>, name: &str, content: &str) -> Result<Node, ()> {
    let c_name = CString::new(name).unwrap();
    let c_content = CString::new(content).unwrap();
    let ns_ptr = match ns {
      None => ptr::null_mut(),
      Some(ns) => ns.ns_ptr,
    };
    unsafe {
      let new_ptr = xmlNewTextChild(self.node_ptr, ns_ptr, c_name.as_ptr(), c_content.as_ptr());
      Ok(Node { node_ptr: new_ptr })
    }
  }

  /// Append text to this `Node`
  pub fn append_text(&mut self, content: &str) -> Result<(),()> {
    let c_len = content.len() as i32;
    let c_content = CString::new(content).unwrap();
    unsafe {
      let result = xmlTextConcat(self.node_ptr, c_content.as_ptr(), c_len);
      if result != 0 {
        Err(())
      } else {
        Ok(())
      }
    }
  }
}

///An xml namespace
#[derive(Clone)]
pub struct Namespace {
  ///libxml's xmlNsPtr
  pub ns_ptr: *mut c_void,
}

impl Namespace {
  /// Creates a new namespace
  pub fn new(prefix: &str, href: &str, node: &Node) -> Result<Self, ()> {
    let c_href = CString::new(href).unwrap();
    let c_prefix = CString::new(prefix).unwrap();
    let c_prefix_ptr = if prefix.is_empty() {
      ptr::null()
    } else {
      c_prefix.as_ptr()
    };

    unsafe {
      let ns = xmlNewNs(node.node_ptr, c_href.as_ptr(), c_prefix_ptr);
      if ns.is_null() {
        Err(())
      } else {
        Ok(Namespace { ns_ptr: ns })
      }
    }
  }

  /// The namespace prefix
  pub fn get_prefix(&self) -> String {
    unsafe {
      let prefix_ptr = xmlNsPrefix(self.ns_ptr);
      if prefix_ptr.is_null() {
        String::new()
      } else {
        let c_prefix = CStr::from_ptr(prefix_ptr);
        let prefix = str::from_utf8(c_prefix.to_bytes()).unwrap().to_owned();
        prefix
      }
    }
  }

  /// The namespace href
  pub fn get_href(&self) -> String {
    unsafe {
      let href_ptr = xmlNsHref(self.ns_ptr);
      if href_ptr.is_null() {
        String::new()
      } else {
        let c_href = CStr::from_ptr(href_ptr);
        let href = str::from_utf8(c_href.to_bytes()).unwrap().to_owned();
        href
      }
    }
  }
}

impl Drop for Namespace {
  ///Free namespace
  fn drop(&mut self) {
    // unsafe {
    //   xmlFreeNs(self.ns_ptr);
    // }
  }
}