]> git.sesse.net Git - xml-template/blob - php5/xml-template.php
Fix overwriting in PHP5.
[xml-template] / php5 / xml-template.php
1 <?php
2
3 function XML_Template_process_file($filename, $obj, $clean = 1)
4 {
5         $doc = new DOMDocument;
6         $doc->load($filename);
7         XML_Template_process($doc, $obj, $clean);
8         return $doc;
9 }
10
11 function XML_Template_process($node, $obj, $clean = 1)
12 {
13         if (is_a($obj, 'DOMNode')) {                          # overwrite
14                 while ($node->childNodes->length > 0) {
15                         $child = $node->childNodes->item(0);
16                         $node->removeChild($child);
17                 }
18
19                 if (is_a($obj, 'DOMDocument')) {
20                         $obj = $obj->documentElement;
21                 }
22
23                 $frag = $node->ownerDocument->createDocumentFragment();
24                 $frag->appendChild($node->ownerDocument->importNode($obj, true));
25                 XML_Template_process($frag, array(), $clean);
26                 $node->appendChild($frag);
27         } else if (!is_array($obj)) {                         # overwrite
28                 while ($node->childNodes->length > 0) {
29                         $node->removeChild($node->firstChild);
30                 }
31                 $doc = $node->ownerDocument;
32                 $node->appendChild($doc->createTextNode($obj));
33         } else if (is_associative_array($obj)) {              # substitute
34                 $num_children = ($node->childNodes == null) ? 0 : $node->childNodes->length;
35                 for ($i = 0; $i < $num_children; ++$i) {
36                         $child = $node->childNodes->item($i);
37                         $processed = false;
38
39                         if ($child->nodeType == XML_ELEMENT_NODE) {
40                                 unset($id);
41
42                                 $tag = $child->localName;
43                                 $attrs = $child->attributes;
44
45                                 if (isset($attrs)) {
46                                         $replace_child = 0;
47                                         foreach ($child->attributes as $attr) {
48                                                 if ($attr->namespaceURI == 'http://template.sesse.net/' && $attr->name == 'id') {
49                                                         $id = $attr->value;
50                                                         if ($clean) {
51                                                                 $child->removeAttributeNode($attr);
52                                                                 $replace_child = 1;
53                                                         }
54                                                 }
55                                         }
56
57                                         # This seems to be the only way to reliably get rid of the excess
58                                         # XML namespace declarations.
59                                         if ($replace_child == 1) {
60                                                 $newchild = own_clone_element($child, $child->ownerDocument);
61                                                 while ($child->childNodes->length > 0) {
62                                                         $newchild->appendChild($child->firstChild);
63                                                 }
64                                                 $node->replaceChild($newchild, $child);
65                                                 $child = $newchild;
66                                         }
67                                 }
68                         
69                                 # check all substitutions to see if we found anything
70                                 # appropriate
71                                 foreach (array_keys($obj) as $key) {
72                                         # FIXME: we would want something like \Q and \E here...
73                                         if (preg_match('/^' . $tag . '\/(.*)$/', $key, $matches) ||
74                                             (isset($id) && preg_match('/^#' . $id . '\/(.*)$/', $key, $matches))) {
75                                                 $child->setAttribute($matches[1], $obj[$key]);
76                                         }
77
78                                         if ($processed) {
79                                                 continue;
80                                         }
81                                         if ($key == $tag || (isset($id) && $key == ('#'.$id))) {
82                                                 XML_Template_process($child, $obj[$key], $clean);
83                                                 $processed = true;
84                                         }
85                                 }
86                         }
87
88                         if (!$processed) {
89                                 XML_Template_process($child, $obj, $clean);
90                         }
91                 }
92         } else {                                                # repeat
93                 $doc = $node->ownerDocument;
94                 $frag = $doc->createDocumentFragment();
95
96                 while ($node->childNodes->length > 0) {
97                         $child = $node->childNodes->item(0);
98                         $node->removeChild($child);
99                         $frag->appendChild($child);
100                 }
101
102                 foreach ($obj as $instance) {
103                         if (!isset($instance)) {
104                                 continue;
105                         }
106
107                         $newnode = own_clone_node($frag, $frag->ownerDocument);
108                         XML_Template_process($newnode, $instance, $clean);
109
110                         $node->appendChild($newnode);
111                         if ($clean) {
112                                 XML_Template_clean($newnode);
113                         }
114                 }
115         }
116
117         if ($clean) {
118                 XML_Template_clean($node);
119         }
120 }
121
122 function XML_Template_clean($node)
123 {
124         if ($node->nodeType == XML_ELEMENT_NODE) {
125                 if ($node->namespaceURI != 'http://template.sesse.net/') {
126                         return;
127                 }
128
129                 # as this is a dummy node, we want to remove it and move everything further up
130                 # after we've done any required replacements
131                 $doc = $node->ownerDocument;
132                 $parent = $node->parentNode;
133
134                 while ($node->childNodes->length > 0) {
135                         $child = $node->childNodes->item(0);
136                         $node->removeChild($child);
137                         $parent->insertBefore($child, $node);
138                 }
139                 $parent->removeChild($node);
140         }
141 }
142
143 # FIXME: use varargs here
144 function XML_Template_alternate($tag, $array, $elems)
145 {
146         $num = count($elems);
147
148         for ($i = 0, $j = 0; $i < count($array); $i++) {
149                 if (isset($array[$i])) {
150                         $array[$i][$tag] = $elems[$j++ % $num];
151                 }
152         }
153
154         return $array;
155 }
156                 
157 # Ideally, this would be "return $obj->clone_node(true)". But surprise,
158 # surprise, PHP is buggy; it does not preserve the prefix information on
159 # attributes properly during a clone. (PHP4 and PHP5 are broken in different
160 # ways here...) IOW, we'll have to clone everything ourselves.
161 function own_clone_node($node, $doc)
162 {
163         // we only need these two
164         if ($node->nodeType == XML_DOCUMENT_FRAG_NODE) {
165                 $newnode = $doc->createDocumentFragment();
166
167                 for ($i = 0; $i < $node->childNodes->length; ++$i) {
168                         $child = $node->childNodes->item($i);
169                         $newnode->appendChild(own_clone_node($child, $doc));
170                 }
171                 return $newnode;
172         } else if ($node->nodeType == XML_ELEMENT_NODE) {
173                 $newnode = own_clone_element($node, $doc);
174                 for ($i = 0; $i < $node->childNodes->length; ++$i) {
175                         $child = $node->childNodes->item($i);
176                         $newnode->appendChild(own_clone_node($child, $doc));
177                 }
178                 return $newnode;
179         } else {
180                 return $node->cloneNode(true);
181         }
182 }
183                 
184 function own_clone_element($node, $doc)
185 {
186         $nsuri = $node->namespaceURI;
187         if (isset($nsuri) && $node->prefix != "default") {
188                 $newnode = $doc->createElementNS($node->namespaceURI, $node->nodeName, $node->prefix);
189         
190                 // remove useless empty text child
191                 $newnode->removeChild($newnode->firstChild);
192         } else {
193                 $newnode = $doc->createElement($node->localName);
194         }
195         
196         $attrs = $node->attributes;
197         if (isset($attrs)) {
198                 foreach ($node->attributes as $attr) {
199                         $nsuri = $attr->namespaceURI;
200                         if (isset($nsuri) && $attr->prefix != "default") {
201                                 $attr2 = $doc->createAttributeNS($nsuri, $attr->prefix . ":" . $attr->name);
202                         } else {
203                                 $attr2 = $doc->createAttribute($attr->localName);
204                         }
205                         $attr2->value = $attr->value;
206                         $newnode->appendChild($attr2);
207                 }
208         }
209         return $newnode;
210 }
211
212 function is_associative_array($arr)
213 {
214         if (!is_array($arr)) {
215                 return false;
216         }
217         $diff = array_diff(range(0, count($arr) - 1), array_keys($arr));
218         return (count($diff) > 0);
219 }
220 ?>