blib/lib/MARC/File/MARCXML.pm untested

filter: Statementstmt 0.0% 0 / 131 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 187 CC 11 SCAR 23.9
CC
11
Cov
0%
decode
30.0
encode
20.0
_get_xml_text
12.0
CRAP 10.9
1 #
2 # MARCXML implementation for MARC records
3 #
4 # Copyright (c) 2011-2014, 2016 University Of Helsinki (The National Library Of Finland)
5 #
6 # This file is part of marc-file-marcxml
7 #
8 # This project's source code is licensed under the terms of GNU General Public License Version 3.
9 #
10  
11 package MARC::File::MARCXML;
12  
13 =head1 NAME
14  
15 MARC::File::MARCXML - MARCXML implementation for MARC records
16  
17 =cut
18  
19 use strict;
20 use integer;
21  
22 use XML::DOM;
23 use XML::Writer;
24 use vars qw( $ERROR );
25 use MARC::File::Encode qw( marc_to_utf8 );
26  
27 use MARC::File;
28 use vars qw( @ISA ); @ISA = qw( MARC::File );
29  
30 use MARC::Record qw( LEADER_LEN );
31 use MARC::Field;
32 use constant SUBFIELD_INDICATOR => "\x1F";
33 use constant END_OF_FIELD => "\x1E";
34 use constant END_OF_RECORD => "\x1D";
35 use constant DIRECTORY_ENTRY_LEN => 12;
36  
37 =head1 SYNOPSIS
38  
39 use MARC::File::MARCXML;
40  
41 my $file = MARC::File::MARCXML->in( $filename );
42  
43 while ( my $marc = $file->next() ) {
44 # Do something
45 }
46 $file->close();
47 undef $file;
48  
49 =head1 EXPORT
50  
51 None.
52  
53 =head1 METHODS
54  
55 =cut
56  
57 sub _next {
58 my $self = shift;
59 my $fh = $self->{fh};
60  
61 my $reclen;
62 return if eof($fh);
63  
64 local $/ = END_OF_RECORD;
65 my $MARCXML = <$fh>;
66  
67 # remove illegal garbage that sometimes occurs between records
68 $MARCXML =~ s/^[ \x00\x0a\x0d\x1a]+//;
69  
70 return $MARCXML;
71 }
72  
73 sub _get_xml_text($)
74 {
75 my ($node) = @_;
76  
77 return '' if (!$node);
78  
79 $node = $node->getFirstChild();
80 return '' if (!$node);
81  
82 my $str = $node->getData();
83 return $str;
84 # return pack('C*', unpack('U0C*', $str));
85 }
86  
87 =head2 decode( $string [, \&filter_func ] )
88  
89 Constructor for handling data from a MARCXML file. This function takes care of
90 all the tag directory parsing & mangling.
91  
92 Any warnings or coercions can be checked in the C<warnings()> function.
93  
94 The C<$filter_func> is an optional reference to a user-supplied function
95 that determines on a tag-by-tag basis if you want the tag passed to it
96 to be put into the MARC record. The function is passed the tag number
97 and the raw tag data, and must return a boolean. The return of a true
98 value tells MARC::File::MARCXML::decode that the tag should get put into
99 the resulting MARC record.
100  
101 For example, if you only want title and subject tags in your MARC record,
102 try this:
103  
104 sub filter {
105 my ($tagno,$tagdata) = @_;
106  
107 return ($tagno == 245) || ($tagno >= 600 && $tagno <= 699);
108 }
109  
110 my $marc = MARC::File::MARCXML->decode( $string, \&filter );
111  
112 Why would you want to do such a thing? The big reason is that creating
113 fields is processor-intensive, and if your program is doing read-only
114 data analysis and needs to be as fast as possible, you can save time by
115 not creating fields that you'll be ignoring anyway.
116  
117 Another possible use is if you're only interested in printing certain
118 tags from the record, then you can filter them when you read from disc
119 and not have to delete unwanted tags yourself.
120  
121 =cut
122  
123 sub decode {
124  
125 my $text;
126 my $location = '';
127  
128 ## decode can be called in a variety of ways
129 ## $object->decode( $string )
130 ## MARC::File::MARCXML->decode( $string )
131 ## MARC::File::MARCXML::decode( $string )
132 ## this bit of code covers all three
133  
134 my $self = shift;
135 if ( ref($self) =~ /^MARC::File/ ) {
136 $location = 'in record '.$self->{recnum};
137 $text = shift;
138 } else {
139 $location = 'in record 1';
140 $text = $self=~/MARC::File/ ? shift : $self;
141 }
142 my $filter_func = shift;
143  
144 my $parser = new XML::DOM::Parser;
145  
146 my $doc = undef;
147 eval { $doc = $parser->parse($text) };
148 if ($@)
149 {
150 die("could not parse xml: $!");
151  
152 }
153  
154  
155 # ok this the empty shell we will fill
156 my $marc = MARC::Record->new();
157  
158 my @leaders = $doc->getElementsByTagName('leader');
159 foreach my $leader (@leaders) {
160 my $fielddata = _get_xml_text($leader);
161 $marc->leader( $fielddata );
162 }
163  
164 my @controlfields = $doc->getElementsByTagName('controlfield');
165 foreach my $controlfield (@controlfields)
166 {
167 my $tag = $controlfield->getAttributeNode('tag')->getValue();
168  
169 my $fielddata = _get_xml_text($controlfield);
170 $fielddata =~ s/\r\n/ /g;
171 $fielddata =~ s/\r//g;
172 $fielddata =~ s/\n/ /g;
173  
174 my $field = MARC::Field->new($tag, $fielddata);
175 $marc->append_fields($field);
176  
177 }
178  
179 my @datafields = $doc->getElementsByTagName('datafield');
180 foreach my $datafield (@datafields)
181 {
182 my $tag = $datafield->getAttributeNode('tag')->getValue();
183  
184 my $ind1 = $datafield->getAttributeNode('ind1')->getValue();
185 my $ind2 = $datafield->getAttributeNode('ind2')->getValue();
186  
187 my $field;
188  
189 my @subfields = $datafield->getElementsByTagName('subfield');
190 foreach my $subfield (@subfields)
191 {
192 my $sub_code = $subfield->getAttributeNode('code')->getValue();
193 my $sub_contents = _get_xml_text($subfield);
194 $sub_contents =~ s/\r\n/ /g;
195 $sub_contents =~ s/\r//g;
196 $sub_contents =~ s/\n/ /g;
197  
198 if (!defined($field)) {
199 $field = MARC::Field->new($tag, $ind1, $ind2,$sub_code => $sub_contents);
200 } else {
201 $field->add_subfields($sub_code => $sub_contents);
202 }
203 }
204  
205 $marc->append_fields($field);
206  
207  
208 }
209 $doc->dispose();
210  
211  
212 return $marc;
213 }
214  
215 =head2 encode()
216  
217 Returns a string of characters suitable for writing out to a MARCXML file
218  
219 =cut
220  
221 sub encode() {
222 my $marc = shift;
223 $marc = shift if (ref($marc)||$marc) =~ /^MARC::File/;
224  
225 my $opts = shift;
226  
227 my $doc;
228 my $writer = new XML::Writer(OUTPUT => \$doc, ENCODING => 'utf-8', DATA_INDENT => 2);
229 if (defined($opts) && defined($opts->{'skipDeclaration'}) && $opts->{'skipDeclaration'}) {
230 # don't write xml declaration
231 } else {
232 $writer->xmlDecl("UTF-8");
233 }
234  
235  
236 $writer->startTag("record");
237  
238  
239 $writer->startTag("leader");
240 $writer->characters($marc->leader());
241 $writer->endTag("leader");
242  
243 for my $field ($marc->fields()) {
244  
245 if ($field->is_control_field()) {
246  
247 $writer->startTag("controlfield", "tag" => $field->tag());
248  
249 $writer->characters($field->data());
250  
251 $writer->endTag("controlfield");
252  
253 } else {
254  
255 $writer->startTag("datafield", "tag" => $field->tag(), "ind1"=>$field->indicator(1), "ind2"=>$field->indicator(2));
256  
257 for my $subfield ($field->subfields) {
258  
259  
260 $writer->startTag("subfield", "code" => $subfield->[0]);
261 $writer->characters($subfield->[1]);
262 $writer->endTag("subfield");
263  
264 }
265  
266  
267 $writer->endTag("datafield");
268 }
269  
270  
271 }
272  
273  
274 $writer->endTag("record");
275 $writer->end();
276  
277 return $doc;
278  
279 }
280 1;
281  
282 __END__