]> git.sesse.net Git - xml-template/blob - perl-sax/XML/TemplateSAX/Handler.pm
use strict, use warnins. Glah!
[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                         push @{$self->{'stack'}}, [ $data->{'Name'}, $obj ];
62
63                         #
64                         # This is sort of ugly. We special-case replacement by outputting
65                         # the string immediately, and then just ignoring the rest of the
66                         # events until we get to the right end tag. It's not 100% technically
67                         # correct for the case where you replace an entire document by a
68                         # string, but that's nonsensical anyway.
69                         #
70                         if (!ref($match)) {
71                                 $self->SUPER::characters({ Data => $match });
72                                 $self->{'obj'} = undef;
73                                 return;
74                         }
75
76                         #
77                         # Sort of the same, for cloning. Cloning works by gobbling up all the all the
78                         # input until the end element, and put it into a buffer. when we get to the end
79                         # element, spew it all out again as many times as we need, onto ourselves so we
80                         # get filtering etc. right.
81                         #
82                         # We let the buffer object keep the actual array, so we can fetch it out later.
83                         #
84                         if (ref($match) eq 'ARRAY') {
85                                 $self->{'obj'} = XML::TemplateSAX::Buffer->new($match);
86                                 return;
87                         }
88                         
89                         $self->{'obj'} = $match;
90                         return;
91                 }
92         }
93         
94
95         $self->SUPER::start_element($data);
96 }
97
98 sub characters {
99         my ($self, $data) = @_;
100
101         return if (!defined($self->{'obj'}));
102         
103         if (ref($self->{'obj'}) eq 'XML::TemplateSAX::Buffer') {
104                 $self->{'obj'}->characters($data);
105                 return;
106         }
107
108         $self->SUPER::characters($data);
109 }
110
111 sub comment {
112         my ($self, $data) = @_;
113
114         return if (!defined($self->{'obj'}));
115         
116         if (ref($self->{'obj'}) eq 'XML::TemplateSAX::Buffer') {
117                 $self->{'obj'}->comment($data);
118                 return;
119         }
120
121         $self->SUPER::comment($data);
122 }
123
124 sub processing_instruction {
125         my ($self, $data) = @_;
126
127         return if (!defined($self->{'obj'}));
128         
129         if (ref($self->{'obj'}) eq 'XML::TemplateSAX::Buffer') {
130                 $self->{'obj'}->processing_instruction($data);
131                 return;
132         }
133
134         $self->SUPER::processing_instruction($data);
135 }
136
137 sub end_element {
138         my ($self, $data) = @_;
139
140         my $stack = $self->{'stack'};
141         if (scalar @$stack > 0) {
142                 my $top = $stack->[scalar @$stack - 1];
143                 
144                 if ($data->{'Name'} eq $top->[0]) {
145                         my $obj = $self->{'obj'};
146
147                         # did we just finish a clone operation?
148                         if (ref($obj) eq 'XML::TemplateSAX::Buffer') {
149                                 for my $instance (@{$obj->{'ptr'}}) {
150                                         $self->{'obj'} = $instance;
151                                         $obj->replay($self);
152                                 }
153                         }
154
155                         $self->SUPER::end_element($data);
156                         $self->{'obj'} = $top->[1];
157                         pop @$stack;
158                         return;
159                 }
160         }
161         
162         return if (!defined($self->{'obj'}));
163         
164         if (ref($self->{'obj'}) eq 'XML::TemplateSAX::Buffer') {
165                 $self->{'obj'}->end_element($data);
166                 return;
167         }
168
169         $self->SUPER::end_element($data);
170 }
171
172 1;