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