From b4cac7fd661b6d2a385e89399854dde984d594fe Mon Sep 17 00:00:00 2001 From: Charles McGarvey Date: Wed, 12 Jun 2019 22:05:15 -0600 Subject: [PATCH] decode entities in attribute values --- lib/File/HomeBank.pm | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/lib/File/HomeBank.pm b/lib/File/HomeBank.pm index 31ca39d..c786fdd 100644 --- a/lib/File/HomeBank.pm +++ b/lib/File/HomeBank.pm @@ -30,15 +30,13 @@ use App::HomeBank2Ledger::Util qw(commify); use Exporter qw(import); use Scalar::Util qw(refaddr); use Time::Piece; +use XML::Entities; use XML::Parser::Lite; our $VERSION = '9999.999'; # VERSION our @EXPORT_OK = qw(parse_string parse_file); -sub _croak { require Carp; Carp::croak(@_) } -sub _usage { _croak("Usage: @_\n") } - my %ACCOUNT_TYPES = ( 0 => 'none', 1 => 'bank', @@ -104,6 +102,9 @@ my %TRANSACTION_PAYMODES = ( 11 => 'directdebit', ); +sub _croak { require Carp; Carp::croak(@_) } +sub _usage { _croak("Usage: @_\n") } + =method new $homebank = File::HomeBank->new(string => $str); @@ -415,7 +416,7 @@ sub find_transaction_transfer_pair { $transations = $homebank->sorted_transactions; -Get an arrayref of transactions sorted by date (earliest first). +Get an arrayref of transactions sorted by date (oldest first). =cut @@ -532,6 +533,12 @@ sub parse_string { shift; my $node = shift; my %attr = @_; + + # decode all attribute values + for my $key (keys %attr) { + $attr{$key} = _decode_xml_entities($attr{$key}); + } + if ($node eq 'properties') { $attr{currency} = delete $attr{curr} if $attr{curr}; %properties = %attr; @@ -603,6 +610,14 @@ sub parse_string { }; } +sub _decode_xml_entities { + my $str = shift; + # decoding entities can be extremely slow, so don't bother if it doesn't look like there are any + # entities to decode + return $str if $str !~ /&(?:#\d+)|[A-Za-z0-9]+;/; + return XML::Entities::decode('all', $str); +} + sub _rdn_to_unix_epoch { my $rdn = shift; my $jan01_1970 = 719163; -- 2.45.2