Detrans/Config.pm

filter: Statementstmt 98.6% 147 / 149 Branchbran 89.2% 50 / 56 Conditioncond 54.1% 13 / 24 MC/DCmcdc 20.0% 3 / 15 Subroutinesub 100% 28 / 28 Podpod 100% 5 / 5 totaltotal 88.8% 246 / 277 CC 37 SCAR 18.5
CC
37
Cov
92%
start_element
16.1
end_element
7.0
new
4.5
CRAP 6.3
« Detrans.pm ↑ Summary Detrans/Rule.pm »
1 package MARC::Detrans::Config;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use base qw( Class::Accessor );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use XML::SAX::ParserFactory;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 MARC::Detrans::Config - Stores de-transliteration configuration data
12  
13 =head1 SYNOPSIS
14  
15 use MARC::Detrans::Config;
16 my $config = MARC::Detrans::Config->new( 'file.xml' );
17  
18 =head1 DESCRIPTION
19  
20 MARC::Detrans::Config will read detransliteration rules from an XML file which
21 you can then use to create a MARC::Detrans::Converter object to actually convert
22 MARC records.
23  
24 <?xml version="1.0" encoding="UTF-8"?>
25 <config>
26  
27 <!-- the language we are detransliterating -->
28 <language name="Russian" code="rus" />
29  
30 <!-- the script that will be used -->
31 <script name="Cyrillic" code="(N" />
32  
33 <!-- which fields/subfields to detransliterate or copy -->
34 <detrans-fields>
35 <field tag="245">
36 <subfield code="a" />
37 <subfield code="q" />
38 <subfield code="d" copy="true" />
39 </field>
40 <field tag="440">
41 <subfield code="a" />
42 </field>
43 </detrans-fields>
44  
45 <!-- a single character mapping -->
46 <rule>
47 <roman>b</roman>
48 <marc escape="(N">B</marc>
49 </rule>
50  
51 <!-- more rules ... -->
52  
53 <!-- a single authority mapping -->
54 <name>
55 <roman>$aNicholas $bI, $cEmperor of Russia, $d1796-1855</roman>
56 <marc>$a^ESC(NnIKOLAJ^ESCs, $bI, $c^ESC(NiMPERATOR^ESCs ^ESC(NwSEROSSIJSKIJ^ESCs, $d1796-1855</marc>
57 </name>
58  
59 </config>
60  
61 =head1 METHODS
62  
63 =head2 new()
64  
65 The constructor, which you should pass the file path for the XML configuration.
66 If you want to configure the MARC::Detrans::Config object manually you
67 can not pass in a path, but you ordinarily wouldn't want to do this.
68  
69 =cut
70  
71 7 ▶sub new {
Subroutine new: called CC=4 SCAR=15.0
Pod documented
72 7 my ( $class, $file ) = @_;
73 7 ▶ croak( "config file doesn't exist" ) if $file and ! -f $file;
Branch: 1 branch
BranchTrueFalseTotal
if $file and not -f $file 0 7 7
Condition66%2/3
$file and not -f $file
!ll&&!rl&&r
250
MC/DC0%0/2
$file and not -f $file
$file not -f $file
Truth table66%2/3
A and not(B)
A= $fileB= not -f $file
ABresult
0X0
100
111
74 7 ▶ my $self = bless { file => $file }, $class || $class;
Condition33%1/3
$class || $class
l!l&&r!l&&!r
700
MC/DC0%0/2
$class || $class
$class $class
Truth table33%1/3
A || B
A= $classB= $class
ABresult
1X1
011
000
75 7 $self->_parse( $file );
76 7 return( $self );
77 }
78  
79 =head2 rules()
80  
81 Returns a MARC::Detrans::Rules object that contains the transliteration
82 rules being used in the configuration.
83  
84 =head2 names()
85  
86 Returns a MARC::Detrans::Names object that contains the authority mappings
87 being used in the configuration.
88  
89 =head2 allEscapeCodes()
90  
91 Returns a list of all the MARC8 escape codes that are in use in this
92 configuration. Useful for when you are building 006 fields that itemize
93 the different character set codes in use.
94  
95 =cut
96  
97 21 ▶sub allEscapeCodes {
Subroutine allEscapeCodes: called CC=1 SCAR=0.0
Pod documented
98 21 return @{ shift->{allEscapeCodes} };
99 }
100  
101 =head2 detransFields()
102  
103 Returns a list of fields that the configuration lists as desiring
104 de-transliteration. If you need to you can pass in an array of
105 field names you'd like to detransliterate...but normally you won't
106 want to do this since the value come from the XML configuration.
107  
108 =cut
109  
110 26 ▶sub detransFields {
Subroutine detransFields: called CC=2 SCAR=6.9
Pod documented
111 26 my ($self,@fields) = @_;
112 26 ▶ if ( @fields ) { $self->{lookForFields} = \@fields; }
Branch: 1 branch
BranchTrueFalseTotal
if (@fields) 2 24 26
113 26 return @{ $self->{lookForFields} };
114 }
115  
116 =head2 needsDetrans()
117  
118 Returns true (1) or false (undef) to indicate whether the config lists
119 a particular field/subfield combination as needing detransliteration.
120  
121 =cut
122  
123 41 ▶sub needsDetrans {
Subroutine needsDetrans: called CC=4 SCAR=14.6
Pod documented
124 41 my ( $self, %args ) = @_;
125 croak( "must supply field and subfield parameters" )
126 41 ▶ if ! exists $args{field} or ! exists $args{subfield};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"field"} and exists $args{"subfield"} 0 41 41
Condition33%1/3
exists $args{"field"} and exists $args{"subfield"}
!ll&&!rl&&r
0041
MC/DC0%0/2
exists $args{"field"} and exists $args{"subfield"}
exists $args{"field"} exists $args{"subfield"}
Truth table33%1/3
A and B
A= exists $args{"field"}B= exists $args{"subfield"}
ABresult
0X0
100
111
127 41 ▶ return 1 if $self->{detransFields}{$args{field}.$args{subfield}};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"detransFields"}{$args{"field"} . $args{"subfield"}} 36 5 41
128 5 return;
129 }
130  
131 =head2 needsCopy()
132  
133 Returns true (1) or false (undef) to indicate wheter the config lists
134 a particular field/subfield combination as needing a copy.
135  
136 =cut
137  
138 6 ▶sub needsCopy {
Subroutine needsCopy: called CC=4 SCAR=14.6
Pod documented
139 6 my ( $self, %args ) = @_;
140 croak( "must supply field and subfield parameters" )
141 6 ▶ if ! exists $args{field} or ! exists $args{subfield};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"field"} and exists $args{"subfield"} 0 6 6
Condition33%1/3
exists $args{"field"} and exists $args{"subfield"}
!ll&&!rl&&r
006
MC/DC0%0/2
exists $args{"field"} and exists $args{"subfield"}
exists $args{"field"} exists $args{"subfield"}
Truth table33%1/3
A and B
A= exists $args{"field"}B= exists $args{"subfield"}
ABresult
0X0
100
111
142 6 ▶ return 1 if $self->{copyFields}{$args{field}.$args{subfield}};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"copyFields"}{$args{"field"} . $args{"subfield"}} 5 1 6
143 1 return;
144 }
145  
146 =head1 AUTHORS
147  
148 =over 4
149  
150 =item * Ed Summers <ehs@pobox.com>
151  
152 =cut
153  
154 7 MARC::Detrans::Config->mk_accessors( qw(
155 rules
156 names
157 languageName
158 languageCode
159 scriptName
160 scriptCode
161 scriptOrientation
162 ) );
163  
164 7 ▶sub _parse {
Subroutine _parse: called CC=2 SCAR=6.9
165 7 my $self = shift;
166 7 my $handler = ConfigHandler->new();
167 7 my $parser = XML::SAX::ParserFactory->parser( Handler => $handler );
168 ## we skip parsing if we don't have a file to parse, which
169 ## can happen when the configuration is being manually configured
170 ## in tests...
171 7 ▶ $parser->parse_uri( $self->{ file } ) if $self->{ file };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"file"} 5 2 7
172 7 $self->rules( $handler->rules() );
173 7 $self->names( $handler->names() );
174 7 $self->languageName( $handler->languageName() );
175 7 $self->languageCode( $handler->languageCode() );
176 7 $self->scriptName( $handler->scriptName() );
177 7 $self->scriptCode( $handler->scriptCode() );
178 7 $self->scriptOrientation( $handler->scriptOrientation() );
179 7 $self->{lookForFields} = $handler->{lookForFields};
180 7 $self->{detransFields} = $handler->{detransFields};
181 7 $self->{copyFields} = $handler->{copyFields};
182 7 $self->{allEscapeCodes} = [ sort keys %{ $handler->{allEscapeCodes} } ];
183 }
184  
185  
186 ## the SAX handler for the config file
187  
188 package ConfigHandler;
189  
190 7 ▶use base qw( XML::SAX::Base );
Subroutine BEGIN: called CC=1 SCAR=0.0
191 7 ▶use MARC::Detrans::Rules;
Subroutine BEGIN: called CC=1 SCAR=0.0
192 7 ▶use MARC::Detrans::Rule;
Subroutine BEGIN: called CC=1 SCAR=0.0
193 7 ▶use MARC::Detrans::Names;
Subroutine BEGIN: called CC=1 SCAR=0.0
194 7 ▶use MARC::Detrans::Name;
Subroutine BEGIN: called CC=1 SCAR=0.0
195  
196 7 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
197 7 my $class = shift;
198 7 my $self = $class->SUPER::new();
199 7 $self->resetFlags();
200 7 $self->{rules} = MARC::Detrans::Rules->new();
201 7 $self->{names} = MARC::Detrans::Names->new();
202 7 $self->{lookForFields} = [];
203 7 $self->{detransFields} = {};
204 7 $self->{copyFields} = {};
205 7 $self->{allEscapeCodes} = {};
206 7 ▶ return bless $self, $class || ref($class);
Condition33%1/3
$class || ref $class
l!l&&r!l&&!r
700
MC/DC0%0/2
$class || ref $class
$class ref $class
Truth table33%1/3
A || B
A= $classB= ref $class
ABresult
1X1
011
000
207 }
208  
209 7 ▶sub rules { return shift->{rules}; }
Subroutine rules: called CC=1 SCAR=0.0
210 7 ▶sub names { return shift->{names}; }
Subroutine names: called CC=1 SCAR=0.0
211 7 ▶sub languageName { return shift->{languageName}; }
Subroutine languageName: called CC=1 SCAR=0.0
212 7 ▶sub languageCode { return shift->{languageCode}; }
Subroutine languageCode: called CC=1 SCAR=0.0
213 7 ▶sub scriptName { return shift->{scriptName}; }
Subroutine scriptName: called CC=1 SCAR=0.0
214 7 ▶sub scriptCode { return shift->{scriptCode}; }
Subroutine scriptCode: called CC=1 SCAR=0.0
215 7 ▶sub scriptOrientation { return shift->{scriptOrientation}; }
Subroutine scriptOrientation: called CC=1 SCAR=0.0
216  
217 1435 ▶sub start_element {
Subroutine start_element: called CC=16 SCAR=27.8
218 1435 my ( $self, $data ) = @_;
219 1435 my $tag = $data->{Name};
220 ## start of rule
221 1435 ▶ if ( $tag eq 'rule' ) {
Branch: 10 branches
BranchTrueFalseTotal
if ($tag eq 'rule') { } 453 982 1435
elsif ($tag eq 'name') { } 4 978 982
elsif ($tag eq 'roman') { } 457 521 978
elsif ($tag eq 'marc') { } 457 64 521
elsif ($tag eq 'language') { } 5 59 64
elsif ($tag eq 'script') { } 4 55 59
elsif ($tag eq 'detrans-fields') { } 9 46 55
elsif ($tag eq 'copy-fields') { } 0 46 46
elsif ($tag eq 'field') { } 9 37 46
elsif ($tag eq 'subfield') { } 32 5 37
222 453 $self->resetFlags();
223 453 $self->{currentPosition} = $data->{Attributes}{'{}position'}{Value};
224 453 $self->{insideRule} = 1;;
225 }
226 ## start of name
227 elsif ( $tag eq 'name' ) {
228 4 $self->resetFlags();
229 4 $self->{insideName} = 1;
230 }
231 ## start of roman
232 elsif ( $tag eq 'roman' ) {
233 457 $self->{insideRoman} = 1;
234 }
235 ## start of marc
236 elsif ( $tag eq 'marc' ) {
237 457 $self->{insideMarc} = 1;
238 457 $self->{currentEscape} = $data->{Attributes}{'{}escape'}{Value};
239 ## keep track of all escape codes used
240 $self->{allEscapeCodes}{ $self->{currentEscape} }++
241 457 ▶ if $self->{currentEscape};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"currentEscape"} 452 5 457
242 }
243 ## language name/code
244 elsif ( $tag eq 'language' ) {
245 5 $self->{languageName} = $data->{Attributes}{'{}name'}{Value};
246 5 $self->{languageCode} = $data->{Attributes}{'{}code'}{Value};
247 }
248 ## script name/code
249 elsif ( $tag eq 'script' ) {
250 4 $self->{scriptName} = $data->{Attributes}{'{}name'}{Value};
251 4 $self->{scriptCode} = $data->{Attributes}{'{}code'}{Value};
252 4 $self->{scriptOrientation}=$data->{Attributes}{'{}orientation'}{Value};
253 }
254 ## start of fields to detransliterate
255 elsif ( $tag eq 'detrans-fields' ) {
256 9 $self->{insideDetransFields} = 1;
257 }
258 ## start of fields to copy
259 elsif ( $tag eq 'copy-fields' ) {
260 0 $self->{insideCopyFields} = 1;
261 }
262 ## start of field in either detrans-fields and copy-fields
263 elsif ( $tag eq 'field' ) {
264 9 my $field = $data->{Attributes}{'{}tag'}{Value};
265 9 $self->{field} = $field;
266 9 push( @{ $self->{lookForFields} }, $field )
267 9 ▶ unless grep /$field/, @{$self->{lookForFields}};
Branch: 1 branch
BranchTrueFalseTotal
unless grep /$field/, @{$$self{"lookForFields"};} 9 0 9
268 }
269 ## start of subfield in field element
270 elsif ( $tag eq 'subfield' ) {
271 32 my $subfield = $data->{Attributes}{'{}code'}{Value};
272 32 my $field = $self->{field};
273
274 ## figure out if this subfield needs copying or detransliterating
275 32 my $copy = 0;
276 32 ▶ if ( $data->{Attributes}{'{}copy'}
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"Attributes"}{"{}copy"} and $data->{"Attributes"}{"{}copy"}{"Value"} eq "true") 14 18 32
Condition66%2/3
$data->{"Attributes"}{"{}copy"} and $data->{"Attributes"}{"{}copy"}{"Value"} eq "true"
!ll&&!rl&&r
18014
MC/DC50%1/2
$data->{"Attributes"}{"{}copy"} and $data->{"Attributes"}{"{}copy"}{"Value"} eq "true"
$data->{"Attributes"}{"{}copy"} $data->{"Attributes"}{"{}copy"}{"Value"} eq "true"
Truth table66%2/3
A and B
A= $data->{"Attributes"}{"{}copy"}B= $data->{"Attributes"}{"{}copy"}{"Value"} eq "true"
ABresult
0X0
100
111
277 and $data->{Attributes}{'{}copy'}{Value} eq 'true' ) {
278 14 $copy = 1;
279 }
280 ## add the field/subfield combo to appropriate hash
281 ## to use later to figure out if it needs detrans or copy
282 32 ▶ if ( $copy ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($copy) { } 14 18 32
283 14 $self->{copyFields}{$field.$subfield} = 1;
284 } else {
285 18 $self->{detransFields}{$field.$subfield} = 1;
286 }
287 }
288 }
289  
290 1435 ▶sub end_element {
Subroutine end_element: called CC=7 SCAR=19.5
291 1435 my ( $self, $data ) = @_;
292 1435 my $tag = $data->{Name};
293 ## end of rule, so build the rule and add it
294 1435 ▶ if ( $tag eq 'rule' ) {
Branch: 6 branches
BranchTrueFalseTotal
if ($tag eq 'rule') { } 453 982 1435
elsif ($tag eq 'name') { } 4 978 982
elsif ($tag eq 'marc') { } 457 521 978
elsif ($tag eq 'roman') { } 457 64 521
elsif ($tag eq 'detrans-fields') { } 9 55 64
elsif ($tag eq 'copy-fields') { } 0 55 55
295 my $rule = MARC::Detrans::Rule->new(
296 from => $self->{romanText},
297 to => $self->{marcText},
298 escape => $self->{currentEscape},
299 position => $self->{currentPosition}
300 453 );
301 453 $self->{rules}->addRule( $rule );
302 453 $self->resetFlags();
303 }
304 ## end of name, so build the name and ad it
305 elsif ( $tag eq 'name' ) {
306 my $name = MARC::Detrans::Name->new(
307 from => $self->{romanText},
308 to => $self->{marcText},
309 4 );
310 4 $self->{names}->addName( $name );
311 4 $self->resetFlags();
312 }
313 ## end of marc section, store away the text
314 elsif ( $tag eq 'marc' ) {
315 457 $self->{marcText} = $self->{currentText};
316 457 $self->{currentText} = '';
317 457 $self->{insideMarc} = 0;
318 }
319 ## end of roman section, store away the text
320 elsif ( $tag eq 'roman' ) {
321 457 $self->{romanText} = $self->{currentText};
322 457 $self->{currentText} = '';
323 457 $self->{insideRoman} = 0;
324 }
325 ## end of detrans-fields
326 elsif ( $tag eq 'detrans-fields' ) {
327 9 $self->{insideDetransFields} = 0;
328 }
329 ## end of copy fields
330 elsif ( $tag eq 'copy-fields' ) {
331 0 $self->{insideCopyFields} = 0;
332 }
333 }
334  
335 2812 ▶sub characters {
Subroutine characters: called CC=4 SCAR=13.9
336 2812 my ( $self, $data ) = @_;
337 ## only store text if we're in a marc or roman tag
338 2812 ▶ if ( $self->{insideMarc} or $self->{insideRoman}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"insideMarc"} or $self->{"insideRoman"} or $self->{"insideSubfield"}) 902 1910 2812
Condition83%5/6
$self->{"insideMarc"} or $self->{"insideRoman"} or $self->{"insideSubfield"}
l!l&&r!l&&!r
90201910
$self->{"insideMarc"} or $self->{"insideRoman"}
l!l&&r!l&&!r
4374651910
MC/DC66%2/3
$self->{"insideMarc"} or $self->{"insideRoman"} or $self->{"insideSubfield"}
$self->{"insideMarc"} $self->{"insideRoman"} $self->{"insideSubfield"}
Truth table75%3/4
A or B or C
A= $self->{"insideMarc"}B= $self->{"insideRoman"}C= $self->{"insideSubfield"}
ABCresult
1XX1
01X1
0011
0000
339 or $self->{insideSubfield} ) {
340 902 $self->{currentText} .= $data->{Data};
341 }
342 }
343  
344 921 ▶sub resetFlags {
Subroutine resetFlags: called CC=1 SCAR=0.0
345 921 my $self = shift;
346 921 $self->{insideRule} = 0;
347 921 $self->{insideName} = 0;
348 921 $self->{insideRoman} = 0;
349 921 $self->{insideMarc} = 0;
350 921 $self->{insideDetransFields} = 0;
351 921 $self->{insideCopyFields} = 0;
352 921 $self->{currentText} = '';
353 921 $self->{marcText} = '';
354 921 $self->{romanText} = '';
355 921 $self->{currentEscape} = '';
356 921 $self->{field} = '';
357 }
358  
359 7 1;
« Detrans.pm ↑ Summary Detrans/Rule.pm »