[aix-pm-cvs] modules/util/Util/XML File.pm,NONE,1.1
Status: Alpha
Brought to you by:
gonter
|
From: Gerhard G. <go...@us...> - 2017-08-01 16:28:44
|
Update of /cvsroot/aix-pm/modules/util/Util/XML In directory sfp-cvs-1.v30.ch3.sourceforge.com:/tmp/cvs-serv23293/modules/util/Util/XML Added Files: File.pm Log Message: file cache for XML items fetched via LWP --- NEW FILE: File.pm --- package Util::XML::File; use strict; # use XML::Simple; use XML::LibXML::Simple; my $DEBUG= 1; sub fetch { my $ua= shift; my $url= shift; my $cache_file= shift; my $max_age= shift; my ($xmlref, $xml); my $use_cache_file= 0; if (defined ($cache_file) && -f $cache_file # TODO: check age too! ) { $use_cache_file= 1; if (defined ($max_age)) { my @st= stat $cache_file; $use_cache_file= 0 if ($st[9] + $max_age <= time ()); print __FILE__, ' ', __LINE__, " cache: mtime=$st[9] max_age=$max_age use=$use_cache_file file=[$cache_file]\n"; } } if ($use_cache_file) { open (FI, '<:utf8', $cache_file); while (<FI>) { $xml .= $_ }; $xmlref= XMLin($xml, ForceContent => 1, ForceArray => 1); # TODO: if the file is not ok, ignore the cached file and refetch return ($xmlref, $xml); } print __LINE__, " fetch_xml_file: url=[$url]\n"; my $res= $ua->get ($url); unless ($res->is_success) { print "ERROR: Aleph-XML-Query (find) failed: ", $res->status_line, "\n"; return undef; } $xml= $res->decoded_content; print __LINE__, " get_aleph_metadata: xml=[$xml]\n" if ($DEBUG >= 0); if (defined ($cache_file)) { if (open (FO, '>:utf8', $cache_file)) { print "saving to [$cache_file]\n"; $xml=~ s#<session-id>[\w\d]+</session-id>#<session-id>dummy-session-id</session-id>#; print FO $xml; close (FO); } # TODO: else report that we can not write to the cache as we should } $xmlref= XMLin ($xml, ForceContent => 1, ForceArray => 1, KeyAttr => [ ] ); print __LINE__, " xmlref: ", main::Dumper ($xmlref) if ($DEBUG >= 0); return ($xmlref, $xml); } 1; |