XML.pm

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