]> git.sesse.net Git - xml-template/blob - php/xml-template.php
91dfa8d45ad1017625b9c6b17dd9e473d5fbfaf1
[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                                                 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                                 foreach (array_keys($obj) as $key) {
53                                         # FIXME: we would want something like \Q and \E here...
54                                         if (preg_match('/^' . $tag . '\/(.*)$/', $key, $matches) ||
55                                             (isset($id) && preg_match('/^#' . $id . '\/(.*)$/', $key, $matches))) {
56                                                 $child->set_attribute($matches[1], $obj[$key]);
57                                         }
58
59                                         if ($processed) {
60                                                 continue;
61                                         }
62                                         if ($key == $tag || (isset($id) && $key == ('#'.$id))) {
63                                                 XML_Template_process($child, $obj[$key], $clean);
64                                                 $processed = true;
65                                         }
66                                 }
67                         }
68
69                         if (!$processed) {
70                                 XML_Template_process($child, $obj, $clean);
71                         }
72                 }
73         } else {                                                # repeat
74                 $doc = $node->owner_document();
75                 $frag = $doc->create_element("temporary-fragment");    # ugh
76
77                 foreach ($node->child_nodes() as $child) {
78                         $frag->append_child($child);
79                         $node->remove_child($child);
80                 }
81
82                 foreach ($obj as $instance) {
83                         $newnode = $frag->clone_node(true);
84                         $node->append_child($newnode);
85                         XML_Template_process($newnode, $instance, $clean);
86                         if ($clean) {
87                                 XML_Template_clean($newnode);
88                         }
89                 }
90
91                 # remove all the <fragment> tags
92
93                 foreach ($node->child_nodes() as $child) {
94                         if ($child->name() != 'temporary-fragment') {
95                                 continue;
96                         }
97                         foreach ($child->child_nodes() as $child2) {
98                                 $node->append_child($child2);
99                         }
100                         $node->remove_child($child);
101                 }       
102
103                 return;
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 function is_associative_array($arr)
144 {
145         if (!is_array($arr)) {
146                 return false;
147         }
148         $diff = array_diff(range(0, count($arr) - 1), array_keys($arr));
149         return (count($diff) > 0);
150 }
151 ?>