]> git.sesse.net Git - xml-template/blob - php/xml-template.php
Fix some cleaning bugs.
[xml-template] / php / xml-template.php
1 <?php
2
3 function XML_Template_process_file($filename, $obj, $clean = 1)
4 {
5         $doc = domxml_open_file($filename);
6         XML_Template_process($doc, $obj, $clean);
7         return $doc;
8 }
9
10 function XML_Template_process($node, $obj, $clean = 1)
11 {
12         if (is_a($obj, 'domnode')) {                          # overwrite
13                 foreach ($node->child_nodes() as $child) {
14                         $node->remove_child($child);
15                 }
16
17                 if (is_a($obj, 'domdocument')) {
18                         $obj = $obj->document_element();
19                 }
20
21                 $newobj = own_clone_node($obj, $node->owner_document());
22                 $node->append_child($newobj);
23
24                 XML_Template_process($newobj, array(), $clean);
25         } else if (!is_array($obj)) {                         # overwrite
26                 foreach ($node->child_nodes() as $child) {
27                         $node->remove_child($child);
28                 }
29                 $doc = $node->owner_document();
30                 $node->add_child($doc->create_text_node($obj));
31         } else if (is_associative_array($obj)) {              # substitute
32                 foreach ($node->child_nodes() as $child) {
33                         $processed = false;
34
35                         if ($child->node_type() == XML_ELEMENT_NODE) {
36                                 $tag = $child->node_name();
37                                 $attrs = $child->attributes();
38                                 if (isset($attrs)) {
39                                         foreach ($child->attributes() as $attr) {
40                                                 // PHP's DOMXML module forgets the prefix information when
41                                                 // cloning nodes, so we'll have to avoid the namespace check
42                                                 // here, unfortunately
43                                                 if (/* $attr->namespace_uri() == 'http://template.sesse.net/' && */ $attr->name() == 'id') {
44                                                         $id = $attr->value();
45                                                         if ($clean) {
46                                                                 // FIXME: this won't work since we're not in the right namespace
47                                                                 // $child->remove_attribute($attr->name());
48                                                         }
49                                                 }
50                                         }
51                                 }
52                         
53                                 # check all substitutions to see if we found anything
54                                 # appropriate
55                                 foreach (array_keys($obj) as $key) {
56                                         # FIXME: we would want something like \Q and \E here...
57                                         if (preg_match('/^' . $tag . '\/(.*)$/', $key, $matches) ||
58                                             (isset($id) && preg_match('/^#' . $id . '\/(.*)$/', $key, $matches))) {
59                                                 $child->set_attribute($matches[1], $obj[$key]);
60                                         }
61
62                                         if ($processed) {
63                                                 continue;
64                                         }
65                                         if ($key == $tag || (isset($id) && $key == ('#'.$id))) {
66                                                 XML_Template_process($child, $obj[$key], $clean);
67                                                 $processed = true;
68                                         }
69                                 }
70                         }
71
72                         if (!$processed) {
73                                 XML_Template_process($child, $obj, $clean);
74                         }
75                 }
76         } else {                                                # repeat
77                 $doc = $node->owner_document();
78                 $frag = $doc->create_element("temporary-fragment");    # ugh
79
80                 foreach ($node->child_nodes() as $child) {
81                         $frag->append_child($child);
82                         $node->remove_child($child);
83                 }
84
85                 foreach ($obj as $instance) {
86                         $newnode = own_clone_node($frag, $frag->owner_document());
87                         $node->append_child($newnode);
88                         XML_Template_process($newnode, $instance, $clean);
89                         if ($clean) {
90                                 XML_Template_clean($newnode);
91                         }
92                 }
93
94                 # remove all the <fragment> tags
95
96                 foreach ($node->child_nodes() as $child) {
97                         if ($child->name() != 'temporary-fragment') {
98                                 continue;
99                         }
100                         foreach ($child->child_nodes() as $child2) {
101                                 $node->append_child($child2);
102                         }
103                         $node->remove_child($child);
104                 }       
105         }
106
107         if ($clean) {
108                 XML_Template_clean($node);
109         }
110 }
111
112 function XML_Template_clean($node)
113 {
114         if ($node->node_type() == XML_ELEMENT_NODE) {
115                 if ($node->namespace_uri() != 'http://template.sesse.net/') {
116                         return;
117                 }
118
119                 # as this is a dummy node, we want to remove it and move everything further up
120                 # after we've done any required replacements
121                 $doc = $node->owner_document();
122                 $parent = $node->parent_node();
123                 foreach ($node->child_nodes() as $child) {
124                         $node->remove_child($child);
125                         $node->insert_before($child, $node);
126                 }
127                 $parent->remove_child($node);
128         }
129 }
130
131 # FIXME: use varargs here
132 function XML_Template_alternate($tag, $array, $elems)
133 {
134         $i = 0;
135         $num = count($elems);
136
137         for ($i = 0; $i < count($array); $i++) {
138                 $array[$i][$tag] = $elems[$i % $num];
139         }
140
141         return $array;
142 }
143                 
144 # Ideally, this would be "return $obj->clone_node(true)". But surprise, surprise,
145 # PHP is buggy (at least PHP4); it removes the prefix information from all attributes
146 # during a clone. IOW, we'll have to clone evverything ourselves.
147 function own_clone_node($node, $doc)
148 {
149         // we only need these two
150         if ($node->node_type() == XML_ELEMENT_NODE) {
151                 $newnode = $doc->create_element_ns($node->namespace_uri(), $node->node_name(), $node->prefix());
152                 $attrs = $node->attributes();
153                 if (isset($attrs)) {
154                         foreach ($node->attributes() as $attr) {
155                                 // set_attribute_node() does not exist...
156                                 $prefix = $attr->prefix();
157                                 if (isset($prefix)) {
158                                         $newnode->set_attribute($prefix . ":" . $attr->name(), $attr->value());
159                                 } else {
160                                         $newnode->set_attribute($attr->name(), $attr->value());
161                                 }
162                         }
163                 }
164                 foreach ($node->child_nodes() as $child) {
165                         $newnode->append_child(own_clone_node($child, $doc));
166                 }
167                 return $newnode;
168         } else {
169                 return $node->clone_node(true);
170         }
171 }
172
173 function is_associative_array($arr)
174 {
175         if (!is_array($arr)) {
176                 return false;
177         }
178         $diff = array_diff(range(0, count($arr) - 1), array_keys($arr));
179         return (count($diff) > 0);
180 }
181 ?>