]> git.sesse.net Git - xml-template/blob - php5/xml-template.php
7d0ab1985102e734fa256deaa0111492b4bd5960
[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                 for ($i = 0; $i < $node->childNodes->length; ++$i) {
15                         $child = $node->childNodes->item($i);
16                         $node->removeChild($child);
17                 }
18
19                 if (is_a($obj, 'domdocument')) {
20                         $obj = $obj->document_element();
21                 }
22
23                 $newobj = own_clone_node($obj, $node->ownerDocument);
24                 $node->appendChild($newobj);
25
26                 XML_Template_process($newobj, array(), $clean);
27         } else if (!is_array($obj)) {                         # overwrite
28                 for ($i = 0; $i < $node->childNodes->length; ++$i) {
29                         $child = $node->childNodes->item($i);
30                         $node->removeChild($child);
31                 }
32                 $doc = $node->ownerDocument;
33                 $node->appendChild($doc->createTextNode($obj));
34         } else if (is_associative_array($obj)) {              # substitute
35                 for ($i = 0; $i < $node->childNodes->length; ++$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->nodeName;
43                                 $attrs = $child->attributes;
44                                 if (isset($attrs)) {
45                                         foreach ($child->attributes as $attr) {
46                                                 if ($attr->namespaceURI == 'http://template.sesse.net/' && $attr->name == 'id') {
47                                                         $id = $attr->value;
48                                                         if ($clean) {
49                                                                 $child->removeAttributeNode($attr);
50                                                         }
51                                                 }
52                                         }
53                                 }
54                         
55                                 # check all substitutions to see if we found anything
56                                 # appropriate
57                                 foreach (array_keys($obj) as $key) {
58                                         # FIXME: we would want something like \Q and \E here...
59                                         if (preg_match('/^' . $tag . '\/(.*)$/', $key, $matches) ||
60                                             (isset($id) && preg_match('/^#' . $id . '\/(.*)$/', $key, $matches))) {
61                                                 $child->set_attribute($matches[1], $obj[$key]);
62                                         }
63
64                                         if ($processed) {
65                                                 continue;
66                                         }
67                                         if ($key == $tag || (isset($id) && $key == ('#'.$id))) {
68                                                 XML_Template_process($child, $obj[$key], $clean);
69                                                 $processed = true;
70                                         }
71                                 }
72                         }
73
74                         if (!$processed) {
75                                 XML_Template_process($child, $obj, $clean);
76                         }
77                 }
78         } else {                                                # repeat
79                 $doc = $node->ownerDocument;
80                 $frag = $doc->createElement("temporary-fragment");    # ugh
81
82                 for ($i = 0; $i < $node->childNodes->length; ++$i) {
83                         $child = $node->childNodes->item($i);
84                         $frag->appendChild($child);
85                         $node->removeChild($child);
86                 }
87
88                 foreach ($obj as $instance) {
89                         if (!isset($instance)) {
90                                 continue;
91                         }
92
93                         $newnode = own_clone_node($frag, $frag->ownerDocument);
94                         $node->appendChild($newnode);
95                         XML_Template_process($newnode, $instance, $clean);
96                         if ($clean) {
97                                 XML_Template_clean($newnode);
98                         }
99                 }
100
101                 # remove all the <fragment> tags
102
103                 for ($i = 0; $i < $node->childNodes->length; ++$i) {
104                         $child = $node->childNodes->item($i);
105                         if ($child->name != 'temporary-fragment') {
106                                 continue;
107                         }
108                         for ($j = 0; $j < $child->childNodes->length; ++$j) {
109                                 $child2 = $child->childNodes->item($j);
110                                 $node->appendChild($child2);
111                         }
112                         $node->removeChild($child);
113                 }       
114         }
115
116         if ($clean) {
117                 XML_Template_clean($node);
118         }
119 }
120
121 function XML_Template_clean($node)
122 {
123         if ($node->nodeType == XML_ELEMENT_NODE) {
124                 if ($node->namespaceURI != 'http://template.sesse.net/') {
125                         return;
126                 }
127
128                 # as this is a dummy node, we want to remove it and move everything further up
129                 # after we've done any required replacements
130                 $doc = $node->ownerDocument;
131                 $parent = $node->parentNode;
132                 for ($i = 0; $i < $node->childNodes->length; ++$i) {
133                         $child = $node->childNodes->item($i);
134                         $node->removeChild($child);
135                         $node->insert_before($child, $node);
136                 }
137                 $parent->removeChild($node);
138         }
139 }
140
141 # FIXME: use varargs here
142 function XML_Template_alternate($tag, $array, $elems)
143 {
144         $num = count($elems);
145
146         for ($i = 0, $j = 0; $i < count($array); $i++) {
147                 if (isset($array[$i])) {
148                         $array[$i][$tag] = $elems[$j++ % $num];
149                 }
150         }
151
152         return $array;
153 }
154                 
155 # Ideally, this would be "return $obj->clone_node(true)". But surprise, surprise,
156 # PHP is buggy (at least PHP4); it removes the prefix information from all attributes
157 # during a clone. IOW, we'll have to clone evverything ourselves.
158 function own_clone_node($node, $doc)
159 {
160         // we only need these two
161         if ($node->nodeType == XML_ELEMENT_NODE) {
162                 $nsuri = $node->namespaceURI;
163                 if (isset($nsuri)) {
164                         $newnode = $doc->createElementNS($node->namespaceURI, $node->nodeName, $node->prefix());
165                 } else {
166                         $newnode = $doc->createElement($node->nodeName);
167                 }
168                 
169                 $attrs = $node->attributes;
170                 if (isset($attrs)) {
171                         foreach ($node->attributes as $attr) {
172                                 $attr2 = $doc->createAttribute($attr->name, $attr->value);
173                                 $nsuri = $attr->namespaceURI;
174                                 if (isset($nsuri)) {
175                                         $attr2->set_namespace($nsuri, $attr->prefix);
176                                 }
177                                 $newnode->appendChild($attr2);
178                         }
179                 }
180                 for ($i = 0; $i < $node->childNodes->length; ++$i) {
181                         $child = $node->childNodes->item($i);
182                         $newnode->appendChild(own_clone_node($child, $doc));
183                 }
184                 return $newnode;
185         } else {
186                 return $node->clone_node(true);
187         }
188 }
189
190 function is_associative_array($arr)
191 {
192         if (!is_array($arr)) {
193                 return false;
194         }
195         $diff = array_diff(range(0, count($arr) - 1), array_keys($arr));
196         return (count($diff) > 0);
197 }
198 ?>