7 package XML::TemplateSAX::Handler;
8 use base qw(XML::SAX::Base);
15 obj => $options{'Content'},
17 Handler => $options{'Handler'}
24 my ($self, $data) = @_;
25 my $obj = $self->{'obj'};
28 my $id = $data->{'Attributes'}->{'{http://template.sesse.net/}id'};
29 $id = $id->{'Value'} if (defined($id));
31 # within a replacement; just ignore everything
32 return if (!defined($obj));
34 # within a cloning; slurp it up
35 if (ref($obj) eq 'XML::TemplateSAX::Buffer') {
36 $obj->start_element($data);
40 # substitution: see if this element matches anything. if so,
41 # descend down into the tree.
42 if (ref($obj) eq 'HASH') {
43 # first of all, see if we have an attribute match.
44 for my $key (keys %$obj) {
45 next unless ($key =~ /^(#?)(.*)\/(.*)$/);
46 my ($idmarker, $name, $attr) = ($1, $2, $3);
48 if (($idmarker eq '#' && $id eq $name) ||
49 ($idmarker ne '#' && $data->{'LocalName'} eq $name)) {
50 $data->{'Attributes'}->{$attr} = {
61 for my $key (keys %$obj) {
62 if ($key =~ /^#(.*)$/) {
63 if (defined($id) && $id eq $1) {
64 $match = $obj->{$key};
68 if ($data->{'LocalName'} eq $key) {
69 $match = $obj->{$key};
75 if (defined($match)) {
76 $self->SUPER::start_element($data);
78 # FIXME: we should match on something better than the name. But what?
79 push @{$self->{'stack'}}, [ $data->{'Name'}, $obj ];
82 # This is sort of ugly. We special-case replacement by outputting
83 # the string immediately, and then just ignoring the rest of the
84 # events until we get to the right end tag. It's not 100% technically
85 # correct for the case where you replace an entire document by a
86 # string, but that's nonsensical anyway.
89 $self->SUPER::characters({ Data => $match });
90 $self->{'obj'} = undef;
95 # Sort of the same, for cloning. Cloning works by gobbling up all the all the
96 # input until the end element, and put it into a buffer. when we get to the end
97 # element, spew it all out again as many times as we need, onto ourselves so we
98 # get filtering etc. right.
100 # We let the buffer object keep the actual array, so we can fetch it out later.
102 if (ref($match) eq 'ARRAY') {
103 $self->{'obj'} = XML::TemplateSAX::Buffer->new($match);
108 # If someone tries to insert a full tree, do it, just like the character
111 if (ref($match) eq 'XML::TemplateSAX::Buffer') {
112 $match->replay($self);
113 $self->{'obj'} = undef;
116 $self->{'obj'} = $match;
122 $self->SUPER::start_element($data);
126 my ($self, $data) = @_;
128 return if (!defined($self->{'obj'}));
130 if (ref($self->{'obj'}) eq 'XML::TemplateSAX::Buffer') {
131 $self->{'obj'}->characters($data);
135 $self->SUPER::characters($data);
139 my ($self, $data) = @_;
141 return if (!defined($self->{'obj'}));
143 if (ref($self->{'obj'}) eq 'XML::TemplateSAX::Buffer') {
144 $self->{'obj'}->comment($data);
148 $self->SUPER::comment($data);
151 sub processing_instruction {
152 my ($self, $data) = @_;
154 return if (!defined($self->{'obj'}));
156 if (ref($self->{'obj'}) eq 'XML::TemplateSAX::Buffer') {
157 $self->{'obj'}->processing_instruction($data);
161 $self->SUPER::processing_instruction($data);
165 my ($self, $data) = @_;
167 my $stack = $self->{'stack'};
168 if (scalar @$stack > 0) {
169 my $top = $stack->[scalar @$stack - 1];
171 if ($data->{'Name'} eq $top->[0]) {
172 my $obj = $self->{'obj'};
174 # did we just finish a clone operation?
175 if (ref($obj) eq 'XML::TemplateSAX::Buffer') {
176 for my $instance (@{$obj->{'ptr'}}) {
177 $self->{'obj'} = $instance;
182 $self->SUPER::end_element($data);
183 $self->{'obj'} = $top->[1];
189 return if (!defined($self->{'obj'}));
191 if (ref($self->{'obj'}) eq 'XML::TemplateSAX::Buffer') {
192 $self->{'obj'}->end_element($data);
196 $self->SUPER::end_element($data);