#!/usr/local/lib/perl -w
my
$ns
= QB->new(
"nsef"
,
<<'XML_END' );
<root
xmlns:foo="foo-ns"
a="A"
foo:a="FOOA"
><e aa1="AA1" foo:aa1="AA2"><f>B1</f><foo:f>B2</foo:f></e></root>
XML_END
my
@out
;
my
@tests
= (
sub
{
@out
= ();
$ns
->playback( XML::Filter::Dispatcher->new(
Namespaces
=> {
goo
=>
"foo-ns"
,
},
Rules
=> [
'root'
=>
sub
{
push
@out
,
"root"
},
'@goo:a'
=>
sub
{
push
@out
,
"foo:a"
},
'goo:f'
=>
sub
{
push
@out
,
"foo:f"
},
'f'
=>
sub
{
push
@out
,
"f"
},
],
) );
ok 1;
},
sub
{ ok
int
@out
, 4 },
sub
{ ok
$out
[0],
"root"
,
"out[0]"
},
sub
{ ok
$out
[1],
"foo:a"
,
"out[1]"
},
sub
{ ok
$out
[2],
"f"
,
"out[2]"
},
sub
{ ok
$out
[3],
"foo:f"
,
"out[3]"
},
sub
{
@out
= ();
$ns
->playback( XML::Filter::Dispatcher->new(
Namespaces
=> {
none
=>
""
,
},
Rules
=> [
'none:*'
=>
sub
{
push
@out
,
$_
[1]->{Name} },
'none:f'
=>
sub
{
push
@out
,
"none:"
.
$_
[1]->{Name} },
],
) );
ok 1;
},
sub
{ ok
int
@out
, 3 },
sub
{ ok
$out
[0],
"root"
,
"out[0]"
},
sub
{ ok
$out
[1],
"e"
,
"out[1]"
},
sub
{ ok
$out
[2],
"none:f"
,
"out[2]"
},
sub
{
@out
= ();
$ns
->playback( XML::Filter::Dispatcher->new(
Namespaces
=> {
none
=>
""
,
},
Rules
=> [
'none:*/@none:*'
=>
sub
{
push
@out
,
$_
[1]->{Name} },
],
) );
ok 1;
},
sub
{ ok
int
@out
, 2 },
sub
{ ok
$out
[0],
"a"
,
"out[0]"
},
sub
{ ok
$out
[1],
"aa1"
,
"out[1]"
},
);
plan
tests
=>
scalar
@tests
;
$_
->()
for
@tests
;
use
vars
qw( $AUTOLOAD )
;
sub
new {
my
$self
=
bless
[],
shift
;
my
(
$name
,
$doc
) =
@_
;
my
$cache_fn
= basename( $0 ) .
".cache.$name"
;
if
( -e
$cache_fn
&& -M
$cache_fn
< -M $0 ) {
my
$old_self
=
do
$cache_fn
;
return
$old_self
if
defined
$old_self
;
warn
"$!$@"
;
unlink
$cache_fn
;
}
my
$p
= XML::SAX::PurePerl->new(
Handler
=>
$self
);
$p
->parse_string(
$doc
);
if
(
open
F,
">$cache_fn"
) {
local
$Data::Dumper::Terse
;
$Data::Dumper::Terse
= 1;
print
F Data::Dumper::Dumper(
$self
);
close
F;
}
return
$self
;
}
sub
DESTROY;
sub
AUTOLOAD {
my
$self
=
shift
;
$AUTOLOAD
=~ s/.*://;
if
(
$AUTOLOAD
eq
"start_element"
) {
push
@$self
, [
$AUTOLOAD
, [ { %{
$_
[0]} } ] ];
}
else
{
push
@$self
, [
$AUTOLOAD
, [
$_
[0] ] ];
}
}
sub
playback {
my
$self
=
shift
;
my
$h
=
shift
;
my
$r
;
for
(
@$self
) {
my
$m
=
$_
->[0];
no
strict
"refs"
;
$r
=
$h
->
$m
( @{
$_
->[1]} )
if
$h
->can(
$m
);
}
return
$r
;
}