-<?php
-
-function XML_Template_process_file($filename, $obj, $clean = 1)
-{
- $doc = new DOMDocument;
- $doc->load($filename);
- XML_Template_process($doc, $obj, $clean);
- return $doc;
-}
-
-function XML_Template_process($node, $obj, $clean = 1)
-{
- if (is_a($obj, 'DOMNode')) { # overwrite
- while ($node->childNodes->length > 0) {
- $child = $node->childNodes->item(0);
- $node->removeChild($child);
- }
-
- if (is_a($obj, 'DOMDocument')) {
- $obj = $obj->documentElement;
- }
-
- $frag = $node->ownerDocument->createDocumentFragment();
- $frag->appendChild($node->ownerDocument->importNode($obj, true));
- XML_Template_process($frag, array(), $clean);
- $node->appendChild($frag);
- } else if (!is_array($obj)) { # overwrite
- while ($node->childNodes->length > 0) {
- $node->removeChild($node->firstChild);
- }
- $doc = $node->ownerDocument;
- $node->appendChild($doc->createTextNode($obj));
- } else if (is_associative_array($obj)) { # substitute
- $num_children = ($node->childNodes == null) ? 0 : $node->childNodes->length;
- $children = array();
- for ($i = 0; $i < $num_children; ++$i) {
- $children[] = $node->childNodes->item($i);
- }
-
- for ($i = 0; $i < $num_children; ++$i) {
- $child = $children[$i];
- $processed = false;
-
- if ($child->nodeType == XML_ELEMENT_NODE) {
- unset($id);
-
- $tag = $child->localName;
- $attrs = $child->attributes;
-
- if (isset($attrs)) {
- $replace_child = 0;
- $id_node = $child->getAttributeNodeNS('http://template.sesse.net/', 'id');
- if (isset($id_node)) {
- $id = $id_node->value;
- if ($clean) {
- $child->removeAttributeNode($id_node);
- $replace_child = 1;
- }
- }
-
- # This seems to be the only way to reliably get rid of the excess
- # XML namespace declarations.
- if ($replace_child == 1) {
- $newchild = own_clone_element($child, $child->ownerDocument);
- while ($child->childNodes->length > 0) {
- $newchild->appendChild($child->firstChild);
- }
- $node->replaceChild($newchild, $child);
- $child = $newchild;
- }
- }
-
- # check all substitutions to see if we found anything
- # appropriate
- foreach (array_keys($obj) as $key) {
- # FIXME: we would want something like \Q and \E here...
- if (preg_match('/^' . $tag . '\/(.*)$/', $key, $matches) ||
- (isset($id) && preg_match('/^#' . $id . '\/(.*)$/', $key, $matches))) {
- $child->setAttribute($matches[1], $obj[$key]);
- }
-
- if ($processed) {
- continue;
- }
- if ($key == $tag || (isset($id) && $key == ('#'.$id))) {
- XML_Template_process($child, $obj[$key], $clean);
- $processed = true;
- }
- }
- }
-
- if (!$processed) {
- XML_Template_process($child, $obj, $clean);
- }
- }
- } else { # repeat
- $doc = $node->ownerDocument;
- $frag = $doc->createDocumentFragment();
-
- while ($node->childNodes->length > 0) {
- $child = $node->childNodes->item(0);
- $node->removeChild($child);
- $frag->appendChild($child);
- }
-
- foreach ($obj as $instance) {
- if (!isset($instance)) {
- continue;
- }
-
- $newnode = own_clone_node($frag, $frag->ownerDocument);
- XML_Template_process($newnode, $instance, $clean);
-
- $node->appendChild($newnode);
- if ($clean) {
- XML_Template_clean($newnode);
- }
- }
- }
-
- if ($clean) {
- XML_Template_clean($node);
- }
-}
-
-function XML_Template_clean($node)
-{
- if ($node->nodeType == XML_ELEMENT_NODE) {
- if ($node->namespaceURI != 'http://template.sesse.net/') {
- return;
- }
-
- # as this is a dummy node, we want to remove it and move everything further up
- # after we've done any required replacements
- $doc = $node->ownerDocument;
- $parent = $node->parentNode;
-
- while ($node->childNodes->length > 0) {
- $child = $node->childNodes->item(0);
- $node->removeChild($child);
- $parent->insertBefore(own_clone_node($child, $doc), $node);
- }
- $parent->removeChild($node);
- }
-}
-
-# FIXME: use varargs here
-function XML_Template_alternate($tag, $array, $elems)
-{
- $num = count($elems);
-
- for ($i = 0, $j = 0; $i < count($array); $i++) {
- if (isset($array[$i])) {
- $array[$i][$tag] = $elems[$j++ % $num];
- }
- }
-
- return $array;
-}
-
-# Ideally, this would be "return $obj->clone_node(true)". But surprise,
-# surprise, PHP is buggy; it does not preserve the prefix information on
-# attributes properly during a clone. (PHP4 and PHP5 are broken in different
-# ways here...) IOW, we'll have to clone everything ourselves.
-function own_clone_node($node, $doc)
-{
- // we only need these two
- if ($node->nodeType == XML_DOCUMENT_FRAG_NODE) {
- $newnode = $doc->createDocumentFragment();
-
- for ($i = 0; $i < $node->childNodes->length; ++$i) {
- $child = $node->childNodes->item($i);
- $newnode->appendChild(own_clone_node($child, $doc));
- }
- return $newnode;
- } else if ($node->nodeType == XML_ELEMENT_NODE) {
- $newnode = own_clone_element($node, $doc);
- for ($i = 0; $i < $node->childNodes->length; ++$i) {
- $child = $node->childNodes->item($i);
- $newnode->appendChild(own_clone_node($child, $doc));
- }
- return $newnode;
- } else {
- return $node->cloneNode(true);
- }
-}
-
-function own_clone_element($node, $doc)
-{
- $nsuri = $node->namespaceURI;
- if (isset($nsuri) && $node->prefix != "default") {
- $newnode = $doc->createElementNS($node->namespaceURI, $node->nodeName, $node->prefix);
-
- // remove useless empty text child
- $newnode->removeChild($newnode->firstChild);
- } else {
- $newnode = $doc->createElement($node->localName);
- }
-
- $attrs = $node->attributes;
- if (isset($attrs)) {
- foreach ($node->attributes as $attr) {
- $nsuri = $attr->namespaceURI;
- if (isset($nsuri) && $attr->prefix != "default") {
- $attr2 = $doc->createAttributeNS($nsuri, $attr->prefix . ":" . $attr->name);
- } else {
- $attr2 = $doc->createAttribute($attr->localName);
- }
-
- # You've got to be kidding me...
- $attr2->value = preg_replace("/&/", "&", $attr->value);
- $newnode->appendChild($attr2);
- }
- }
- return $newnode;
-}
-
-function is_associative_array($arr)
-{
- if (!is_array($arr)) {
- return false;
- }
- $diff = array_diff(range(0, count($arr) - 1), array_keys($arr));
- return (count($diff) > 0);
-}
-?>