]> git.sesse.net Git - xml-template/blobdiff - perl-sax/XML/TemplateSAX/Handler.pm
Add deferred parsing for perl-sax. Doesn't work yet, though, so it uses
[xml-template] / perl-sax / XML / TemplateSAX / Handler.pm
index 24371cebe180a5d26e08d3ca0b13fc93cbef7da8..319883bfd6ad883938bbab26d64a42bc5d6352ed 100644 (file)
@@ -1,5 +1,7 @@
 #! /usr/bin/perl
 
+use strict;
+use warnings;
 use Data::Dumper;
 
 package XML::TemplateSAX::Handler;
@@ -12,6 +14,7 @@ sub new {
        my $self = {
                obj => $options{'Content'},
                stack => [],
+               level => 0,
                Handler => $options{'Handler'}
        };
        bless($self, $class);
@@ -22,6 +25,8 @@ sub start_element {
        my ($self, $data) = @_;
        my $obj = $self->{'obj'};
 
+       ++$self->{'level'};
+
        # find the ID, if any
        my $id = $data->{'Attributes'}->{'{http://template.sesse.net/}id'};
        $id = $id->{'Value'} if (defined($id));
@@ -29,9 +34,32 @@ sub start_element {
        # within a replacement; just ignore everything  
        return if (!defined($obj));
 
+       # within a cloning; slurp it up
+       if (ref($obj) eq 'XML::TemplateSAX::Buffer') {
+               $obj->start_element($data);
+               return;
+       }
+
        # substitution: see if this element matches anything. if so,
        # descend down into the tree.
        if (ref($obj) eq 'HASH') {
+               # first of all, see if we have an attribute match.
+               for my $key (keys %$obj) {
+                       next unless ($key =~ /^(#?)(.*)\/(.*)$/);
+                       my ($idmarker, $name, $attr) = ($1, $2, $3);
+
+                       if (($idmarker eq '#' && $id eq $name) ||
+                           ($idmarker ne '#' && $data->{'LocalName'} eq $name)) {
+                               $data->{'Attributes'}->{$attr} = {
+                                       Prefix => '',
+                                       LocalName => $attr,
+                                       Name => $attr,
+                                       NamespaceURI => '',
+                                       Value => $obj->{$key}
+                               };
+                       }
+               }
+
                my $match = undef;
                for my $key (keys %$obj) {
                        if ($key =~ /^#(.*)$/) {
@@ -49,9 +77,9 @@ sub start_element {
 
                if (defined($match)) {
                        $self->SUPER::start_element($data);
+               
+                       push @{$self->{'stack'}}, [ $self->{'level'}, $obj ];
 
-                       push @{$self->{'stack'}}, [ $data->{'Name'}, $obj ];
-                       
                        #
                        # This is sort of ugly. We special-case replacement by outputting
                        # the string immediately, and then just ignoring the rest of the
@@ -61,39 +89,117 @@ sub start_element {
                        #
                        if (!ref($match)) {
                                $self->SUPER::characters({ Data => $match });
-                               $match = undef;
+                               $self->{'obj'} = undef;
+                               return;
                        }
 
-                       $self->{'obj'} = $match;
+                       #
+                       # Sort of the same, for cloning. Cloning works by gobbling up all the all the
+                       # input until the end element, and put it into a buffer. when we get to the end
+                       # element, spew it all out again as many times as we need, onto ourselves so we
+                       # get filtering etc. right.
+                       #
+                       # We let the buffer object keep the actual array, so we can fetch it out later.
+                       #
+                       if (ref($match) eq 'ARRAY') {
+                               $self->{'obj'} = XML::TemplateSAX::Buffer->new($match);
+                               return;
+                       }
+
+                       #
+                       # If someone tries to insert a full tree, do it, just like the character
+                       # replacement above.
+                       #
+                       if (ref($match) eq 'XML::TemplateSAX::Buffer') {
+                               $match->replay($self);
+                               $self->{'obj'} = undef;
+                       } elsif (ref($match) eq 'XML::TemplateSAX::Deferred') {
+                               $match->parse($self);
+                               $self->{'obj'} = undef;
+                       } else {
+                               $self->{'obj'} = $match;
+                       }
                        return;
                }
        }
        
+
        $self->SUPER::start_element($data);
 }
 
 sub characters {
        my ($self, $data) = @_;
+
        return if (!defined($self->{'obj'}));
+       
+       if (ref($self->{'obj'}) eq 'XML::TemplateSAX::Buffer') {
+               $self->{'obj'}->characters($data);
+               return;
+       }
+
        $self->SUPER::characters($data);
 }
 
-sub end_element {
+sub comment {
        my ($self, $data) = @_;
 
+       return if (!defined($self->{'obj'}));
+       
+       if (ref($self->{'obj'}) eq 'XML::TemplateSAX::Buffer') {
+               $self->{'obj'}->comment($data);
+               return;
+       }
+
+       $self->SUPER::comment($data);
+}
+
+sub processing_instruction {
+       my ($self, $data) = @_;
+
+       return if (!defined($self->{'obj'}));
+       
+       if (ref($self->{'obj'}) eq 'XML::TemplateSAX::Buffer') {
+               $self->{'obj'}->processing_instruction($data);
+               return;
+       }
+
+       $self->SUPER::processing_instruction($data);
+}
+
+sub end_element {
+       my ($self, $data) = @_;
+       
        my $stack = $self->{'stack'};
        if (scalar @$stack > 0) {
-               my $top = $stack->[$#stack];
+               my $top = $stack->[scalar @$stack - 1];
                
-               if ($data->{'Name'} eq $top->[0]) {
+               if ($self->{'level'} == $top->[0]) {
+                       my $obj = $self->{'obj'};
+
+                       # did we just finish a clone operation?
+                       if (ref($obj) eq 'XML::TemplateSAX::Buffer') {
+                               for my $instance (@{$obj->{'ptr'}}) {
+                                       $self->{'obj'} = $instance;
+                                       $obj->replay($self);
+                               }
+                       }
+
                        $self->SUPER::end_element($data);
                        $self->{'obj'} = $top->[1];
                        pop @$stack;
+                       --$self->{'level'};
                        return;
                }
        }
        
+       --$self->{'level'};
+       
        return if (!defined($self->{'obj'}));
+       
+       if (ref($self->{'obj'}) eq 'XML::TemplateSAX::Buffer') {
+               $self->{'obj'}->end_element($data);
+               return;
+       }
 
        $self->SUPER::end_element($data);
 }