| 1 |
#!/usr/bin/perl -w |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
use Test::More tests => 1; |
| 7 |
|
| 8 |
use lib './t/lib'; |
| 9 |
use TestHelpers qw(eq_or_diff); |
| 10 |
|
| 11 |
BEGIN { |
| 12 |
$XML::SAX::ParserPackage = "XML::LibXML::SAX"; |
| 13 |
} |
| 14 |
|
| 15 |
use XML::SAX::ParserFactory; |
| 16 |
|
| 17 |
my @got_warnings; |
| 18 |
local $SIG{__WARN__} = sub { |
| 19 |
my ($warning) = @_; |
| 20 |
|
| 21 |
if ($warning =~ /\AUse of uninitialized value/) |
| 22 |
{ |
| 23 |
push @got_warnings, $warning; |
| 24 |
} |
| 25 |
}; |
| 26 |
|
| 27 |
my $metadataHandler = innerSAX->new(); |
| 28 |
my $oaiHandler = outerSAX->new(metadataHandler => $metadataHandler, |
| 29 |
oaiNS => "http://www.openarchives.org/OAI/2.0/"); |
| 30 |
|
| 31 |
my $parser = XML::SAX::ParserFactory->parser(Handler => $oaiHandler); |
| 32 |
|
| 33 |
$parser->parse_string(<<'END_OF_XML'); |
| 34 |
<?xml version="1.0" encoding="UTF-8"?> |
| 35 |
<OAI-PMH xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd" xmlns="http://www.openarchives.org/OAI/2.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><responseDate>2013-12-16T20:19:20Z</responseDate><request metadataPrefix="RDFxml" verb="GetRecord" identifier="oai:dnb.de/authorities/1045601462">http://services.d-nb.de/oai/repository</request><GetRecord xsi:schemaLocation="http://www.w3.org/1999/02/22-rdf-syntax-ns http://www.w3.org/2000/07/rdf.xsd"><record><header><identifier>oai:dnb.de/authorities/1045601462</identifier><datestamp>2013-12-16T18:47:23Z</datestamp><setSpec>authorities</setSpec></header><metadata><rdf:RDF xmlns:gnd="http://d-nb.info/standards/elementset/gnd#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rda="http://rdvocab.info/" xmlns:foaf="http://xmlns.com/foaf/0.1/" xmlns:isbd="http://iflastandards.info/ns/isbd/elements/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xmlns:marcRole="http://id.loc.gov/vocabulary/relators/" xmlns:lib="http://purl.org/library/" xmlns:umbel="http://umbel.org/umbel#" xmlns:bibo="http://purl.org/ontology/bibo/" xmlns:owl="http://www.w3.org/2002/07/owl#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:skos="http://www.w3.org/2004/02/skos/core#"> |
| 36 |
|
| 37 |
<rdf:Description rdf:about="http://d-nb.info/gnd/1045601462"> |
| 38 |
<rdf:type rdf:resource="http://d-nb.info/standards/elementset/gnd#DifferentiatedPerson"/> |
| 39 |
<gnd:gndIdentifier>1045601462</gnd:gndIdentifier> |
| 40 |
<gnd:preferredNameForThePerson>Kencena, Rain</gnd:preferredNameForThePerson> |
| 41 |
<gnd:preferredNameEntityForThePerson rdf:parseType="Resource"> |
| 42 |
<gnd:forename>Rain</gnd:forename> |
| 43 |
<gnd:surname>Kencena</gnd:surname> |
| 44 |
</gnd:preferredNameEntityForThePerson> |
| 45 |
<gnd:professionOrOccupation rdf:resource="http://d-nb.info/gnd/4281949-0"/> |
| 46 |
<gnd:geographicAreaCode rdf:resource="http://d-nb.info/standards/vocab/gnd/geographic-area-code#XB-TR"/> |
| 47 |
<gnd:dateOfBirthAndDeath>ca. 20. / 21. Jh.</gnd:dateOfBirthAndDeath> |
| 48 |
<gnd:gender rdf:resource="http://d-nb.info/standards/vocab/gnd/Gender#notKnown"/> |
| 49 |
</rdf:Description> |
| 50 |
</rdf:RDF></metadata></record></GetRecord></OAI-PMH> |
| 51 |
END_OF_XML |
| 52 |
|
| 53 |
eq_or_diff( |
| 54 |
\@got_warnings, |
| 55 |
[], |
| 56 |
"No warnings were generated.", |
| 57 |
); |
| 58 |
|
| 59 |
|
| 60 |
package outerSAX; |
| 61 |
use parent qw(XML::SAX::Base); |
| 62 |
|
| 63 |
sub new { |
| 64 |
my ($class, %opts) = @_; |
| 65 |
my $self = bless \%opts, ref($class) || $class; |
| 66 |
$self->set_handler( undef ); |
| 67 |
return $self; |
| 68 |
} |
| 69 |
|
| 70 |
sub start_element { |
| 71 |
my ($self, $element) = @_; |
| 72 |
|
| 73 |
return $self->SUPER::start_element($element) unless $element->{NamespaceURI} eq $self->{oaiNS}; |
| 74 |
|
| 75 |
if ( $element->{LocalName} eq 'metadata' ) { |
| 76 |
$self->{ OLD_Handler } = $self->get_handler(); |
| 77 |
$self->set_handler( $self->{metadataHandler} ); |
| 78 |
} |
| 79 |
else { |
| 80 |
return $self->SUPER::start_element($element)}; |
| 81 |
} |
| 82 |
|
| 83 |
sub end_element { |
| 84 |
my ($self, $element) = @_; |
| 85 |
|
| 86 |
return $self->SUPER::end_element($element) unless $element->{NamespaceURI} eq $self->{oaiNS}; |
| 87 |
|
| 88 |
if ( $element->{LocalName} eq 'metadata' ) { |
| 89 |
$self->set_handler( $self->{OLD_Handler} ); |
| 90 |
} |
| 91 |
else { |
| 92 |
$self->SUPER::end_element($element); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
package innerSAX; |
| 98 |
use parent qw(XML::SAX::Base); |
| 99 |
use XML::LibXML::SAX::Builder; |
| 100 |
|
| 101 |
sub new { |
| 102 |
my ($class, %opts) = @_; |
| 103 |
my $self = bless \%opts, ref($class) || $class; |
| 104 |
$self->{'tagStack'} = []; |
| 105 |
return $self; |
| 106 |
} |
| 107 |
|
| 108 |
sub start_element { |
| 109 |
my ($self, $element) = @_; |
| 110 |
|
| 111 |
unless ( $self->{'tagStack'}[0] ) { |
| 112 |
my $builder = XML::LibXML::SAX::Builder->new() |
| 113 |
or die "cannot instantiate SAX builder"; |
| 114 |
$self->set_handler($builder); |
| 115 |
$self->SUPER::start_document(); # i.e. $builder->start_document(); |
| 116 |
# DEBUG ME: warnings occur here |
| 117 |
$self->SUPER::start_element($element); |
| 118 |
} |
| 119 |
else { |
| 120 |
$self->SUPER::start_element($element)}; |
| 121 |
|
| 122 |
push(@{$self->{'tagStack'}}, $element->{Name}); |
| 123 |
} |
| 124 |
|
| 125 |
sub end_element { |
| 126 |
my ($self, $element) = @_; |
| 127 |
$self->SUPER::end_element($element); |
| 128 |
pop (@{$self->{'tagStack'}}); |
| 129 |
|
| 130 |
unless ( $self->{'tagStack'}[0] ) { |
| 131 |
my $hdl = $self->get_handler(); |
| 132 |
$self->set_handler(undef); |
| 133 |
|
| 134 |
# Convert fragment to document, do something with it |
| 135 |
# (in real life: XSLT) |
| 136 |
my $fragment = $hdl->done(); |
| 137 |
my $child = $fragment->firstChild(); |
| 138 |
while ($child && $child->nodeName eq "#text") |
| 139 |
{ |
| 140 |
$child = $child->nextSibling; |
| 141 |
} |
| 142 |
my $tempdoc = XML::LibXML::Document->createDocument() |
| 143 |
or die "cannot create new Document"; |
| 144 |
$tempdoc->addChild($child) |
| 145 |
or die "cannot addChild"; |
| 146 |
# Removing because it was converted into a test script. |
| 147 |
# print $tempdoc->toString; |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
1; |
| 152 |
|