blib/lib/MARC/Charset/Compiler.pm

filter: Statementstmt 100% 67 / 67 Branchbran 88.4% 23 / 26 Conditioncond 94.4% 17 / 18 MC/DCmcdc 90.9% 10 / 11 Subroutinesub 100% 13 / 13 Podpod 50.0% 3 / 6 totaltotal 94.3% 133 / 141 CC 20 SCAR 21.9
CC
20
Cov
96%
end_element
16.0
start_element
4.0
characters
2.0
CRAP 9.0
« blib/lib/MARC/Charset/Code.pm ↑ Summary blib/lib/MARC/Charset/Constants.pm »
1 package MARC::Charset::Compiler;
2  
3 =head1 NAME
4  
5 MARC::Charset::Compiler - compile XML mapping rules from LoC
6  
7 =head1 SYNOPSIS
8  
9 $compiler = MARC::Charset::Compiler->new();
10 $table = $compiler->compile('codetables.xml');
11  
12 =head1 DESCRIPTION
13  
14 MARC::Charset uses mapping rules from the Library of Congress for
15 generating a MARC::Charset::Table for looking up utf8 values based on the
16 source MARC-8 character set and the character.
17  
18 =head1 METHODS
19  
20 =cut
21  
22 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 1 ▶use base qw( XML::SAX::Base );
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use XML::SAX::ParserFactory;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use Unicode::UCD qw(charinfo);
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use MARC::Charset::Table;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use MARC::Charset::Code;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31  
32 =head1 new()
33  
34 The constructor.
35  
36 =cut
37  
38 sub new
39 1 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
40 1 my $self = bless {}, 'MARC::Charset::Compiler';
41 1 $self->{table} = MARC::Charset::Table->brand_new();
42 1 $self->{current_code} = undef;
43 1 $self->{text} = '';
44 1 return $self;
45 }
46  
47  
48 =head1 compile()
49  
50 Pass in the path to an XML file to compile.
51  
52 =cut
53  
54 sub compile
55 2 ▶{
Subroutine compile: called CC=1 SCAR=0.0
Pod undocumented
56 2 my ($self, $file) = @_;
57  
58 2 my $factory = XML::SAX::ParserFactory->new();
59 2 my $parser = $factory->parser(Handler => $self);
60 2 $parser->parse_uri($file);
61 }
62  
63  
64 ## SAX event handlers are below
65  
66 sub start_element
67 82298 ▶{
Subroutine start_element: called CC=4 SCAR=13.9
Pod documented
68 82298 my ($self, $data) = @_;
69 82298 my $name = $data->{Name};
70 82298 ▶ if ($name eq 'code')
Branch: 2 branches
BranchTrueFalseTotal
if ($name eq 'code') { } 16406 65892 82298
elsif ($name eq 'characterSet') { } 13 65879 65892
71 {
72 16406 $self->{current_code} = MARC::Charset::Code->new();
73 }
74 elsif ($name eq 'characterSet')
75 {
76 13 my $charset = $data->{Attributes}{'{}ISOcode'}{Value};
77 13 ▶ warn('missing ISOcode in characterSet element') unless $charset;
Branch: 1 branch
BranchTrueFalseTotal
unless $charset 0 13 13
78 13 $self->{current_charset} = $charset;
79 }
80 }
81  
82  
83 sub end_element
84 82298 ▶{
Subroutine end_element: called CC=16 SCAR=27.8
Pod documented
85 82298 my ($self, $data) = @_;
86 82298 my $name = $data->{Name};
87  
88 # normalize some names for method lookup
89 82298 ▶ $name = 'is_combining' if $name eq 'isCombining';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "isCombining" 61 82237 82298
90  
91 # get the existing code if we have one
92 82298 my $code = $self->{current_code};
93  
94 # if we're ending a code element
95 82298 ▶ if ($code and $name eq 'code')
Branch: 3 branches
BranchTrueFalseTotal
if ($code and $name eq 'code') { } 16406 65892 82298
elsif ($code and $name eq 'marc') { } 16406 49486 65892
elsif ($code and $name =~ /^(marc|ucs|is_combining|alt|marc_right_half|marc_left_half)$/) { } 16536 33021 49557
Condition100%3/3
$code and $name eq "code"
!ll&&!rl&&r
716582116406
MC/DC100%2/2
$code and $name eq "code"
$code $name eq "code"
Truth table100%3/3
A and B
A= $codeB= $name eq "code"
ABresult
0X0
100
111
96 {
97 # if there is no ucs code, use what's in alt
98 16406 ▶ $code->ucs($code->alt()) unless $code->ucs;
Branch: 1 branch
BranchTrueFalseTotal
unless $code->ucs 2 16404 16406
99  
100 # can't process a code point that lacks a unicode representation
101 16406 ▶ die("invalid code: " . $code->to_string()) unless $code->ucs;
Branch: 1 branch
BranchTrueFalseTotal
unless $code->ucs 0 16406 16406
102
103 # set the charset code
104 16406 $code->charset($self->{current_charset});
105  
106 # lookup the name from perl's character db
107 16406 my $info = charinfo(hex($code->ucs()));
108 16406 ▶ $code->name($info->{name}) if $info;
Branch: 1 branch
BranchTrueFalseTotal
if $info 16406 0 16406
109  
110 # add it to the table
111 16406 $self->{table}->add_code($code);
112  
113 # start with a clean slate
114 16406 $self->{current_code} = undef;
115 }
116
117 ▶ elsif ($code and $name eq 'marc')
Condition100%3/3
$code and $name eq "marc"
!ll&&!rl&&r
714941516406
MC/DC100%2/2
$code and $name eq "marc"
$code $name eq "marc"
Truth table100%3/3
A and B
A= $codeB= $name eq "marc"
ABresult
0X0
100
111
118 {
119 16406 my $codepoint = $self->text();
120 16406 ▶ if ($self->{current_charset} eq '51' ||
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"current_charset"} eq 51 or $self->{"current_charset"} eq 34 or $self->{"current_charset"} eq 45) 203 16203 16406
Condition100%6/6
$self->{"current_charset"} eq 51 or $self->{"current_charset"} eq 34 or $self->{"current_charset"} eq 45
l!l&&r!l&&!r
1346916203
$self->{"current_charset"} eq 51 or $self->{"current_charset"} eq 34
l!l&&r!l&&!r
429216272
MC/DC100%3/3
$self->{"current_charset"} eq 51 or $self->{"current_charset"} eq 34 or $self->{"current_charset"} eq 45
$self->{"current_charset"} eq 51 $self->{"current_charset"} eq 34 $self->{"current_charset"} eq 45
Truth table100%4/4
A or B or C
A= $self->{"current_charset"} eq 51B= $self->{"current_charset"} eq 34C= $self->{"current_charset"} eq 45
ABCresult
1XX1
01X1
0011
0000
121 $self->{current_charset} eq '34' ||
122 $self->{current_charset} eq '45')
123 {
124 # codetables.xml supplied by the Library of Congress mistakenly
125 # lists the G1 value of characters in the extended Latin, extended
126 # Cyrillic and extended Arabic sets rather than the G0 value.
127 # MARC::Charset's table uses the G0 value internally.
128  
129 203 ▶ if (hex($codepoint) >= 0xa1 && hex($codepoint) <= 0xfe) {
Branch: 1 branch
BranchTrueFalseTotal
if (hex $codepoint >= 161 and hex $codepoint <= 254) 197 6 203
Condition66%2/3
hex $codepoint >= 161 and hex $codepoint <= 254
!ll&&!rl&&r
60197
MC/DC50%1/2
hex $codepoint >= 161 and hex $codepoint <= 254
hex $codepoint >= 161 hex $codepoint <= 254
Truth table66%2/3
A and B
A= hex $codepoint >= 161B= hex $codepoint <= 254
ABresult
0X0
100
111
130 197 $codepoint = sprintf("%x", hex($codepoint) - 128);
131 }
132 }
133 16406 $code->marc($codepoint);
134 }
135 # add these elements
136 ▶ elsif ($code and $name =~ /^(marc|ucs|is_combining|alt|marc_right_half|marc_left_half)$/)
Condition100%3/3
$code and $name =~ /^(marc|ucs|is_combining|alt|marc_right_half|marc_left_half)$/
!ll&&!rl&&r
713287916536
MC/DC100%2/2
$code and $name =~ /^(marc|ucs|is_combining|alt|marc_right_half|marc_left_half)$/
$code $name =~ /^(marc|ucs|is_combining|alt|marc_right_half|marc_left_half)$/
Truth table100%3/3
A and B
A= $codeB= $name =~ /^(marc|ucs|is_combining|alt|marc_right_half|marc_left_half)$/
ABresult
0X0
100
111
137 {
138 16536 $code->$name($self->text());
139 }
140  
141 # ending an element so forget all text
142 82298 $self->{text} = '';
143 }
144  
145  
146 sub characters
147 164799 ▶{
Subroutine characters: called CC=2 SCAR=6.9
Pod documented
148 164799 my ($self, $data) = @_;
149 164799 ▶ return unless $self->{current_code};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"current_code"} 16562 148237 164799
150 148237 my $text = $data->{Data};
151 148237 $self->{text} .= $data->{Data};
152 }
153  
154  
155 sub text
156 32942 ▶{
Subroutine text: called CC=1 SCAR=0.0
Pod undocumented
157 32942 my $text = shift->{text};
158 # collapse whitespace
159 32942 $text =~ s/\s\s+/ /g;
160 # strip new lines
161 32942 $text =~ s/[\r\n]//g;
162 # strip leading whitespace
163 32942 $text =~ s/^\s+//;
164 # strip trailing whitespace
165 32942 $text =~ s/\s+$//;
166 32942 return $text;
167 }
168  
169 1 1;
« blib/lib/MARC/Charset/Code.pm ↑ Summary blib/lib/MARC/Charset/Constants.pm »