]> git.sesse.net Git - xml-template/commitdiff
Add php5 (not working yet, just copied php4/).
authorsgunderson@bigfoot.com <>
Mon, 21 Apr 2008 19:23:19 +0000 (21:23 +0200)
committersgunderson@bigfoot.com <>
Mon, 21 Apr 2008 19:23:19 +0000 (21:23 +0200)
14 files changed:
php5/attribute-empty.php [new file with mode: 0644]
php5/attribute.php [new file with mode: 0644]
php5/attribute2.php [new file with mode: 0644]
php5/attribute3.php [new file with mode: 0644]
php5/attribute4.php [new file with mode: 0644]
php5/clone.php [new file with mode: 0644]
php5/include.php [new file with mode: 0644]
php5/namespace.php [new file with mode: 0644]
php5/namespace2.php [new file with mode: 0644]
php5/passthru.php [new file with mode: 0644]
php5/simple.php [new file with mode: 0644]
php5/structure.php [new file with mode: 0644]
php5/xml-template.php [new file with mode: 0644]
tests/test.sh

diff --git a/php5/attribute-empty.php b/php5/attribute-empty.php
new file mode 100644 (file)
index 0000000..d461140
--- /dev/null
@@ -0,0 +1,12 @@
+<?php
+require('xml-template.php');
+
+# NULL values are ignored, but having one helps pointing out that
+# the array given is non-associative
+
+$doc = XML_Template_process_file('../xml/clone.xml', array(
+       'color' => 'blue',
+       '#things' => array(NULL)
+));
+print $doc->dump_mem();
+?>
diff --git a/php5/attribute.php b/php5/attribute.php
new file mode 100644 (file)
index 0000000..626812e
--- /dev/null
@@ -0,0 +1,14 @@
+<?php
+require('xml-template.php');
+
+$doc = XML_Template_process_file('../xml/clone.xml', array(
+       'color' => 'red',
+       '#things' => array(
+               array( 'li' => 'Raindrops on roses',    'li/class' => 'odd' ),
+               array( 'li' => 'Whiskers on kittens',   'li/class' => 'even' ),
+               array( 'li' => 'Bright copper kettles', 'li/class' => 'odd' ),
+               array( 'li' => 'Warm, woolen mittens',  'li/class' => 'even' )
+       )
+));
+print $doc->dump_mem();
+?>
diff --git a/php5/attribute2.php b/php5/attribute2.php
new file mode 100644 (file)
index 0000000..186828f
--- /dev/null
@@ -0,0 +1,14 @@
+<?php
+require('xml-template.php');
+
+$doc = XML_Template_process_file('../xml/clone.xml', array(
+       'color' => 'blue',
+       '#things' => XML_Template_alternate('li/class', array(
+               array( 'li' => 'Raindrops on roses' ),
+               array( 'li' => 'Whiskers on kittens' ),
+               array( 'li' => 'Bright copper kettles' ),
+               array( 'li' => 'Warm, woolen mittens' ),
+       ), array('odd', 'even'))
+));
+print $doc->dump_mem();
+?>
diff --git a/php5/attribute3.php b/php5/attribute3.php
new file mode 100644 (file)
index 0000000..f4fa718
--- /dev/null
@@ -0,0 +1,15 @@
+<?php
+require('xml-template.php');
+
+$doc = XML_Template_process_file('../xml/clone.xml', array(
+       'color' => 'blue',
+       '#things' => XML_Template_alternate('li/class', array(
+               array( 'li' => 'Raindrops on roses' ),
+               array( 'li' => 'Whiskers on kittens' ),
+               NULL,
+               array( 'li' => 'Bright copper kettles' ),
+               array( 'li' => 'Warm, woolen mittens' ),
+       ), array('odd', 'even'))
+));
+print $doc->dump_mem();
+?>
diff --git a/php5/attribute4.php b/php5/attribute4.php
new file mode 100644 (file)
index 0000000..b2c39f9
--- /dev/null
@@ -0,0 +1,8 @@
+<?php
+require('xml-template.php');
+
+$doc = XML_Template_process_file('../xml/attribute4.xml', array(
+       '#foo/value' => 'bar',
+));
+print $doc->dump_mem();
+?>
diff --git a/php5/clone.php b/php5/clone.php
new file mode 100644 (file)
index 0000000..2bbd266
--- /dev/null
@@ -0,0 +1,14 @@
+<?php
+require('xml-template.php');
+
+$doc = XML_Template_process_file('../xml/clone.xml', array(
+       'color' => 'blue',
+       '#things' => array(
+               array( 'li' => 'Raindrops on roses' ),
+               array( 'li' => 'Whiskers on kittens' ),
+               array( 'li' => 'Bright copper kettles' ),
+               array( 'li' => 'Warm, woolen mittens' ) 
+       )
+));
+print $doc->dump_mem();
+?>
diff --git a/php5/include.php b/php5/include.php
new file mode 100644 (file)
index 0000000..8819ef7
--- /dev/null
@@ -0,0 +1,13 @@
+<?php
+require('xml-template.php');
+
+$doc = XML_Template_process_file('../xml/included.xml', array(
+       'color' => 'red'
+), 0);
+$master = XML_Template_process_file('../xml/master.xml', array(
+       'title' => 'Main HTML title',
+       'h1' => 'Nice heading here',
+       'contents' => $doc
+));
+print $master->dump_mem();
+?>
diff --git a/php5/namespace.php b/php5/namespace.php
new file mode 100644 (file)
index 0000000..35bb707
--- /dev/null
@@ -0,0 +1,12 @@
+<?php
+require('xml-template.php');
+
+$doc = XML_Template_process_file('../xml/namespace.xml', array( 
+       'title' => 'Namespace tests',
+       '#hello' => 'Hello world!',
+       '#test' => 'Replaced.',
+       'tagname' => 'foo',
+       '#moretest' => 'bar'
+));
+print $doc->dump_mem();
+?>
diff --git a/php5/namespace2.php b/php5/namespace2.php
new file mode 100644 (file)
index 0000000..6ff09fd
--- /dev/null
@@ -0,0 +1,9 @@
+<?php
+require('xml-template.php');
+
+$doc = XML_Template_process_file('../xml/namespace2.xml', array( 
+       'title' => 'Namespace tests',
+       '#hello' => 'Replaced.',
+));
+print $doc->dump_mem();
+?>
diff --git a/php5/passthru.php b/php5/passthru.php
new file mode 100644 (file)
index 0000000..95dd1be
--- /dev/null
@@ -0,0 +1,6 @@
+<?php
+require('xml-template.php');
+
+$doc = XML_Template_process_file('../xml/passthru.xml', array()); 
+print $doc->dump_mem();
+?>
diff --git a/php5/simple.php b/php5/simple.php
new file mode 100644 (file)
index 0000000..10e73f1
--- /dev/null
@@ -0,0 +1,9 @@
+<?php
+require('xml-template.php');
+
+$doc = XML_Template_process_file('../xml/simple.xml', array( 
+       'title' => 'A very basic example',
+       '#hello' => 'Hello world!'
+));
+print $doc->dump_mem();
+?>
diff --git a/php5/structure.php b/php5/structure.php
new file mode 100644 (file)
index 0000000..adb153e
--- /dev/null
@@ -0,0 +1,12 @@
+<?php
+require('xml-template.php');
+
+$doc = XML_Template_process_file('../xml/structure.xml', array(
+       '#outer' => array(
+               array( '#inner' => 'One' ),
+               array( '#inner' => 'Two' ),
+               array( '#inner' => 'Three' ),
+       )
+));
+print $doc->dump_mem();
+?>
diff --git a/php5/xml-template.php b/php5/xml-template.php
new file mode 100644 (file)
index 0000000..6fbf4bf
--- /dev/null
@@ -0,0 +1,189 @@
+<?php
+
+function XML_Template_process_file($filename, $obj, $clean = 1)
+{
+       $doc = domxml_open_file($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, 'domdocument')) {
+                       $obj = $obj->document_element();
+               }
+
+               $newobj = own_clone_node($obj, $node->owner_document());
+               $node->append_child($newobj);
+
+               XML_Template_process($newobj, array(), $clean);
+       } else if (!is_array($obj)) {                         # overwrite
+               foreach ($node->child_nodes() as $child) {
+                       $node->remove_child($child);
+               }
+               $doc = $node->owner_document();
+               $node->add_child($doc->create_text_node($obj));
+       } else if (is_associative_array($obj)) {              # substitute
+               foreach ($node->child_nodes() as $child) {
+                       $processed = false;
+
+                       if ($child->node_type() == XML_ELEMENT_NODE) {
+                               unset($id);
+
+                               $tag = $child->node_name();
+                               $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();
+                                                       if ($clean) {
+                                                               $attr->unlink_node();
+                                                       }
+                                               }
+                                       }
+                               }
+                       
+                               # 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->set_attribute($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->owner_document();
+               $frag = $doc->create_element("temporary-fragment");    # ugh
+
+               foreach ($node->child_nodes() as $child) {
+                       $frag->append_child($child);
+                       $node->remove_child($child);
+               }
+
+               foreach ($obj as $instance) {
+                       if (!isset($instance)) {
+                               continue;
+                       }
+
+                       $newnode = own_clone_node($frag, $frag->owner_document());
+                       $node->append_child($newnode);
+                       XML_Template_process($newnode, $instance, $clean);
+                       if ($clean) {
+                               XML_Template_clean($newnode);
+                       }
+               }
+
+               # remove all the <fragment> tags
+
+               foreach ($node->child_nodes() as $child) {
+                       if ($child->name() != 'temporary-fragment') {
+                               continue;
+                       }
+                       foreach ($child->child_nodes() as $child2) {
+                               $node->append_child($child2);
+                       }
+                       $node->remove_child($child);
+               }       
+       }
+
+       if ($clean) {
+               XML_Template_clean($node);
+       }
+}
+
+function XML_Template_clean($node)
+{
+       if ($node->node_type() == XML_ELEMENT_NODE) {
+               if ($node->namespace_uri() != '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);
+               }
+               $parent->remove_child($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 (at least PHP4); 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 (isset($nsuri)) {
+                       $newnode = $doc->create_element_ns($node->namespace_uri(), $node->node_name(), $node->prefix());
+               } else {
+                       $newnode = $doc->create_element($node->node_name());
+               }
+               
+               $attrs = $node->attributes();
+               if (isset($attrs)) {
+                       foreach ($node->attributes() as $attr) {
+                               $attr2 = $doc->create_attribute($attr->name(), $attr->value());
+                               $nsuri = $attr->namespace_uri();
+                               if (isset($nsuri)) {
+                                       $attr2->set_namespace($nsuri, $attr->prefix());
+                               }
+                               $newnode->append_child($attr2);
+                       }
+               }
+               foreach ($node->child_nodes() as $child) {
+                       $newnode->append_child(own_clone_node($child, $doc));
+               }
+               return $newnode;
+       } else {
+               return $node->clone_node(true);
+       }
+}
+
+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);
+}
+?>
index ed601caf60981c8d7734503262e782e3162f3aa1..3b281158f06c1bd37d2266d8ccd5bf8d256d73c9 100755 (executable)
@@ -4,7 +4,7 @@ if [ -z "$TESTS" ]; then
        TESTS="passthru simple clone include attribute attribute2 attribute3 attribute-empty namespace namespace2 structure"
 fi
 if [ -z "$LANGUAGES" ]; then
-       LANGUAGES="perl perl-sax php4 python ruby"
+       LANGUAGES="perl perl-sax php4 php5 python ruby"
 fi
 
 for L in $LANGUAGES; do
@@ -21,6 +21,9 @@ for L in $LANGUAGES; do
                if [ "$L" = "php4" ]; then
                        php4-cgi -q ../php4/$T.php > $TEMPFILE
                fi
+               if [ "$L" = "php5" ]; then
+                       php5-cgi -q ../php5/$T.php > $TEMPFILE
+               fi
                if [ "$L" = "python" ]; then
                        python ../python/$T.py > $TEMPFILE
                fi