| 1 | package Net::OAI::Record::DocumentHelper; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use base qw( XML::SAX::Base ); | ||
| 7 | use Carp qw( carp croak ); | ||
| 8 | our $VERSION = "1.16_12"; | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | Net::OAI::Record::DocumentHelper - filter class for fine tuning of document events tweaking processing results | ||
| 13 | |||
| 14 | =head1 SYNOPSIS | ||
| 15 | |||
| 16 | $filter = Net::OAI::Record::DocumentHelper->new( | ||
| 17 | Handler => 'XML::SAX::Writer' | ||
| 18 | provideDocumentEvents => 1, | ||
| 19 | # since XML::SAX::Writer->new() returns objects of a differentClass | ||
| 20 | dontVerifyHandler => 1, | ||
| 21 | ); | ||
| 22 | $records = $harvester->listRecords( | ||
| 23 | 'metadataPrefix' => 'oai_dc', | ||
| 24 | 'metadataHandler' => $filter; | ||
| 25 | ); | ||
| 26 | |||
| 27 | |||
| 28 | $builder = XML::LibXML::SAX::Builder->new(); | ||
| 29 | $helper = Net::OAI::Recrd::DocumentHelper->new( | ||
| 30 | Handler => $builder, | ||
| 31 | provideDocumentEvents => 1, | ||
| 32 | finalizeHook => sub { return $_[0]->finalize()->serialize() } | ||
| 33 | ); | ||
| 34 | $records = $harvester->listRecords( | ||
| 35 | 'metadataPrefix' => 'oai_dc', | ||
| 36 | 'metadataHandler' => $helper; | ||
| 37 | ); | ||
| 38 | |||
| 39 | |||
| 40 | |||
| 41 | =head1 DESCRIPTION | ||
| 42 | |||
| 43 | This helper class acts as a SAX filter and can be used as recordHandler or | ||
| 44 | metadataHandler for Net::OAI::Harvester->listRecord() or getRecord() | ||
| 45 | requests or within a SAX filter chain. | ||
| 46 | |||
| 47 | The Handler may be a class name, in which case a new object of that | ||
| 48 | class is created on instantiation of a DocumentHelper object, or | ||
| 49 | an existing SAX filter which then will be reused. (In this case it | ||
| 50 | is your responsibility to use only filters which support the processing | ||
| 51 | of multiple documents by one instance.) | ||
| 52 | |||
| 53 | If the option C<provideDocumentEvents> is set to a true value, the filter | ||
| 54 | will generate start_document() and end_document() events. | ||
| 55 | |||
| 56 | If the (exlusive to the previous) option C<suppressDocumentEvents> is set to | ||
| 57 | a true value, the filter will stop start_document() and end_document() events | ||
| 58 | from propagating up the chain. | ||
| 59 | |||
| 60 | If the option C<dontVerifyHandler> is set to a true value, class sanity checks | ||
| 61 | for existing or freshly created ones are skipped. | ||
| 62 | |||
| 63 | The option C<finalizeHook> may specify a callback for postprocessing the | ||
| 64 | result just after an end_document() event has been forwarded. It is | ||
| 65 | called with two arguments, the handler object and the result of the | ||
| 66 | forwarded end_document() and should return something suitable for processing | ||
| 67 | with L<Storable>. | ||
| 68 | |||
| 69 | =head1 METHODS | ||
| 70 | |||
| 71 | =head2 new( %options ) | ||
| 72 | |||
| 73 | Creates a Handler suitable as recordHandler or metadataHandler or intermediate | ||
| 74 | filter. | ||
| 75 | |||
| 76 | =cut | ||
| 77 | |||
| 78 | sub new { | ||
| 79 | my ( $class, %opts ) = @_; | ||
| 80 | my $self = bless { %opts }, ref( $class ) || $class; | ||
| 81 | $self->{ _tagStack } = []; | ||
| 82 | $self->{ _prefixmap } = {}; | ||
| 83 | my $handler = $opts{ Handler }; | ||
| 84 | if ( ref($handler) ) { # active handler | ||
| 85 | # Net::OAI::Harvester::_verifyHandler( $handler ) unless $opts{ dontVerifyHandler }; | ||
| 86 | $self->set_handler($handler); | ||
| 87 | } | ||
| 88 | elsif ( $opts{ dontVerifyHandler } ) { | ||
| 89 | eval( "use $handler" ); | ||
| 90 | Net::OAI::Harvester::_fatal( "unable to locate Handler $handler in: " . | ||
| 91 | join( "\n\t", @INC ) ) if $@; | ||
| 92 | my $instance = $handler->new(); | ||
| 93 | carp( "Handler $handler must inherit from XML::SAX::Base\n" ) | ||
| 94 | if ( ! grep { 'XML::SAX::Base' } eval( '@' . $handler . '::ISA' ) ); | ||
| 95 | $self->set_handler( $handler->new() ); | ||
| 96 | } | ||
| 97 | else { # instantiate from class name | ||
| 98 | Net::OAI::Harvester::_verifyHandler( $handler ); | ||
| 99 | $self->set_handler( $handler->new() ); | ||
| 100 | }; | ||
| 101 | croak("finalizeHook must be a coderef") if $opts{ finalizeHook } | ||
| 102 | and (ref($opts{ finalizeHook }) ne "CODE"); | ||
| 103 | croak("only one option of provideDocumentEvents and suppressDocumentEvents may be given") | ||
| 104 | if $opts{ provideDocumentEvents } and $opts{ suppressDocumentEvents }; | ||
| 105 | $self->{ _inDocument } = 0; | ||
| 106 | $self->{ _result } = undef; | ||
| 107 | return( $self ); | ||
| 108 | } | ||
| 109 | |||
| 110 | =head2 result ( ) | ||
| 111 | |||
| 112 | Returns the result of the last end_document() forwarded to the handler, | ||
| 113 | usually this is the result of some sort of finalizing process. | ||
| 114 | |||
| 115 | =cut | ||
| 116 | |||
| 117 | sub result { | ||
| 118 | my ( $self ) = @_; | ||
| 119 | return $self->{ _result }; | ||
| 120 | } | ||
| 121 | |||
| 122 | =head1 AUTHOR | ||
| 123 | |||
| 124 | Thomas Berger <ThB@gymel.com> | ||
| 125 | |||
| 126 | =cut | ||
| 127 | |||
| 128 | ## Storable hooks | ||
| 129 | |||
| 130 | sub STORABLE_freeze { | ||
| 131 | my ($obj, $cloning) = @_; | ||
| 132 | return if $cloning; | ||
| 133 | return ref($obj->{ _result }) ? ("", $obj->{ _result }) : ($obj->{ _result }); # || undef; | ||
| 134 | } | ||
| 135 | |||
| 136 | sub STORABLE_thaw { | ||
| 137 | my ($obj, $cloning, $serialized, $listref) = @_; | ||
| 138 | return if $cloning; | ||
| 139 | $obj->{ _result } = ($serialized eq "") ? $listref : $serialized; | ||
| 140 | #carp "thawed @$listref"; | ||
| 141 | } | ||
| 142 | |||
| 143 | |||
| 144 | ## SAX handlers | ||
| 145 | |||
| 146 | sub start_document { | ||
| 147 | my ($self, $document) = @_; | ||
| 148 | return if $self->{ _inDocument }; | ||
| 149 | $self->{ _inDocument } = 1; | ||
| 150 | $self->{ _result } = undef; | ||
| 151 | my $result = $self->{ suppressDocumentEvents } ? undef : $self->SUPER::start_document( $document ); | ||
| 152 | foreach my $deferred ( values %{$self->{ _prefixmap }} ) { | ||
| 153 | $self->SUPER::start_prefix_mapping( $deferred )}; | ||
| 154 | return $result; | ||
| 155 | } | ||
| 156 | |||
| 157 | sub end_document { | ||
| 158 | my ($self, $document) = @_; | ||
| 159 | return unless $self->{ _inDocument }; | ||
| 160 | $self->{ _inDocument } = 0; | ||
| 161 | my $result = $self->{ suppressDocumentEvents } ? undef : $self->SUPER::end_document( $document ); | ||
| 162 | $self->{ _result } = $self->{ finalizeHook } | ||
| 163 | ? &{$self->{ finalizeHook }}($self->get_handler(), $result) | ||
| 164 | : $result; | ||
| 165 | return $result; | ||
| 166 | } | ||
| 167 | |||
| 168 | sub start_prefix_mapping { | ||
| 169 | my ($self, $mapping) = @_; | ||
| 170 | if ( $self->{ _inDocument } ) { | ||
| 171 | return $self->SUPER::start_prefix_mapping( $mapping )}; | ||
| 172 | $self->{ _prefixmap }->{ $mapping->{Prefix} } = $mapping; | ||
| 173 | } | ||
| 174 | |||
| 175 | sub end_prefix_mapping { | ||
| 176 | my ($self, $mapping) = @_; | ||
| 177 | if ( $self->{ _inDocument } ) { | ||
| 178 | return $self->SUPER::end_prefix_mapping( $mapping )}; | ||
| 179 | delete $self->{ _prefixmap }->{ $mapping->{Prefix} }; | ||
| 180 | } | ||
| 181 | |||
| 182 | sub start_element { | ||
| 183 | my ( $self, $element ) = @_; | ||
| 184 | push (@{$self->{ _tagStack }}, $element->{ Name }); | ||
| 185 | unless ( $self->{ _inDocument } ) { | ||
| 186 | if ( $self->{ provideDocumentEvents } ) { | ||
| 187 | $self->SUPER::start_document( {} ); | ||
| 188 | foreach my $deferred ( values %{$self->{ _prefixmap }} ) { | ||
| 189 | $self->SUPER::start_prefix_mapping( $deferred )}; | ||
| 190 | }; | ||
| 191 | $self->{ _inDocument } = 1; | ||
| 192 | } | ||
| 193 | return $self->SUPER::start_element( $element ); | ||
| 194 | } | ||
| 195 | |||
| 196 | sub end_element { | ||
| 197 | my ( $self, $element ) = @_; | ||
| 198 | pop (@{$self->{ _tagStack }}); | ||
| 199 | if ( $self->{ _tagStack }->[0] ) { | ||
| 200 | return $self->SUPER::end_element( $element ); | ||
| 201 | } | ||
| 202 | my $elemresult = $self->SUPER::end_element( $element ); | ||
| 203 | if ( $self->{ provideDocumentEvents } ) { | ||
| 204 | foreach my $deferred ( values %{$self->{ _prefixmap }} ) { | ||
| 205 | $self->SUPER::end_prefix_mapping( $deferred )}; | ||
| 206 | my $docresult = $self->SUPER::end_document( {} ); | ||
| 207 | $self->{ _result } = $self->{ finalizeHook } | ||
| 208 | ? &{$self->{ finalizeHook }}($self->get_handler(), $docresult) | ||
| 209 | : $docresult; | ||
| 210 | } | ||
| 211 | $self->{ _inDocument } = 0; | ||
| 212 | return $elemresult; | ||
| 213 | } | ||
| 214 | |||
| 215 | sub characters { | ||
| 216 | my ( $self, $characters ) = @_; | ||
| 217 | return $self->{ _inDocument } ? $self->SUPER::characters( $characters ) : undef; | ||
| 218 | } | ||
| 219 | |||
| 220 | 1; | ||
| 221 |