]> git.sesse.net Git - xml-template/blobdiff - php5/xml-template.php
More cloning fixes. clone.xml doesn't pass, but it passes morally.
[xml-template] / php5 / xml-template.php
index 6fbf4bf8962c246740c619045985d176be34dae4..08c78ab57bcaf4998890628303b5790918c7eb99 100644 (file)
@@ -2,47 +2,52 @@
 
 function XML_Template_process_file($filename, $obj, $clean = 1)
 {
-       $doc = domxml_open_file($filename);
+       $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
-               foreach ($node->child_nodes() as $child) {
-                       $node->remove_child($child);
+       if (is_a($obj, 'DOMNode')) {                          # overwrite
+               for ($i = 0; $i < $node->childNodes->length; ++$i) {
+                       $child = $node->childNodes->item($i);
+                       $node->removeChild($child);
                }
 
-               if (is_a($obj, 'domdocument')) {
+               if (is_a($obj, 'DOMDocument')) {
                        $obj = $obj->document_element();
                }
 
-               $newobj = own_clone_node($obj, $node->owner_document());
-               $node->append_child($newobj);
+               $newobj = own_clone_node($obj, $node->ownerDocument);
+               $node->appendChild($newobj);
 
                XML_Template_process($newobj, array(), $clean);
        } else if (!is_array($obj)) {                         # overwrite
-               foreach ($node->child_nodes() as $child) {
-                       $node->remove_child($child);
+               for ($i = 0; $i < $node->childNodes->length; ++$i) {
+                       $child = $node->childNodes->item($i);
+                       $node->removeChild($child);
                }
-               $doc = $node->owner_document();
-               $node->add_child($doc->create_text_node($obj));
+               $doc = $node->ownerDocument;
+               $node->appendChild($doc->createTextNode($obj));
        } else if (is_associative_array($obj)) {              # substitute
-               foreach ($node->child_nodes() as $child) {
+               for ($i = 0; $i < $node->childNodes->length; ++$i) {
+                       $child = $node->childNodes->item($i);
                        $processed = false;
 
-                       if ($child->node_type() == XML_ELEMENT_NODE) {
+                       if ($child->nodeType == XML_ELEMENT_NODE) {
                                unset($id);
 
-                               $tag = $child->node_name();
-                               $attrs = $child->attributes();
+                               $tag = $child->localName;
+                               $attrs = $child->attributes;
+
                                if (isset($attrs)) {
-                                       foreach ($child->attributes() as $attr) {
-                                               if ($attr->namespace_uri() == 'http://template.sesse.net/' && $attr->name() == 'id') {
-                                                       $id = $attr->value();
+                                       foreach ($child->attributes as $attr) {
+                                               if ($attr->namespaceURI == 'http://template.sesse.net/' && $attr->name == 'id') {
+                                                       $id = $attr->value;
                                                        if ($clean) {
-                                                               $attr->unlink_node();
+                                                               $child->removeAttributeNode($attr);
                                                        }
                                                }
                                        }
@@ -54,7 +59,7 @@ function XML_Template_process($node, $obj, $clean = 1)
                                        # FIXME: we would want something like \Q and \E here...
                                        if (preg_match('/^' . $tag . '\/(.*)$/', $key, $matches) ||
                                            (isset($id) && preg_match('/^#' . $id . '\/(.*)$/', $key, $matches))) {
-                                               $child->set_attribute($matches[1], $obj[$key]);
+                                               $child->setAttribute($matches[1], $obj[$key]);
                                        }
 
                                        if ($processed) {
@@ -72,12 +77,12 @@ function XML_Template_process($node, $obj, $clean = 1)
                        }
                }
        } else {                                                # repeat
-               $doc = $node->owner_document();
-               $frag = $doc->create_element("temporary-fragment");    # ugh
+               $doc = $node->ownerDocument;
+               $frag = $doc->createElement("temporary-fragment");    # ugh
 
-               foreach ($node->child_nodes() as $child) {
-                       $frag->append_child($child);
-                       $node->remove_child($child);
+               while ($node->childNodes->length > 0) {
+                       $child = $node->childNodes->item(0);
+                       $frag->appendChild($child);
                }
 
                foreach ($obj as $instance) {
@@ -85,8 +90,8 @@ function XML_Template_process($node, $obj, $clean = 1)
                                continue;
                        }
 
-                       $newnode = own_clone_node($frag, $frag->owner_document());
-                       $node->append_child($newnode);
+                       $newnode = own_clone_node($frag, $frag->ownerDocument);
+                       $node->appendChild($newnode);
                        XML_Template_process($newnode, $instance, $clean);
                        if ($clean) {
                                XML_Template_clean($newnode);
@@ -94,15 +99,17 @@ function XML_Template_process($node, $obj, $clean = 1)
                }
 
                # remove all the <fragment> tags
-
-               foreach ($node->child_nodes() as $child) {
-                       if ($child->name() != 'temporary-fragment') {
+               for ($i = 0; $i < $node->childNodes->length; ++$i) {
+                       $child = $node->childNodes->item($i);
+                       if ($child->localName != 'temporary-fragment') {
                                continue;
                        }
-                       foreach ($child->child_nodes() as $child2) {
-                               $node->append_child($child2);
+                       while ($child->childNodes->length > 0) {
+                               $child2 = $child->childNodes->item(0);
+                               $node->appendChild($child2);
                        }
-                       $node->remove_child($child);
+                       --$i;
+                       $node->removeChild($child);
                }       
        }
 
@@ -113,20 +120,22 @@ function XML_Template_process($node, $obj, $clean = 1)
 
 function XML_Template_clean($node)
 {
-       if ($node->node_type() == XML_ELEMENT_NODE) {
-               if ($node->namespace_uri() != 'http://template.sesse.net/') {
+       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->owner_document();
-               $parent = $node->parent_node();
-               foreach ($node->child_nodes() as $child) {
-                       $node->remove_child($child);
-                       $node->insert_before($child, $node);
+               $doc = $node->ownerDocument;
+               $parent = $node->parentNode;
+
+               while ($node->childNodes->length > 0) {
+                       $child = $node->childNodes->item(0);
+                       $node->removeChild($child);
+                       $parent->insertBefore($child, $node);
                }
-               $parent->remove_child($node);
+               $parent->removeChild($node);
        }
 }
 
@@ -145,36 +154,40 @@ function XML_Template_alternate($tag, $array, $elems)
 }
                
 # Ideally, this would be "return $obj->clone_node(true)". But surprise, surprise,
-# PHP is buggy (at least PHP4); it removes the prefix information from all attributes
-# during a clone. IOW, we'll have to clone evverything ourselves.
+# PHP is buggy (at least both PHP4 and PHP5); it removes the prefix information
+# from all attributes during a clone. IOW, we'll have to clone evverything
+# ourselves.
 function own_clone_node($node, $doc)
 {
        // we only need these two
-       if ($node->node_type() == XML_ELEMENT_NODE) {
-               $nsuri = $node->namespace_uri();
+       if ($node->nodeType == XML_ELEMENT_NODE) {
+               $nsuri = $node->namespaceURI;
                if (isset($nsuri)) {
-                       $newnode = $doc->create_element_ns($node->namespace_uri(), $node->node_name(), $node->prefix());
+                       $newnode = $doc->createElementNS($node->namespaceURI, $node->nodeName, $node->prefix);
                } else {
-                       $newnode = $doc->create_element($node->node_name());
+                       $newnode = $doc->createElement($node->nodeName);
                }
                
-               $attrs = $node->attributes();
+               $attrs = $node->attributes;
                if (isset($attrs)) {
-                       foreach ($node->attributes() as $attr) {
-                               $attr2 = $doc->create_attribute($attr->name(), $attr->value());
-                               $nsuri = $attr->namespace_uri();
+                       foreach ($node->attributes as $attr) {
+                               $nsuri = $attr->namespaceURI;
                                if (isset($nsuri)) {
-                                       $attr2->set_namespace($nsuri, $attr->prefix());
+                                       $attr2 = $doc->createAttribute($attr->name);
+                               } else {
+                                       $attr2 = $doc->createAttributeNS($nsuri, $attr->prefix . ":" . $attr->name);
                                }
-                               $newnode->append_child($attr2);
+                               $attr2->value = $attr->value;
+                               $newnode->appendChild($attr2);
                        }
                }
-               foreach ($node->child_nodes() as $child) {
-                       $newnode->append_child(own_clone_node($child, $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->clone_node(true);
+               return $node->cloneNode(true);
        }
 }