3 function XML_Template_process_file($filename, $obj, $clean = 1)
5 $doc = new DOMDocument;
7 XML_Template_process($doc, $obj, $clean);
11 function XML_Template_process($node, $obj, $clean = 1)
13 if (is_a($obj, 'domnode')) { # overwrite
14 for ($i = 0; $i < $node->childNodes->length; ++$i) {
15 $child = $node->childNodes->item($i);
16 $node->removeChild($child);
19 if (is_a($obj, 'domdocument')) {
20 $obj = $obj->document_element();
23 $newobj = own_clone_node($obj, $node->ownerDocument);
24 $node->appendChild($newobj);
26 XML_Template_process($newobj, array(), $clean);
27 } else if (!is_array($obj)) { # overwrite
28 for ($i = 0; $i < $node->childNodes->length; ++$i) {
29 $child = $node->childNodes->item($i);
30 $node->removeChild($child);
32 $doc = $node->ownerDocument;
33 $node->appendChild($doc->createTextNode($obj));
34 } else if (is_associative_array($obj)) { # substitute
35 for ($i = 0; $i < $node->childNodes->length; ++$i) {
36 $child = $node->childNodes->item($i);
39 if ($child->nodeType == XML_ELEMENT_NODE) {
42 $tag = $child->nodeName;
43 $attrs = $child->attributes;
45 foreach ($child->attributes as $attr) {
46 if ($attr->namespaceURI == 'http://template.sesse.net/' && $attr->name == 'id') {
49 $child->removeAttributeNode($attr);
55 # check all substitutions to see if we found anything
57 foreach (array_keys($obj) as $key) {
58 # FIXME: we would want something like \Q and \E here...
59 if (preg_match('/^' . $tag . '\/(.*)$/', $key, $matches) ||
60 (isset($id) && preg_match('/^#' . $id . '\/(.*)$/', $key, $matches))) {
61 $child->set_attribute($matches[1], $obj[$key]);
67 if ($key == $tag || (isset($id) && $key == ('#'.$id))) {
68 XML_Template_process($child, $obj[$key], $clean);
75 XML_Template_process($child, $obj, $clean);
79 $doc = $node->ownerDocument;
80 $frag = $doc->createElement("temporary-fragment"); # ugh
82 for ($i = 0; $i < $node->childNodes->length; ++$i) {
83 $child = $node->childNodes->item($i);
84 $frag->appendChild($child);
85 $node->removeChild($child);
88 foreach ($obj as $instance) {
89 if (!isset($instance)) {
93 $newnode = own_clone_node($frag, $frag->ownerDocument);
94 $node->appendChild($newnode);
95 XML_Template_process($newnode, $instance, $clean);
97 XML_Template_clean($newnode);
101 # remove all the <fragment> tags
103 for ($i = 0; $i < $node->childNodes->length; ++$i) {
104 $child = $node->childNodes->item($i);
105 if ($child->name != 'temporary-fragment') {
108 for ($j = 0; $j < $child->childNodes->length; ++$j) {
109 $child2 = $child->childNodes->item($j);
110 $node->appendChild($child2);
112 $node->removeChild($child);
117 XML_Template_clean($node);
121 function XML_Template_clean($node)
123 if ($node->nodeType == XML_ELEMENT_NODE) {
124 if ($node->namespaceURI != 'http://template.sesse.net/') {
128 # as this is a dummy node, we want to remove it and move everything further up
129 # after we've done any required replacements
130 $doc = $node->ownerDocument;
131 $parent = $node->parentNode;
132 for ($i = 0; $i < $node->childNodes->length; ++$i) {
133 $child = $node->childNodes->item($i);
134 $node->removeChild($child);
135 $node->insert_before($child, $node);
137 $parent->removeChild($node);
141 # FIXME: use varargs here
142 function XML_Template_alternate($tag, $array, $elems)
144 $num = count($elems);
146 for ($i = 0, $j = 0; $i < count($array); $i++) {
147 if (isset($array[$i])) {
148 $array[$i][$tag] = $elems[$j++ % $num];
155 # Ideally, this would be "return $obj->clone_node(true)". But surprise, surprise,
156 # PHP is buggy (at least PHP4); it removes the prefix information from all attributes
157 # during a clone. IOW, we'll have to clone evverything ourselves.
158 function own_clone_node($node, $doc)
160 // we only need these two
161 if ($node->nodeType == XML_ELEMENT_NODE) {
162 $nsuri = $node->namespaceURI;
164 $newnode = $doc->createElementNS($node->namespaceURI, $node->nodeName, $node->prefix());
166 $newnode = $doc->createElement($node->nodeName);
169 $attrs = $node->attributes;
171 foreach ($node->attributes as $attr) {
172 $attr2 = $doc->createAttribute($attr->name, $attr->value);
173 $nsuri = $attr->namespaceURI;
175 $attr2->set_namespace($nsuri, $attr->prefix);
177 $newnode->appendChild($attr2);
180 for ($i = 0; $i < $node->childNodes->length; ++$i) {
181 $child = $node->childNodes->item($i);
182 $newnode->appendChild(own_clone_node($child, $doc));
186 return $node->clone_node(true);
190 function is_associative_array($arr)
192 if (!is_array($arr)) {
195 $diff = array_diff(range(0, count($arr) - 1), array_keys($arr));
196 return (count($diff) > 0);