blib/lib/MARC/File/XML.pm

filter: Statementstmt 90.0% 189 / 210 Branchbran 65.9% 58 / 88 Conditioncond 48.7% 38 / 78 MC/DCmcdc 18.3% 9 / 49 Subroutinesub 90.6% 29 / 32 Podpod 66.6% 10 / 15 totaltotal 70.5% 333 / 472 CC 76 SCAR 21.8
CC
76
Cov
76%
_unimarc_encoding
30.0
decode
17.1
record
11.6
CRAP 8.8
↑ Summary
1 package MARC::File::XML;
2  
3 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use vars qw( $VERSION %_load_args );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use base qw( MARC::File );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use MARC::Record;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use MARC::Field;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 11 ▶use MARC::Charset qw( marc8_to_utf8 utf8_to_marc8 );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 11 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 11 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 11 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 11 $VERSION = '1.0.6';
17  
18 11 our $parser;
19  
20 9 ▶sub import {
Subroutine import: called CC=3 SCAR=11.4
21 9 my $class = shift;
22 9 %_load_args = @_;
23 9 ▶ $_load_args{ DefaultEncoding } ||= 'UTF-8';
Condition50%1/2
$MARC::File::XML::_load_args{"DefaultEncoding"} ||= "UTF-8"
l!l
09
MC/DC0%0/1
$MARC::File::XML::_load_args{"DefaultEncoding"} ||= "UTF-8"
$MARC::File::XML::_load_args{"DefaultEncoding"}
Truth table50%1/2
A
A= $MARC::File::XML::_load_args{"DefaultEncoding"}
Aresult
11
00
24 9 ▶ $_load_args{ RecordFormat } ||= 'USMARC';
Condition50%1/2
$MARC::File::XML::_load_args{"RecordFormat"} ||= "USMARC"
l!l
09
MC/DC0%0/1
$MARC::File::XML::_load_args{"RecordFormat"} ||= "USMARC"
$MARC::File::XML::_load_args{"RecordFormat"}
Truth table50%1/2
A
A= $MARC::File::XML::_load_args{"RecordFormat"}
Aresult
11
00
25 }
26  
27 =head1 NAME
28  
29 MARC::File::XML - Work with MARC data encoded as XML
30  
31 =head1 SYNOPSIS
32  
33 ## Loading with USE options
34 use MARC::File::XML ( BinaryEncoding => 'utf8', RecordFormat => 'UNIMARC' );
35  
36 ## Setting the record format without USE options
37 MARC::File::XML->default_record_format('USMARC');
38
39 ## reading with MARC::Batch
40 my $batch = MARC::Batch->new( 'XML', $filename );
41 my $record = $batch->next();
42  
43 ## or reading with MARC::File::XML explicitly
44 my $file = MARC::File::XML->in( $filename );
45 my $record = $file->next();
46  
47 ## serialize a single MARC::Record object as XML
48 print $record->as_xml();
49  
50 ## write a bunch of records to a file
51 my $file = MARC::File::XML->out( 'myfile.xml' );
52 $file->write( $record1 );
53 $file->write( $record2 );
54 $file->write( $record3 );
55 $file->close();
56  
57 ## instead of writing to disk, get the xml directly
58 my $xml = join( "\n",
59 MARC::File::XML::header(),
60 MARC::File::XML::record( $record1 ),
61 MARC::File::XML::record( $record2 ),
62 MARC::File::XML::footer()
63 );
64  
65 =head1 DESCRIPTION
66  
67 The MARC-XML distribution is an extension to the MARC-Record distribution for
68 working with MARC21 data that is encoded as XML. The XML encoding used is the
69 MARC21slim schema supplied by the Library of Congress. More information may
70 be obtained here: http://www.loc.gov/standards/marcxml/
71  
72 You must have MARC::Record installed to use MARC::File::XML. In fact
73 once you install the MARC-XML distribution you will most likely not use it
74 directly, but will have an additional file format available to you when you
75 use MARC::Batch.
76  
77 This version of MARC-XML supersedes an the versions ending with 0.25 which
78 were used with the MARC.pm framework. MARC-XML now uses MARC::Record
79 exclusively.
80  
81 If you have any questions or would like to contribute to this module please
82 sign on to the perl4lib list. More information about perl4lib is available
83 at L<http://perl4lib.perl.org>.
84  
85 =head1 METHODS
86  
87 When you use MARC::File::XML your MARC::Record objects will have two new
88 additional methods available to them:
89  
90 =head2 MARC::File::XML->default_record_format([$format])
91  
92 Sets or returns the default record format used by MARC::File::XML. Valid
93 formats are B<MARC21>, B<USMARC>, B<UNIMARC> and B<UNIMARCAUTH>.
94  
95 MARC::File::XML->default_record_format('UNIMARC');
96  
97 =cut
98  
99 0 ▶sub default_record_format {
Subroutine default_record_format: not called CC=2 SCAR=17.9
Pod documented
100 0 my $self = shift;
101 0 my $format = shift;
102  
103 0 ▶ $_load_args{RecordFormat} = $format if ($format);
Branch: 1 branch
BranchTrueFalseTotal
if $format 0 0 0
104  
105 0 return $_load_args{RecordFormat};
106 }
107  
108  
109 =head2 as_xml()
110  
111 Returns a MARC::Record object serialized in XML. You can pass an optional format
112 parameter to tell MARC::File::XML what type of record (USMARC, UNIMARC, UNIMARCAUTH) you are
113 serializing.
114  
115 print $record->as_xml([$format]);
116  
117 =cut
118  
119 3 ▶sub MARC::Record::as_xml {
Subroutine as_xml: called CC=2 SCAR=7.6
Pod undocumented
120 3 my $record = shift;
121 3 ▶ my $format = shift || $_load_args{RecordFormat};
Condition33%1/3
shift() || $MARC::File::XML::_load_args{'RecordFormat'}
l!l&&r!l&&!r
030
MC/DC0%0/2
shift() || $MARC::File::XML::_load_args{'RecordFormat'}
shift() $MARC::File::XML::_load_args{'RecordFormat'}
Truth table33%1/3
A || B
A= shift()B= $MARC::File::XML::_load_args{'RecordFormat'}
ABresult
1X1
011
000
122 3 return( MARC::File::XML::encode( $record, $format ) );
123 }
124  
125 =head2 as_xml_record([$format])
126  
127 Returns a MARC::Record object serialized in XML without a collection wrapper.
128 You can pass an optional format parameter to tell MARC::File::XML what type of
129 record (USMARC, UNIMARC, UNIMARCAUTH) you are serializing.
130  
131 print $record->as_xml_record('UNIMARC');
132  
133 =cut
134  
135 0 ▶sub MARC::Record::as_xml_record {
Subroutine as_xml_record: not called CC=2 SCAR=17.9
Pod undocumented
136 0 my $record = shift;
137 0 ▶ my $format = shift || $_load_args{RecordFormat};
Condition0%0/3
shift() || $MARC::File::XML::_load_args{'RecordFormat'}
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $MARC::File::XML::_load_args{'RecordFormat'}
shift() $MARC::File::XML::_load_args{'RecordFormat'}
Truth table0%0/3
A || B
A= shift()B= $MARC::File::XML::_load_args{'RecordFormat'}
ABresult
1X1
011
000
138 0 return( MARC::File::XML::encode( $record, $format, 1 ) );
139 }
140  
141 =head2 new_from_xml([$encoding, $format])
142  
143 If you have a chunk of XML and you want a record object for it you can use
144 this method to generate a MARC::Record object. You can pass an optional
145 encoding parameter to specify which encoding (UTF-8 or MARC-8) you would like
146 the resulting record to be in. You can also pass a format parameter to specify
147 the source record type, such as UNIMARC, UNIMARCAUTH, USMARC or MARC21.
148  
149 my $record = MARC::Record->new_from_xml( $xml, $encoding, $format );
150  
151 Note: only works for single record XML chunks.
152  
153 =cut
154  
155 8 ▶sub MARC::Record::new_from_xml {
Subroutine new_from_xml: called CC=5 SCAR=16.8
Pod undocumented
156 8 my $xml = shift;
157 ## to allow calling as MARC::Record::new_from_xml()
158 ## or MARC::Record->new_from_xml()
159 8 ▶ $xml = shift if ( ref($xml) || ($xml eq "MARC::Record") );
Branch: 1 branch
BranchTrueFalseTotal
if ref $xml or $xml eq "MARC::Record" 7 1 8
Condition66%2/3
ref $xml or $xml eq "MARC::Record"
l!l&&r!l&&!r
071
MC/DC50%1/2
ref $xml or $xml eq "MARC::Record"
ref $xml $xml eq "MARC::Record"
Truth table66%2/3
A or B
A= ref $xmlB= $xml eq "MARC::Record"
ABresult
1X1
011
000
160  
161 8 ▶ my $enc = shift || $_load_args{BinaryEncoding};
Condition66%2/3
shift() || $MARC::File::XML::_load_args{'BinaryEncoding'}
l!l&&r!l&&!r
305
MC/DC50%1/2
shift() || $MARC::File::XML::_load_args{'BinaryEncoding'}
shift() $MARC::File::XML::_load_args{'BinaryEncoding'}
Truth table66%2/3
A || B
A= shift()B= $MARC::File::XML::_load_args{'BinaryEncoding'}
ABresult
1X1
011
000
162 8 ▶ my $format = shift || $_load_args{RecordFormat};
Condition33%1/3
shift() || $MARC::File::XML::_load_args{'RecordFormat'}
l!l&&r!l&&!r
080
MC/DC0%0/2
shift() || $MARC::File::XML::_load_args{'RecordFormat'}
shift() $MARC::File::XML::_load_args{'RecordFormat'}
Truth table33%1/3
A || B
A= shift()B= $MARC::File::XML::_load_args{'RecordFormat'}
ABresult
1X1
011
000
163 8 return( MARC::File::XML::decode( $xml, $enc, $format ) );
164 }
165  
166 =pod
167  
168 If you want to write records as XML to a file you can use out() with write()
169 to serialize more than one record as XML.
170  
171 =head2 out()
172  
173 A constructor for creating a MARC::File::XML object that can write XML to a
174 file. You must pass in the name of a file to write XML to. If the $encoding
175 parameter or the DefaultEncoding (see above) is set to UTF-8 then the binmode
176 of the output file will be set appropriately.
177  
178 my $file = MARC::File::XML->out( $filename [, $encoding] );
179  
180 =cut
181  
182 1 ▶sub out {
Subroutine out: called CC=5 SCAR=18.7
Pod documented
183 1 my ( $class, $filename, $enc ) = @_;
184 1 ▶ my $fh = IO::File->new( ">$filename" ) or croak( $! );
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::File"->new(">$filename") or croak($!) 1 0 1
185 1 ▶ $enc ||= $_load_args{DefaultEncoding};
Condition33%1/3
$enc ||= $MARC::File::XML::_load_args{"DefaultEncoding"}
l!l&&r!l&&!r
010
MC/DC0%0/2
$enc ||= $MARC::File::XML::_load_args{"DefaultEncoding"}
$enc $MARC::File::XML::_load_args{"DefaultEncoding"}
Truth table33%1/3
A ||= B
A= $encB= $MARC::File::XML::_load_args{"DefaultEncoding"}
ABresult
1X1
011
000
186  
187 1 ▶ if ($enc =~ /^utf-?8$/oi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($enc =~ /^utf-?8$/io) { } 1 0 1
188 1 $fh->binmode(':utf8');
189 } else {
190 0 $fh->binmode(':raw');
191 }
192  
193 1 my %self = (
194 filename => $filename,
195 fh => $fh,
196 header => 0,
197 encoding => $enc
198 );
199 1 ▶ return( bless \%self, ref( $class ) || $class );
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
200 }
201  
202 =head2 write()
203  
204 Used in tandem with out() to write records to a file.
205  
206 my $file = MARC::File::XML->out( $filename );
207 $file->write( $record1 );
208 $file->write( $record2 );
209  
210 =cut
211  
212 1 ▶sub write {
Subroutine write: called CC=7 SCAR=23.2
Pod documented
213 1 my ( $self, $record, $enc ) = @_;
214 1 ▶ if ( ! $self->{ fh } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"fh"}) 0 1 1
215 0 croak( "MARC::File::XML object not open for writing" );
216 }
217 1 ▶ if ( ! $record ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($record) 0 1 1
218 0 croak( "must pass write() a MARC::Record object" );
219 }
220 ## print the XML header if we haven't already
221 1 ▶ if ( ! $self->{ header } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"header"}) 1 0 1
222 1 ▶ $enc ||= $self->{ encoding } || $_load_args{DefaultEncoding};
Condition33%2/6
$enc ||= $self->{"encoding"} || $MARC::File::XML::_load_args{"DefaultEncoding"}
l!l&&r!l&&!r
010
$self->{'encoding'} || $MARC::File::XML::_load_args{'DefaultEncoding'}
l!l&&r!l&&!r
100
MC/DC0%0/3
$enc ||= $self->{"encoding"} || $MARC::File::XML::_load_args{"DefaultEncoding"}
$enc $self->{'encoding'} $MARC::File::XML::_load_args{'DefaultEncoding'}
Truth table0%0/4
A ||= B || C
A= $encB= $self->{'encoding'}C= $MARC::File::XML::_load_args{'DefaultEncoding'}
ABCresult
1XX1
01X1
0011
0000
223 1 $self->{ fh }->print( header( $enc ) );
224 1 $self->{ header } = 1;
225 }
226 ## print out the record
227 1 ▶ $self->{ fh }->print( record( $record ) ) || croak( $! );
Branch: 1 branch
BranchTrueFalseTotal
$self->{"fh"}->print(record($record)) or croak($!) 1 0 1
228 1 return( 1 );
229 }
230  
231 =head2 close()
232  
233 When writing records to disk the filehandle is automatically closed when you
234 the MARC::File::XML object goes out of scope. If you want to close it explicitly
235 use the close() method.
236  
237 =cut
238  
239 9 ▶sub close {
Subroutine close: called CC=3 SCAR=11.0
Pod documented
240 9 my $self = shift;
241 9 ▶ if ( $self->{ fh } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"fh"}) 8 1 9
242 8 ▶ $self->{ fh }->print( footer() ) if $self->{ header };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"header"} 1 7 8
243 8 $self->{ fh } = undef;
244 8 $self->{ filename } = undef;
245 8 $self->{ header } = undef;
246 }
247 9 return( 1 );
248 }
249  
250 ## makes sure that the XML file is closed off
251  
252 8 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
253 8 shift->close();
254 }
255  
256 =pod
257  
258 If you want to generate batches of records as XML, but don't want to write to
259 disk you'll have to use header(), record() and footer() to generate the
260 different portions.
261  
262 $xml = join( "\n",
263 MARC::File::XML::header(),
264 MARC::File::XML::record( $record1 ),
265 MARC::File::XML::record( $record2 ),
266 MARC::File::XML::record( $record3 ),
267 MARC::File::XML::footer()
268 );
269  
270 =head2 header()
271  
272 Returns a string of XML to use as the header to your XML file.
273  
274 =cut
275  
276 5 ▶sub header {
Subroutine header: called CC=5 SCAR=17.2
Pod documented
277 5 my $enc = shift;
278 5 ▶ $enc = shift if ( $enc && (ref($enc) || ($enc eq "MARC::File::XML")) );
Branch: 1 branch
BranchTrueFalseTotal
if $enc and ref $enc || $enc eq "MARC::File::XML" 0 5 5
Condition50%3/6
$enc and ref $enc || $enc eq "MARC::File::XML"
!ll&&!rl&&r
140
ref $enc or $enc eq "MARC::File::XML"
l!l&&r!l&&!r
004
MC/DC0%0/3
$enc and ref $enc || $enc eq "MARC::File::XML"
$enc ref $enc $enc eq "MARC::File::XML"
Truth table50%2/4
A and B or C
A= $encB= ref $encC= $enc eq "MARC::File::XML"
ABCresult
0XX0
1000
11X1
1011
279 5 ▶ $enc ||= 'UTF-8';
Condition100%2/2
$enc ||= "UTF-8"
l!l
41
MC/DC100%1/1
$enc ||= "UTF-8"
$enc
Truth table100%2/2
A
A= $enc
Aresult
11
00
280 5 return( <<MARC_XML_HEADER );
281 <?xml version="1.0" encoding="$enc"?>
282 <collection
283 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
284 xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd"
285 xmlns="http://www.loc.gov/MARC21/slim">
286 MARC_XML_HEADER
287 }
288  
289 =head2 footer()
290  
291 Returns a string of XML to use at the end of your XML file.
292  
293 =cut
294  
295 5 ▶sub footer {
Subroutine footer: called CC=1 SCAR=0.0
Pod documented
296 5 return( "</collection>" );
297 }
298  
299 =head2 record()
300  
301 Returns a chunk of XML suitable for placement between the header and the footer.
302  
303 =cut
304  
305 5 ▶sub record {
Subroutine record: called CC=11 SCAR=24.5
Pod documented
306 5 my $record = shift;
307 5 my $format = shift;
308 5 my $include_full_record_header = shift;
309 5 my $enc = shift;
310  
311 5 ▶ $format ||= $_load_args{RecordFormat};
Condition66%2/3
$format ||= $MARC::File::XML::_load_args{"RecordFormat"}
l!l&&r!l&&!r
320
MC/DC0%0/2
$format ||= $MARC::File::XML::_load_args{"RecordFormat"}
$format $MARC::File::XML::_load_args{"RecordFormat"}
Truth table66%2/3
A ||= B
A= $formatB= $MARC::File::XML::_load_args{"RecordFormat"}
ABresult
1X1
011
000
312  
313 5 my $_transcode = 0;
314 5 my $ldr = $record->leader;
315 5 my $original_encoding = substr($ldr,9,1);
316  
317 # Does the record think it is already Unicode?
318 5 ▶ if ($original_encoding ne 'a' && lc($format) !~ /^unimarc/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($original_encoding ne "a" and not lc($format) =~ /^unimarc/o) 5 0 5
Condition33%1/3
$original_encoding ne "a" and not lc($format) =~ /^unimarc/o
!ll&&!rl&&r
005
MC/DC0%0/2
$original_encoding ne "a" and not lc($format) =~ /^unimarc/o
$original_encoding ne "a" not lc($format) =~ /^unimarc/o
Truth table33%1/3
A and not(B)
A= $original_encoding ne "a"B= not lc($format) =~ /^unimarc/o
ABresult
0X0
100
111
319 # If not, we'll make it so
320 5 $_transcode++;
321 5 substr($ldr,9,1,'a');
322 5 $record->leader( $ldr );
323 }
324  
325 5 my @xml = ();
326  
327 5 ▶ if ($include_full_record_header) {
Branch: 1 branch
BranchTrueFalseTotal
if ($include_full_record_header) { } 0 5 5
328 0 push @xml, <<HEADER
329 <?xml version="1.0" encoding="$enc"?>
330 <record
331 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
332 xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd"
333 xmlns="http://www.loc.gov/MARC21/slim">
334 HEADER
335  
336 } else {
337 5 push( @xml, "<record>" );
338 }
339  
340 5 push( @xml, " <leader>" . escape( $record->leader ) . "</leader>" );
341  
342 5 foreach my $field ( $record->fields() ) {
343 38 my ($tag) = escape( $field->tag() );
344 38 ▶ if ( $field->is_control_field() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->is_control_field) { } 7 31 38
345 7 my $data = $field->data;
346 7 ▶ push( @xml, qq( <controlfield tag="$tag">) .
Branch: 1 branch
BranchTrueFalseTotal
$_transcode ? : 7 0 7
347 escape( ($_transcode ? marc8_to_utf8($data) : $data) ). qq(</controlfield>) );
348 } else {
349 31 my ($i1) = escape( $field->indicator( 1 ) );
350 31 my ($i2) = escape( $field->indicator( 2 ) );
351 31 push( @xml, qq( <datafield tag="$tag" ind1="$i1" ind2="$i2">) );
352 31 foreach my $subfield ( $field->subfields() ) {
353 59 my ( $code, $data ) = ( escape( $$subfield[0] ), $$subfield[1] );
354 59 ▶ push( @xml, qq( <subfield code="$code">).
Branch: 1 branch
BranchTrueFalseTotal
$_transcode ? : 59 0 59
355 escape( ($_transcode ? marc8_to_utf8($data) : $data) ).qq(</subfield>) );
356 }
357 31 push( @xml, " </datafield>" );
358 }
359 }
360 5 push( @xml, "</record>\n" );
361  
362 5 ▶ if ($_transcode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_transcode) 5 0 5
363 5 substr($ldr,9,1,$original_encoding);
364 5 $record->leader( $ldr );
365 }
366  
367 5 return( join( "\n", @xml ) );
368 }
369  
370 11 my %ESCAPES = (
371 '&' => '&amp;',
372 '<' => '&lt;',
373 '>' => '&gt;',
374 );
375 11 my $_base_escape_regex = join( '|', map { "\Q$_\E" } keys %ESCAPES );
376 11 my $ESCAPE_REGEX = qr/$_base_escape_regex/;
377  
378 233 ▶sub escape {
Subroutine escape: called CC=3 SCAR=11.8
Pod undocumented
379 233 my $string = shift;
380 233 ▶ return '' if ! defined $string or $string eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $string or $string eq "" 0 233 233
Condition33%1/3
not defined $string or $string eq ""
l!l&&r!l&&!r
00233
MC/DC0%0/2
not defined $string or $string eq ""
not defined $string $string eq ""
Truth table33%1/3
not(A) or B
A= not defined $stringB= $string eq ""
ABresult
1X1
011
000
381 233 $string =~ s/($ESCAPE_REGEX)/$ESCAPES{$1}/oge;
382 233 return( $string );
383 }
384  
385 29 ▶sub _next {
Subroutine _next: called CC=7 SCAR=19.5
386 29 my $self = shift;
387 29 my $fh = $self->{ fh };
388  
389 ## return undef at the end of the file
390 29 ▶ return if eof($fh);
Branch: 1 branch
BranchTrueFalseTotal
if eof $fh 0 29 29
391  
392 ## get a chunk of xml for a record
393 29 local $/ = 'record>';
394 29 my $xml = <$fh>;
395  
396 29 ▶ return if $xml =~ m!</([^:]+:){0,1}collection>$!;
Branch: 1 branch
BranchTrueFalseTotal
if $xml =~ m[</([^:]+:){0,1}collection>$] 3 26 29
397  
398 ## do we have enough?
399 26 ▶ $xml .= <$fh> if $xml !~ m!</([^:]+:){0,1}record>$! && !eof($fh);
Branch: 1 branch
BranchTrueFalseTotal
unless $xml =~ m[</([^:]+:){0,1}record>$] or eof $fh 21 5 26
Condition100%3/3
$xml =~ m[</([^:]+:){0,1}record>$] or eof $fh
l!l&&r!l&&!r
4121
MC/DC100%2/2
$xml =~ m[</([^:]+:){0,1}record>$] or eof $fh
$xml =~ m[</([^:]+:){0,1}record>$] eof $fh
Truth table100%3/3
A or B
A= $xml =~ m[</([^:]+:){0,1}record>$]B= eof $fh
ABresult
1X1
011
000
400 ## trim stuff before the start record element
401 26 $xml =~ s/.*?<(([^:]+:){0,1})record.*?>/<$1record>/s;
402  
403 ## return undef if there isn't a good chunk of xml
404 26 ▶ return if ( $xml !~ m|<(([^:]+:){0,1})record>.*</\1record>|s );
Branch: 1 branch
BranchTrueFalseTotal
unless $xml =~ m[<(([^:]+:){0,1})record>.*</\1record>]s 1 25 26
405  
406 ## if we have a namespace prefix, restore the declaration
407 25 ▶ if ($xml =~ /<([^:]+:)record>/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xml =~ /<([^:]+:)record>/) 10 15 25
408 10 $xml =~ s!<([^:]+):record>!<$1:record xmlns:$1="http://www.loc.gov/MARC21/slim">!;
409 }
410  
411 ## return the chunk of xml
412 25 return( $xml );
413 }
414  
415 33 ▶sub _parser {
Subroutine _parser: called CC=2 SCAR=7.0
416 $parser ||= XML::LibXML->new(
417 expand_entities => 1,
418 1 ▶ ext_ent_handler => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
419 1 die "External entities are not supported\n";
420 }
421 33 ▶ );
Condition66%2/3
$MARC::File::XML::parser ||= "XML::LibXML"->new("expand_entities", 1, "ext_ent_handler", sub { die "External entities are not supported\n"; } )
l!l&&r!l&&!r
23100
MC/DC0%0/2
$MARC::File::XML::parser ||= "XML::LibXML"->new("expand_entities", 1, "ext_ent_handler", sub { die "External entities are not supported\n"; } )
$MARC::File::XML::parser "XML::LibXML"->new("expand_entities", 1, "ext_ent_handler", sub { die "External entities are not supported\n"; } )
Truth table66%2/3
A ||= B
A= $MARC::File::XML::parserB= "XML::LibXML"->new("expand_entities", 1, "ext_ent_handler", sub { die "External entities are not supported\n"; } )
ABresult
1X1
011
000
422 33 return $parser;
423 }
424  
425 =head2 decode()
426  
427 You probably don't ever want to call this method directly. If you do
428 you should pass in a chunk of XML as the argument.
429  
430 It is normally invoked by a call to next(), see L<MARC::Batch> or L<MARC::File>.
431  
432 =cut
433  
434 33 ▶sub decode {
Subroutine decode: called CC=17 SCAR=28.4
Pod documented
435 33 my $self = shift;
436 33 my $text;
437 33 my $location = '';
438  
439 33 ▶ if ( ref($self) =~ /^MARC::File/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($self) =~ /^MARC::File/) { } 25 8 33
440 25 $location = 'in record '.$self->{recnum};
441 25 $text = shift;
442 } else {
443 8 $location = 'in record 1';
444 8 ▶ $text = $self=~/MARC::File/ ? shift : $self;
Branch: 1 branch
BranchTrueFalseTotal
$self =~ /MARC::File/ ? : 0 8 8
445 }
446  
447 33 ▶ my $enc = shift || $_load_args{BinaryEncoding};
Condition66%2/3
shift() || $MARC::File::XML::_load_args{'BinaryEncoding'}
l!l&&r!l&&!r
3030
MC/DC50%1/2
shift() || $MARC::File::XML::_load_args{'BinaryEncoding'}
shift() $MARC::File::XML::_load_args{'BinaryEncoding'}
Truth table66%2/3
A || B
A= shift()B= $MARC::File::XML::_load_args{'BinaryEncoding'}
ABresult
1X1
011
000
448 33 ▶ my $format = shift || $_load_args{RecordFormat};
Condition100%3/3
shift() || $MARC::File::XML::_load_args{'RecordFormat'}
l!l&&r!l&&!r
8232
MC/DC100%2/2
shift() || $MARC::File::XML::_load_args{'RecordFormat'}
shift() $MARC::File::XML::_load_args{'RecordFormat'}
Truth table100%3/3
A || B
A= shift()B= $MARC::File::XML::_load_args{'RecordFormat'}
ABresult
1X1
011
000
449  
450 33 my $parser = _parser();
451 33 my $xml = $parser->parse_string($text);
452  
453 32 my $root = $xml->documentElement;
454 32 ▶ croak('MARCXML document has no root element') unless defined $root;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $root 0 32 32
455 32 ▶ if ($root->localname eq 'collection') {
Branch: 1 branch
BranchTrueFalseTotal
if ($root->localname eq "collection") 4 28 32
456 4 my @records = $root->getChildrenByLocalName('record');
457 4 ▶ croak('MARCXML document has no record element') unless @records;
Branch: 1 branch
BranchTrueFalseTotal
unless @records 1 3 4
458 3 $root = $records[0];
459 }
460  
461 31 my $rec = MARC::Record->new();
462 31 my @leaders = $root->getElementsByLocalName('leader');
463 31 my $transcode_to_marc8 = 0;
464 31 ▶ if (@leaders) {
Branch: 1 branch
BranchTrueFalseTotal
if (@leaders) 28 3 31
465 28 my $leader = $leaders[0]->textContent;
466  
467 # this bit is rather questionable
468 28 ▶ $transcode_to_marc8 = substr($leader, 9, 1) eq 'a' && decideMARC8Binary($format, $enc) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
substr($leader, 9, 1) eq 'a' && decideMARC8Binary($format, $enc) ? : 8 20 28
Condition66%2/3
substr($leader, 9, 1) eq "a" and decideMARC8Binary($format, $enc)
!ll&&!rl&&r
2008
MC/DC50%1/2
substr($leader, 9, 1) eq "a" and decideMARC8Binary($format, $enc)
substr($leader, 9, 1) eq "a" decideMARC8Binary($format, $enc)
Truth table66%2/3
A and B
A= substr($leader, 9, 1) eq "a"B= decideMARC8Binary($format, $enc)
ABresult
0X0
100
111
469 28 ▶ substr($leader, 9, 1) = ' ' if $transcode_to_marc8;
Branch: 1 branch
BranchTrueFalseTotal
if $transcode_to_marc8 8 20 28
470
471 28 $rec->leader($leader);
472 }
473  
474 31 my @fields = ();
475 31 foreach my $elt ($root->getChildrenByLocalName('*')) {
476 423 ▶ if ($elt->localname eq 'controlfield') {
Branch: 2 branches
BranchTrueFalseTotal
if ($elt->localname eq 'controlfield') { } 87 336 423
elsif ($elt->localname eq 'datafield') { } 308 28 336
477 87 push @fields, MARC::Field->new($elt->getAttribute('tag'), $elt->textContent);
478 } elsif ($elt->localname eq 'datafield') {
479 308 my @sfs = ();
480 308 foreach my $sfelt ($elt->getChildrenByLocalName('subfield')) {
481 505 ▶ push @sfs, $sfelt->getAttribute('code'),
Branch: 1 branch
BranchTrueFalseTotal
$transcode_to_marc8 ? : 77 428 505
482 $transcode_to_marc8 ? utf8_to_marc8($sfelt->textContent()) : $sfelt->textContent();
483 }
484 308 push @fields, MARC::Field->new(
485 $elt->getAttribute('tag'),
486 $elt->getAttribute('ind1'),
487 $elt->getAttribute('ind2'),
488 @sfs
489 );
490 }
491 }
492 31 $rec->append_fields(@fields);
493 31 return $rec;
494
495 }
496  
497 =head2 MARC::File::XML->set_parser($parser)
498  
499 Pass a XML::LibXML parser to MARC::File::XML
500 for it to use. This is optional, meant for
501 use by applications that maintain a shared
502 parser object or which require that external
503 entities be processed. Note that the latter
504 is a potential security risk; see
505 L<https://www.owasp.org/index.php/XML_External_Entity_(XXE)_Processing>.
506  
507 =cut
508  
509 2 ▶sub set_parser {
Subroutine set_parser: called CC=2 SCAR=6.9
Pod documented
510 2 my $self = shift;
511  
512 2 $parser = shift;
513 2 ▶ undef $parser unless ref($parser) =~ /XML::LibXML/;
Branch: 1 branch
BranchTrueFalseTotal
unless ref($MARC::File::XML::parser) =~ /XML::LibXML/ 1 1 2
514 }
515  
516 8 ▶sub decideMARC8Binary {
Subroutine decideMARC8Binary: called CC=5 SCAR=17.8
Pod undocumented
517 8 my $format = shift;
518 8 my $enc = shift;
519  
520 8 ▶ return 0 if (defined($format) && lc($format) =~ /^unimarc/o);
Branch: 1 branch
BranchTrueFalseTotal
if defined $format and lc($format) =~ /^unimarc/o 0 8 8
Condition66%2/3
defined $format and lc($format) =~ /^unimarc/o
!ll&&!rl&&r
260
MC/DC0%0/2
defined $format and lc($format) =~ /^unimarc/o
defined $format lc($format) =~ /^unimarc/o
Truth table66%2/3
A and B
A= defined $formatB= lc($format) =~ /^unimarc/o
ABresult
0X0
100
111
521 8 ▶ return 0 if (defined($enc) && lc($enc) =~ /^utf-?8/o);
Branch: 1 branch
BranchTrueFalseTotal
if defined $enc and lc($enc) =~ /^utf-?8/o 0 8 8
Condition33%1/3
defined $enc and lc($enc) =~ /^utf-?8/o
!ll&&!rl&&r
800
MC/DC0%0/2
defined $enc and lc($enc) =~ /^utf-?8/o
defined $enc lc($enc) =~ /^utf-?8/o
Truth table33%1/3
A and B
A= defined $encB= lc($enc) =~ /^utf-?8/o
ABresult
0X0
100
111
522 8 return 1;
523 }
524  
525  
526 =head2 encode()
527  
528 You probably want to use the as_xml() method on your MARC::Record object
529 instead of calling this directly. But if you want to you just need to
530 pass in the MARC::Record object you wish to encode as XML, and you will be
531 returned the XML as a scalar.
532  
533 =cut
534  
535 3 ▶sub encode {
Subroutine encode: called CC=7 SCAR=22.0
Pod documented
536 3 my $record = shift;
537 3 ▶ my $format = shift || $_load_args{RecordFormat};
Condition33%1/3
shift() || $MARC::File::XML::_load_args{'RecordFormat'}
l!l&&r!l&&!r
300
MC/DC0%0/2
shift() || $MARC::File::XML::_load_args{'RecordFormat'}
shift() $MARC::File::XML::_load_args{'RecordFormat'}
Truth table33%1/3
A || B
A= shift()B= $MARC::File::XML::_load_args{'RecordFormat'}
ABresult
1X1
011
000
538 3 my $without_collection_header = shift;
539 3 ▶ my $enc = shift || $_load_args{DefaultEncoding};
Condition33%1/3
shift() || $MARC::File::XML::_load_args{'DefaultEncoding'}
l!l&&r!l&&!r
030
MC/DC0%0/2
shift() || $MARC::File::XML::_load_args{'DefaultEncoding'}
shift() $MARC::File::XML::_load_args{'DefaultEncoding'}
Truth table33%1/3
A || B
A= shift()B= $MARC::File::XML::_load_args{'DefaultEncoding'}
ABresult
1X1
011
000
540  
541 3 ▶ if (lc($format) =~ /^unimarc/o) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc($format) =~ /^unimarc/o) 0 3 3
542 0 $enc = _unimarc_encoding( $format => $record );
543 }
544  
545 3 my @xml = ();
546 3 ▶ push( @xml, header( $enc ) ) unless ($without_collection_header);
Branch: 1 branch
BranchTrueFalseTotal
unless $without_collection_header 3 0 3
547 # verbose, but naming the header output flags this way to avoid
548 # the potential confusion identified in CPAN bug #34082
549 # http://rt.cpan.org/Public/Bug/Display.html?id=34082
550 3 ▶ my $include_full_record_header = ($without_collection_header) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$without_collection_header ? : 0 3 3
551 3 push( @xml, record( $record, $format, $include_full_record_header, $enc ) );
552 3 ▶ push( @xml, footer() ) unless ($without_collection_header);
Branch: 1 branch
BranchTrueFalseTotal
unless $without_collection_header 3 0 3
553  
554 3 return( join( "\n", @xml ) );
555 }
556  
557 0 ▶sub _unimarc_encoding {
Subroutine _unimarc_encoding: not called CC=5 SCAR=34.0
558 0 my $f = shift;
559 0 my $r = shift;
560  
561 0 my $pos = 26;
562 0 ▶ $pos = 13 if (lc($f) eq 'unimarcauth');
Branch: 1 branch
BranchTrueFalseTotal
if lc $f eq "unimarcauth" 0 0 0
563  
564 0 my $enc = substr( $r->subfield(100 => 'a'), $pos, 2 );
565  
566 0 ▶ if ($enc eq '01' || $enc eq '03') {
Branch: 2 branches
BranchTrueFalseTotal
if ($enc eq '01' or $enc eq '03') { } 0 0 0
elsif ($enc eq '50') { } 0 0 0
Condition0%0/3
$enc eq "01" or $enc eq "03"
l!l&&r!l&&!r
000
MC/DC0%0/2
$enc eq "01" or $enc eq "03"
$enc eq "01" $enc eq "03"
Truth table0%0/3
A or B
A= $enc eq "01"B= $enc eq "03"
ABresult
1X1
011
000
567 0 return 'ISO-8859-1';
568 } elsif ($enc eq '50') {
569 0 return 'UTF-8';
570 } else {
571 0 die "Unsupported UNIMARC character encoding [$enc] for XML output for $f; 100\$a -> " . $r->subfield(100 => 'a');
572 }
573 }
574  
575 =head1 TODO
576  
577 =over 4
578  
579 =item * Support for callback filters in decode().
580  
581 =back
582  
583 =head1 SEE ALSO
584  
585 =over 4
586  
587 =item L<http://www.loc.gov/standards/marcxml/>
588  
589 =item L<MARC::File::USMARC>
590  
591 =item L<MARC::Batch>
592  
593 =item L<MARC::Record>
594  
595 =back
596  
597 =head1 AUTHORS
598  
599 =over 4
600  
601 =item * Ed Summers <ehs@pobox.com>
602  
603 =back
604  
605 =cut
606  
607 11 1;
↑ Summary