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