You are viewing a plain text version of this content. The canonical link for it is here.
Posted to axkit-dev@xml.apache.org by ma...@sergeant.org on 2006/08/01 06:13:39 UTC

[SVN] [25] Make handle arrayrefs and hashrefs transparently, and support an as-xml attribute which takes the expr as an XML well balanced chunk.

Revision: 25
Author:   matt
Date:     2006-08-01 04:13:07 +0000 (Tue, 01 Aug 2006)

Log Message:
-----------
Make <xsp:expr> handle arrayrefs and hashrefs transparently, and support an as-xml attribute which takes the expr as an XML well balanced chunk.

Modified Paths:
--------------
    trunk/lib/AxKit2/Transformer/XSP.pm

Modified: trunk/lib/AxKit2/Transformer/XSP.pm
===================================================================
--- trunk/lib/AxKit2/Transformer/XSP.pm	2006-08-01 03:12:17 UTC (rev 24)
+++ trunk/lib/AxKit2/Transformer/XSP.pm	2006-08-01 04:13:07 UTC (rev 25)
@@ -205,7 +205,7 @@
                 "package $e->{XSP_Package};",
                 "use AxKit2::Constants;",
                 "use XML::LibXML;",
-                "AxKit2::Transformer::XSP::Page->import( qw(__mk_text_node __mk_comment_node __mk_ns_element_node __mk_element_node) );",
+                "AxKit2::Transformer::XSP::Page->import( qw(__mk_expr_node __mk_text_node __mk_comment_node __mk_ns_element_node __mk_element_node) );",
                 ($] >= 5.008?"use utf8;":""),
                 );
 
@@ -610,15 +610,21 @@
 
         if (AxKit2::Transformer::XSP::is_xsp_namespace($node->{Parent}->{NamespaceURI})) {
             if (!$e->manage_text() || $node->{Parent}->{Name} =~ /^(?:.*:)?(?:content|element)$/) {
-                return '__mk_text_node($document, $parent, "" . do {';
+                return $attribs{'as-xml'}
+                            ? '__mk_expr_node($document, $parent, 1, do {'
+                            : '__mk_expr_node($document, $parent, 0, do {';
             }
             elsif ($node->{Parent}->{Name} =~ /^(.*:)?(logic|expr)$/) {
+                # <xsp:expr> within <xsp:expr>...
                 return 'do {';
             }
+            # <xsp:expr> inside a taglib
             return ' . do {';
         }
         else {
-            return '__mk_text_node($document, $parent, "" . do {';
+            return $attribs{'as-xml'}
+                        ? '__mk_expr_node($document, $parent, 1, do {'
+                        : '__mk_expr_node($document, $parent, 0, do {';
         }
         warn("EEEK - Should never get here!!!");
 #        warn "start Expr: CurrentEl: ", $e->current_element, "\n";
@@ -1010,6 +1016,7 @@
 @AxKit2::Transformer::XSP::Page::ISA = qw(Exporter);
 @AxKit2::Transformer::XSP::Page::EXPORT_OK = 
   qw(
+      __mk_expr_node
       __mk_text_node
       __mk_element_node
       __mk_comment_node
@@ -1039,6 +1046,53 @@
     $parent->appendChild($node);
 }
 
+sub __mk_expr_node {
+    my ($document, $parent, $as_xml, @data) = @_;
+    for my $data (@data) {
+        if ($as_xml) {
+            $parent->appendWellBalancedChunk($data);
+            return;
+        }
+        
+        if (my $ref = ref($data)) {
+            if ($ref eq 'ARRAY') {
+                my $i = 0;
+                for my $item (@$data) {
+                    my $node = $document->createElement('item');
+                    $node->setAttribute('idx' => $i++);
+                    __mk_expr_node($document, $node, $as_xml, $item);
+                    $parent->appendChild($node);
+                }
+            }
+            elsif ($ref eq 'HASH') {
+                for my $k (keys %$data) {
+                    my $item = $data->{$k};
+                    my $node;
+                    if ($k =~ s/^(.*?)://) {
+                        my $prefix = $1;
+                        my $uri = $parent->lookupNamespaceURI($prefix)
+                            || die "No namespace URI for prefix '$prefix'";
+                        $node = $document->createElementNS($uri, $k);
+                    }
+                    else {
+                        $node = $document->createElement($k);
+                    }
+                    __mk_expr_node($document, $node, $as_xml, $item);
+                    $parent->appendChild($node);
+                }
+            }
+            else {
+                die "expr can't yet handle ref type: $ref";
+            }
+        }
+        else {
+            # we stringify here to make sure we don't pass undef in.
+            my $node = $document->createTextNode("$data");
+            $parent->appendChild($node);
+        }
+    }
+}
+
 sub __mk_element_node {
     my ($document, $parent, $name) = @_;
     if ($name =~ s/^(.*?)://) {