| 1 |
#!/usr/bin/perl -w |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
use Test::More; |
| 5 |
|
| 6 |
use XML::LibXML; |
| 7 |
|
| 8 |
BEGIN{ |
| 9 |
if (1000*$] < 5008) { |
| 10 |
plan skip_all => "Reader interface only supported in Perl >= 5.8"; |
| 11 |
exit; |
| 12 |
} elsif (!XML::LibXML::HAVE_READER()) { |
| 13 |
plan skip_all => "Reader not supported in this libxml2 build"; |
| 14 |
exit; |
| 15 |
} else { |
| 16 |
plan tests => 100; |
| 17 |
} |
| 18 |
|
| 19 |
use_ok('XML::LibXML::Reader'); |
| 20 |
}; |
| 21 |
|
| 22 |
my $file = "test/textReader/countries.xml"; |
| 23 |
{ |
| 24 |
my $reader = XML::LibXML::Reader->new(location => $file, {expand_entities => 1}); |
| 25 |
isa_ok($reader, "XML::LibXML::Reader"); |
| 26 |
is($reader->read, 1, "read"); |
| 27 |
is($reader->byteConsumed, 488, "byteConsumed"); |
| 28 |
is($reader->attributeCount, 0, "attributeCount"); |
| 29 |
is($reader->baseURI, $file, "baseURI"); |
| 30 |
is($reader->encoding, 'UTF-8', "encoding"); |
| 31 |
is($reader->localName, 'countries', "localName"); |
| 32 |
is($reader->name, 'countries', "name"); |
| 33 |
is($reader->prefix, undef, "prefix"); |
| 34 |
is($reader->value, undef, "value"); |
| 35 |
is($reader->xmlLang, undef, "xmlLang"); |
| 36 |
is($reader->xmlVersion, '1.0', "xmlVersion"); |
| 37 |
$reader->read; |
| 38 |
$reader->read; |
| 39 |
$reader->read; # skipping to country node |
| 40 |
is($reader->name, 'country', "skipping to country"); |
| 41 |
is($reader->depth, "1", "depth"); |
| 42 |
is($reader->getAttribute("acronym"), "AL", "getAttribute"); |
| 43 |
is($reader->getAttributeNo(0), "AL", "getAttributeNo"); |
| 44 |
is($reader->getAttributeNs("acronym", undef), "AL", "getAttributeNs"); |
| 45 |
is($reader->lineNumber, "20", "lineNumber"); |
| 46 |
is($reader->columnNumber, "1", "columnNumber"); |
| 47 |
ok($reader->hasAttributes, "hasAttributes"); |
| 48 |
ok(! $reader->hasValue, "hasValue"); |
| 49 |
ok(! $reader->isDefault, "isDefault"); |
| 50 |
ok(! $reader->isEmptyElement, "isEmptyElement"); |
| 51 |
ok(! $reader->isNamespaceDecl, "isNamespaceDecl"); |
| 52 |
ok(! $reader->isValid, "isValid"); |
| 53 |
is($reader->localName, "country", "localName"); |
| 54 |
is($reader->lookupNamespace(undef), undef, "lookupNamespace"); |
| 55 |
|
| 56 |
ok($reader->moveToAttribute("acronym"), "moveToAttribute"); |
| 57 |
ok($reader->moveToAttributeNo(0), "moveToAttributeNo"); |
| 58 |
ok($reader->moveToAttributeNs("acronym", undef), "moveToAttributeNs"); |
| 59 |
|
| 60 |
ok($reader->moveToElement, "moveToElement"); |
| 61 |
ok($reader->moveToFirstAttribute, "moveToFirstAttribute"); |
| 62 |
ok($reader->moveToNextAttribute, "moveToNextAttribute"); |
| 63 |
ok($reader->readAttributeValue, "attributeValue"); |
| 64 |
|
| 65 |
$reader->moveToElement; |
| 66 |
is($reader->name, "country", "name"); |
| 67 |
is($reader->namespaceURI, undef, "namespaceURI"); |
| 68 |
|
| 69 |
ok($reader->nextSibling, "nextSibling"); |
| 70 |
is($reader->nodeType, XML_READER_TYPE_SIGNIFICANT_WHITESPACE, "nodeType"); |
| 71 |
is($reader->prefix, undef, "prefix"); |
| 72 |
|
| 73 |
is($reader->readInnerXml, "", "readInnerXml"); |
| 74 |
is($reader->readOuterXml, "\n", "readOuterXml"); |
| 75 |
ok($reader->readState, "readState"); |
| 76 |
|
| 77 |
is($reader->getParserProp('expand_entities'), 1, "getParserProp"); |
| 78 |
|
| 79 |
ok($reader->standalone, "standalone"); |
| 80 |
is($reader->value, "\n", "value"); |
| 81 |
is($reader->xmlLang, undef, "xmlLang"); |
| 82 |
|
| 83 |
|
| 84 |
ok($reader->close, "close"); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
# FD interface |
| 89 |
for my $how (qw(FD IO)) { |
| 90 |
# my $fd; |
| 91 |
open my $fd, '<', $file or die "cannot open $file: $!\n"; |
| 92 |
my $reader = XML::LibXML::Reader->new($how => $fd, URI => $file); |
| 93 |
isa_ok($reader, "XML::LibXML::Reader"); |
| 94 |
$reader->read; |
| 95 |
$reader->read; |
| 96 |
is($reader->name, "countries","name in fd"); |
| 97 |
$reader->read; |
| 98 |
$reader->read; |
| 99 |
$reader->read; |
| 100 |
close $fd; |
| 101 |
} |
| 102 |
|
| 103 |
# scalar interface |
| 104 |
{ |
| 105 |
open my $fd, '<', $file or die "cannot open $file: $!\n"; |
| 106 |
my $doc; |
| 107 |
{ |
| 108 |
local $/; |
| 109 |
$doc = <$fd>; |
| 110 |
} |
| 111 |
close $fd; |
| 112 |
my $reader = XML::LibXML::Reader->new(string => $doc, URI => $file); |
| 113 |
isa_ok($reader, "XML::LibXML::Reader"); |
| 114 |
$reader->read; |
| 115 |
$reader->read; |
| 116 |
is($reader->name, "countries","name in string"); |
| 117 |
} |
| 118 |
|
| 119 |
# DOM |
| 120 |
{ |
| 121 |
my $DOM = XML::LibXML->new->parse_file($file); |
| 122 |
my $reader = XML::LibXML::Reader->new(DOM => $DOM); |
| 123 |
isa_ok($reader, "XML::LibXML::Reader"); |
| 124 |
$reader->read; |
| 125 |
$reader->read; |
| 126 |
is($reader->name, "countries","name in string"); |
| 127 |
ok($reader->document,"document"); |
| 128 |
ok($reader->document->isSameNode($DOM),"document is DOM"); |
| 129 |
} |
| 130 |
|
| 131 |
# Expand |
| 132 |
{ |
| 133 |
my ($node1,$node2, $node3); |
| 134 |
my $xml = <<'EOF'; |
| 135 |
<root> |
| 136 |
<AA foo="FOO"> text1 <inner/> </AA> |
| 137 |
<DD/><BB bar="BAR">text2<CC> xx </CC>foo<FF/> </BB>x |
| 138 |
<EE baz="BAZ"> xx <PP>preserved</PP> yy <XX>FOO</XX></EE> |
| 139 |
<a/> |
| 140 |
<b/> |
| 141 |
<x:ZZ xmlns:x="foo"/> |
| 142 |
<QQ/> |
| 143 |
<YY/> |
| 144 |
</root> |
| 145 |
EOF |
| 146 |
{ |
| 147 |
my $reader = XML::LibXML::Reader->new(string => $xml); |
| 148 |
$reader->preservePattern('//PP'); |
| 149 |
$reader->preservePattern('//x:ZZ',{ x => "foo"}); |
| 150 |
|
| 151 |
isa_ok($reader, "XML::LibXML::Reader"); |
| 152 |
$reader->nextElement; |
| 153 |
is($reader->name, "root","root node"); |
| 154 |
$reader->nextElement; |
| 155 |
$node1 = $reader->copyCurrentNode(1); |
| 156 |
is($node1->nodeName, "AA","deep copy node"); |
| 157 |
$reader->next; |
| 158 |
ok($reader->nextElement("DD"),"next named element"); |
| 159 |
is($reader->name, "DD","name"); |
| 160 |
is($reader->readOuterXml, "<DD/>","readOuterXml"); |
| 161 |
ok($reader->read,"read"); |
| 162 |
is($reader->name, "BB","name"); |
| 163 |
$node2 = $reader->copyCurrentNode(0); |
| 164 |
is($node2->nodeName, "BB","shallow copy node"); |
| 165 |
$reader->nextElement; |
| 166 |
is($reader->name, "CC","nextElement"); |
| 167 |
$reader->nextSibling; |
| 168 |
is( $reader->nodeType(), XML_READER_TYPE_TEXT, "text node" ); |
| 169 |
is( $reader->value,"foo", "text content" ); |
| 170 |
$reader->skipSiblings; |
| 171 |
is( $reader->nodeType(), XML_READER_TYPE_END_ELEMENT, "end element type" ); |
| 172 |
$reader->nextElement; |
| 173 |
is($reader->name, "EE","name"); |
| 174 |
ok($reader->nextSiblingElement("ZZ","foo"),"namespace"); |
| 175 |
is($reader->namespaceURI, "foo","namespaceURI"); |
| 176 |
$reader->nextElement; |
| 177 |
$node3= $reader->preserveNode; |
| 178 |
is( $reader->readOuterXml(), $node3->toString(),"outer xml"); |
| 179 |
ok($node3,"preserve node"); |
| 180 |
$reader->finish; |
| 181 |
my $doc = $reader->document; |
| 182 |
ok($doc, "document"); |
| 183 |
ok($doc->documentElement, "doc root element"); |
| 184 |
is($doc->documentElement->toString,q(<root><EE baz="BAZ"><PP>preserved</PP></EE><x:ZZ xmlns:x="foo"/><QQ/></root>), |
| 185 |
"preserved content"); |
| 186 |
} |
| 187 |
ok($node1->hasChildNodes,"copy w/ child nodes"); |
| 188 |
ok($node1->toString(),q(<AA foo="FOO"> text1 <inner/> </AA>)); |
| 189 |
ok(!defined $node2->firstChild, "copy w/o child nodes"); |
| 190 |
ok($node2->toString(),q(<BB bar="BAR"/>)); |
| 191 |
ok($node3->toString(),q(<QQ/>)); |
| 192 |
} |
| 193 |
|
| 194 |
{ |
| 195 |
my $bad_xml = <<'EOF'; |
| 196 |
<root> |
| 197 |
<foo/> |
| 198 |
<x> |
| 199 |
foo |
| 200 |
</u> |
| 201 |
<x> |
| 202 |
foo |
| 203 |
</x> |
| 204 |
</root> |
| 205 |
EOF |
| 206 |
my $reader = XML::LibXML::Reader->new( |
| 207 |
string => $bad_xml, |
| 208 |
URI => "mystring.xml" |
| 209 |
); |
| 210 |
eval { $reader->finish }; |
| 211 |
my $Err = $@; |
| 212 |
use Data::Dumper; |
| 213 |
# print Dumper($Err); |
| 214 |
# print $Err; |
| 215 |
ok((defined($Err) and $Err =~ /in mystring.xml at line 3:|mystring.xml:5:/), |
| 216 |
'caught the error'); |
| 217 |
} |
| 218 |
|
| 219 |
{ |
| 220 |
my $rng = "test/relaxng/demo.rng"; |
| 221 |
for my $RNG ($rng, XML::LibXML::RelaxNG->new(location => $rng)) { |
| 222 |
{ |
| 223 |
my $reader = XML::LibXML::Reader->new( |
| 224 |
location => "test/relaxng/demo.xml", |
| 225 |
RelaxNG => $RNG, |
| 226 |
); |
| 227 |
ok($reader->finish, "validate using ".(ref($RNG) ? 'XML::LibXML::RelaxNG' : 'RelaxNG file')); |
| 228 |
} |
| 229 |
{ |
| 230 |
my $reader = XML::LibXML::Reader->new( |
| 231 |
location => "test/relaxng/invaliddemo.xml", |
| 232 |
RelaxNG => $RNG, |
| 233 |
); |
| 234 |
eval { $reader->finish }; |
| 235 |
print $@; |
| 236 |
ok($@, "catch validation error for a ".(ref($RNG) ? 'XML::LibXML::RelaxNG' : 'RelaxNG file')); |
| 237 |
} |
| 238 |
|
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
SKIP: { |
| 243 |
if ((!XML::LibXML::HAVE_SCHEMAS) |
| 244 |
or (XML::LibXML::LIBXML_DOTTED_VERSION eq '2.9.4') |
| 245 |
) |
| 246 |
{ |
| 247 |
skip "https://github.com/shlomif/libxml2-2.9.4-reader-schema-regression", 4; |
| 248 |
} |
| 249 |
my $xsd = "test/schema/schema.xsd"; |
| 250 |
for my $XSD ($xsd, XML::LibXML::Schema->new(location => $xsd)) { |
| 251 |
{ |
| 252 |
my $reader = XML::LibXML::Reader->new( |
| 253 |
location => "test/schema/demo.xml", |
| 254 |
Schema => $XSD, |
| 255 |
); |
| 256 |
ok($reader->finish, "validate using ".(ref($XSD) ? 'XML::LibXML::Schema' : 'Schema file')); |
| 257 |
} |
| 258 |
{ |
| 259 |
my $reader = XML::LibXML::Reader->new( |
| 260 |
location => "test/schema/invaliddemo.xml", |
| 261 |
Schema => $XSD, |
| 262 |
); |
| 263 |
eval { $reader->finish }; |
| 264 |
ok($@, "catch validation error for ".(ref($XSD) ? 'XML::LibXML::Schema' : 'Schema file')); |
| 265 |
} |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
# Patterns |
| 270 |
{ |
| 271 |
my ($node1,$node2, $node3); |
| 272 |
my $xml = <<'EOF'; |
| 273 |
<root> |
| 274 |
<AA foo="FOO"> text1 <inner/> </AA> |
| 275 |
<DD/><BB bar="BAR">text2<CC> xx </CC>foo<FF/> </BB>x |
| 276 |
<EE baz="BAZ"> xx <PP>preserved</PP> yy <XX>FOO</XX></EE> |
| 277 |
<a/> |
| 278 |
<b/> |
| 279 |
<x:ZZ xmlns:x="foo"/> |
| 280 |
<QQ/> |
| 281 |
<YY/> |
| 282 |
</root> |
| 283 |
EOF |
| 284 |
my $pattern = XML::LibXML::Pattern->new('//inner|CC|/root/y:ZZ',{y=>'foo'}); |
| 285 |
ok($pattern); |
| 286 |
{ |
| 287 |
my $reader = XML::LibXML::Reader->new(string => $xml); |
| 288 |
ok($reader); |
| 289 |
my $matches=''; |
| 290 |
while ($reader->read) { |
| 291 |
if ($reader->matchesPattern($pattern)) { |
| 292 |
$matches.=$reader->nodePath.','; |
| 293 |
} |
| 294 |
} |
| 295 |
ok($matches,'/root/AA/inner,/root/BB/CC,/root/*,'); |
| 296 |
} |
| 297 |
{ |
| 298 |
my $reader = XML::LibXML::Reader->new(string => $xml); |
| 299 |
ok($reader); |
| 300 |
my $matches=''; |
| 301 |
while ($reader->nextPatternMatch($pattern)) { |
| 302 |
$matches.=$reader->nodePath.','; |
| 303 |
} |
| 304 |
ok($matches,'/root/AA/inner,/root/BB/CC,/root/*,'); |
| 305 |
} |
| 306 |
{ |
| 307 |
my $dom = XML::LibXML->new->parse_string($xml); |
| 308 |
ok($dom); |
| 309 |
my $matches=''; |
| 310 |
for my $node ($dom->findnodes('//node()|@*')) { |
| 311 |
if ($pattern->matchesNode($node)) { |
| 312 |
$matches.=$node->nodePath.','; |
| 313 |
} |
| 314 |
} |
| 315 |
ok($matches,'/root/AA/inner,/root/BB/CC,/root/*,'); |
| 316 |
} |
| 317 |
} |
| 318 |
|