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
|
# $Id: 28new_callbacks_multiple.t 540 2004-11-06 21:34:58Z phish $
use Test;
BEGIN { plan tests => 50 }
END { ok(0) unless $loaded }
use XML::LibXML;
use IO::File;
$loaded = 1;
ok(1);
# --------------------------------------------------------------------- #
# multiple tests
# --------------------------------------------------------------------- #
{
my $string = <<EOF;
<x xmlns:xinclude="http://www.w3.org/2001/XInclude">
<xml>test
<xinclude:include href="/example/test2.xml"/>
<xinclude:include href="/libxml/test2.xml"/></xml>
</x>
EOF
my $icb = XML::LibXML::InputCallback->new();
ok($icb);
$icb->register_callbacks( [ \&match_file, \&open_file,
\&read_file, \&close_file ] );
$icb->register_callbacks( [ \&match_hash, \&open_hash,
\&read_hash, \&close_hash ] );
my $parser = XML::LibXML->new();
$parser->expand_xinclude(1);
$parser->input_callbacks($icb);
my $doc = $parser->parse_string($string);
ok($doc);
ok($doc->string_value(), "\ntest\n..\nbar..\n");
}
{
my $string = <<EOF;
<x xmlns:xinclude="http://www.w3.org/2001/XInclude">
<xml>test
<xinclude:include href="/example/test2.xml"/>
<xinclude:include href="/example/test3.xml"/></xml>
</x>
EOF
my $icb = XML::LibXML::InputCallback->new();
$icb->register_callbacks( [ \&match_file, \&open_file,
\&read_file, \&close_file ] );
$icb->register_callbacks( [ \&match_hash2, \&open_hash,
\&read_hash, \&close_hash ] );
my $parser = XML::LibXML->new();
$parser->expand_xinclude(1);
$parser->input_callbacks($icb);
my $doc = $parser->parse_string($string);
ok($doc);
ok($doc->string_value(), "\ntest\nbar..\nbar..\n");
print $doc->serialize();
$icb->unregister_callbacks( [ \&match_hash2, \&open_hash,
\&read_hash, \&close_hash] );
$doc = $parser->parse_string($string);
ok($doc);
ok($doc->string_value(), "\ntest\n..\n\n \n \n");
}
# --------------------------------------------------------------------- #
# CALLBACKS
# --------------------------------------------------------------------- #
# --------------------------------------------------------------------- #
# callback set 1 (perl file reader)
# --------------------------------------------------------------------- #
sub match_file {
my $uri = shift;
if ( $uri =~ /^\/example\// ){
ok(1);
return 1;
}
return 0;
}
sub open_file {
my $uri = shift;
$file = new IO::File;
if ( $file->open( "< .$uri" ) ){
ok(1);
}
else {
# warn "cannot open file";
$file = 0;
}
return $file;
}
sub read_file {
my $h = shift;
my $buflen = shift;
my $rv = undef;
ok(1);
my $n = $h->read( $rv , $buflen );
return $rv;
}
sub close_file {
my $h = shift;
ok(1);
$h->close();
return 1;
}
# --------------------------------------------------------------------- #
# callback set 2 (perl hash reader)
# --------------------------------------------------------------------- #
sub match_hash {
my $uri = shift;
if ( $uri =~ /^\/libxml\// ){
ok(1);
return 1;
}
}
sub open_hash {
my $uri = shift;
my $hash = { line => 0,
lines => [ "<foo>", "bar", "<xsl/>", "..", "</foo>" ],
};
ok(1);
return $hash;
}
sub read_hash {
my $h = shift;
my $buflen = shift;
my $id = $h->{line};
$h->{line} += 1;
my $rv= $h->{lines}->[$id];
$rv = "" unless defined $rv;
ok(1);
return $rv;
}
sub close_hash {
my $h = shift;
undef $h;
ok(1);
}
# --------------------------------------------------------------------- #
# callback set 3 (perl hash reader)
# --------------------------------------------------------------------- #
sub match_hash2 {
my $uri = shift;
if ( $uri =~ /^\/example\// ){
ok(1);
return 1;
}
}
|