1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
package Data::DumpXML::Parser;
use strict;
use vars qw($VERSION @ISA);
$VERSION = "1.01";
require XML::Parser;
@ISA=qw(XML::Parser);
sub new
{
my($class, %arg) = @_;
$arg{Style} = "Data::DumpXML::ParseStyle";
$arg{Namespaces} = 1;
return $class->SUPER::new(%arg);
}
package Data::DumpXML::ParseStyle;
use Array::RefElem qw(av_push hv_store);
sub Init
{
my $p = shift;
$p->{dump_data} = [];
push(@{$p->{stack}}, $p->{dump_data});
}
sub Start
{
my($p, $tag, %attr) = @_;
$p->{in_str}++ if $tag eq "str" || $tag eq "key";
my $obj = [\%attr];
push(@{$p->{stack}[-1]}, $obj);
push(@{$p->{stack}}, $obj);
}
sub Char
{
my($p, $str) = @_;
return unless $p->{in_str};
push(@{$p->{stack}[-1]}, $str);
}
sub End
{
my($p, $tag) = @_;
my $obj = pop(@{$p->{stack}});
my $attr = shift(@$obj);
my $ref;
if ($tag eq "str" || $tag eq "key") {
$p->{in_str}--;
my $val = join("", @$obj);
if (my $enc = $attr->{encoding}) {
if ($enc eq "base64") {
require MIME::Base64;
$val = MIME::Base64::decode($val);
}
else {
warn "Unknown encoding '$enc'";
}
}
$ref = \$val;
}
elsif ($tag eq "ref") {
my $val = $obj->[0];
$ref = \$val;
}
elsif ($tag eq "array" || $tag eq "data") {
my @val;
for (@$obj) {
av_push(@val, $$_);
}
$ref = \@val;
}
elsif ($tag eq "hash") {
my %val;
while (@$obj) {
my $keyref = shift @$obj;
my $valref = shift @$obj;
hv_store(%val, $$keyref, $$valref);
}
$ref = \%val;
}
elsif ($tag eq "undef") {
my $val = undef;
$ref = \$val;
}
elsif ($tag eq "alias") {
$ref = $p->{alias}{$attr->{ref}};
}
else {
my $val = "*** $tag ***";
$ref = \$val;
}
$p->{stack}[-1][-1] = $ref;
if (my $class = $attr->{class}) {
if (exists $p->{Blesser}) {
my $blesser = $p->{Blesser};
if (ref($blesser) eq "CODE") {
&$blesser($ref, $class);
}
}
else {
bless $ref, $class;
}
}
if (my $id = $attr->{id}) {
$p->{alias}->{$id} = $ref;
}
}
sub Final
{
my $p = shift;
my $data = $p->{dump_data}[0];
return $data;
}
1;
__END__
=head1 NAME
Data::DumpXML::Parser - Restore data dumped by Data::DumpXML
=head1 SYNOPSIS
use Data::DumpXML::Parser;
my $p = Data::DumpXML::Parser->new;
my $data = $p->parsefile(shift || "test.xml");
=head1 DESCRIPTION
C<Data::DumpXML::Parser> is an C<XML::Parser> subclass that can
recreate the data structure from an XML document produced by
C<Data::DumpXML>. The parserfile() method returns a reference to an
array of the values dumped.
The constructor method new() takes a single additional argument to
that of C<XML::Parser>:
=over
=item Blesser => CODEREF
A subroutine that is invoked to bless restored objects. The
subroutine is invoked with two arguments: a reference to the object,
and a string containing the class name. If not provided, the built-in
C<bless> function is used.
For situations where the input file cannot necessarily be trusted and
blessing arbitrary Classes might give malicious input the ability
to exploit the DESTROY methods of modules used by the code, it is a
good idea to provide a no-op blesser:
my $p = Data::DumpXML::Parser->new(Blesser => sub {});
=back
=head1 SEE ALSO
L<Data::DumpXML>, L<XML::Parser>
=head1 AUTHOR
Copyright 2001 Gisle Aas.
This library is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
=cut
|