| 1 | # Filename: Parse.pm | ||||||||||||||||||||||||||||
| 2 | # Parse the Open Financial Exchange format | ||||||||||||||||||||||||||||
| 3 | # http://www.ofx.net/ | ||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||
| 5 | # Created January 30, 2008 Brandon Fosdick <bfoz@bfoz.net> | ||||||||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||||||||
| 7 | # Copyright 2008 Brandon Fosdick <bfoz@bfoz.net> (BSD License) | ||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||
| 9 | # $Id: Parse.pm,v 1.2 2008/03/04 04:22:27 bfoz Exp $ | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | package Finance::OFX::Parse; | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 2 | our $VERSION = '2'; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 2 | ▶ | use Finance::OFX::Tree; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 2 | ▶ | use HTTP::Date; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | sub is_unique | ||||||||||||||||||||||||||||
| 22 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
is_unique: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 23 | 0 | my $a = shift; | |||||||||||||||||||||||||||
| 24 | 0 | ▶ | return undef unless ref($a) eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | 0 | my %saw; | |||||||||||||||||||||||||||
| 26 | 0 | ▶ | $saw{$_->{name}}++ || return 0 for @{$a}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 0 | 1; | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | sub collapse | ||||||||||||||||||||||||||||
| 31 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
collapse: not called CC=10 SCAR=47.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 32 | 0 | my $tree = shift; | |||||||||||||||||||||||||||
| 33 | 0 | ▶ | return $tree unless ref($tree) eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # Recurse on any elements that have arrays for content | ||||||||||||||||||||||||||||
| 36 | 0 | $_->{content} = collapse($_->{content}) for( @{$tree} ); | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | # The passed array can be converted to a hash if all of it's nodes have | ||||||||||||||||||||||||||||
| 39 | # unique names | ||||||||||||||||||||||||||||
| 40 | 0 | my %a; | |||||||||||||||||||||||||||
| 41 | 0 | ▶ | if( is_unique($tree) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | { | ||||||||||||||||||||||||||||
| 43 | 0 | $a{$_->{name}} = $_->{content} for ( @{$tree} ); | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | else # Duplicate names can be converted to an array | ||||||||||||||||||||||||||||
| 46 | { | ||||||||||||||||||||||||||||
| 47 | 0 | my %b; | |||||||||||||||||||||||||||
| 48 | 0 | $b{$_->{name}}++ for @{$tree}; | |||||||||||||||||||||||||||
| 49 | # grep(!$b{$_->{name}}++, @{$tree}); | ||||||||||||||||||||||||||||
| 50 | 0 | ▶ | ($b{$_} > 1) && ($a{$_} = []) for keys %b; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 0 | for( @{$tree} ) | |||||||||||||||||||||||||||
| 52 | { | ||||||||||||||||||||||||||||
| 53 | 0 | ▶ | push(@{$a{$_->{name}}}, $_->{content}), next if $b{$_->{name}} > 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 0 | $a{$_->{name}} = $_->{content}; | |||||||||||||||||||||||||||
| 55 | # ($b{$_->{name}} > 1) ? push(@{$a{$_->{name}}}, $_->{content}) : | ||||||||||||||||||||||||||||
| 56 | # ($a{$_->{name}} = $_->{content}); | ||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | 0 | return \%a; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | sub parse_dates | ||||||||||||||||||||||||||||
| 63 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
parse_dates: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 64 | 0 | my $tree = shift; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 0 | ▶ | if( ref($tree) eq 'ARRAY' ) | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 67 | { | ||||||||||||||||||||||||||||
| 68 | 0 | parse_dates($_) for @{$tree}; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | elsif( ref($tree) eq 'HASH' ) | ||||||||||||||||||||||||||||
| 71 | { | ||||||||||||||||||||||||||||
| 72 | 0 | for( keys %{$tree} ) | |||||||||||||||||||||||||||
| 73 | { | ||||||||||||||||||||||||||||
| 74 | 0 | ▶ | if( /^dt/ ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | { | ||||||||||||||||||||||||||||
| 76 | # Add seconds spacer | ||||||||||||||||||||||||||||
| 77 | 0 | $tree->{$_} =~ s/^([0-9]{12})([0-9]{2})/$1:$2/; | |||||||||||||||||||||||||||
| 78 | # Add minutes spacer | ||||||||||||||||||||||||||||
| 79 | 0 | $tree->{$_} =~ s/^([0-9]{10})([0-9]{2})/$1:$2/; | |||||||||||||||||||||||||||
| 80 | # Add date spacers | ||||||||||||||||||||||||||||
| 81 | 0 | $tree->{$_} =~ s/^([0-9]{4})([0-9]{2})([0-9]{2})/$1-$2\-$3 /; | |||||||||||||||||||||||||||
| 82 | # Add a leading zero to the timezone offset, if needed | ||||||||||||||||||||||||||||
| 83 | 0 | $tree->{$_} =~ s/\[([-+]?)([0-9]):[A-Z]{3}\]/ $1\x30$2\x30\x30/; | |||||||||||||||||||||||||||
| 84 | # Handle timezone offsets that were already 2 digits | ||||||||||||||||||||||||||||
| 85 | 0 | $tree->{$_} =~ s/\[([-+]?)([0-9]{1,2}):[A-Z]{3}\]/ $1$2\x30\x30/; | |||||||||||||||||||||||||||
| 86 | # Do the conversion | ||||||||||||||||||||||||||||
| 87 | 0 | $tree->{$_} = str2time($tree->{$_}, 'GMT'); | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | else | ||||||||||||||||||||||||||||
| 90 | { | ||||||||||||||||||||||||||||
| 91 | 0 | parse_dates($tree->{$_}); | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | sub parse | ||||||||||||||||||||||||||||
| 98 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
parse: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 99 | 0 | $_[0] =~ s/\x0D//g; # Un-networkify newlines | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | my ($header, $body) = split /\n\n/, shift, 2; | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | # Parse the OFX header block | ||||||||||||||||||||||||||||
| 104 | 0 | $header =~ s/^\s//; # Strip leading whitespace | |||||||||||||||||||||||||||
| 105 | 0 | my %header = split /[:\n]/, $header; # Convert to a hash | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 0 | ▶ | return undef unless ($header{OFXHEADER} == '100') and ($header{DATA} eq 'OFXSGML'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$header{"OFXHEADER"} == 100 and $header{"DATA"} eq "OFXSGML"
MC/DC0%0/2
$header{"OFXHEADER"} == 100 and $header{"DATA"} eq "OFXSGML"
$header{"OFXHEADER"} == 100
$header{"DATA"} eq "OFXSGML"
Truth table0%0/3
A and B
A= $header{"OFXHEADER"} == 100B= $header{"DATA"} eq "OFXSGML"
| |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 0 | my $tree = Finance::OFX::Tree::parse($body); | |||||||||||||||||||||||||||
| 110 | 0 | ▶ | return undef unless $tree and ($tree->[0]{name} eq 'ofx'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$tree and $tree->[0]{"name"} eq "ofx"
MC/DC0%0/2
$tree and $tree->[0]{"name"} eq "ofx"
$tree
$tree->[0]{"name"} eq "ofx"
Truth table0%0/3
A and B
A= $treeB= $tree->[0]{"name"} eq "ofx"
| |||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 0 | $tree = collapse($tree); # Collapse the parse tree into a hash | |||||||||||||||||||||||||||
| 113 | 0 | parse_dates($tree); # Convert date elements to Unix time | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | # Merge the header hash into the parse tree | ||||||||||||||||||||||||||||
| 116 | 0 | $tree->{header} = \%header; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | return $tree; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | sub parse_file | ||||||||||||||||||||||||||||
| 122 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
parse_file: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 123 | 0 | my $file = shift; | |||||||||||||||||||||||||||
| 124 | 0 | ▶ | return undef unless $file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | # my $text = do { local(@ARGV, $/) = $file; <> }; | ||||||||||||||||||||||||||||
| 126 | 0 | my $text = read_file($file); | |||||||||||||||||||||||||||
| 127 | 0 | ▶ | return undef unless $text; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 0 | return parse($text); | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 2 | 1; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | __END__ | ||||||||||||||||||||||||||||