| 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 DATA SOURCE | ||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | The XML mapping files this module compiles (F<etc/codetables.xml>, | ||||||||||||||||||||||||||||||||||||||||||
| 21 | F<etc/additional-iii-characters.xml>, F<etc/cccii.xml> in this | ||||||||||||||||||||||||||||||||||||||||||
| 22 | distribution) originate from the Library of Congress's own published | ||||||||||||||||||||||||||||||||||||||||||
| 23 | character set tables: | ||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | https://www.loc.gov/marc/specifications/codetables.xml | ||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | That file is not treated as a live dependency, and this module never | ||||||||||||||||||||||||||||||||||||||||||
| 28 | fetches it at install or run time -- the copy in F<etc/> is vendored | ||||||||||||||||||||||||||||||||||||||||||
| 29 | and versioned with the distribution. It's also known to differ from | ||||||||||||||||||||||||||||||||||||||||||
| 30 | whatever the LC URL currently serves: LC has published more than one | ||||||||||||||||||||||||||||||||||||||||||
| 31 | revision of this file over the years, and the copy here has picked up | ||||||||||||||||||||||||||||||||||||||||||
| 32 | maintainer corrections for at least one known mistake in LC's data | ||||||||||||||||||||||||||||||||||||||||||
| 33 | (see the G0/G1 workaround in C<end_element()> below, for | ||||||||||||||||||||||||||||||||||||||||||
| 34 | characterSet codes 51, 34 and 45). Diffing against the live URL is | ||||||||||||||||||||||||||||||||||||||||||
| 35 | useful context when investigating a specific character mapping, but | ||||||||||||||||||||||||||||||||||||||||||
| 36 | copying it over F<etc/codetables.xml> wholesale is not safe without | ||||||||||||||||||||||||||||||||||||||||||
| 37 | re-checking that those corrections are still needed against whatever | ||||||||||||||||||||||||||||||||||||||||||
| 38 | LC is serving at the time. | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | 1 | ▶ | use base qw( XML::SAX::Base ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 1 | ▶ | use XML::SAX::ParserFactory; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 1 | ▶ | use Unicode::UCD qw(charinfo); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 1 | ▶ | use MARC::Charset::Table; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 1 | ▶ | use MARC::Charset::Code; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | =head1 new() | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | The constructor. | ||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | sub new | ||||||||||||||||||||||||||||||||||||||||||
| 61 | 1 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 1 | my $self = bless {}, 'MARC::Charset::Compiler'; | |||||||||||||||||||||||||||||||||||||||||
| 63 | 1 | $self->{table} = MARC::Charset::Table->brand_new(); | |||||||||||||||||||||||||||||||||||||||||
| 64 | 1 | $self->{current_code} = undef; | |||||||||||||||||||||||||||||||||||||||||
| 65 | 1 | $self->{text} = ''; | |||||||||||||||||||||||||||||||||||||||||
| 66 | 1 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | =head1 compile() | ||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | Pass in the path to an XML file to compile. | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | sub compile | ||||||||||||||||||||||||||||||||||||||||||
| 77 | 3 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
compile: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 3 | my ($self, $file) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | 3 | my $factory = XML::SAX::ParserFactory->new(); | |||||||||||||||||||||||||||||||||||||||||
| 81 | 3 | my $parser = $factory->parser(Handler => $self); | |||||||||||||||||||||||||||||||||||||||||
| 82 | 3 | $parser->parse_uri($file); | |||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | ## SAX event handlers are below | ||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | sub start_element | ||||||||||||||||||||||||||||||||||||||||||
| 89 | 82309 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
start_element: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | 82309 | my ($self, $data) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 91 | 82309 | my $name = $data->{Name}; | |||||||||||||||||||||||||||||||||||||||||
| 92 | 82309 | ▶ | if ($name eq 'code') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | { | ||||||||||||||||||||||||||||||||||||||||||
| 94 | 16407 | $self->{current_code} = MARC::Charset::Code->new(); | |||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||
| 96 | elsif ($name eq 'characterSet') | ||||||||||||||||||||||||||||||||||||||||||
| 97 | { | ||||||||||||||||||||||||||||||||||||||||||
| 98 | 14 | my $charset = $data->{Attributes}{'{}ISOcode'}{Value}; | |||||||||||||||||||||||||||||||||||||||||
| 99 | 14 | ▶ | warn('missing ISOcode in characterSet element') unless $charset; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 14 | $self->{current_charset} = $charset; | |||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | sub end_element | ||||||||||||||||||||||||||||||||||||||||||
| 106 | 82309 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
end_element: called CC=16 SCAR=27.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | 82309 | my ($self, $data) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 108 | 82309 | my $name = $data->{Name}; | |||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | # normalize some names for method lookup | ||||||||||||||||||||||||||||||||||||||||||
| 111 | 82309 | ▶ | $name = 'is_combining' if $name eq 'isCombining'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | # get the existing code if we have one | ||||||||||||||||||||||||||||||||||||||||||
| 114 | 82309 | my $code = $self->{current_code}; | |||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | # if we're ending a code element | ||||||||||||||||||||||||||||||||||||||||||
| 117 | 82309 | ▶ | if ($code and $name eq 'code') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition100%3/3
$code and $name eq "code"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | { | ||||||||||||||||||||||||||||||||||||||||||
| 119 | # if there is no ucs code, use what's in alt | ||||||||||||||||||||||||||||||||||||||||||
| 120 | 16407 | ▶ | $code->ucs($code->alt()) unless $code->ucs; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | # can't process a code point that lacks a unicode representation | ||||||||||||||||||||||||||||||||||||||||||
| 123 | 16407 | ▶ | die("invalid code: " . $code->to_string()) unless $code->ucs; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | # set the charset code | ||||||||||||||||||||||||||||||||||||||||||
| 126 | 16407 | $code->charset($self->{current_charset}); | |||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | # lookup the name from perl's character db | ||||||||||||||||||||||||||||||||||||||||||
| 129 | 16407 | my $info = charinfo(hex($code->ucs())); | |||||||||||||||||||||||||||||||||||||||||
| 130 | 16407 | ▶ | $code->name($info->{name}) if $info; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | # add it to the table | ||||||||||||||||||||||||||||||||||||||||||
| 133 | 16407 | $self->{table}->add_code($code); | |||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | # start with a clean slate | ||||||||||||||||||||||||||||||||||||||||||
| 136 | 16407 | $self->{current_code} = undef; | |||||||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | ▶ | elsif ($code and $name eq 'marc') | |||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$code and $name eq "marc"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | { | ||||||||||||||||||||||||||||||||||||||||||
| 141 | 16407 | my $codepoint = $self->text(); | |||||||||||||||||||||||||||||||||||||||||
| 142 | 16407 | ▶ | if ($self->{current_charset} eq '51' || | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$self->{"current_charset"} eq 51 or $self->{"current_charset"} eq 34 or $self->{"current_charset"} eq 45
$self->{"current_charset"} eq 51 or $self->{"current_charset"} eq 34
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
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | $self->{current_charset} eq '34' || | ||||||||||||||||||||||||||||||||||||||||||
| 144 | $self->{current_charset} eq '45') | ||||||||||||||||||||||||||||||||||||||||||
| 145 | { | ||||||||||||||||||||||||||||||||||||||||||
| 146 | # codetables.xml supplied by the Library of Congress mistakenly | ||||||||||||||||||||||||||||||||||||||||||
| 147 | # lists the G1 value of characters in the extended Latin, extended | ||||||||||||||||||||||||||||||||||||||||||
| 148 | # Cyrillic and extended Arabic sets rather than the G0 value. | ||||||||||||||||||||||||||||||||||||||||||
| 149 | # MARC::Charset's table uses the G0 value internally. | ||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | 203 | ▶ | if (hex($codepoint) >= 0xa1 && hex($codepoint) <= 0xfe) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
hex $codepoint >= 161 and hex $codepoint <= 254
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
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | 197 | $codepoint = sprintf("%x", hex($codepoint) - 128); | |||||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||||||||
| 155 | 16407 | $code->marc($codepoint); | |||||||||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||||||||
| 157 | # add these elements | ||||||||||||||||||||||||||||||||||||||||||
| 158 | ▶ | 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)$/
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)$/
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | { | ||||||||||||||||||||||||||||||||||||||||||
| 160 | 16537 | $code->$name($self->text()); | |||||||||||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | # ending an element so forget all text | ||||||||||||||||||||||||||||||||||||||||||
| 164 | 82309 | $self->{text} = ''; | |||||||||||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||
| 168 | sub characters | ||||||||||||||||||||||||||||||||||||||||||
| 169 | 164820 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
characters: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 170 | 164820 | my ($self, $data) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 171 | 164820 | ▶ | return unless $self->{current_code}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 172 | 148246 | my $text = $data->{Data}; | |||||||||||||||||||||||||||||||||||||||||
| 173 | 148246 | $self->{text} .= $data->{Data}; | |||||||||||||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||
| 177 | sub text | ||||||||||||||||||||||||||||||||||||||||||
| 178 | 32944 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
text: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 179 | 32944 | my $text = shift->{text}; | |||||||||||||||||||||||||||||||||||||||||
| 180 | # collapse whitespace | ||||||||||||||||||||||||||||||||||||||||||
| 181 | 32944 | $text =~ s/\s\s+/ /g; | |||||||||||||||||||||||||||||||||||||||||
| 182 | # strip new lines | ||||||||||||||||||||||||||||||||||||||||||
| 183 | 32944 | $text =~ s/[\r\n]//g; | |||||||||||||||||||||||||||||||||||||||||
| 184 | # strip leading whitespace | ||||||||||||||||||||||||||||||||||||||||||
| 185 | 32944 | $text =~ s/^\s+//; | |||||||||||||||||||||||||||||||||||||||||
| 186 | # strip trailing whitespace | ||||||||||||||||||||||||||||||||||||||||||
| 187 | 32944 | $text =~ s/\s+$//; | |||||||||||||||||||||||||||||||||||||||||
| 188 | 32944 | return $text; | |||||||||||||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||
| 191 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||