| 1 | # Copyright 2009 by Prasad Balan | ||||||||||||||
| 2 | # All rights reserved. | ||||||||||||||
| 3 | # | ||||||||||||||
| 4 | # This library is free software; you can redistribute it and/or modify | ||||||||||||||
| 5 | # it under the same terms as Perl itself. | ||||||||||||||
| 6 | package X12::Parser; | ||||||||||||||
| 7 | 2 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 2 | require Exporter; | |||||||||||||
| 9 | 2 | our @ISA = qw(Exporter); | |||||||||||||
| 10 | |||||||||||||||
| 11 | # Items to export into callers namespace by default. Note: do not export | ||||||||||||||
| 12 | # names by default without a very good reason. Use EXPORT_OK instead. | ||||||||||||||
| 13 | # Do not simply export all your public functions/methods/constants. | ||||||||||||||
| 14 | # This allows declaration use X12 ':all'; | ||||||||||||||
| 15 | # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK | ||||||||||||||
| 16 | # will save memory. | ||||||||||||||
| 17 | 2 | our %EXPORT_TAGS = ( | |||||||||||||
| 18 | 'all' => [ | ||||||||||||||
| 19 | qw( | ||||||||||||||
| 20 | ) | ||||||||||||||
| 21 | ] | ||||||||||||||
| 22 | ); | ||||||||||||||
| 23 | 2 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | |||||||||||||
| 24 | 2 | our @EXPORT = qw( | |||||||||||||
| 25 | ); | ||||||||||||||
| 26 | 2 | our $VERSION = '0.80'; | |||||||||||||
| 27 | |||||||||||||||
| 28 | # Preloaded methods go here. | ||||||||||||||
| 29 | 2 | ▶ | use X12::Parser::Tree; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 30 | 2 | ▶ | use X12::Parser::Cf; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 31 | |||||||||||||||
| 32 | #constructor. | ||||||||||||||
| 33 | 2 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 34 | 2 | my $self = { | |||||||||||||
| 35 | file => undef, | ||||||||||||||
| 36 | conf => undef, | ||||||||||||||
| 37 | _TREE_ROOT => undef, | ||||||||||||||
| 38 | _TREE_POS => undef, | ||||||||||||||
| 39 | _FILE_HANDLE => undef, | ||||||||||||||
| 40 | _FILE_CLOSED => undef, | ||||||||||||||
| 41 | _SEGMENT_SEPARATOR => undef, | ||||||||||||||
| 42 | _ELEMENT_SEPARATOR => undef, | ||||||||||||||
| 43 | _SUBELEMENT_SEPARATOR => undef, | ||||||||||||||
| 44 | _NEXT_LOOP => undef, | ||||||||||||||
| 45 | _NEXT_SEGMENT => undef, | ||||||||||||||
| 46 | }; | ||||||||||||||
| 47 | 2 | return bless $self; | |||||||||||||
| 48 | } | ||||||||||||||
| 49 | |||||||||||||||
| 50 | #public method, takes the X12 handle and Cf file name as input. | ||||||||||||||
| 51 | #loads the config file and sets the separators. | ||||||||||||||
| 52 | 5 | ▶ | sub parse { | ||||||||||||
|
Subroutine
parse: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 53 | 5 | my $self = shift; | |||||||||||||
| 54 | 5 | my %params = @_; | |||||||||||||
| 55 | 5 | $self->{handle} = $params{handle}; | |||||||||||||
| 56 | 5 | $self->{conf} = $params{conf}; | |||||||||||||
| 57 | 5 | $self->{_FILE_HANDLE} = $params{handle}; | |||||||||||||
| 58 | 5 | $self->{_FILE_CLOSED} = undef; | |||||||||||||
| 59 | |||||||||||||||
| 60 | #read the config file to create the TREE object | ||||||||||||||
| 61 | 5 | my $cf = X12::Parser::Cf->new(); | |||||||||||||
| 62 | $self->{_TREE_ROOT} = $cf->load( file => "$self->{conf}" ) | ||||||||||||||
| 63 | 5 | ▶ | if defined $self->{conf}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 64 | 5 | $self->{_TREE_POS} = $self->{_TREE_ROOT}; | |||||||||||||
| 65 | |||||||||||||||
| 66 | #set the separators | ||||||||||||||
| 67 | 5 | $self->_set_separator; | |||||||||||||
| 68 | } | ||||||||||||||
| 69 | |||||||||||||||
| 70 | #public method, takes the X12 and Cf file name as input. | ||||||||||||||
| 71 | #loads the config file and sets the separators. | ||||||||||||||
| 72 | 3 | ▶ | sub parsefile { | ||||||||||||
|
Subroutine
parsefile: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||
| 73 | 3 | my $self = shift; | |||||||||||||
| 74 | 3 | my %params = @_; | |||||||||||||
| 75 | 3 | $self->{file} = $params{file}; | |||||||||||||
| 76 | 3 | $self->{conf} = $params{conf}; | |||||||||||||
| 77 | |||||||||||||||
| 78 | #chose the handle just in case this method is being called the second time | ||||||||||||||
| 79 | #without closing the file | ||||||||||||||
| 80 | 3 | ▶ | if ( defined $self->{_FILE_HANDLE} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 81 | 1 | close( $self->{_FILE_HANDLE} ); | |||||||||||||
| 82 | 1 | $self->{_FILE_CLOSED} = 1; | |||||||||||||
| 83 | } | ||||||||||||||
| 84 | 3 | ▶ | open( my $handle, "$self->{file}" ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 85 | || die "error: cannot open file $self->{file}\n"; | ||||||||||||||
| 86 | 3 | $self->parse( handle => $handle, conf => $self->{conf} ); | |||||||||||||
| 87 | } | ||||||||||||||
| 88 | |||||||||||||||
| 89 | #close the file | ||||||||||||||
| 90 | 2 | ▶ | sub closefile { | ||||||||||||
|
Subroutine
closefile: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||
| 91 | 2 | my $self = shift; | |||||||||||||
| 92 | 2 | ▶ | if ( defined $self->{_FILE_HANDLE} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 93 | 2 | close( $self->{_FILE_HANDLE} ); | |||||||||||||
| 94 | 2 | $self->{_FILE_CLOSED} = 1; | |||||||||||||
| 95 | } | ||||||||||||||
| 96 | } | ||||||||||||||
| 97 | |||||||||||||||
| 98 | #private method. sets the separators. | ||||||||||||||
| 99 | 5 | ▶ | sub _set_separator { | ||||||||||||
|
Subroutine
_set_separator: called CC=4 SCAR=16.1
| |||||||||||||||
| 100 | 5 | my $self = shift; | |||||||||||||
| 101 | 5 | my $isa = undef; | |||||||||||||
| 102 | 5 | ▶ | if ( read( $self->{_FILE_HANDLE}, $isa, 108 ) != 108 ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 103 | 0 | close( $self->{_FILE_HANDLE} ); | |||||||||||||
| 104 | 0 | $self->{_FILE_CLOSED} = 1; | |||||||||||||
| 105 | 0 | die "error: invalid file format $self->{file}\n"; | |||||||||||||
| 106 | } | ||||||||||||||
| 107 | |||||||||||||||
| 108 | #set the segment terminator | ||||||||||||||
| 109 | 5 | my $terminator = substr( $isa, 106, 2 ); | |||||||||||||
| 110 | 5 | ▶ | if ( $terminator =~ /\r\n/ ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 111 | 0 | $self->{_SEGMENT_SEPARATOR} = substr( $isa, 105, 3 ); | |||||||||||||
| 112 | } | ||||||||||||||
| 113 | elsif ( $terminator =~ /^\n/ ) { | ||||||||||||||
| 114 | 0 | $self->{_SEGMENT_SEPARATOR} = substr( $isa, 105, 2 ); | |||||||||||||
| 115 | } | ||||||||||||||
| 116 | else { | ||||||||||||||
| 117 | 5 | $self->{_SEGMENT_SEPARATOR} = substr( $isa, 105, 1 ); | |||||||||||||
| 118 | } | ||||||||||||||
| 119 | |||||||||||||||
| 120 | #set the element separator | ||||||||||||||
| 121 | 5 | $self->{_ELEMENT_SEPARATOR} = substr( $isa, 3, 1 ); | |||||||||||||
| 122 | |||||||||||||||
| 123 | #set the sub element separator | ||||||||||||||
| 124 | 5 | $self->{_SUBELEMENT_SEPARATOR} = substr( $isa, 104, 1 ); | |||||||||||||
| 125 | 5 | seek( $self->{_FILE_HANDLE}, -108, 1 ); | |||||||||||||
| 126 | } | ||||||||||||||
| 127 | |||||||||||||||
| 128 | #public method. gets the next loop. | ||||||||||||||
| 129 | 8 | ▶ | sub get_next_loop { | ||||||||||||
|
Subroutine
get_next_loop: called CC=2 SCAR=9.2
Pod
documented
| |||||||||||||||
| 130 | 8 | my $self = shift; | |||||||||||||
| 131 | 8 | ▶ | if ( defined $self->{_NEXT_LOOP} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 132 | 0 | my $loop = $self->{_NEXT_LOOP}; | |||||||||||||
| 133 | 0 | $self->{_NEXT_LOOP} = undef; | |||||||||||||
| 134 | 0 | return $loop; | |||||||||||||
| 135 | } | ||||||||||||||
| 136 | else { | ||||||||||||||
| 137 | 8 | return $self->_get_next_loop(); | |||||||||||||
| 138 | } | ||||||||||||||
| 139 | } | ||||||||||||||
| 140 | |||||||||||||||
| 141 | 4 | ▶ | sub get_next_pos_loop { | ||||||||||||
|
Subroutine
get_next_pos_loop: called CC=3 SCAR=13.1
Pod
documented
| |||||||||||||||
| 142 | 4 | my $self = shift; | |||||||||||||
| 143 | 4 | my $loop = undef; | |||||||||||||
| 144 | 4 | ▶ | if ( defined $self->{_NEXT_LOOP} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 145 | 0 | $loop = $self->{_NEXT_LOOP}; | |||||||||||||
| 146 | 0 | $self->{_NEXT_LOOP} = undef; | |||||||||||||
| 147 | 0 | return ( $., $loop ); | |||||||||||||
| 148 | } | ||||||||||||||
| 149 | else { | ||||||||||||||
| 150 | 4 | $loop = $self->_get_next_loop(); | |||||||||||||
| 151 | 4 | ▶ | if ( defined $loop ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 152 | 4 | return ( $., $loop ); | |||||||||||||
| 153 | } | ||||||||||||||
| 154 | else { | ||||||||||||||
| 155 | 0 | return; | |||||||||||||
| 156 | } | ||||||||||||||
| 157 | } | ||||||||||||||
| 158 | } | ||||||||||||||
| 159 | |||||||||||||||
| 160 | 17 | ▶ | sub get_next_pos_level_loop { | ||||||||||||
|
Subroutine
get_next_pos_level_loop: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||
| 161 | 17 | my $self = shift; | |||||||||||||
| 162 | 17 | my $loop = undef; | |||||||||||||
| 163 | 17 | ▶ | if ( defined $self->{_NEXT_LOOP} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 164 | 11 | $loop = $self->{_NEXT_LOOP}; | |||||||||||||
| 165 | 11 | $self->{_NEXT_LOOP} = undef; | |||||||||||||
| 166 | 11 | return ( $., $self->{_TREE_POS}->get_depth(), $loop ); | |||||||||||||
| 167 | } | ||||||||||||||
| 168 | else { | ||||||||||||||
| 169 | 6 | $loop = $self->_get_next_loop(); | |||||||||||||
| 170 | 6 | ▶ | if ( defined $loop ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 171 | 5 | return ( $., $self->{_TREE_POS}->get_depth(), $loop ); | |||||||||||||
| 172 | } | ||||||||||||||
| 173 | else { | ||||||||||||||
| 174 | 1 | return; | |||||||||||||
| 175 | } | ||||||||||||||
| 176 | } | ||||||||||||||
| 177 | } | ||||||||||||||
| 178 | |||||||||||||||
| 179 | #private method. does the hard lifting. | ||||||||||||||
| 180 | 30 | ▶ | sub _get_next_loop { | ||||||||||||
|
Subroutine
_get_next_loop: called CC=6 SCAR=17.9
| |||||||||||||||
| 181 | 30 | my $self = shift; | |||||||||||||
| 182 | 30 | my ( $segment, $file_handle, $node, $loop, @element ); | |||||||||||||
| 183 | 30 | local $/; | |||||||||||||
| 184 | 30 | $/ = $self->{_SEGMENT_SEPARATOR}; | |||||||||||||
| 185 | 30 | $file_handle = $self->{_FILE_HANDLE}; | |||||||||||||
| 186 | 30 | $node = $self->{_TREE_POS}; | |||||||||||||
| 187 | 30 | $self->{_LOOP} = []; | |||||||||||||
| 188 | 30 | ▶ | if ( defined $self->{_NEXT_SEGMENT} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 189 | 27 | push( @{ $self->{_LOOP} }, $self->{_NEXT_SEGMENT} ); | |||||||||||||
| 190 | 27 | $self->{_NEXT_SEGMENT} = undef; | |||||||||||||
| 191 | } | ||||||||||||||
| 192 | 30 | ▶ | if ( defined $self->{_FILE_CLOSED} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 193 | 1 | return undef; | |||||||||||||
| 194 | } | ||||||||||||||
| 195 | 29 | ▶ | while ( $segment = <$file_handle> ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 196 | 60 | chomp($segment); | |||||||||||||
| 197 | 60 | @element = split( /\Q$self->{_ELEMENT_SEPARATOR}\E/, $segment ); | |||||||||||||
| 198 | 60 | $loop = $self->_check_child_match( $node, \@element ); | |||||||||||||
| 199 | 60 | ▶ | if ( defined $loop ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 200 | 7 | $self->{_NEXT_SEGMENT} = $segment; | |||||||||||||
| 201 | 7 | return $loop; | |||||||||||||
| 202 | } | ||||||||||||||
| 203 | 53 | $loop = $self->_check_parent_match( $node, \@element ); | |||||||||||||
| 204 | 53 | ▶ | if ( defined $loop ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 205 | 21 | $self->{_NEXT_SEGMENT} = $segment; | |||||||||||||
| 206 | 21 | return $loop; | |||||||||||||
| 207 | } | ||||||||||||||
| 208 | 32 | push( @{ $self->{_LOOP} }, $segment ); | |||||||||||||
| 209 | } | ||||||||||||||
| 210 | 1 | close($file_handle); | |||||||||||||
| 211 | 1 | $self->{_FILE_CLOSED} = 1; | |||||||||||||
| 212 | 1 | return undef; | |||||||||||||
| 213 | } | ||||||||||||||
| 214 | |||||||||||||||
| 215 | #private method. check if any of the child loops match | ||||||||||||||
| 216 | 60 | ▶ | sub _check_child_match { | ||||||||||||
|
Subroutine
_check_child_match: called CC=3 SCAR=11.0
| |||||||||||||||
| 217 | 60 | my ( $self, $node, $elements ) = @_; | |||||||||||||
| 218 | 60 | ▶ | for ( my $i = 0 ; $i < $node->get_child_count() ; $i++ ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 219 | 23 | my $child = $node->get_child($i); | |||||||||||||
| 220 | 23 | ▶ | if ( $child->is_loop_start($elements) ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 221 | 7 | $self->{_TREE_POS} = $child; | |||||||||||||
| 222 | 7 | return $child->get_name(); | |||||||||||||
| 223 | } | ||||||||||||||
| 224 | } | ||||||||||||||
| 225 | 53 | return undef; | |||||||||||||
| 226 | } | ||||||||||||||
| 227 | |||||||||||||||
| 228 | #private method. check if any of the parent loops match | ||||||||||||||
| 229 | 98 | ▶ | sub _check_parent_match { | ||||||||||||
|
Subroutine
_check_parent_match: called CC=4 SCAR=13.9
| |||||||||||||||
| 230 | 98 | my ( $self, $node, $elements ) = @_; | |||||||||||||
| 231 | 98 | my $parent = $node->get_parent(); | |||||||||||||
| 232 | 98 | ▶ | if ( !defined $parent ) { return undef; } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 233 | 66 | ▶ | for ( my $i = 0 ; $i < $parent->get_child_count() ; $i++ ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 234 | 387 | my $child = $parent->get_child($i); | |||||||||||||
| 235 | 387 | ▶ | if ( $child->is_loop_start($elements) ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 236 | 21 | $self->{_TREE_POS} = $child; | |||||||||||||
| 237 | 21 | return $child->get_name(); | |||||||||||||
| 238 | } | ||||||||||||||
| 239 | } | ||||||||||||||
| 240 | 45 | $self->_check_parent_match( $parent, $elements ); | |||||||||||||
| 241 | } | ||||||||||||||
| 242 | |||||||||||||||
| 243 | #get the segments in the loop | ||||||||||||||
| 244 | 12 | ▶ | sub get_loop_segments { | ||||||||||||
|
Subroutine
get_loop_segments: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 245 | 12 | my $self = shift; | |||||||||||||
| 246 | 12 | my $loop = $self->_get_next_loop(); | |||||||||||||
| 247 | 12 | $self->{_NEXT_LOOP} = $loop; | |||||||||||||
| 248 | 12 | return @{ $self->{_LOOP} }; | |||||||||||||
| 249 | } | ||||||||||||||
| 250 | |||||||||||||||
| 251 | 0 | ▶ | sub get_segment_separator { | ||||||||||||
|
Subroutine
get_segment_separator: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 252 | 0 | my $self = shift; | |||||||||||||
| 253 | 0 | return $self->{_SEGMENT_SEPARATOR}; | |||||||||||||
| 254 | } | ||||||||||||||
| 255 | |||||||||||||||
| 256 | 0 | ▶ | sub get_element_separator { | ||||||||||||
|
Subroutine
get_element_separator: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 257 | 0 | my $self = shift; | |||||||||||||
| 258 | 0 | return $self->{_ELEMENT_SEPARATOR}; | |||||||||||||
| 259 | } | ||||||||||||||
| 260 | |||||||||||||||
| 261 | 0 | ▶ | sub get_subelement_separator { | ||||||||||||
|
Subroutine
get_subelement_separator: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 262 | 0 | my $self = shift; | |||||||||||||
| 263 | 0 | return $self->{_SUBELEMENT_SEPARATOR}; | |||||||||||||
| 264 | } | ||||||||||||||
| 265 | |||||||||||||||
| 266 | 0 | ▶ | sub print_tree { | ||||||||||||
|
Subroutine
print_tree: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||
| 267 | 0 | my $self = shift; | |||||||||||||
| 268 | 0 | my ( $pad, $index, $segment ); | |||||||||||||
| 269 | 0 | ▶ | while ( my ( $pos, $level, $loop ) = $self->get_next_pos_level_loop ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 270 | 0 | $pad = ' |' x $level; | |||||||||||||
| 271 | 0 | print " $pad--$loop\n"; | |||||||||||||
| 272 | 0 | $pad = ' |' x ( $level + 1 ); | |||||||||||||
| 273 | 0 | my @loop = $self->get_loop_segments; | |||||||||||||
| 274 | 0 | foreach $segment (@loop) { | |||||||||||||
| 275 | 0 | $index = sprintf( "%+7s", $pos++ ); | |||||||||||||
| 276 | 0 | print "$index$pad-- $segment\n"; | |||||||||||||
| 277 | } | ||||||||||||||
| 278 | } | ||||||||||||||
| 279 | } | ||||||||||||||
| 280 | |||||||||||||||
| 281 | #private method only called for tests | ||||||||||||||
| 282 | 1 | ▶ | sub _print_tree { | ||||||||||||
|
Subroutine
_print_tree: called CC=3 SCAR=11.0
| |||||||||||||||
| 283 | 1 | my $self = shift; | |||||||||||||
| 284 | 1 | my ( $pad, $index, $segment, $tree ); | |||||||||||||
| 285 | 1 | ▶ | while ( my ( $pos, $level, $loop ) = $self->get_next_pos_level_loop ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 286 | 12 | $pad = ' |' x $level; | |||||||||||||
| 287 | 12 | $tree .= " $pad--$loop\n"; | |||||||||||||
| 288 | 12 | $pad = ' |' x ( $level + 1 ); | |||||||||||||
| 289 | 12 | my @loop = $self->get_loop_segments; | |||||||||||||
| 290 | 12 | foreach $segment (@loop) { | |||||||||||||
| 291 | 32 | $index = sprintf( "%+7s", $pos++ ); | |||||||||||||
| 292 | 32 | $tree .= "$index$pad-- $segment\n"; | |||||||||||||
| 293 | } | ||||||||||||||
| 294 | } | ||||||||||||||
| 295 | 1 | return $tree; | |||||||||||||
| 296 | } | ||||||||||||||
| 297 | 2 | 1; | |||||||||||||
| 298 | __END__ | ||||||||||||||