]> git.sesse.net Git - xml-template/blob - perl-sax/XML/TemplateSAX/Handler.pm
eb10ac6b3e81b58bcf24d33a2e9007514ec667f1
[xml-template] / perl-sax / XML / TemplateSAX / Handler.pm
1 #! /usr/bin/perl
2
3 use strict;
4 use warnings;
5 use Data::Dumper;
6
7 package XML::TemplateSAX::Handler;
8 use base qw(XML::SAX::Base);
9
10 sub new {
11         my $class = shift;
12         my %options = @_;
13
14         my $self = {
15                 obj => $options{'Content'},
16                 stack => [],
17                 Handler => $options{'Handler'}
18         };
19         bless($self, $class);
20         return $self;
21 }
22
23 sub start_element {
24         my ($self, $data) = @_;
25         my $obj = $self->{'obj'};
26
27         # find the ID, if any
28         my $id = $data->{'Attributes'}->{'{http://template.sesse.net/}id'};
29         $id = $id->{'Value'} if (defined($id));
30
31         # within a replacement; just ignore everything  
32         return if (!defined($obj));
33
34         # within a cloning; slurp it up
35         if (ref($obj) eq 'XML::TemplateSAX::Buffer') {
36                 $obj->start_element($data);
37                 return;
38         }
39
40         # substitution: see if this element matches anything. if so,
41         # descend down into the tree.
42         if (ref($obj) eq 'HASH') {
43                 my $match = undef;
44                 for my $key (keys %$obj) {
45                         if ($key =~ /^#(.*)$/) {
46                                 if (defined($id) && $id eq $1) {
47                                         $match = $obj->{$key};
48                                         last;
49                                 }
50                         } else {
51                                 if ($data->{'LocalName'} eq $key) {
52                                         $match = $obj->{$key};
53                                         last;
54                                 }
55                         }
56                 }
57
58                 if (defined($match)) {
59                         $self->SUPER::start_element($data);
60                 
61                         # FIXME: we should match on something better than the name. But what?
62                         push @{$self->{'stack'}}, [ $data->{'Name'}, $obj ];
63
64                         #
65                         # This is sort of ugly. We special-case replacement by outputting
66                         # the string immediately, and then just ignoring the rest of the
67                         # events until we get to the right end tag. It's not 100% technically
68                         # correct for the case where you replace an entire document by a
69                         # string, but that's nonsensical anyway.
70                         #
71                         if (!ref($match)) {
72                                 $self->SUPER::characters({ Data => $match });
73                                 $self->{'obj'} = undef;
74                                 return;
75                         }
76
77                         #
78                         # Sort of the same, for cloning. Cloning works by gobbling up all the all the
79                         # input until the end element, and put it into a buffer. when we get to the end
80                         # element, spew it all out again as many times as we need, onto ourselves so we
81                         # get filtering etc. right.
82                         #
83                         # We let the buffer object keep the actual array, so we can fetch it out later.
84                         #
85                         if (ref($match) eq 'ARRAY') {
86                                 $self->{'obj'} = XML::TemplateSAX::Buffer->new($match);
87                                 return;
88                         }
89                         
90                         $self->{'obj'} = $match;
91                         return;
92                 }
93         }
94         
95
96         $self->SUPER::start_element($data);
97 }
98
99 sub characters {
100         my ($self, $data) = @_;
101
102         return if (!defined($self->{'obj'}));
103         
104         if (ref($self->{'obj'}) eq 'XML::TemplateSAX::Buffer') {
105                 $self->{'obj'}->characters($data);
106                 return;
107         }
108
109         $self->SUPER::characters($data);
110 }
111
112 sub comment {
113         my ($self, $data) = @_;
114
115         return if (!defined($self->{'obj'}));
116         
117         if (ref($self->{'obj'}) eq 'XML::TemplateSAX::Buffer') {
118                 $self->{'obj'}->comment($data);
119                 return;
120         }
121
122         $self->SUPER::comment($data);
123 }
124
125 sub processing_instruction {
126         my ($self, $data) = @_;
127
128         return if (!defined($self->{'obj'}));
129         
130         if (ref($self->{'obj'}) eq 'XML::TemplateSAX::Buffer') {
131                 $self->{'obj'}->processing_instruction($data);
132                 return;
133         }
134
135         $self->SUPER::processing_instruction($data);
136 }
137
138 sub end_element {
139         my ($self, $data) = @_;
140
141         my $stack = $self->{'stack'};
142         if (scalar @$stack > 0) {
143                 my $top = $stack->[scalar @$stack - 1];
144                 
145                 if ($data->{'Name'} eq $top->[0]) {
146                         my $obj = $self->{'obj'};
147
148                         # did we just finish a clone operation?
149                         if (ref($obj) eq 'XML::TemplateSAX::Buffer') {
150                                 for my $instance (@{$obj->{'ptr'}}) {
151                                         $self->{'obj'} = $instance;
152                                         $obj->replay($self);
153                                 }
154                         }
155
156                         $self->SUPER::end_element($data);
157                         $self->{'obj'} = $top->[1];
158                         pop @$stack;
159                         return;
160                 }
161         }
162         
163         return if (!defined($self->{'obj'}));
164         
165         if (ref($self->{'obj'}) eq 'XML::TemplateSAX::Buffer') {
166                 $self->{'obj'}->end_element($data);
167                 return;
168         }
169
170         $self->SUPER::end_element($data);
171 }
172
173 1;