1<?php
2
3// Wrapper for ActiveLink pure PHP DOM extension
4class ActiveLinkDOMTree {
5  var $xml;
6  var $index;
7  var $parent_indices;
8  var $parents;
9  var $content;
10
11  function ActiveLinkDOMTree($xml, $index, $indices, $parents) {
12    $this->xml            = $xml;
13    $this->index          = $index;
14    $this->parent_indices = $indices;
15    $this->parents        = $parents;
16
17    if (is_a($this->xml,"XMLLeaf")) {
18      $this->content = $xml->value;
19    } else {
20      $this->content = $xml->getXMLContent();
21    };
22  }
23
24  function &from_XML($xml) {
25    $tree =& new ActiveLinkDomTree($xml,0, array(), array());
26    return $tree;
27  }
28
29  function node_type() {
30    return is_a($this->xml,"XMLLeaf") ? XML_TEXT_NODE : XML_ELEMENT_NODE;
31  }
32
33  function tagname() {
34    return is_a($this->xml,"XMLLeaf") ? "text" : $this->xml->getTagName();
35  }
36
37  function get_attribute($name) {
38    return $this->xml->getTagAttribute($name);
39  }
40
41  function has_attribute($name) {
42    return $this->xml->getTagAttribute($name) !== false;
43  }
44
45  function get_content() {
46    return $this->xml->getXMLContent();
47  }
48
49  function &document_element() {
50    return $this;
51  }
52
53  function &last_child() {
54    $child =& $this->first_child();
55
56    if ($child) {
57      $sibling =& $child->next_sibling();
58      while ($sibling) {
59        $child =& $sibling;
60        $sibling =& $child->next_sibling();
61      };
62    };
63
64    return $child;
65  }
66
67  function &parent() {
68    if (!(is_a($this->xml,"XMLBranch") || is_a($this->xml,"XMLLeaf"))) {
69      $null = false;
70      return $null;
71    }
72
73    if (count($this->parents) > 0) {
74      $parents =& $this->parents;
75      $parent =& array_pop($parents);
76      return $parent;
77    } else {
78      $null = false;
79      return $null;
80    };
81  }
82
83  function &first_child() {
84    $children = $this->xml->nodes;
85    $indices = $this->parent_indices;
86    array_push($indices, $this->index);
87    $parents = $this->parents;
88    array_push($parents, $this);
89
90    if ($children) {
91      $node =& new ActiveLinkDOMTree($children[0], 0, $indices, $parents);
92      return $node;
93    } else {
94      $null = false;
95      return $null;
96    };
97  }
98
99  function &previous_sibling() {
100    $parent =& $this->parents[count($this->parents)-1];
101    $nodes  =& $parent->xml->nodes;
102
103    if ($this->index <= 0) {
104      $null = false;
105      return $null;
106    };
107
108    $sibling = &new ActiveLinkDOMTree($nodes[$this->index-1],$this->index-1, $this->parent_indices, $this->parents);
109    return $sibling;
110  }
111
112  function &next_sibling() {
113    $parent =& $this->parents[count($this->parents)-1];
114    $nodes  =& $parent->xml->nodes;
115
116    if ($this->index >= count($nodes)-1) {
117      $null = false;
118      return $null;
119    };
120
121    $node =& new ActiveLinkDOMTree($nodes[$this->index+1], $this->index+1, $this->parent_indices, $this->parents);
122    return $node;
123  }
124}
125
126?>