From e43f0189bb91124641b89b75da3bd3e612bbab6a Mon Sep 17 00:00:00 2001 From: "sgunderson@bigfoot.com" <> Date: Thu, 1 Mar 2007 12:09:52 +0100 Subject: [PATCH] Split out the TemplateSAX modules into separate files. --- perl-sax/XML/TemplateSAX.pm | 115 +--------------------------- perl-sax/XML/TemplateSAX/Cleaner.pm | 19 +++++ perl-sax/XML/TemplateSAX/Handler.pm | 101 ++++++++++++++++++++++++ 3 files changed, 122 insertions(+), 113 deletions(-) create mode 100644 perl-sax/XML/TemplateSAX/Cleaner.pm create mode 100644 perl-sax/XML/TemplateSAX/Handler.pm diff --git a/perl-sax/XML/TemplateSAX.pm b/perl-sax/XML/TemplateSAX.pm index ff68c1f..65f8d22 100644 --- a/perl-sax/XML/TemplateSAX.pm +++ b/perl-sax/XML/TemplateSAX.pm @@ -17,119 +17,8 @@ use XML::SAX::Expat; use XML::SAX::Writer; -use Data::Dumper; - -package XML::TemplateSAX::Handler; -use base qw(XML::SAX::Base); - -sub new { - my $class = shift; - my %options = @_; - - my $self = { - obj => $options{'Content'}, - stack => [], - Handler => $options{'Handler'} - }; - bless($self, $class); - return $self; -} - -sub start_element { - my ($self, $data) = @_; - my $obj = $self->{'obj'}; - - # find the ID, if any - my $id = $data->{'Attributes'}->{'{http://template.sesse.net/}id'}; - $id = $id->{'Value'} if (defined($id)); - - # within a replacement; just ignore everything - return if (!defined($obj)); - - # substitution: see if this element matches anything. if so, - # descend down into the tree. - if (ref($obj) eq 'HASH') { - my $match = undef; - for my $key (keys %$obj) { - if ($key =~ /^#(.*)$/) { - if (defined($id) && $id eq $1) { - $match = $obj->{$key}; - last; - } - } else { - if ($data->{'LocalName'} eq $key) { - $match = $obj->{$key}; - last; - } - } - } - - if (defined($match)) { - $self->SUPER::start_element($data); - - 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 - # events until we get to the right end tag. It's not 100% technically - # correct for the case where you replace an entire document by a - # string, but that's nonsensical anyway. - # - if (!ref($match)) { - $self->SUPER::characters({ Data => $match }); - $match = undef; - } - - $self->{'obj'} = $match; - return; - } - } - - $self->SUPER::start_element($data); -} - -sub characters { - my ($self, $data) = @_; - return if (!defined($self->{'obj'})); - $self->SUPER::characters($data); -} - -sub end_element { - my ($self, $data) = @_; - - my $stack = $self->{'stack'}; - if (scalar @$stack > 0) { - my $top = $stack->[$#stack]; - - if ($data->{'Name'} eq $top->[0]) { - $self->SUPER::end_element($data); - $self->{'obj'} = $top->[1]; - pop @$stack; - return; - } - } - - return if (!defined($self->{'obj'})); - - $self->SUPER::end_element($data); -} - -package XML::TemplateSAX::Cleaner; -use base qw(XML::SAX::Base); - -sub start_element { - my ($self, $data) = @_; - my $attrs = $data->{'Attributes'}; - - for my $a (keys %$attrs) { - if ($attrs->{$a}->{'NamespaceURI'} eq 'http://template.sesse.net/') { - delete $attrs->{$a}; - } - } - - $self->SUPER::start_element($data); -} +use XML::TemplateSAX::Cleaner; +use XML::TemplateSAX::Handler; package XML::TemplateSAX; diff --git a/perl-sax/XML/TemplateSAX/Cleaner.pm b/perl-sax/XML/TemplateSAX/Cleaner.pm new file mode 100644 index 0000000..92c8da9 --- /dev/null +++ b/perl-sax/XML/TemplateSAX/Cleaner.pm @@ -0,0 +1,19 @@ +#! /usr/bin/perl + +package XML::TemplateSAX::Cleaner; +use base qw(XML::SAX::Base); + +sub start_element { + my ($self, $data) = @_; + my $attrs = $data->{'Attributes'}; + + for my $a (keys %$attrs) { + if ($attrs->{$a}->{'NamespaceURI'} eq 'http://template.sesse.net/') { + delete $attrs->{$a}; + } + } + + $self->SUPER::start_element($data); +} + +1; diff --git a/perl-sax/XML/TemplateSAX/Handler.pm b/perl-sax/XML/TemplateSAX/Handler.pm new file mode 100644 index 0000000..24371ce --- /dev/null +++ b/perl-sax/XML/TemplateSAX/Handler.pm @@ -0,0 +1,101 @@ +#! /usr/bin/perl + +use Data::Dumper; + +package XML::TemplateSAX::Handler; +use base qw(XML::SAX::Base); + +sub new { + my $class = shift; + my %options = @_; + + my $self = { + obj => $options{'Content'}, + stack => [], + Handler => $options{'Handler'} + }; + bless($self, $class); + return $self; +} + +sub start_element { + my ($self, $data) = @_; + my $obj = $self->{'obj'}; + + # find the ID, if any + my $id = $data->{'Attributes'}->{'{http://template.sesse.net/}id'}; + $id = $id->{'Value'} if (defined($id)); + + # within a replacement; just ignore everything + return if (!defined($obj)); + + # substitution: see if this element matches anything. if so, + # descend down into the tree. + if (ref($obj) eq 'HASH') { + my $match = undef; + for my $key (keys %$obj) { + if ($key =~ /^#(.*)$/) { + if (defined($id) && $id eq $1) { + $match = $obj->{$key}; + last; + } + } else { + if ($data->{'LocalName'} eq $key) { + $match = $obj->{$key}; + last; + } + } + } + + if (defined($match)) { + $self->SUPER::start_element($data); + + 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 + # events until we get to the right end tag. It's not 100% technically + # correct for the case where you replace an entire document by a + # string, but that's nonsensical anyway. + # + if (!ref($match)) { + $self->SUPER::characters({ Data => $match }); + $match = undef; + } + + $self->{'obj'} = $match; + return; + } + } + + $self->SUPER::start_element($data); +} + +sub characters { + my ($self, $data) = @_; + return if (!defined($self->{'obj'})); + $self->SUPER::characters($data); +} + +sub end_element { + my ($self, $data) = @_; + + my $stack = $self->{'stack'}; + if (scalar @$stack > 0) { + my $top = $stack->[$#stack]; + + if ($data->{'Name'} eq $top->[0]) { + $self->SUPER::end_element($data); + $self->{'obj'} = $top->[1]; + pop @$stack; + return; + } + } + + return if (!defined($self->{'obj'})); + + $self->SUPER::end_element($data); +} + +1; -- 2.39.2