/usr/share/perl5/Business/Edifact/Interchange.pm is in libbusiness-edifact-interchange-perl 0.07-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 | package Business::Edifact::Interchange;
use warnings;
use strict;
use 5.010;
use Carp;
use Encode;
use Business::Edifact::Message;
=head1 NAME
Business::Edifact::Interchange - Parse Edifact Messages For Book Ordering
=head1 VERSION
Version 0.07
=cut
our $VERSION = '0.07';
# UNOA and UNOB "correspond to the basic ascii sets of iso 646 and iso 6937"
# Version 4 of edifact should extend this to unicode
my %encoding_map = (
'UNOA' => 'ascii',
'UNOB' => 'ascii',
'UNOC' => 'iso-8859-1',
'UNOD' => 'iso-8859-2',
'UNOE' => 'iso-8859-5',
'UNOF' => 'iso-8859-7',
);
=head1 SYNOPSIS
This is a support module for EDI ordering modules being developed for the
Koha and Evergreen OS Library Management Systems
use Business::Edifact::Interchange;
my $foo = Business::Edifact::Interchange->new();
$foo->parse($edifact_message);
or
$foo->parse_file($filename);
...
The standards for using Edifact in Library Book Supply are available from
www.editeur.org
=head1 SUBROUTINES/METHODS
=head2 new
Create an Business::Edifact::Interchange object
=cut
sub new {
my $class = shift;
my $print_trace = shift;
my $self = {};
bless $self, $class;
return $self;
}
=head2 parse
parse the edifact interchange passed in the message
=cut
sub parse {
my $self = shift;
my $doc = shift;
$self->{separator} = {
component => q{\:},
data => q{\+},
decimal => q{.},
release => q{\?},
reserved => q{ },
segment => q{\'},
};
$self->{sep_class} = '\:\+\'\?';
if ( $doc =~ s/^UNA// ) { # optional
my $element = substr $doc, 0, 6, q{};
$self->read_service_string_advice($element);
}
my @segments =
split /(?<!$self->{separator}->{release})$self->{separator}->{segment} */,
$doc;
$self->{interchange} = [];
$self->{messages} = [];
$self->{msg_cnt} = 0;
if ( $segments[0] =~ m/^UNB/ ) {
my $hdr = shift @segments;
my @hdr_fields =
split /(?<!$self->{separator}->{release})$self->{separator}->{data}/,
$hdr;
push @{ $self->{interchange} },
$self->interchange_header( @hdr_fields[ 1 .. $#hdr_fields ] );
}
else {
croak 'Interchange does not begin with an Interchange header';
}
my $current_msg;
while ( my $segment = shift @segments ) {
my ( $tag, @data ) =
split /(?<!$self->{separator}->{release})$self->{separator}->{data}/,
$segment;
if ( $tag =~ /UNH/ ) {
$current_msg = $self->message_header(@data);
next;
}
if ( $tag =~ /UNT/ ) {
$self->message_trailer( $current_msg, @data );
$current_msg = undef;
next;
}
if ( $tag =~ /^UNZ/ ) {
$self->interchange_trailer(@data);
next;
}
if ( $tag =~ /^UNG/ ) {
$self->message_group_header(@data);
next;
}
if ( $tag =~ /^UNE/ ) {
$self->message_group_trailer(@data);
next;
}
$self->user_data_segment( $current_msg, $tag, @data );
}
return;
}
=head2 parse_file
Reads an edifact message from a file and parses it
Will strip the lineendings added to files by some suppliers
=cut
sub parse_file {
my $self = shift;
my $filename = shift;
$filename ||= 'SampleQuote3.txt';
open my $fh, '<', $filename or croak "Cannot open $filename : $!";
my @lines = <$fh>;
close $fh;
for (@lines) {
chomp;
s/\r$//;
}
my $msg;
if ( @lines == 1 ) {
$msg = $lines[0];
}
elsif ( @lines > 1 ) {
$msg = join q{}, @lines;
}
if ($msg) {
$self->parse($msg);
}
return;
}
=head2 user_data_segment
internal method for handling message data segments
pass to the current Business::Edifact::Message object for fuller passing
=cut
sub user_data_segment {
my ( $self, $msg, $tag, @data ) = @_;
my $d = $self->split_components(@data);
# un release data
$msg->add_segment( $tag, $d );
return;
}
=head2 message_header
create a new Business::Edifact::Message object
=cut
sub message_header {
my ( $self, @data ) = @_;
my $msg = Business::Edifact::Message->new( $self->split_components(@data) );
return $msg;
}
=head2 message_trailer
End message add completed message to my messages array
=cut
sub message_trailer {
my ( $self, $msg, @data ) = @_;
$self->{msg_cnt}++;
push @{ $self->{messages} }, $msg;
return;
}
=head2 interchange_trailer
internal method to parse and validate the
interchange trailer
=cut
sub interchange_trailer {
my ( $self, @data ) = @_;
if ( $data[0] != $self->{msg_cnt} ) {
carp "Message count error trailer says $data[0] I counted "
. $self->{msg_cnt};
}
if ( $data[1] ne $self->{control_ref} ) {
carp 'Error mismatched control refs Header:'
. $self->{control_ref}
. ' Trailer:'
. $data[1];
}
return;
}
=head2 read_service_string_advice
internal method to parse the service string advice
and set the separator values for the interchange
accordingly
=cut
sub read_service_string_advice {
my $self = shift;
my $ssa = shift;
# The six characters represent
# component data element separator :
# Data element sep +
# Decimal notation .
# Release Indicator ?
# Reseved (space)
# Segment terminator '
if ( $ssa eq q{:+.? '} ) {
# 'Standard Service String Advice';
return;
}
else {
#'Non standard Service String Advice';
my @char = unpack 'C6', $ssa;
foreach (@char) {
$_ = quotemeta $_;
}
$self->{separator} = {
component => $char[0],
data => $char[1],
decimal => $char[2],
release => $char[3],
reserved => $char[4],
segment => $char[5],
};
$self->{sep_class} = join q{}, $char[0], $char[1], $char[3], $char[5];
}
return;
}
=head2 split_components
internal method to split data field into components
=cut
sub split_components {
my ( $self, @data ) = @_;
my $d_arr = [];
for my $data_field (@data) {
my @components =
split
/(?<!$self->{separator}->{release})$self->{separator}->{component}/,
$data_field;
foreach (@components) {
s/$self->{separator}->{release}([$self->{sep_class}])/$1/g;
# convert data to utf-8
$_ = $self->{enc}->decode($_);
}
push @{$d_arr}, \@components;
}
return $d_arr;
}
=head2 interchange_header
Internal method to parse the interchange header
=cut
sub interchange_header {
my ( $self, @hdr ) = @_;
$self->{control_ref} = $hdr[4];
my $charencoding = 'iso-8859-1';
my $syntax_id = substr $hdr[0], 0, 4;
if ( exists $encoding_map{$syntax_id} ) {
$charencoding = $encoding_map{$syntax_id};
}
$self->{enc} = find_encoding($charencoding);
croak qq(encoding "$charencoding" not found) unless ref $self->{enc};
my $interchange_header = $self->split_components(@hdr);
# syntax identifier :: Syntax_id a4 'UNO'[ABC]:Syntax_version
# interchange_sender :: Sender_id:id_code_qualifier[:Address]
# interchange_recipient :: Recepient_id:code_qualifier[:Address]
# DateTime of Prep : YYMMDD:HHMM
# Interchange Control Ref
# Password
# Application Ref
# [Priority Code]
# [Ack Request]
# [Comm Agreement ID]
# [Test Indicator [ 1 == a test]]
return $interchange_header;
}
=head2 message_group_header
internal method to parse the message group header
(Currently a nop )
=cut
sub message_group_header {
my ( $self, @data ) = @_;
#TBD parse data
#say 'message_group_header';
return;
}
=head2 message_group_trailer
internal method to parse the message group trailer
(Currently a nop )
=cut
sub message_group_trailer {
my ( $self, @data ) = @_;
#TBD parse data
#say 'message_group_trailer';
return;
}
=head2 messages
Returns and array_ref of Edifact::Message objects representing
the contents of the interchange
=cut
sub messages {
my $self = shift;
if ( exists $self->{messages} ) {
return $self->{messages};
}
return;
}
=head1 WARNINGS
At present this is tested for quotes. Beware suppliers' interpretation of the
Edifact Standard can vary considerably. (And the standard is large enough to
allow considerable leeway on this). Its intended to expand this module based on
practical experience.
=head1 AUTHOR
Colin Campbell, C<< <colinsc@cpan.org> >>
=head1 BUGS
Please report any bugs or feature requests to C<bug-edifact-interchange at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Business-Edifact-Interchange>. I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.
=head1 SUPPORT
You can find documentation for this module with the perldoc command.
perldoc Business::Edifact::Interchange
=head1 ACKNOWLEDGEMENTS
=head1 LICENSE AND COPYRIGHT
Copyright 2011-2014 Colin Campbell. <colin.campbell@ptfs-europe.com>
This program is free software; you can redistribute it and/or modify it
under the terms of either: the GNU General Public License as published
by the Free Software Foundation; or the Artistic License.
See http://dev.perl.org/licenses/ for more information.
=cut
1; # End of Business::Edifact::Interchange
|