use XML::Simple;
use File::Find;
+use File::Slurp;
use Data::Dump qw/dump/;
=cut
-our $VERSION = '0.01';
+our $VERSION = '0.02';
=head1 FUNCTIONS
# do something with $l which is line of input file
return $l;
},
+ mungle => 'conf/mungle/xml-mungle.pl',
}
Options:
path to directory with xml files ending in C<.xml>
+=item mungle
+
+path to perl data_structure mungler which will be called to pre-normalize
+hash produced by this module.
+
+It's ugly and souldn't be really here, but I didn't wanted to write separate
+input module for each possible XML in the face of the earth, and having perl
+power to transform hash is just... Best solution :-)
+
=back
=cut
my $arg = {@_};
+warn "#### arg = ",dump( $arg );
+
my $log = $self->_get_logger();
$log->logdie("can't find path ", $arg->{path}, ": $!\n") unless -d $arg->{path};
$self->{_files} = [ sort @files ];
+ if ( my $path = $arg->{mungle} ) {
+ $log->logdie("can't find $path: $!") unless -r $path;
+ $log->info("using $path as mungle rules");
+
+ $self->{mungle_rules} = read_file( $path ) || $log->logdie("can't open $path: $!");
+ }
+
$self ? return $self : return undef;
}
my $log = $self->_get_logger();
- my $xml = XMLin(
+ our $xml = XMLin(
$path,
# ForceArray => 1,
# ForceContent => 1,
-# KeepRoot => 1,
+ KeepRoot => 1,
# SuppressEmpty => 1, # '' undef
) || $log->logdie("can't open $path: $!");
$log->debug("fetch_rec( $mfn ) => $path => xml is ",sub { dump($xml) });
+ our $ds;
+
+ if ( my $rules = $self->{mungle_rules} ) {
+
+ sub get_ds {
+ warn "### get_ds xml = ",dump($xml);
+ return $xml;
+ }
+ sub set_ds {
+ my $hash = {@_};
+ warn "### set_ds hash = ",dump($hash);
+ foreach my $f ( keys %$hash ) {
+ warn "+++ $f ", dump( $hash->{$f} ),"\n";
+ $ds->{$f} = $hash->{$f};
+ }
+ warn "### set_ds mungle_ds = ",dump($ds);
+ }
+ eval "$rules";
+ $log->logdie("mungle rules $path error: $@") if $@;
+
+ warn "### set_ds after mungle_rules ds = ",dump($ds);
+ } else {
+
+ $ds = $xml;
+
+ }
+
# add mfn
- $xml->{'000'} = [ $mfn ];
+ $ds->{'000'} = [ $mfn ];
- return $xml;
+ return $ds;
}
use strict;
use blib;
-use Test::More tests => 12;
+use Test::More tests => 18;
BEGIN {
use_ok( 'WebPAC::Test' );
throws_ok { $input->open( path => '/tmp/does_not_exit', %LOG ) } qr/can't find path/, 'open with non-existant path';
ok(my $db = $input->open(
- path => "$abs_path/data/xml/"
+ path => "$abs_path/data/xml/",
+ mungle => "$abs_path/conf/mungle/xml.pl",
), "open");
ok(my $size = $input->size, "size");
--- /dev/null
+<xml>
+<array>element 1</array>
+<array>element 2</array>
+<array>element 3</array>
+<array>element 4</array>
+<array>element 5</array>
+<array>element 6</array>
+<array>element 7</array>
+<array>element 8</array>
+<array>element 9</array>
+</xml>