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