]> git.sesse.net Git - xml-template/blob - php/xml-template.php
d7e8aef7428d4817fd03b3121be80271bcc20498
[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 = $obj->clone_node(true);
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                                 $tag = $child->node_name();
37                                 $attrs = $child->attributes();
38                                 if (isset($attrs)) {
39                                         foreach ($child->attributes() as $attr) {
40                                                 // PHP's DOMXML module forgets the prefix information when
41                                                 // cloning nodes, so we'll have to avoid the namespace check
42                                                 // here, unfortunately
43                                                 if (/* $attr->namespace_uri() == 'http://template.sesse.net/' && */ $attr->name() == 'id') {
44                                                         $id = $attr->value();
45                                                         if ($clean) {
46                                                                 // FIXME: this won't work since we're not in the right namespace
47                                                                 // $child->remove_attribute($attr->name());
48                                                         }
49                                                 }
50                                         }
51                                 }
52                         
53                                 # check all substitutions to see if we found anything
54                                 # appropriate
55                                 foreach (array_keys($obj) as $key) {
56                                         # FIXME: we would want something like \Q and \E here...
57                                         if (preg_match('/^' . $tag . '\/(.*)$/', $key, $matches) ||
58                                             (isset($id) && preg_match('/^#' . $id . '\/(.*)$/', $key, $matches))) {
59                                                 $child->set_attribute($matches[1], $obj[$key]);
60                                         }
61
62                                         if ($processed) {
63                                                 continue;
64                                         }
65                                         if ($key == $tag || (isset($id) && $key == ('#'.$id))) {
66                                                 XML_Template_process($child, $obj[$key], $clean);
67                                                 $processed = true;
68                                         }
69                                 }
70                         }
71
72                         if (!$processed) {
73                                 XML_Template_process($child, $obj, $clean);
74                         }
75                 }
76         } else {                                                # repeat
77                 $doc = $node->owner_document();
78                 $frag = $doc->create_element("temporary-fragment");    # ugh
79
80                 foreach ($node->child_nodes() as $child) {
81                         $frag->append_child($child);
82                         $node->remove_child($child);
83                 }
84
85                 foreach ($obj as $instance) {
86                         $newnode = $frag->clone_node(true);
87                         $node->append_child($newnode);
88                         XML_Template_process($newnode, $instance, $clean);
89                         if ($clean) {
90                                 XML_Template_clean($newnode);
91                         }
92                 }
93
94                 # remove all the <fragment> tags
95
96                 foreach ($node->child_nodes() as $child) {
97                         if ($child->name() != 'temporary-fragment') {
98                                 continue;
99                         }
100                         foreach ($child->child_nodes() as $child2) {
101                                 $node->append_child($child2);
102                         }
103                         $node->remove_child($child);
104                 }       
105
106                 return;
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         $i = 0;
137         $num = count($elems);
138
139         for ($i = 0; $i < count($array); $i++) {
140                 $array[$i][$tag] = $elems[$i % $num];
141         }
142
143         return $array;
144 }
145
146 function is_associative_array($arr)
147 {
148         if (!is_array($arr)) {
149                 return false;
150         }
151         $diff = array_diff(range(0, count($arr) - 1), array_keys($arr));
152         return (count($diff) > 0);
153 }
154 ?>