XML-Simple-2.24/ 0000775 0001750 0001750 00000000000 13075040223 012336 5 ustar grant grant XML-Simple-2.24/t/ 0000775 0001750 0001750 00000000000 13075040223 012601 5 ustar grant grant XML-Simple-2.24/t/B_Hooks.t 0000644 0001750 0001750 00000005346 13075040223 014320 0 ustar grant grant
use strict;
use Test::More;
use File::Spec;
plan tests => 12;
use_ok('XML::Simple');
SKIP: {
eval { require Tie::IxHash };
skip "Tie::IxHash not installed", 3 if $@;
eval <<'EOF';
package SimpleOrder;
use base qw(XML::Simple);
use Tie::IxHash;
sub new_hashref {
my $self = shift;
my %hash;
tie %hash, 'Tie::IxHash', @_;
return \%hash;
}
EOF
ok(!$@, 'no errors processing SimpleOrder');
my $xs = SimpleOrder->new;
my $xml = q{
IIIIIIIVVVIVII
};
my $expected = {
'one' => { 'content' => 'I' },
'two' => { 'content' => 'II' },
'three' => { 'content' => 'III' },
'four' => { 'content' => 'IV' },
'five' => { 'content' => 'V' },
'six' => { 'content' => 'VI' },
'seven' => { 'content' => 'VII' },
};
my $data = $xs->xml_in($xml);
is_deeply($data->{num}, $expected, 'hash content looks good');
is_deeply(
[ keys %{$data->{num}} ],
[ qw(one two three four five six seven) ],
'order of the hash keys looks good too'
);
}
my $xs = XML::Simple->new(cache => 'storable');
my $sx = ElbarotsXS->new(cache => 'storable');
isa_ok($sx, 'XML::Simple', 'object of class ElbarotsXS');
my $src_file = File::Spec->catfile('t', 'test1.xml');
is(
$xs->storable_filename($src_file),
File::Spec->catfile('t', 'test1.stor'),
'default storable cache filename looks good'
);
my $cache_file = File::Spec->catfile('t', '1tset.stor'),;
is(
$sx->storable_filename($src_file),
$cache_file,
'overridden storable cache filename looks good'
);
SKIP: {
eval { require Storable };
skip "Storable not installed", 2 if $@;
unlink($cache_file),
ok(! -e $cache_file, 'overridden cache file does not exist before parse');
my $data = $sx->xml_in($src_file);
ok(-e $cache_file, 'overridden cache file does exist after parse');
unlink($cache_file),
}
my $data = eval {
$xs = XML::Simple->new(cache => 'floogle');
$xs->xml_in($src_file);
};
ok($@, 'bad cache scheme was rejected');
$data = eval {
$sx = ElbarotsXS->new(cache => 'floogle');
$sx->xml_in($src_file);
};
ok(! $@, 'custom cache scheme was not rejected');
is_deeply(
$data,
{ data => 'floogle' },
'custom cache reading method delivered the goods'
);
exit 0;
package ElbarotsXS;
use base 'XML::Simple';
sub storable_filename {
my($self, $path) = @_;
my($vol, $dir, $file) = File::Spec->splitpath( $path );
$file =~ s{\.xml$}{};
return File::Spec->catpath($vol, $dir, reverse($file) . '.stor');
}
sub cache_read_floogle {
return { data => 'floogle' };
}
XML-Simple-2.24/t/test1.xml 0000644 0001750 0001750 00000000037 13075040223 014361 0 ustar grant grant
XML-Simple-2.24/t/4_MemShare.t 0000644 0001750 0001750 00000010775 13075040223 014722 0 ustar grant grant
use strict;
use warnings;
use Test::More;
use File::Spec;
# Initialise filenames and check they're there
my $SrcFile = File::Spec->catfile('t', 'desertnet.src');
my $XMLFile = File::Spec->catfile('t', 'desertnet4.xml');
unless(-e $SrcFile) {
plan skip_all => 'test data missing';
}
# Make sure we can write to the filesystem and check it uses the same
# clock as the machine we're running on.
my $t0 = time();
unless(open(XML, '>', $XMLFile)) {
plan skip_all => "can't create test file '$XMLFile': $!";
}
close(XML);
my $t1 = (stat($XMLFile))[9];
my $t2 = time();
if($t1 < $t0 or $t2 < $t1) {
plan skip_all => 'time moved backwards!'
}
plan tests => 8;
##############################################################################
# S U P P O R T R O U T I N E S
##############################################################################
##############################################################################
# Copy a file
#
sub CopyFile {
my($src, $dst) = @_;
open(my $in, $src) or die "open(<$src): $!";
local($/) = undef;
my $data = <$in>;
close($in);
open(my $out, '>', $dst) or die "open(>$dst): $!";
print $out $data;
close($out);
return(1);
}
##############################################################################
# Wait until the current time is greater than the supplied value
#
sub PassTime {
my($Target) = @_;
while(time <= $Target) {
sleep 1;
}
}
##############################################################################
# T E S T R O U T I N E S
##############################################################################
use XML::Simple;
# Initialise test data
my $Expected = {
'server' => {
'sahara' => {
'osversion' => '2.6',
'osname' => 'solaris',
'address' => [
'10.0.0.101',
'10.0.1.101'
]
},
'gobi' => {
'osversion' => '6.5',
'osname' => 'irix',
'address' => '10.0.0.102'
},
'kalahari' => {
'osversion' => '2.0.34',
'osname' => 'linux',
'address' => [
'10.0.0.103',
'10.0.1.103'
]
}
}
};
ok(CopyFile($SrcFile, $XMLFile), 'copied known good source file');
$t0 = (stat($XMLFile))[9]; # Remember its timestamp
# Parse it with caching enabled
my $opt = XMLin($XMLFile, cache => 'memshare');
is_deeply($opt, $Expected, 'parsed expected data from file');
if ('VMS' eq $^O) {
1 while (unlink($XMLFile));
} else {
unlink($XMLFile);
}
ok(! -e $XMLFile, 'deleted the XML source file');
open(FILE, ">$XMLFile"); # Re-create it (empty)
close(FILE);
ok(-e $XMLFile, 'and recreated it (empty)');
$t1 = $t0 - 1;
eval { utime($t1, $t1, $XMLFile); }; # but wind back the clock
$t2 = (stat($XMLFile))[9]; # Skip these tests if that didn't work
SKIP: {
skip 'no utime', 2 if($t2 >= $t0);
$opt = XMLin($XMLFile, cache => 'memshare');
is_deeply($opt, $Expected, 'got expected values from the cache');
is(-s $XMLFile, 0, 'even though the XML file is empty');
}
PassTime(time()); # Ensure timestamp changes
open(FILE, ">$XMLFile"); # Write some new data to the XML file
print FILE qq(\n);
close(FILE);
PassTime(time()); # Ensure current time later than file time
# Parse again with caching enabled
$opt = XMLin($XMLFile, cache => 'memshare');
is_deeply($opt, { one => 1, two => 2}, 'parsed new data through cache');
$opt->{three} = 3; # Alter the returned structure
# Retrieve again from the cache
my $opt2 = XMLin($XMLFile, cache => 'memshare');
is($opt2->{three}, 3, 'cache was modified');
unlink($XMLFile);
exit(0);
XML-Simple-2.24/t/lib/ 0000775 0001750 0001750 00000000000 13075040223 013347 5 ustar grant grant XML-Simple-2.24/t/lib/TagsToUpper.pm 0000755 0001750 0001750 00000001314 13075040223 016122 0 ustar grant grant package TagsToUpper;
use XML::SAX::Base;
use vars qw(@ISA);
@ISA = ('XML::SAX::Base');
sub start_element {
my $self = shift;
my $element = shift;
# print Data::Dumper->Dump([$element], ['element']);
to_upper($element);
foreach (values(%{$element->{Attributes}})) { to_upper($_); }
$self->SUPER::start_element($element);
}
sub end_element {
my $self = shift;
my $element = shift;
to_upper($element);
$self->SUPER::end_element($element);
}
sub to_upper {
my $ref = shift;
$ref->{LocalName} = uc($ref->{LocalName}) if($ref->{LocalName});
$ref->{Name} = uc($ref->{Name}) if($ref->{LocalName});
$ref->{Prefix} = uc($ref->{Prefix}) if($ref->{LocalName});
}
1;
XML-Simple-2.24/t/author-pod-syntax.t 0000644 0001750 0001750 00000000503 13075040223 016370 0 ustar grant grant #!perl
BEGIN {
unless ($ENV{AUTHOR_TESTING}) {
require Test::More;
Test::More::plan(skip_all => 'these tests are for testing by the author');
}
}
# This file was automatically generated by Dist::Zilla::Plugin::PodSyntaxTests.
use strict; use warnings;
use Test::More;
use Test::Pod 1.41;
all_pod_files_ok();
XML-Simple-2.24/t/5_MemCopy.t 0000644 0001750 0001750 00000011132 13075040223 014557 0 ustar grant grant
use strict;
use warnings;
use Test::More;
use File::Spec;
eval { require Storable; };
unless($INC{'Storable.pm'}) {
plan skip_all => 'no Storable.pm';
}
# Initialise filenames and check they're there
my $SrcFile = File::Spec->catfile('t', 'desertnet.src');
my $XMLFile = File::Spec->catfile('t', 'desertnet5.xml');
unless(-e $SrcFile) {
plan skip_all => 'test data missing';
}
# Make sure we can write to the filesystem and check it uses the same
# clock as the machine we're running on.
my $t0 = time();
unless(open(XML, '>', $XMLFile)) {
plan skip_all => "can't create test file '$XMLFile': $!";
}
close(XML);
my $t1 = (stat($XMLFile))[9];
my $t2 = time();
if($t1 < $t0 or $t2 < $t1) {
plan skip_all => 'time moved backwards!'
}
plan tests => 7;
##############################################################################
# S U P P O R T R O U T I N E S
##############################################################################
##############################################################################
# Copy a file
#
sub CopyFile {
my($src, $dst) = @_;
open(my $in, $src) or die "open(<$src): $!";
local($/) = undef;
my $data = <$in>;
close($in);
open(my $out, '>', $dst) or die "open(>$dst): $!";
print $out $data;
close($out);
return(1);
}
##############################################################################
# Wait until the current time is greater than the supplied value
#
sub PassTime {
my($Target) = @_;
while(time <= $Target) {
sleep 1;
}
}
##############################################################################
# T E S T R O U T I N E S
##############################################################################
use XML::Simple;
# Initialise test data
my $Expected = {
'server' => {
'sahara' => {
'osversion' => '2.6',
'osname' => 'solaris',
'address' => [
'10.0.0.101',
'10.0.1.101'
]
},
'gobi' => {
'osversion' => '6.5',
'osname' => 'irix',
'address' => '10.0.0.102'
},
'kalahari' => {
'osversion' => '2.0.34',
'osname' => 'linux',
'address' => [
'10.0.0.103',
'10.0.1.103'
]
}
}
};
ok(CopyFile($SrcFile, $XMLFile), 'copied source XML file');
$t0 = (stat($XMLFile))[9]; # Remember its timestamp
# Parse it with caching enabled
my $opt = XMLin($XMLFile, cache => 'memcopy');
is_deeply($opt, $Expected, 'parsed expected data through the cache');
if ('VMS' eq $^O) {
1 while (unlink($XMLFile));
} else {
unlink($XMLFile);
}
ok(! -e $XMLFile, 'deleted the source XML file');
open(FILE, ">$XMLFile"); # Re-create it (empty)
close(FILE);
$t1 = $t0 - 1;
eval { utime($t1, $t1, $XMLFile); }; # but wind back the clock
$t2 = (stat($XMLFile))[9]; # Skip these tests if that didn't work
SKIP: {
skip 'no utime', 2 if($t2 >= $t0);
$opt = XMLin($XMLFile, cache => 'memcopy');
is_deeply($opt, $Expected, 'got what we expected from the cache');
is(-s $XMLFile, 0, 'even though the source XML file is empty');
}
PassTime(time()); # Ensure source file will be newer
open(FILE, ">$XMLFile"); # Write some new data to the XML file
print FILE qq(\n);
close(FILE);
PassTime(time()); # Ensure current time later than file time
# Parse again with caching enabled
$opt = XMLin($XMLFile, cache => 'memcopy');
is_deeply($opt, { one => 1, two => 2}, 'parsed expected data through cache');
$opt->{three} = 3; # Alter the returned structure
# Retrieve again from the cache
my $opt2 = XMLin($XMLFile, cache => 'memcopy');
ok(!defined($opt2->{three}), 'cache not modified');
# Clean up and go
unlink($XMLFile);
exit(0);
XML-Simple-2.24/t/A_XMLParser.t 0000644 0001750 0001750 00000004723 13075040223 015047 0 ustar grant grant
use strict;
use warnings;
use Test::More;
use IO::File;
use File::Spec;
# Initialise filenames and check they're there
my $XMLFile = File::Spec->catfile('t', 'test1.xml'); # t/test1.xml
unless(-e $XMLFile) {
plan skip_all => 'Test data missing';
}
eval { require XML::Parser; };
unless($INC{'XML/Parser.pm'}) {
plan skip_all => 'no XML::Parser';
}
plan tests => 14;
use XML::Simple;
my $last_warning = '';
my $opt;
# Use environment variable to set preferred parser
$ENV{XML_SIMPLE_PREFERRED_PARSER} = 'XML::Parser';
# Try using a SAX-only option
{
local($SIG{__WARN__}) = \&warn_handler;
$opt = XMLin('', nsexpand => 1);
}
isnt($last_warning, '', "Parsing caused warning (as expected)");
like($last_warning, qr/'nsexpand' option requires XML::SAX/,
'Message contained expected text');
is_deeply($opt, {y => 'z'}, "Parsing was successful");
# Check for deprecation warning
{
local($SIG{__WARN__}) = \&warn_handler;
$last_warning = '';
$opt = XMLin('', ParserOpts => [ ParseParamEnt => 1 ]);
}
isnt($last_warning, '', "Using ParserOpts caused warning (as expected)");
like($last_warning, qr/'ParserOpts' is deprecated/,
'Message contained expected text');
is_deeply($opt, {y => 'z'}, "Parsing was successful");
# Check it doesn't happen if warnings disabled
{
no warnings;
local($SIG{__WARN__}) = \&warn_handler;
$last_warning = '';
$opt = XMLin('', ParserOpts => [ ParseParamEnt => 1 ]);
}
is($last_warning, '', "ParserOpts warning uppressed successfully");
is_deeply($opt, {y => 'z'}, "Parsing was successful");
# Try parsing a string
$opt = eval {
XMLin(q());
};
my $expected = {
name1 => 'value1',
name2 => 'value2',
};
is($@, '', "No error when parsing");
is_deeply($opt, $expected, 'matches expectations (attributes)');
# Try parsing a named external file
$opt = eval{ XMLin($XMLFile); };
is($@, '', "XML::Parser didn't choke on named external file");
is_deeply($opt, {
location => 't/test1.xml'
}, 'and contents parsed as expected');
# Try parsing from an IO::Handle
my $fh = new IO::File;
$XMLFile = File::Spec->catfile('t', '1_XMLin.xml'); # t/1_XMLin.xml
eval {
$fh->open($XMLFile) || die "$!";
$opt = XMLin($fh);
};
is($@, '', "XML::Parser didn't choke on an IO::File object");
is($opt->{location}, 't/1_XMLin.xml', 'and it parsed the right file');
exit(0);
sub warn_handler {
$last_warning = $_[0];
}
XML-Simple-2.24/t/1_XMLin.t 0000644 0001750 0001750 00000112713 13075040223 014200 0 ustar grant grant
use strict;
use warnings;
use Test::More;
use IO::File;
use File::Spec;
use XML::Simple;
# Initialise filenames and check they're there
my $XMLFile = File::Spec->catfile('t', 'test1.xml'); # t/test1.xml
unless(-e $XMLFile) {
plan skip_all => 'Test data missing';
}
plan tests => 132;
my $last_warning = '';
is($@, '', 'Module compiled OK');
my $version = 'unknown';
if(open my $chg, ') {
last if ($version) = $_ =~ /^([\d\._]+) /;
}
close($chg);
}
unless($XML::Simple::VERSION eq $version) {
diag("Warning: XML::Simple::VERSION = $XML::Simple::VERSION (Changes version: $version)");
}
# Start by parsing an extremely simple piece of XML
my $opt = XMLin(q());
my $expected = {
name1 => 'value1',
name2 => 'value2',
};
ok(1, "XMLin() didn't crash");
ok(defined($opt), 'and it returned a value');
is(ref($opt), 'HASH', 'and a hasref at that');
is_deeply($opt, $expected, 'matches expectations (attributes)');
# Now try a slightly more complex one that returns the same value
$opt = XMLin(q(
value1value2
));
is_deeply($opt, $expected, 'same again with nested elements');
# And something else that returns the same (line break included to pick up
# missing /s bug)
$opt = XMLin(q());
is_deeply($opt, $expected, 'attributes in empty element');
# Try something with two lists of nested values
$opt = XMLin(q(
value1.1value1.2value1.3value2.1value2.2value2.3)
);
is_deeply($opt, {
name1 => [ 'value1.1', 'value1.2', 'value1.3' ],
name2 => [ 'value2.1', 'value2.2', 'value2.3' ],
}, 'repeated child elements give arrays of scalars');
# Now a simple nested hash
$opt = XMLin(q(
)
);
is_deeply($opt, {
item => { name1 => 'value1', name2 => 'value2' }
}, 'nested element gives hash');
# Now a list of nested hashes
$opt = XMLin(q(
)
);
is_deeply($opt, {
item => [
{ name1 => 'value1', name2 => 'value2' },
{ name1 => 'value3', name2 => 'value4' }
]
}, 'repeated child elements give list of hashes');
# Now a list of nested hashes transformed into a hash using default key names
my $string = q(
);
my $target = {
item => {
item1 => { attr1 => 'value1', attr2 => 'value2' },
item2 => { attr1 => 'value3', attr2 => 'value4' }
}
};
$opt = XMLin($string);
is_deeply($opt, $target, "array folded on default key 'name'");
# Same thing left as an array by suppressing default key names
$target = {
item => [
{name => 'item1', attr1 => 'value1', attr2 => 'value2' },
{name => 'item2', attr1 => 'value3', attr2 => 'value4' }
]
};
my @cont_key = (contentkey => '-content');
$opt = XMLin($string, keyattr => [], @cont_key);
is_deeply($opt, $target, 'not folded when keyattr turned off');
# Same again with alternative key suppression
$opt = XMLin($string, keyattr => {}, @cont_key);
is_deeply($opt, $target, 'still works when keyattr is empty hash');
# Try the other two default key attribute names
$opt = XMLin(q(
), @cont_key);
is_deeply($opt, {
item => {
item1 => { attr1 => 'value1', attr2 => 'value2' },
item2 => { attr1 => 'value3', attr2 => 'value4' }
}
}, "folded on default key 'key'");
$opt = XMLin(q(
), @cont_key);
is_deeply($opt, {
item => {
item1 => { attr1 => 'value1', attr2 => 'value2' },
item2 => { attr1 => 'value3', attr2 => 'value4' }
}
}, "folded on default key 'id'");
# Similar thing using non-standard key names
my $xml = q(
);
$target = {
item => {
item1 => { attr1 => 'value1', attr2 => 'value2' },
item2 => { attr1 => 'value3', attr2 => 'value4' }
}
};
$opt = XMLin($xml, keyattr => [qw(xname)], @cont_key);
is_deeply($opt, $target, "folded on non-default key 'xname'");
# And with precise element/key specification
$opt = XMLin($xml, keyattr => { 'item' => 'xname' }, @cont_key);
is_deeply($opt, $target, 'same again but keyattr set with hash');
# Same again but with key field further down the list
$opt = XMLin($xml, keyattr => [qw(wibble xname)], @cont_key);
is_deeply($opt, $target, 'keyattr as array with value in second position');
# Same again but with key field supplied as scalar
$opt = XMLin($xml, keyattr => qw(xname), @cont_key);
is_deeply($opt, $target, 'keyattr as scalar');
# Same again but with mixed-case option name
$opt = XMLin($xml, KeyAttr => qw(xname), @cont_key);
is_deeply($opt, $target, 'KeyAttr as scalar');
# Same again but with underscores in option name
$opt = XMLin($xml, key_attr => qw(xname), @cont_key);
is_deeply($opt, $target, 'key_attr as scalar');
# Weird variation, not exactly what we wanted but it is what we expected
# given the current implementation and we don't want to break it accidently
$xml = q(
);
$target = { item => {
'three' => { 'value' => 3 },
'a' => { 'value' => 1, 'id' => 'one' },
'two' => { 'value' => 2 }
}
};
$opt = XMLin($xml, @cont_key);
is_deeply($opt, $target, 'fold same array on two different keys');
# Or somewhat more as one might expect
$target = { item => {
'one' => { 'value' => '1', 'name' => 'a' },
'two' => { 'value' => '2' },
'three' => { 'value' => '3' },
}
};
$opt = XMLin($xml, keyattr => { 'item' => 'id' }, @cont_key);
is_deeply($opt, $target, 'same again but with priority switch');
# Now a somewhat more complex test of targetting folding
$xml = q(
);
$target = {
'car' => {
'LW1804' => {
'id' => 2,
'make' => 'GM',
'option' => {
'9926543-1167' => { 'key' => 1, 'desc' => 'Steering Wheel' }
}
},
'SH6673' => {
'id' => 1,
'make' => 'Ford',
'option' => {
'6389733317-12' => { 'key' => 1, 'desc' => 'Electric Windows' },
'3735498158-01' => { 'key' => 2, 'desc' => 'Leather Seats' },
'5776155953-25' => { 'key' => 3, 'desc' => 'Sun Roof' }
}
}
}
};
$opt = XMLin($xml, forcearray => 1,
keyattr => { 'car' => 'license', 'option' => 'pn' }, @cont_key);
is_deeply($opt, $target, 'folded on multi-key keyattr hash');
# Now try leaving the keys in place
$target = {
'car' => {
'LW1804' => {
'id' => 2,
'make' => 'GM',
'option' => {
'9926543-1167' => { 'key' => 1, 'desc' => 'Steering Wheel',
'-pn' => '9926543-1167' }
},
license => 'LW1804'
},
'SH6673' => {
'id' => 1,
'make' => 'Ford',
'option' => {
'6389733317-12' => { 'key' => 1, 'desc' => 'Electric Windows',
'-pn' => '6389733317-12' },
'3735498158-01' => { 'key' => 2, 'desc' => 'Leather Seats',
'-pn' => '3735498158-01' },
'5776155953-25' => { 'key' => 3, 'desc' => 'Sun Roof',
'-pn' => '5776155953-25' }
},
license => 'SH6673'
}
}
};
$opt = XMLin($xml, forcearray => 1, keyattr => { 'car' => '+license', 'option' => '-pn' }, @cont_key);
is_deeply($opt, $target, "same again but with '+' prefix to copy keys");
# Confirm the stringifying references bug is fixed
$xml = q(
Bob21Kate22);
$target = {
item => [
{ age => '21', name => { firstname => 'Bob'} },
{ age => '22', name => { firstname => 'Kate'} },
]
};
{
local($SIG{__WARN__}) = \&warn_handler;
$last_warning = '';
$opt = XMLin($xml, @cont_key);
is_deeply($opt, $target, "did not fold on default key with non-scalar value");
is($last_warning, '', 'no warning issued');
$last_warning = '';
$opt = XMLin($xml, keyattr => { item => 'name' }, @cont_key);
is_deeply($opt, $target, "did not fold on specific key with non-scalar value");
isnt($last_warning, '', 'warning issued as expected');
like($last_warning,
qr{ element has non-scalar 'name' key attribute},
'text in warning is correct'
);
$last_warning = '';
$opt = XMLin($xml, keyattr => [ 'name' ], @cont_key);
is_deeply($opt, $target, "same again but with keyattr as array");
isnt($last_warning, '', 'warning issued as expected');
like($last_warning,
qr{ element has non-scalar 'name' key attribute},
'text in warning is correct'
);
$last_warning = '';
{
no warnings 'XML::Simple';
$opt = XMLin($xml, keyattr => { item => 'name' }, @cont_key);
is_deeply($opt, $target, "did not fold on specific key with non-scalar value");
is($last_warning, '', 'no warning issued (as expected)');
}
$last_warning = '';
my $xitems = q(redheavyornery);
my $items = {
'item' => [
{ 'name' => 'color', 'content' => 'red', },
{ 'name' => 'mass', 'content' => 'heavy', },
{ 'nime' => 'disposition', 'content' => 'ornery', }
]
};
$opt = XMLin($xitems, keyattr => { item => 'name' }, @cont_key);
is_deeply($opt, $items, "did not fold when element missing key attribute");
like($last_warning, qr{Warning: element has no 'name' key attribute},
'expected warning issued');
$last_warning = '';
{
no warnings;
$opt = XMLin($xitems, keyattr => { item => 'name' }, @cont_key);
is_deeply($opt, $items, "same again");
is($last_warning, '', 'but with no warning this time');
}
$last_warning = '';
$xitems = q(redheavyornerygreen);
$items = {
'item' => {
'color' => 'green',
'mass' => 'heavy',
'disposition' => 'ornery',
}
};
$opt = XMLin($xitems, keyattr => { item => 'name' }, @cont_key);
is_deeply($opt, $items, "folded elements despite non-unique key attribute");
like($last_warning, qr{Warning: element has non-unique value in 'name' key attribute: color},
'expected warning issued');
$last_warning = '';
$opt = XMLin($xitems, keyattr => [ 'name' ], @cont_key);
is_deeply($opt, $items, "same again but with keyattr as array");
like($last_warning, qr{Warning: element has non-unique value in 'name' key attribute: color},
'expected warning issued');
$last_warning = '';
{
no warnings;
$opt = XMLin($xitems, keyattr => { item => 'name' }, @cont_key);
is_deeply($opt, $items, "same again");
is($last_warning, '', 'but with no warning this time');
}
}
# Make sure that the root element name is preserved if we ask for it
$target = XMLin("$xml", forcearray => 1,
keyattr => { 'car' => '+license', 'option' => '-pn' },
@cont_key);
$opt = XMLin( $xml, forcearray => 1, keeproot => 1,
keyattr => { 'car' => '+license', 'option' => '-pn' },
@cont_key);
is_deeply($opt, $target, 'keeproot option works');
# confirm that CDATA sections parse correctly
$xml = q{Hello, world!]]>};
$opt = XMLin($xml, @cont_key);
is_deeply($opt, {
'cdata' => 'Hello, world!'
}, 'CDATA section parsed correctly');
$xml = q{one]]>two]]>};
$opt = XMLin($xml, @cont_key);
is_deeply($opt, {
'x' => 'onetwo'
}, 'CDATA section containing markup characters parsed correctly');
# Try parsing a named external file
$opt = eval{ XMLin($XMLFile); };
is($@, '', "XMLin didn't choke on named external file");
is_deeply($opt, {
location => 't/test1.xml'
}, 'and contents parsed as expected');
# Try parsing default external file (scriptname.xml in script directory)
$opt = eval { XMLin(); };
is($@, '', "XMLin didn't choke on un-named (default) external file");
is_deeply($opt, {
location => 't/1_XMLin.xml'
}, 'and contents parsed as expected');
# Try parsing named file in a directory in the searchpath
$opt = eval {
XMLin('test2.xml', searchpath => [
'dir1', 'dir2', File::Spec->catdir('t', 'subdir'), @cont_key
] );
};
is($@, '', 'XMLin found file using searchpath');
is_deeply($opt, {
location => 't/subdir/test2.xml'
}, 'and contents parsed as expected');
# Ensure we get expected result if file does not exist
$opt = undef;
$opt = eval {
XMLin('bogusfile.xml', searchpath => 't' ); # should 'die'
};
is($opt, undef, 'XMLin choked on nonexistant file');
like($@, qr/Could not find bogusfile.xml in/, 'with the expected message');
# same again, but with no searchpath
$opt = undef;
$opt = eval { XMLin('bogusfile.xml'); };
is($opt, undef, 'nonexistant file not found in current directory');
like($@, qr/File does not exist: bogusfile.xml/, 'with the expected message');
# Confirm searchpath is ignored if filename includes directory component
$opt = undef;
$opt = eval {
XMLin(File::Spec->catfile('subdir', 'test2.xml'), searchpath => 't' );
};
is($opt, undef, 'search path ignored when pathname supplied');
like($@, qr/Could not find/, 'failed with expected message');
# Try parsing from an IO::Handle
my $fh = new IO::File;
$XMLFile = File::Spec->catfile('t', '1_XMLin.xml'); # t/1_XMLin.xml
eval {
$fh->open($XMLFile) || die "$!";
$opt = XMLin($fh, @cont_key);
};
is($@, '', "XMLin didn't choke on an IO::File object");
is($opt->{location}, 't/1_XMLin.xml', 'and it parsed the right file');
# Try parsing from STDIN
close(STDIN);
eval {
open(STDIN, $XMLFile) || die "$!";
$opt = XMLin('-');
};
is($@, '', "XMLin didn't choke on STDIN ('-')");
is($opt->{location}, 't/1_XMLin.xml', 'and data parsed correctly');
# Confirm anonymous array handling works in general
$xml = q{
0.00.10.21.01.11.22.02.12.2
};
$expected = {
row => [
[ '0.0', '0.1', '0.2' ],
[ '1.0', '1.1', '1.2' ],
[ '2.0', '2.1', '2.2' ]
]
};
$opt = XMLin($xml, @cont_key);
is_deeply($opt, $expected, 'anonymous arrays parsed correctly');
# Confirm it still works with array folding disabled (was a bug)
$opt = XMLin($xml, keyattr => [], @cont_key);
is_deeply($opt, $expected, 'anonymous arrays parsed correctly');
# Confirm anonymous array handling works in special top level case
$opt = XMLin(q{
onetwothree
}, @cont_key);
is_deeply($opt, [
qw(one two three)
], 'top level anonymous array returned arrayref');
$opt = XMLin(q(
12.12.2.12.2.2
), @cont_key);
is_deeply($opt, [
1,
[
'2.1', [ '2.2.1', '2.2.2']
]
], 'nested anonymous arrays parsed correctly');
# Check for the dreaded 'content' attribute
$xml = q(
text
);
$opt = XMLin($xml);
is_deeply($opt, {
item => {
content => 'text',
attr => 'value'
}
}, "'content' key appears as expected");
# And check that we can change its name if required
$opt = XMLin($xml, contentkey => 'text_content');
is_deeply($opt, {
item => {
text_content => 'text',
attr => 'value'
}
}, "'content' key successfully renamed to 'text'");
# Check that it doesn't get screwed up by forcearray option
$xml = q(text content);
$opt = XMLin($xml, forcearray => 1);
is_deeply($opt, {
'attr' => 'value',
'content' => 'text content'
}, "'content' key not munged by forcearray");
# Test that we can force all text content to parse to hash values
$xml = q(text1text2);
$opt = XMLin($xml, forcecontent => 1);
is_deeply($opt, {
'x' => { 'content' => 'text1' },
'y' => { 'a' => 2, 'content' => 'text2' }
}, 'gratuitous use of content key works as expected');
# And that this is compatible with changing the key name
$opt = XMLin($xml, forcecontent => 1, contentkey => '0');
is_deeply($opt, {
'x' => { 0 => 'text1' },
'y' => { 'a' => 2, 0 => 'text2' }
}, "even when we change it's name to 'text'");
# Confirm that spurious 'content' keys are *not* eliminated after array folding
$xml = q(FirstSecond);
$opt = XMLin($xml, forcearray => [ 'x' ], keyattr => {x => 'y'});
is_deeply($opt, {
x => {
one => { content => 'First' },
two => { content => 'Second' },
}
}, "spurious content keys not eliminated after folding");
# unless we ask nicely
$xml = q(FirstSecond);
$opt = XMLin(
$xml, forcearray => [ 'x' ], keyattr => {x => 'y'}, contentkey => '-content'
);
is_deeply($opt, {
x => {
one => 'First',
two => 'Second',
}
}, "spurious content keys not eliminated after folding");
# Check that mixed content parses in the weird way we expect
$xml = q(Text with a bold wordMixed but no attributes);
is_deeply(XMLin($xml, @cont_key), {
'p1' => {
'content' => [ 'Text with a ', ' word' ],
'class' => 'mixed',
'b' => 'bold'
},
'p2' => {
'content' => [ 'Mixed ', ' no attributes' ],
'b' => 'but'
}
}, "mixed content doesn't work - no surprises there");
# Confirm single nested element rolls up into a scalar attribute value
$string = q(
value
);
$opt = XMLin($string);
is_deeply($opt, {
name => 'value'
}, 'nested element rolls up to scalar');
# Unless 'forcearray' option is specified
$opt = XMLin($string, forcearray => 1, @cont_key);
is_deeply($opt, {
name => [ 'value' ]
}, 'except when forcearray is enabled');
# Confirm array folding of single nested hash
$string = q();
$opt = XMLin($string, forcearray => 1, @cont_key);
is_deeply($opt, {
'inner' => { 'one' => { 'value' => 1 } }
}, 'array folding works with single nested hash');
# But not without forcearray option specified
$opt = XMLin($string, forcearray => 0, @cont_key);
is_deeply($opt, {
'inner' => { 'name' => 'one', 'value' => 1 }
}, 'but not if forcearray is turned off');
# Test advanced features of forcearray
$xml = q(iiiiii3c
);
$opt = XMLin($xml, forcearray => [ 'two' ], @cont_key);
is_deeply($opt, {
'zero' => '0',
'one' => 'i',
'two' => [ 'ii' ],
'three' => [ 'iii', 3, 'c' ]
}, 'selective application of forcearray successful');
# Test forcearray regexes
$xml = q(iiiiiiivv
);
$opt = XMLin($xml, forcearray => [ qr/^f/, 'two', qr/n/ ], @cont_key);
is_deeply($opt, {
'zero' => '0',
'one' => [ 'i' ],
'two' => [ 'ii' ],
'three' => 'iii',
'four' => [ 'iv' ],
'five' => [ 'v' ],
}, 'forcearray using regex successful');
# Same again but a single regexp rather than in an arrayref
$opt = XMLin($xml, forcearray => qr/^f|e$/, @cont_key);
is_deeply($opt, {
'zero' => '0',
'one' => [ 'i' ],
'two' => 'ii',
'three' => [ 'iii'],
'four' => [ 'iv' ],
'five' => [ 'v' ],
}, 'forcearray using a single regex successful');
# Test 'noattr' option
$xml = q(text
);
$opt = XMLin($xml, noattr => 1, @cont_key);
is_deeply($opt, { nest => 'text' }, 'attributes successfully skipped');
# And make sure it doesn't screw up array folding
$xml = q{aalphabbetaggamma
};
$opt = XMLin($xml, noattr => 1, @cont_key);
is_deeply($opt, {
'item' => {
'a' => { 'value' => 'alpha' },
'b' => { 'value' => 'beta' },
'g' => { 'value' => 'gamma' }
}
}, 'noattr does not intefere with array folding');
# Confirm empty elements parse to empty hashrefs
$xml = q(
bob
);
$opt = XMLin($xml, noattr => 1, @cont_key);
is_deeply($opt, {
'name' => 'bob',
'outer' => {
'inner1' => {},
'inner2' => {}
}
}, 'empty elements parse to hashrefs');
# Unless 'suppressempty' is enabled
$opt = XMLin($xml, noattr => 1, suppressempty => 1, @cont_key);
is_deeply($opt, { 'name' => 'bob', }, 'or are suppressed');
# Check behaviour when 'suppressempty' is set to to undef;
$opt = XMLin($xml, noattr => 1, suppressempty => undef, @cont_key);
is_deeply($opt, {
'name' => 'bob',
'outer' => {
'inner1' => undef,
'inner2' => undef
}
}, "or parse to 'undef'");
# Check behaviour when 'suppressempty' is set to to empty string;
$opt = XMLin($xml, noattr => 1, suppressempty => '', @cont_key);
is_deeply($opt, {
'name' => 'bob',
'outer' => {
'inner1' => '',
'inner2' => ''
}
}, 'or parse to an empty string');
# Confirm completely empty XML parses to undef with 'suppressempty'
$xml = q(
);
$opt = XMLin($xml, noattr => 1, suppressempty => 1, @cont_key);
is($opt, undef, 'empty document parses to undef');
# Confirm nothing magical happens with grouped elements
$xml = q(before/usr/bin/usr/local/binafter);
$opt = XMLin($xml);
is_deeply($opt, {
prefix => 'before',
dirs => {
dir => [ '/usr/bin', '/usr/local/bin' ]
},
suffix => 'after',
}, 'grouped tags parse normally');
# unless we specify how the grouping works
$xml = q(before/usr/bin/usr/local/binafter);
$opt = XMLin($xml, grouptags => {dirs => 'dir'} );
is_deeply($opt, {
prefix => 'before',
dirs => [ '/usr/bin', '/usr/local/bin' ],
suffix => 'after',
}, 'disintermediation of grouped tags works');
# try again with multiple groupings
$xml = q(before/usr/bin/usr/local/binbetweenvt100xtermafter);
$opt = XMLin($xml, grouptags => {dirs => 'dir', terms => 'term'} );
is_deeply($opt, {
prefix => 'before',
dirs => [ '/usr/bin', '/usr/local/bin' ],
infix => 'between',
terms => [ 'vt100', 'xterm' ],
suffix => 'after',
}, 'disintermediation works with multiple groups');
# confirm folding and ungrouping work together
$xml = q(before/usr/bin/usr/local/binafter);
$opt = XMLin($xml, keyattr => {dir => 'name'}, grouptags => {dirs => 'dir'} );
is_deeply($opt, {
prefix => 'before',
dirs => {
first => { content => '/usr/bin' },
second => { content => '/usr/local/bin' },
},
suffix => 'after',
}, 'folding and ungrouping work together');
# confirm folding, ungrouping and content stripping work together
$xml = q(before/usr/bin/usr/local/binafter);
$opt = XMLin($xml,
contentkey => '-text',
keyattr => {dir => 'name'},
grouptags => {dirs => 'dir'}
);
is_deeply($opt, {
prefix => 'before',
dirs => {
first => '/usr/bin',
second => '/usr/local/bin',
},
suffix => 'after',
}, 'folding, ungrouping and content stripping work together');
# confirm folding fails as expected even with ungrouping but (no forcearray)
$xml = q(before/usr/binafter);
$opt = XMLin($xml,
contentkey => '-text',
keyattr => {dir => 'name'},
grouptags => {dirs => 'dir'}
);
is_deeply($opt, {
prefix => 'before',
dirs => { name => 'first', text => '/usr/bin'},
suffix => 'after',
}, 'folding without forcearray but with ungrouping fails as expected');
# but works with forcearray enabled
$xml = q(before/usr/binafter);
$opt = XMLin($xml,
contentkey => '-text',
forcearray => [ 'dir' ],
keyattr => {dir => 'name'},
grouptags => {dirs => 'dir'}
);
is_deeply($opt, {
prefix => 'before',
dirs => {'first' => '/usr/bin'},
suffix => 'after',
}, 'folding with forcearray and ungrouping works');
# Test variable expansion - when no variables are defined
$xml = q(${conf_dir}/appname.conf${log_dir}/appname.log${log_dir}/appname.dbg);
$opt = XMLin($xml, contentkey => '-content');
is_deeply($opt, {
file => {
config_file => '${conf_dir}/appname.conf',
log_file => '${log_dir}/appname.log',
debug_file => '${log_dir}/appname.dbg',
},
opt => { docs => '${have_docs}' },
bogus => { value => '${undef}' }
}, 'undefined variables are left untouched');
# try again but with variables defined in advance
$opt = XMLin($xml,
contentkey => '-content',
variables => { conf_dir => '/etc', log_dir => '/var/log',
have_docs => 'true' }
);
is_deeply($opt, {
file => {
config_file => '/etc/appname.conf',
log_file => '/var/log/appname.log',
debug_file => '/var/log/appname.dbg',
},
opt => { docs => 'true' },
bogus => { value => '${undef}' }
}, 'substitution of pre-defined variables works');
# now try defining them in the XML
$xml = q(/etc/var/logfalsesearch.perl.orgbogus${conf_dir}/appname.conf${log_dir}/appname.log${log_dir}/appname.dbg${bad/name});
$opt = XMLin($xml, contentkey => '-content', varattr => 'xsvar');
is_deeply($opt, {
file => {
config_file => '/etc/appname.conf',
log_file => '/var/log/appname.log',
debug_file => '/var/log/appname.dbg',
bogus_file => '${bad/name}', # '/' is not valid in a var name
},
opt => { docs => 'false' },
site => { url => 'http://search.perl.org/' },
dir => [
{ xsvar => 'conf_dir', content => '/etc' },
{ xsvar => 'log_dir', content => '/var/log' },
],
cfg => [
{ xsvar => 'have_docs', content => 'false' },
{ xsvar => 'host.domain', content => 'search.perl.org' },
{ xsvar => 'bad/name', content => 'bogus' },
],
}, 'variables defined in XML work');
# confirm that variables in XML are merged with pre-defined ones
$xml = q(/var/log${conf_dir}/appname.conf${log_dir}/appname.log${log_dir}/appname.dbg);
$opt = XMLin($xml,
contentkey => '-content',
varattr => 'xsvar',
variables => { conf_dir => '/etc', log_dir => '/tmp' }
);
is_deeply($opt, {
file => {
config_file => '/etc/appname.conf',
log_file => '/var/log/appname.log',
debug_file => '/var/log/appname.dbg',
},
dir => { xsvar => 'log_dir', content => '/var/log' },
}, 'variables defined in XML merged successfully with predefined vars');
# confirm that a variables are expanded in variable definitions
$xml = q(/usr/local/apache${prefix}${exec_prefix}/bin);
$opt = XMLin($xml,
contentkey => '-content',
varattr => 'name',
grouptags => { dirs => 'dir' },
);
is_deeply($opt, {
dirs => {
prefix => '/usr/local/apache',
exec_prefix => '/usr/local/apache',
bin_dir => '/usr/local/apache/bin',
}
}, 'variables are expanded in later variable definitions');
# Confirm only a hash is acceptable to grouptags and variables
$_ = eval { $opt = XMLin($xml, grouptags => [ 'dir' ]); };
ok(!defined($_), 'grouptags requires a hash');
like($@, qr/Illegal value for 'GroupTags' option - expected a hashref/,
'with correct error message');
$_ = eval { $opt = XMLin($xml, variables => [ 'dir' ]); };
ok(!defined($_), 'variables requires a hash');
like($@, qr/Illegal value for 'Variables' option - expected a hashref/,
'with correct error message');
# Try to disintermediate on the wrong child key
$xml = q(before/usr/bin/usr/local/binafter);
$opt = XMLin($xml, grouptags => {dirs => 'dir'} );
is_deeply($opt, {
prefix => 'before',
dirs => { lib => [ '/usr/bin', '/usr/local/bin' ] },
suffix => 'after',
}, 'disintermediation using wrong child key - as expected');
# Test option error handling
$_ = eval { XMLin('', rootname => 'fred') }; # not valid for XMLin()
is($_, undef, 'invalid options are trapped');
like($@, qr/Unrecognised option:/, 'with correct error message');
$_ = eval { XMLin('', 'searchpath') };
is($_, undef, 'invalid number of options are trapped');
like($@, qr/Options must be name=>value pairs \(odd number supplied\)/,
'with correct error message');
# Test the NormaliseSpace option
$xml = q( Jane
Doe
three
four
);
$opt = XMLin($xml, KeyAttr => [ 'name' ], NormaliseSpace => 1);
ok(ref($opt->{user}) eq 'HASH', "NS-1: folding OK");
ok(exists($opt->{user}->{'Joe Bloggs'}), "NS-2: space normalised in hash key");
ok(exists($opt->{user}->{'Jane Doe'}), "NS-3: space normalised in hash key");
like($opt->{user}->{'Jane Doe'}->{id}, qr{^\s\s+three\s\s+four\s\s+$}s,
"NS-4: space not normalised in hash value");
$opt = XMLin($xml, KeyAttr => { user => 'name' }, NormaliseSpace => 1);
ok(ref($opt->{user}) eq 'HASH', "NS-1a: folding OK");
ok(exists($opt->{user}->{'Joe Bloggs'}), "NS-2a: space normalised in hash key");
ok(exists($opt->{user}->{'Jane Doe'}), "NS-3a: space normalised in hash key");
like($opt->{user}->{'Jane Doe'}->{id}, qr{^\s\s+three\s\s+four\s\s+$}s,
"NS-4a: space not normalised in hash value");
$opt = XMLin($xml, KeyAttr => [ 'name' ], NormaliseSpace => 2);
ok(ref($opt->{user}) eq 'HASH', "NS-5: folding OK");
ok(exists($opt->{user}->{'Joe Bloggs'}), "NS-6: space normalised in hash key");
like($opt->{user}->{'Joe Bloggs'}->{id}, qr{^one\stwo$}s,
"NS-7: space normalised in attribute value");
ok(exists($opt->{user}->{'Jane Doe'}), "NS-8: space normalised in hash key");
like($opt->{user}->{'Jane Doe'}->{id}, qr{^three\sfour$}s,
"NS-9: space normalised in element text content");
# confirm NormaliseSpace works in anonymous arrays too
$xml = q( one two three
four five six seveneightnine );
$opt = XMLin($xml, NormaliseSpace => 2);
is_deeply($opt, [ 'one two', 'three four five', 'six', 'seveneightnine' ],
"NS-10: space normalised in anonymous array");
# Check that American speeling works too
$opt = XMLin($xml, NormalizeSpace => 2);
is_deeply($opt, [ 'one two', 'three four five', 'six', 'seveneightnine' ],
"NS-11: space normalized in anonymous array");
# Check that attributes called 'value' are not special
$xml = q();
$opt = XMLin($xml);
is_deeply($opt, {
'today' => { value => "today.png" },
'nav-prev' => { value => "prev.png" },
'nav-home' => { value => "home.png" },
'nav-next' => { value => "next.png" },
}, "Nothing special about 'value' attributes");
# Now turn on the ValueAttr option and try again
$opt = XMLin($xml, ValueAttr => [ 'value' ]);
is_deeply($opt, {
'today' => "today.png",
'nav-prev' => "prev.png",
'nav-home' => "home.png",
'nav-next' => "next.png",
}, "ValueAttr as arrayref works");
# Try with a list of different ValueAttr names
$xml = q();
$opt = XMLin($xml, ValueAttr => [ qw(xxx yyy zzz) ]);
is_deeply($opt, {
'today' => "today.png",
'nav-prev' => "prev.png",
'nav-home' => "home.png",
'nav-next' => { value => "next.png" },
}, "ValueAttr as arrayref works");
# Try specifying ValueAttr as a hashref
$xml = q();
$opt = XMLin($xml,
ValueAttr => {
'today' => 'xxx',
'nav-home' => 'yyy',
'nav-next' => 'value'
}
);
is_deeply($opt, {
'today' => "today.png",
'nav-prev' => { value => "prev.png" },
'nav-home' => "home.png",
'nav-next' => "next.png",
}, "ValueAttr as hashref works too");
# Confirm that there's no conflict with KeyAttr or ContentKey defaults
$xml = q(red);
$opt = XMLin($xml, ValueAttr => { 'today' => 'value' });
is_deeply($opt, {
today => 'today.png',
animal => {
lion => { age => 7 },
elephant => { age => 97 },
},
colour => { rgb => '#FF0000', content => 'red' },
}, "ValueAttr as hashref works too");
# Now for a 'real world' test, try slurping in an SRT config file
$opt = XMLin(File::Spec->catfile('t', 'srt.xml'),
forcearray => 1, @cont_key
);
$target = {
'global' => [
{
'proxypswd' => 'bar',
'proxyuser' => 'foo',
'exclude' => [
'/_vt',
'/save\\b',
'\\.bak$',
'\\.\\$\\$\\$$'
],
'httpproxy' => 'http://10.1.1.5:8080/',
'tempdir' => 'C:/Temp'
}
],
'pubpath' => {
'test1' => {
'source' => [
{
'label' => 'web_source',
'root' => 'C:/webshare/web_source'
}
],
'title' => 'web_source -> web_target1',
'package' => {
'images' => { 'dir' => 'wwwroot/images' }
},
'target' => [
{
'label' => 'web_target1',
'root' => 'C:/webshare/web_target1',
'temp' => 'C:/webshare/web_target1/temp'
}
],
'dir' => [ 'wwwroot' ]
},
'test2' => {
'source' => [
{
'label' => 'web_source',
'root' => 'C:/webshare/web_source'
}
],
'title' => 'web_source -> web_target1 & web_target2',
'package' => {
'bios' => { 'dir' => 'wwwroot/staff/bios' },
'images' => { 'dir' => 'wwwroot/images' },
'templates' => { 'dir' => 'wwwroot/templates' }
},
'target' => [
{
'label' => 'web_target1',
'root' => 'C:/webshare/web_target1',
'temp' => 'C:/webshare/web_target1/temp'
},
{
'label' => 'web_target2',
'root' => 'C:/webshare/web_target2',
'temp' => 'C:/webshare/web_target2/temp'
}
],
'dir' => [ 'wwwroot' ]
},
'test3' => {
'source' => [
{
'label' => 'web_source',
'root' => 'C:/webshare/web_source'
}
],
'title' => 'web_source -> web_target1 via HTTP',
'addexclude' => [ '\\.pdf$' ],
'target' => [
{
'label' => 'web_target1',
'root' => 'http://127.0.0.1/cgi-bin/srt_slave.plx',
'noproxy' => 1
}
],
'dir' => [ 'wwwroot' ]
}
}
};
is_deeply($opt, $target, 'successfully read an SRT config file');
exit(0);
sub warn_handler {
$last_warning = $_[0];
}
XML-Simple-2.24/t/srt.xml 0000644 0001750 0001750 00000003720 13075040223 014133 0 ustar grant grant
/_vt/save\b\.bak$\.\$\$\$$wwwrootwwwrootwwwroot\.pdf$
XML-Simple-2.24/t/subdir/ 0000775 0001750 0001750 00000000000 13075040223 014071 5 ustar grant grant XML-Simple-2.24/t/subdir/test2.xml 0000644 0001750 0001750 00000000046 13075040223 015652 0 ustar grant grant
XML-Simple-2.24/t/7_SaxStuff.t 0000644 0001750 0001750 00000020032 13075040223 014752 0 ustar grant grant
use strict;
use warnings;
use Test::More;
use File::Spec;
use IO::File;
BEGIN {
unshift @INC, File::Spec->catfile('t', 'lib');
eval { require XML::SAX; };
if($@) {
plan skip_all => 'no XML::SAX';
}
}
use TagsToUpper;
# Initialise filenames and check they're there
my $SrcFile = File::Spec->catfile('t', 'desertnet.src');
my $XMLFile = File::Spec->catfile('t', 'desertnet7.xml');
my $CacheFile = File::Spec->catfile('t', 'desertnet.stor');
unless(-e $SrcFile) {
plan skip_all => 'test data missing';
}
# Make sure we can write to the filesystem and check it uses the same
# clock as the machine we're running on.
my $t0 = time();
unless(open(XML, '>', $XMLFile)) {
plan skip_all => "can't create test file '$XMLFile': $!";
}
close(XML);
my $t1 = (stat($XMLFile))[9];
my $t2 = time();
if($t1 < $t0 or $t2 < $t1) {
plan skip_all => 'time moved backwards!'
}
plan tests => 14;
##############################################################################
# S U P P O R T R O U T I N E S
##############################################################################
##############################################################################
# Copy a file
#
sub CopyFile {
my($src, $dst) = @_;
open(my $in, $src) or die "open(<$src): $!";
local($/) = undef;
my $data = <$in>;
close($in);
open(my $out, '>', $dst) or die "open(>$dst): $!";
print $out $data;
close($out);
return(1);
}
##############################################################################
# T E S T R O U T I N E S
##############################################################################
use XML::Simple;
# Initialise test data
my $Expected = {
'server' => {
'sahara' => {
'osversion' => '2.6',
'osname' => 'solaris',
'address' => [
'10.0.0.101',
'10.0.1.101'
]
},
'gobi' => {
'osversion' => '6.5',
'osname' => 'irix',
'address' => '10.0.0.102'
},
'kalahari' => {
'osversion' => '2.0.34',
'osname' => 'linux',
'address' => [
'10.0.0.103',
'10.0.1.103'
]
}
}
};
my $xml = '';
# Force default behaviour of using SAX parser if it is available (which it
# is or we wouldn't be here).
$XML::Simple::PREFERRED_PARSER = '';
ok(CopyFile($SrcFile, $XMLFile), 'created source XML file');
if ('VMS' eq $^O) {
1 while (unlink($CacheFile));
} else {
unlink($CacheFile);
}
ok(! -e $CacheFile, 'deleted old cache files');
# Pass in a filename to check parse_uri()
my $opt = XMLin($XMLFile);
is_deeply($opt, $Expected, 'parsed expected value from file');
# Pass in an IO::File object to test parse_file()
my $fh = IO::File->new("<$XMLFile");
isa_ok($fh, 'IO::File', '$fh');
$opt = XMLin($fh);
is_deeply($opt, $Expected, 'parsed expected value from IO::File object');
$fh->close();
# Pass in a string to test parse_string()
if(open(XMLFILE, "<$XMLFile")) {
local($/) = undef;
$xml = ;
close(XMLFILE);
}
$opt = XMLin($xml);
is_deeply($opt, $Expected, 'parsed expected value from string');
# Pass in '-' for STDIN
open(OLDSTDIN, "<&STDIN");
close(STDIN);
open(STDIN, "<$XMLFile");
$opt = XMLin('-');
is_deeply($opt, $Expected, "parsed expected value from STDIN ('-')");
open(STDIN, "<&OLDSTDIN");
close(OLDSTDIN);
# Try using XML:Simple object as a SAX handler
my $simple = XML::Simple->new();
my $parser = XML::SAX::ParserFactory->parser(Handler => $simple);
$opt = $parser->parse_uri($XMLFile);
is_deeply($opt, $Expected,
'XML::Simple as a SAX handler returned expected value');
# Try again but make sure options from the constructor are being used
$simple = XML::Simple->new(
keyattr => { server => 'osname' },
forcearray => ['address'],
);
$parser = XML::SAX::ParserFactory->parser(Handler => $simple);
$opt = $parser->parse_uri($XMLFile);
my $Expected2 = {
'server' => {
'irix' => {
'address' => [ '10.0.0.102' ],
'osversion' => '6.5',
'name' => 'gobi'
},
'solaris' => {
'address' => [ '10.0.0.101', '10.0.1.101' ],
'osversion' => '2.6',
'name' => 'sahara'
},
'linux' => {
'address' => [ '10.0.0.103', '10.0.1.103' ],
'osversion' => '2.0.34',
'name' => 'kalahari'
}
}
};
is_deeply($opt, $Expected2, 'options passed to handler contructor work');
# Try using XML::Simple to drive a SAX pipeline
my $Expected3 = {
'SERVER' => {
'sahara' => {
'OSVERSION' => '2.6',
'OSNAME' => 'solaris',
'ADDRESS' => [
'10.0.0.101',
'10.0.1.101'
]
},
'gobi' => {
'OSVERSION' => '6.5',
'OSNAME' => 'irix',
'ADDRESS' => '10.0.0.102'
},
'kalahari' => {
'OSVERSION' => '2.0.34',
'OSNAME' => 'linux',
'ADDRESS' => [
'10.0.0.103',
'10.0.1.103'
]
}
}
};
my $simple2 = XML::Simple->new(keyattr => [qw(NAME)]);
my $filter = TagsToUpper->new(Handler => $simple2);
my $opt2 = XMLout($opt,
keyattr => { server => 'osname' },
Handler => $filter,
);
is_deeply($opt2, $Expected3, 'driving a SAX pipeline with XML::Simple worked');
# Confirm that 'handler' is a synonym for 'Handler'
$simple2 = XML::Simple->new(keyattr => [qw(NAME)]);
$filter = TagsToUpper->new(Handler => $simple2);
$opt2 = XMLout($opt,
keyattr => { server => 'osname' },
handler => $filter,
);
is_deeply($opt2, $Expected3, "'handler' is a synonym for 'Handler'");
# Confirm that DataHandler routine gets called
$xml = q(onetwothree);
$simple = XML::Simple->new(
DataHandler => sub {
my $xs = shift;
my $data = shift;
return(join(',', @$data));
}
);
$parser = XML::SAX::ParserFactory->parser(Handler => $simple);
my $result = $parser->parse_string($xml);
is($result, 'one,two,three', "'DataHandler' option works");
# Confirm that 'datahandler' is a synonym for 'DataHandler'
$simple = XML::Simple->new(
datahandler => sub {
my $xs = shift;
my $data = shift;
return(join(',', reverse(@$data)));
}
);
$parser = XML::SAX::ParserFactory->parser(Handler => $simple);
$result = $parser->parse_string($xml);
is($result, 'three,two,one', "'datahandler' is a synonym for 'DataHandler'");
# Confirm keeproot logic gets called
$simple = XML::Simple->new(keeproot => 1);
$parser = XML::SAX::ParserFactory->parser(Handler => $simple);
$opt = $parser->parse_string('');
is_deeply($opt, {opt => {a => 1, b => 2}}, "keeproot works with SAX pipelines");
# Clean up and go
unlink($CacheFile);
unlink($XMLFile);
exit(0);
XML-Simple-2.24/t/9_Strict.t 0000644 0001750 0001750 00000020551 13075040223 014467 0 ustar grant grant
use strict;
use warnings;
use Test::More;
plan tests => 44;
##############################################################################
# T E S T R O U T I N E S
##############################################################################
eval "use XML::Simple qw(:strict);";
ok(!$@, 'XML::Simple loads ok with qw(:strict)');
# Check that the basic functionality still works
my $xml = q();
$@ = '';
my $opt = eval {
XMLin($xml, forcearray => 1, keyattr => {});
};
is($@, '', 'XMLin() did not fail');
my $keys = join(' ', sort keys %$opt);
is($keys, 'name1 name2', 'and managed to produce the expected results');
# Confirm that forcearray cannot be omitted
eval {
$opt = XMLin($xml, keyattr => {});
};
isnt($@, '', 'omitting forcearray was a fatal error');
like($@, qr/(?i)No value specified for 'forcearray'/,
'with the correct error message');
# Confirm that keyattr cannot be omitted
eval {
$opt = XMLin($xml, forcearray => []);
};
isnt($@, '', 'omitting keyattr was a fatal error');
like($@, qr/(?i)No value specified for 'keyattr'/,
'with the correct error message');
# Confirm that element names from keyattr cannot be omitted from forcearray
eval {
$opt = XMLin($xml, keyattr => { part => 'partnum' }, forcearray => 0);
};
isnt($@, '', 'omitting forcearray for elements in keyattr was a fatal error');
like($@, qr/(?i) set in keyattr but not in forcearray/,
'with the correct error message');
eval {
$opt = XMLin($xml, keyattr => { part => 'partnum' }, forcearray => ['x','y']);
};
isnt($@, '', 'omitting keyattr elements from forcearray was a fatal error');
like($@, qr/(?i) set in keyattr but not in forcearray/,
'with the correct error message');
# Confirm that missing key attributes are detected
$xml = q(
);
eval {
$opt = XMLin($xml, keyattr => { part => 'partnum' }, forcearray => 1);
};
isnt($@, '', 'key attribute missing from names element was a fatal error');
like($@, qr/(?i) element has no 'partnum' key attribute/,
'with the correct error message');
# Confirm that non-unique values in key attributes are detected
$xml = q(
);
eval {
$opt = XMLin($xml, keyattr => { part => 'partnum' }, forcearray => 1);
};
isnt($@, '', 'non-unique key attribute values was a fatal error');
like($@, qr/(?i) element has non-unique value in 'partnum' key attribute: 12345/,
'with the correct error message');
# Confirm that stringification of references is trapped
$xml = q(
Bob21
);
eval {
$opt = XMLin($xml, keyattr => { item => 'name' }, forcearray => ['item']);
};
isnt($@, '', 'key attribute not a scalar was a fatal error');
like($@, qr/(?i) element has non-scalar 'name' key attribute/,
'with the correct error message');
##############################################################################
# Now confirm that XMLout gets checked too
#
# Check that the basic functionality still works under :strict
my $ref = {
person => [
{ name => 'bob' },
{ name => 'kate' },
]
};
$@ = '';
$xml = eval {
XMLout($ref, keyattr => {}, rootname => 'list');
};
is($@, '', 'XMLout() did not fail');
like($xml, qr{
^\s*
\s*
\s*
\s*\s*$
}xs, 'and managed to produce the expected results');
# Confirm that keyattr cannot be omitted
$@ = '';
eval {
XMLout($ref, rootname => 'list');
};
isnt($@, '', 'omitting keyattr was a fatal error');
like($@, qr/(?i)No value specified for 'keyattr'/,
'with the correct error message');
# Confirm that forcearray can be omitted (only rqd on input)
$@ = '';
eval {
XMLout($ref, keyattr => {x => 'y'});
};
is($@, '', 'omitting forcearray was not a fatal error on output');
##############################################################################
# Now repeat all that using the OO syntax
##############################################################################
# Check that the basic functionality still works
$xml = q();
my $xs = XML::Simple->new(forcearray => 1, keyattr => {});
$@ = '';
$opt = eval {
$xs->XMLin($xml);
};
is($@, '', '$xs->XMLin() did not fail');
$keys = join(' ', sort keys %$opt);
is($keys, 'name1 name2', 'and managed to produce the expected results');
# Confirm that forcearray cannot be omitted
$xs = XML::Simple->new(keyattr => {});
$@ = '';
eval {
$xs->XMLin($xml);
};
isnt($@, '', 'omitting forcearray was a fatal error');
like($@, qr/(?i)No value specified for 'forcearray'/,
'with the correct error message');
# Confirm that keyattr cannot be omitted
$xs = XML::Simple->new(forcearray => []);
eval {
$xs->XMLin($xml);
};
isnt($@, '', 'omitting keyattr was a fatal error');
like($@, qr/(?i)No value specified for 'keyattr'/,
'with the correct error message');
# Confirm that element names from keyattr cannot be omitted from forcearray
$xs = XML::Simple->new(keyattr => { part => 'partnum' }, forcearray => 0);
eval {
$xs->XMLin($xml);
};
isnt($@, '', 'omitting forcearray for elements in keyattr was a fatal error');
like($@, qr/(?i) set in keyattr but not in forcearray/,
'with the correct error message');
$xs = XML::Simple->new(keyattr => { part => 'partnum' }, forcearray => ['x','y']);
eval {
$xs->XMLin($xml);
};
isnt($@, '', 'omitting keyattr elements from forcearray was a fatal error');
like($@, qr/(?i) set in keyattr but not in forcearray/,
'with the correct error message');
# Confirm that missing key attributes are detected
$xml = q(
);
$xs = XML::Simple->new(keyattr => { part => 'partnum' }, forcearray => 1);
eval {
$xs->XMLin($xml);
};
isnt($@, '', 'key attribute missing from names element was a fatal error');
like($@, qr/(?i) element has no 'partnum' key attribute/,
'with the correct error message');
# Confirm that stringification of references is trapped
$xml = q(
Bob21
);
$xs = XML::Simple->new(keyattr => { item => 'name' }, forcearray => ['item']);
eval {
$xs->XMLin($xml);
};
isnt($@, '', 'key attribute not a scalar was a fatal error');
like($@, qr/(?i) element has non-scalar 'name' key attribute/,
'with the correct error message');
##############################################################################
# Now confirm that XMLout gets checked too
#
# Check that the basic functionality still works under :strict
$ref = {
person => [
{ name => 'bob' },
{ name => 'kate' },
]
};
$xs = XML::Simple->new(keyattr => {}, rootname => 'list');
$@ = '';
$xml = eval {
$xs->XMLout($ref);
};
is($@, '', 'XMLout() did not fail');
like($xml, qr{
^\s*
\s*
\s*
\s*\s*$
}xs, 'and managed to produce the expected results');
# Confirm that keyattr cannot be omitted
$xs = XML::Simple->new(rootname => 'list');
eval {
$xs->XMLout($ref);
};
isnt($@, '', 'omitting keyattr was a fatal error');
like($@, qr/(?i)No value specified for 'keyattr'/,
'with the correct error message');
# Confirm that code in other modules can still call XMLin without having
# strict mode forced upon them.
$xml = q();
eval {
$opt = SimpleWrapper::XMLin($xml, keyattr => {});
};
is($@, '', 'other namespaces do not have strict mode forced upon them');
# Unless those calls explicitly enable strict mode
eval {
$opt = SimpleWrapper::XMLin($xml, StrictMode => 1, keyattr => {});
};
isnt($@, '', 'other namespaces do not have strict mode forced upon them');
like($@, qr/(?i)No value specified for 'forcearray'/,
'with the correct error message');
# And calls in this namespace can turn strict mode off
eval {
$opt = XMLin($xml, StrictMode => 0, keyattr => {});
};
is($@, '', 'other namespaces do not have strict mode forced upon them');
exit(0);
package SimpleWrapper;
sub XMLin {
XML::Simple::XMLin(@_);
}
XML-Simple-2.24/t/8_Namespaces.t 0000644 0001750 0001750 00000010625 13075040223 015276 0 ustar grant grant
use strict;
use warnings;
use Test::More;
use File::Spec;
use IO::File;
eval { require XML::SAX; };
if($@) {
plan skip_all => 'no XML::SAX';
}
eval { require XML::NamespaceSupport; };
if($@) {
plan skip_all => "no XML::NamespaceSupport";
}
if($XML::NamespaceSupport::VERSION < 1.04) {
plan skip_all => "XML::NamespaceSupport is too old (upgrade to 1.04 or better)";
}
plan tests => 8;
use XML::Simple;
# Force default behaviour of using SAX parser if it is available (which it
# is or we wouldn't be here).
$XML::Simple::PREFERRED_PARSER = '';
# Confirm that by default qnames are not expanded on input
my $xml = q(onetwothree);
my $expected = {
'perl:list' => {
'count' => '3',
'item' => [
'one',
'two',
'three'
],
'perl:type' => 'array',
'test' => {
'xmlns:perl' => 'http://www.microsoft.com',
'perl:tm' => 'trademark',
}
},
'xmlns:perl' => 'http://www.perl.com/'
};
my $opt = XMLin($xml);
is_deeply($opt, $expected, 'qnames are not expanded by default');
# Try again with nsexpand option set
$expected = {
'{http://www.perl.com/}list' => {
'count' => '3',
'item' => [
'one',
'two',
'three'
],
'{http://www.perl.com/}type' => 'array',
'test' => {
'{http://www.microsoft.com}tm' => 'trademark',
'{http://www.w3.org/2000/xmlns/}perl' => 'http://www.microsoft.com'
}
},
'{http://www.w3.org/2000/xmlns/}perl' => 'http://www.perl.com/'
};
$opt = XMLin($xml, nsexpand => 1);
is_deeply($opt, $expected, 'qnames are expanded on request');
# Confirm that output expansion does not occur by default
$opt = {
'{http://www.w3.org/2000/xmlns/}perl' => 'http://www.perl.com/',
'{http://www.perl.com/}attr' => 'value',
'bare' => 'Beer!',
'{http://www.perl.com/}element' => [ 'data' ],
};
$xml = XMLout($opt);
like($xml, qr[
^\s*
\s*<\{http://www.perl.com/\}element\s*>data\{http://www.perl.com/\}element\s*>
\s*
\s*$
]sx, 'clarkian names not converted to qnames on output by default');
# Confirm nsexpand option works on output
$xml = XMLout($opt, nsexpand => 1);
ok($xml =~ m{
^\s*
\s*data
\s*
\s*$
}sx, 'clarkian names are converted to qnames on output on request');
# Check that default namespace is correctly read in ...
$xml = q(TomDickLarry
);
$expected = {
'xmlns' => 'http://www.orgsoc.org/',
'{http://www.orgsoc.org/}list' => {
'{http://www.orgsoc.org/}member' => [ 'Tom', 'Dick', 'Larry' ],
}
};
$opt = XMLin($xml, nsexpand => 1);
is_deeply($opt, $expected, 'expansion of default namespace works');
# ... and written out
$xml = XMLout($opt, nsexpand => 1);
like($xml, qr{
^\s*
\s*
\s*Tom
\s*Dick
\s*Larry
\s*
\s*
\s*$
}sx, 'default namespaces are output correctly too');
# Check that the autogeneration of namespaces works as we expect
$opt = {
'xmlns' => 'http://www.orgsoc.org/',
'{http://www.orgsoc.org/}list' => {
'{http://www.orgsoc.org/}member' => [ 'Tom', 'Dick', 'Larry' ],
'{http://www.phantom.com/}director' => [ 'Bill', 'Ben' ],
}
};
$xml = XMLout($opt, nsexpand => 1);
my $prefix = '';
if($xml =~ m{}) {
$prefix = $1;
}
# regex match split in two to workaround 5.8.1/utf8/regex match prob
like($xml, qr{
\s*
.*?
\s*
}sx, 'namespace prefixes are generated automatically (part 1)');
like($xml, qr{
(\s*Tom
\s*Dick
\s*Larry
|\s*<${prefix}:director>Bill${prefix}:director>
\s*<${prefix}:director>Ben${prefix}:director>){2}
#\s*
}sx, 'namespace prefixes are generated automatically (part 2)');
exit(0);
XML-Simple-2.24/t/1_XMLin.xml 0000644 0001750 0001750 00000000041 13075040223 014523 0 ustar grant grant
XML-Simple-2.24/t/0_Config.t 0000644 0001750 0001750 00000002763 13075040223 014420 0 ustar grant grant
use strict;
use Test::More tests => 1;
# Build up a list of installed modules
my @mod_list = qw(XML::Simple Storable XML::Parser XML::SAX);
# If XML::SAX is installed, add a list of installed SAX parsers
eval " use XML::SAX; ";
my $default_parser = '';
unless($@) {
push @mod_list, 'XML::NamespaceSupport';
push @mod_list, map { $_->{Name} } @{XML::SAX->parsers()};
$default_parser = ref(XML::SAX::ParserFactory->parser());
}
# Extract the version number from each module
my(%version);
foreach my $module (@mod_list) {
eval " require $module; ";
unless($@) {
no strict 'refs';
$version{$module} = ${$module . '::VERSION'} || "Unknown";
}
}
$default_parser = 'XML::Parser' if(!$default_parser && $version{'XML::Parser'});
# Add version number of the Perl binary
eval ' use Config; $version{perl} = $Config{version} '; # Should never fail
if($@) {
$version{perl} = $];
}
unshift @mod_list, 'perl';
# Check for preferred parser via environment setting
my $preferred_parser = $ENV{XML_SIMPLE_PREFERRED_PARSER} || ' ';
# Print details of installed modules on STDERR
diag(sprintf("\r# %-30s %s\n", 'Package', 'Version'));
foreach my $module (@mod_list) {
$version{$module} = 'Not Installed' unless(defined($version{$module}));
$version{$module} .= " (default parser)" if($module eq $default_parser);
$version{$module} .= " (preferred parser)" if($module eq $preferred_parser);
diag(sprintf(" %-30s %s\n", $module, $version{$module}));
}
# Housekeeping
ok(1, "Dumped config");
XML-Simple-2.24/t/3_Storable.t 0000644 0001750 0001750 00000015430 13075040223 014764 0 ustar grant grant
use strict;
use warnings;
use Test::More;
use File::Spec;
eval { require Storable; };
unless($INC{'Storable.pm'}) {
plan skip_all => 'no Storable.pm';
}
unless(UNIVERSAL::can(Storable => 'lock_nstore')) {
plan skip_all => 'Storable.pm is too old - no file locking support';
}
# Initialise filenames and check they're there
my $SrcFile = File::Spec->catfile('t', 'desertnet.src');
my $XMLFile = File::Spec->catfile('t', 'desertnet3.xml');
my $CacheFile = File::Spec->catfile('t', 'desertnet3.stor');
unless(-e $SrcFile) {
plan skip_all => 'test data missing';
}
# Make sure we can write to the filesystem and check it uses the same
# clock as the machine we're running on.
my $t0 = time();
unless(open(XML, '>', $XMLFile)) {
plan skip_all => "can't create test file '$XMLFile': $!";
}
close(XML);
my $t1 = (stat($XMLFile))[9];
my $t2 = time();
if($t1 < $t0 or $t2 < $t1) {
plan skip_all => 'time moved backwards!'
}
plan tests => 23;
##############################################################################
# S U P P O R T R O U T I N E S
##############################################################################
##############################################################################
# Copy a file
#
sub CopyFile {
my($src, $dst) = @_;
open(my $in, $src) or die "open(<$src): $!";
local($/) = undef;
my $data = <$in>;
close($in);
open(my $out, '>', $dst) or die "open(>$dst): $!";
print $out $data;
close($out);
return(1);
}
##############################################################################
# Delete a file - portably
#
sub DeleteFile {
my($Filename) = @_;
if ('VMS' eq $^O) {
1 while (unlink($Filename));
} else {
unlink($Filename);
}
}
##############################################################################
# Create a file, making sure that its timestamp is newer than another
# existing file.
#
sub MakeNewerFile {
my($File1, $File2, $CodeRef) = @_;
my $t0 = (stat($File1))[9];
while(1) {
unlink($File2);
$CodeRef->();
return if (stat($File2))[9] > $t0;
sleep(1);
}
}
##############################################################################
# Wait until the current time is greater than the supplied value
#
sub PassTime {
my($Target) = @_;
while(time <= $Target) {
sleep 1;
}
}
##############################################################################
# T E S T R O U T I N E S
##############################################################################
use XML::Simple;
# Initialise test data
my $Expected = {
'server' => {
'sahara' => {
'osversion' => '2.6',
'osname' => 'solaris',
'address' => [
'10.0.0.101',
'10.0.1.101'
]
},
'gobi' => {
'osversion' => '6.5',
'osname' => 'irix',
'address' => '10.0.0.102'
},
'kalahari' => {
'osversion' => '2.0.34',
'osname' => 'linux',
'address' => [
'10.0.0.103',
'10.0.1.103'
]
}
}
};
ok(CopyFile($SrcFile, $XMLFile), 'copied known good source file');
unlink($CacheFile);
ok(! -e $CacheFile, 'no cache files lying around');
my $opt = XMLin($XMLFile);
is_deeply($opt, $Expected, 'parsed expected data from file');
ok(! -e $CacheFile, 'and no cache file was created');
PassTime(time()); # Ensure cache file will be newer
$opt = XMLin($XMLFile, cache => 'storable');
is_deeply($opt, $Expected, 'parsed expected data from file (again)');
ok(-e $CacheFile, 'but this time a cache file was created');
$t0 = (stat($CacheFile))[9]; # Remember cache timestamp
PassTime($t0);
$opt = XMLin($XMLFile, cache => ['storable']);
is_deeply($opt, $Expected, 'got expected data from cache');
$t1 = (stat($CacheFile))[9];
is($t0, $t1, 'and cache timestamp has not changed');
PassTime(time());
$t0 = time();
open(FILE, ">>$XMLFile"); # Touch the XML file
print FILE "\n";
close(FILE);
$opt = XMLin($XMLFile, cache => 'storable');
is_deeply($opt, $Expected, 'parsed in expected value again');
$t2 = (stat($CacheFile))[9];
isnt($t1, $t2, 'and this time the cache timestamp has changed');
DeleteFile($XMLFile);
ok(! -e $XMLFile, 'deleted the source file');
open(FILE, ">$XMLFile"); # Re-create it (empty)
close(FILE);
ok(-e $XMLFile, 'recreated the source file');
is(-s $XMLFile, 0, 'but with nothing in it');
MakeNewerFile($XMLFile, $CacheFile, sub { # Make sure cache file is newer
Storable::nstore($Expected, $CacheFile);
});
$opt = XMLin($XMLFile, cache => 'storable');
is_deeply($opt, $Expected, 'got the expected data from the cache');
$t2 = (stat($CacheFile))[9];
PassTime($t2);
open(FILE, ">$XMLFile") || # Write some new data to the XML file
die "open(>$XMLFile): $!\n";
print FILE qq(\n);
close(FILE);
$opt = XMLin($XMLFile); # Parse with no caching
is_deeply($opt, { one => 1, two => 2}, 'parsed in expected data from file');
$t0 = (stat($CacheFile))[9]; # And timestamp on cache file
my $s0 = (-s $CacheFile);
is($t0, $t2, 'and the cache file was not touched');
# Parse again with caching enabled
$opt = XMLin($XMLFile, cache => 'storable');
is_deeply($opt, { one => 1, two => 2}, 'parsed expected data through cache');
$t1 = (stat($CacheFile))[9];
my $s1 = (-s $CacheFile);
ok(($t0 != $t1) || ($s0 != $s1),
'and the cache was updated'); # Content changes but date may not on Win32
ok(CopyFile($SrcFile, $XMLFile), 'copied back the original file');
PassTime($t1);
$opt = XMLin($XMLFile, cache => 'storable');
is_deeply($opt, $Expected, 'parsed expected data in through cache');
# Make sure scheme name is case-insensitive
$opt = XMLin($XMLFile, cache => 'Storable');
is_deeply($opt, $Expected, 'scheme name is case-insensitive');
# Make sure bad scheme names are trapped
$@='';
$_ = eval { XMLin($XMLFile, cache => 'Storubble'); };
is($_, undef, 'bad cache scheme names are trapped');
like($@, qr/Unsupported caching scheme: storubble/,
'with correct error message');
# Clean up and go
unlink($CacheFile);
unlink($XMLFile);
exit(0);
XML-Simple-2.24/t/6_ObjIntf.t 0000644 0001750 0001750 00000025575 13075040223 014562 0 ustar grant grant
use strict;
use warnings;
use Test::More tests => 37;
##############################################################################
# Derived version of XML::Simple that returns everything in upper case
##############################################################################
package XML::Simple::UC;
use vars qw(@ISA);
@ISA = qw(XML::Simple);
sub build_tree {
my $self = shift;
my $tree = $self->SUPER::build_tree(@_);
($tree) = uctree($tree);
return($tree);
}
sub uctree {
foreach my $i (0..$#_) {
my $x = $_[$i];
if(ref($x) eq 'ARRAY') {
$_[$i] = [ uctree(@$x) ];
}
elsif(ref($x) eq 'HASH') {
$_[$i] = { uctree(%$x) };
}
else {
$_[$i] = uc($x);
}
}
return(@_);
}
##############################################################################
# Derived version of XML::Simple that uses CDATA sections for escaping
##############################################################################
package XML::Simple::CDE;
use vars qw(@ISA);
@ISA = qw(XML::Simple);
sub escape_value {
my $self = shift;
my($data) = @_;
if($data =~ /[&<>"]/) {
$data = '';
}
return($data);
}
##############################################################################
# Start of the test script itself
##############################################################################
package main;
use XML::Simple;
# Check error handling in constructor
$@='';
$_ = eval { XML::Simple->new('searchpath') };
is($_, undef, 'invalid number of options are trapped');
like($@, qr/Default options must be name=>value pairs \(odd number supplied\)/,
'with correct error message');
my $xml = q(R.E.M.Automatic For The People
);
my %opts1 = (
keyattr => { disc => 'cddbid', track => 'number' },
keeproot => 1,
contentkey => 'title',
forcearray => [ qw(disc album) ]
);
my %opts2 = (
keyattr => { }
);
my %opts3 = (
keyattr => { disc => 'cddbid', track => 'number' },
keeproot => 1,
contentkey => '-title',
forcearray => [ qw(disc album) ]
);
my $xs1 = new XML::Simple( %opts1 );
my $xs2 = new XML::Simple( %opts2 );
my $xs3 = new XML::Simple( %opts3 );
isa_ok($xs1, 'XML::Simple', 'object one');
isa_ok($xs2, 'XML::Simple', 'object two');
isa_ok($xs3, 'XML::Simple', 'object three');
is_deeply(\%opts1, {
keyattr => { disc => 'cddbid', track => 'number' },
keeproot => 1,
contentkey => 'title',
forcearray => [ qw(disc album) ]
}, 'options hash was not corrupted');
my $exp1 = {
'cddatabase' => {
'disc' => {
'960b750c' => {
'id' => '9362-45055-2',
'album' => [ 'Automatic For The People' ],
'artist' => 'R.E.M.',
'track' => {
1 => { 'title' => 'Drive' },
2 => { 'title' => 'Try Not To Breathe' },
3 => { 'title' => 'The Sidewinder Sleeps Tonite' },
4 => { 'title' => 'Everybody Hurts' },
5 => { 'title' => 'New Orleans Instrumental No. 1' },
6 => { 'title' => 'Sweetness Follows' },
7 => { 'title' => 'Monty Got A Raw Deal' },
8 => { 'title' => 'Ignoreland' },
9 => { 'title' => 'Star Me Kitten' },
10 => { 'title' => 'Man On The Moon' },
11 => { 'title' => 'Nightswimming' },
12 => { 'title' => 'Find The River' }
}
}
}
}
};
my $ref1 = $xs1->XMLin($xml);
is_deeply($ref1, $exp1, 'parsed expected data via object 1');
# Try using the other object
my $exp2 = {
'disc' => {
'album' => 'Automatic For The People',
'artist' => 'R.E.M.',
'cddbid' => '960b750c',
'id' => '9362-45055-2',
'track' => [
{ 'number' => 1, 'content' => 'Drive' },
{ 'number' => 2, 'content' => 'Try Not To Breathe' },
{ 'number' => 3, 'content' => 'The Sidewinder Sleeps Tonite' },
{ 'number' => 4, 'content' => 'Everybody Hurts' },
{ 'number' => 5, 'content' => 'New Orleans Instrumental No. 1' },
{ 'number' => 6, 'content' => 'Sweetness Follows' },
{ 'number' => 7, 'content' => 'Monty Got A Raw Deal' },
{ 'number' => 8, 'content' => 'Ignoreland' },
{ 'number' => 9, 'content' => 'Star Me Kitten' },
{ 'number' => 10, 'content' => 'Man On The Moon' },
{ 'number' => 11, 'content' => 'Nightswimming' },
{ 'number' => 12, 'content' => 'Find The River' }
]
}
};
my $ref2 = $xs2->XMLin($xml);
is_deeply($ref2, $exp2, 'parsed expected data via object 2');
# Try using the third object
my $exp3 = {
'cddatabase' => {
'disc' => {
'960b750c' => {
'id' => '9362-45055-2',
'album' => [ 'Automatic For The People' ],
'artist' => 'R.E.M.',
'track' => {
1 => 'Drive',
2 => 'Try Not To Breathe',
3 => 'The Sidewinder Sleeps Tonite',
4 => 'Everybody Hurts',
5 => 'New Orleans Instrumental No. 1',
6 => 'Sweetness Follows',
7 => 'Monty Got A Raw Deal',
8 => 'Ignoreland',
9 => 'Star Me Kitten',
10 => 'Man On The Moon',
11 => 'Nightswimming',
12 => 'Find The River'
}
}
}
}
};
my $ref3 = $xs3->XMLin($xml);
is_deeply($ref3, $exp3, 'parsed expected data via object 3');
# Confirm default options in object merge correctly with options as args
$ref1 = $xs1->XMLin($xml, keyattr => [], forcearray => 0);
is_deeply($ref1, { # Parsed to what we expected
'cddatabase' => {
'disc' => {
'album' => 'Automatic For The People',
'id' => '9362-45055-2',
'artist' => 'R.E.M.',
'cddbid' => '960b750c',
'track' => [
{ 'number' => 1, 'title' => 'Drive' },
{ 'number' => 2, 'title' => 'Try Not To Breathe' },
{ 'number' => 3, 'title' => 'The Sidewinder Sleeps Tonite' },
{ 'number' => 4, 'title' => 'Everybody Hurts' },
{ 'number' => 5, 'title' => 'New Orleans Instrumental No. 1' },
{ 'number' => 6, 'title' => 'Sweetness Follows' },
{ 'number' => 7, 'title' => 'Monty Got A Raw Deal' },
{ 'number' => 8, 'title' => 'Ignoreland' },
{ 'number' => 9, 'title' => 'Star Me Kitten' },
{ 'number' => 10, 'title' => 'Man On The Moon' },
{ 'number' => 11, 'title' => 'Nightswimming' },
{ 'number' => 12, 'title' => 'Find The River' }
]
}
}
}, 'successfully merged options');
# Confirm that default options in object still work as expected
$ref1 = $xs1->XMLin($xml);
is_deeply($ref1, $exp1, 'defaults were not affected by merge');
# Confirm they work for output too
$_ = $xs1->XMLout($ref1);
ok(s{} {}, 't1');
ok(s{} {}, 't2');
ok(s{} {}, 't3');
ok(s{} {}, 't4');
ok(s{}{}, 't5');
ok(s{} {}, 't6');
ok(s{} {}, 't7');
ok(s{} {}, 't8');
ok(s{} {}, 't9');
ok(s{} {}, 't10');
ok(s{} {}, 't11');
ok(s{} {}, 't12');
ok(s{Automatic For The People} {}, 'ttl');
ok(s{cddbid="960b750c"}{ATTR}, 'cddbid');
ok(s{id="9362-45055-2"}{ATTR}, 'id');
ok(s{artist="R.E.M."} {ATTR}, 'artist');
ok(s{(\s*){13}\s*}{}s, 'disc');
ok(m{^\s*<(cddatabase)>\s*\s*\1>\s*$}, 'database');
# Confirm error when mandatory parameter missing
$_ = eval {
$xs1->XMLout();
};
ok(!defined($_), 'XMLout() method call with no args proves fatal');
like($@, qr/XMLout\(\) requires at least one argument/,
'with correct error message');
# Check that overriding build_tree() method works
$xml = q(Apollo
10 Downing Street
);
my $xsp = new XML::Simple::UC();
$ref1 = $xsp->XMLin($xml);
is_deeply($ref1, {
'SERVER' => {
'NAME' => 'APOLLO',
'ADDRESS' => '10 DOWNING STREET'
}
}, 'inheritance works with build_tree() overridden');
# Check that overriding escape_value() method works
my $ref = {
'server' => {
'address' => '12->14 "Puf&Stuf" Drive'
}
};
$xsp = new XML::Simple::CDE();
$_ = $xsp->XMLout($ref);
like($_, qr{\s*
14\s+"Puf&Stuf"\s+Drive\]\]>"\s*/>\s*
}xs, 'inheritance works with escape_value() overridden');
# Check variables defined in the constructor don't get trounced for
# subsequent parses
$xs1 = XML::Simple->new(
contentkey => '-content',
varattr => 'xsvar',
variables => { conf_dir => '/etc', log_dir => '/tmp' }
);
$xml = q(/var/log${conf_dir}/appname.conf${log_dir}/appname.log${log_dir}/appname.dbg);
my $opt = $xs1->XMLin($xml);
is_deeply($opt, {
file => {
config_file => '/etc/appname.conf',
log_file => '/var/log/appname.log',
debug_file => '/var/log/appname.dbg',
},
dir => { xsvar => 'log_dir', content => '/var/log' },
}, 'variables from XML merged with predefined variables');
$xml = q(${conf_dir}/appname.conf${log_dir}/appname.log${log_dir}/appname.dbg);
$opt = $xs1->XMLin($xml);
is_deeply($opt, {
file => {
config_file => '/etc/appname.conf',
log_file => '/tmp/appname.log',
debug_file => '/tmp/appname.dbg',
},
}, 'variables from XML merged with predefined variables');
# check that unknown options passed to the constructor are rejected
$@ = undef;
eval { $xs1 = XML::Simple->new(KeyAttr => {}, WibbleFlibble => 1) };
ok(defined($@), "unrecognised option caught by constructor");
like($@, qr/^Unrecognised option: WibbleFlibble at/,
"correct message in exception");
exit(0);
XML-Simple-2.24/t/desertnet.src 0000644 0001750 0001750 00000000615 13075040223 015307 0 ustar grant grant
10.0.0.101
10.0.1.101
10.0.0.102
10.0.0.103
10.0.1.103
XML-Simple-2.24/t/2_XMLout.t 0000644 0001750 0001750 00000076477 13075040223 014422 0 ustar grant grant
use strict;
use warnings;
use Test::More;
use FileHandle; # Workaround for test failures on RHEL
plan tests => 201;
##############################################################################
# S U P P O R T R O U T I N E S
##############################################################################
##############################################################################
# Read file and return contents as a scalar.
#
sub ReadFile {
local($/) = undef;
open(_READ_FILE_, $_[0]) || die "open($_[0]): $!";
my $data = <_READ_FILE_>;
close(_READ_FILE_);
return($data);
}
use XML::Simple;
# Confirm error when mandatory parameter missing
$_ = eval {
XMLout();
};
ok(!defined($_), 'call with no args proves fatal');
like($@, qr/XMLout\(\) requires at least one argument/,
'with correct error message');
# Try encoding a scalar value
my $xml = XMLout("scalar");
ok(1, 'XMLout did not crash');
ok(defined($xml), 'and it returned an XML string');
is(XMLin($xml), 'scalar', 'which parses back OK');
# Next try encoding a hash
my $hashref1 = { one => 1, two => 'II', three => '...' };
my $hashref2 = { one => 1, two => 'II', three => '...' };
# Expect:
#
$_ = XMLout($hashref1);
is_deeply(XMLin($_), $hashref1, 'encoded a hash');
ok(s/one="1"//, 'first key encoded OK');
ok(s/two="II"//, 'second key encoded OK');
ok(s/three="..."//, 'third key encoded OK');
like($_, qr/^<\w+\s+\/>/, 'no other attributes encoded');
# Now try encoding a hash with a nested array
my $ref = {array => [qw(one two three)]};
# Expect:
#
# one
# two
# three
#
$_ = XMLout($ref);
is_deeply(XMLin($_), $ref, 'encoded a hash with nested array');
ok(s{one\s*
two\s*
three}{}sx, 'array elements encoded in correct order');
like($_, qr/^<(\w+)\s*>\s*<\/\1>\s*$/s, 'no other spurious encodings');
# Now try encoding a nested hash
$ref = { value => '555 1234',
hash1 => { one => 1 },
hash2 => { two => 2 } };
# Expect:
#
#
#
#
$_ = XMLout($ref);
is_deeply(XMLin($_), $ref, 'encoded nested hashes');
ok(s{\s*}{}s, 'nested hash 1 ok');
ok(s{\s*}{}s, 'nested hash 2 ok');
like($_, qr{^<(\w+)\s+value="555 1234"\s*>\s*\1>\s*$}s, 'whole OK');
# Now try encoding an anonymous array
$ref = [ qw(1 two III) ];
# Expect:
#
# 1
# two
# III
#
$_ = XMLout($ref);
is_deeply(XMLin($_), $ref, 'encoded anonymous array');
like($_, qr{
^<(\w+)\s*>
\s*1
\s*two
\s*III
\s*\1>\s*$}sx, 'output matches expectations');
# Now try encoding a nested anonymous array
$ref = [ [ qw(1.1 1.2) ], [ qw(2.1 2.2) ] ];
# Expect:
#
#
# 1.1
# 1.2
#
#
# 2.1
# 2.2
#
#
$_ = XMLout($ref);
is_deeply(XMLin($_), $ref, 'encoded nested anonymous arrays');
like($_, qr{
<(\w+)\s*>
\s*
\s*1\.1
\s*1\.2
\s*
\s*
\s*2\.1
\s*2\.2
\s*
\s*\1\s*>
}sx, 'output matches expectations');
# Now try encoding a hash of hashes with key folding disabled
$ref = { country => {
England => { capital => 'London' },
France => { capital => 'Paris' },
Turkey => { capital => 'Istanbul' },
}
};
# Expect:
#
#
#
#
#
#
#
$_ = XMLout($ref, keyattr => []);
is_deeply(XMLin($_), $ref, 'encoded hash of hashes with folding disabled');
ok(s{\s*}{}s, 'nested hash 1 ok');
ok(s{\s*}{}s, 'nested hash 2 ok');
ok(s{\s*}{}s, 'nested hash 3 ok');
ok(s{\s*}{}s, 'container hash ok');
ok(s{^<(\w+)\s*>\s*\1>$}{}s, 'document ok');
# Try encoding same again with key folding set to non-standard value
# Expect:
#
#
#
#
#
my $expected = qr{
^<(\w+)\s*>\s*
(
\s*
|\s*
|\s*
){3}
\1>$
}xs;
$xml = XMLout($ref, keyattr => ['fullname']);
is_deeply(XMLin($xml, keyattr => ['fullname']), $ref,
'encoded hash of hashes with explicit folding enabled');
like($xml, $expected, 'output as expected');
# Same again but specify name as scalar rather than array
$xml = XMLout($ref, keyattr => 'fullname');
like($xml, $expected, 'still works when keyattr is scalar');
# Same again but specify keyattr as hash rather than array
$xml = XMLout($ref, keyattr => { country => 'fullname' });
like($xml, $expected, 'still works when keyattr is hash');
# Same again but add leading '+'
$xml = XMLout($ref, keyattr => { country => '+fullname' });
like($xml, $expected, "still works when keyattr is hash with leading '+'");
# and leading '-'
$xml = XMLout($ref, keyattr => { country => '-fullname' });
like($xml, $expected, "still works when keyattr is hash with leading '-'");
# One more time but with default key folding values
# Expect:
#
#
#
#
#
$expected = qr{
^<(\w+)\s*>\s*
(
\s*
|\s*
|\s*
){3}
\1>$
}xs;
$xml = XMLout($ref);
is_deeply(XMLin($xml), $ref,
'encoded hash of hashes with default folding enabled');
like($xml, $expected, "expected output with default keyattr");
# Finally, confirm folding still works with only one nested hash
# Expect:
#
#
#
$ref = { country => { England => { capital => 'London' } } };
$_ = XMLout($ref);
is_deeply(XMLin($_, forcearray => 1), $ref, 'single nested hash unfolded');
ok(s{\s*name="England"}{uk}s, 'attr 1 ok');
ok(s{\s*capital="London"}{uk}s, 'attr 2 ok');
ok(s{\s*}{}s, 'element ok');
ok(s{^<(\w+)\s*>\s*\1>$}{}s, 'document ok');
# Check that default XML declaration works
#
# Expect:
#
#
$ref = { one => 1 };
$_ = XMLout($ref, xmldecl => 1);
is_deeply(XMLin($_), $ref, 'generated doc with XML declaration');
ok(s{^\Q\E}{}s, 'XML declaration OK');
like($_, qr{^\s*}s, 'data OK too');
# Check that custom XML declaration works
#
# Expect:
#
#
$_ = XMLout($ref, xmldecl => "");
is_deeply(XMLin($_), $ref, 'generated doc with custom XML declaration');
ok(s{^\Q\E}{}s, 'XML declaration OK');
like($_, qr{^\s*}s, 'data OK too');
# Check that special characters do get escaped
$ref = { a => '', b => '"B"', c => '&C&' };
$_ = XMLout($ref);
is_deeply(XMLin($_), $ref, 'generated document with escaping');
ok(s{a="<A>"}{}s, 'angle brackets escaped OK');
ok(s{b=""B""}{}s, 'double quotes escaped OK');
ok(s{c="&C&"}{}s, 'ampersands escaped OK');
ok(s{^<(\w+)\s*/>$}{}s, 'data OK too');
# unless we turn escaping off
$ref = { a => '', b => '"B"', c => ['&C&'] };
$_ = XMLout($ref, noescape => 1);
ok(s{a=""}{}s, 'generated unescaped angle brackets');
ok(s{b=""B""}{}s, 'generated unescaped double quotes');
ok(s{&C&}{}s, 'generated unescaped ampersands');
ok(s{^<(\w+)\s*>\s*\1>$}{}s, 'data OK too');
# same again but with a scalar
$xml = XMLout("", noescape => 1);
like($xml, qr{^<(\w+)>\1>}, "Unescaped scalar as expected too");
# Try encoding a circular data structure and confirm that it fails
$_ = eval {
my $ref = { a => '1' };
$ref->{b} = $ref;
XMLout($ref);
};
ok(!defined($_), 'caught circular data structure');
like($@, qr/circular data structures not supported/,
'with correct error message');
# Try encoding a repetitive (but non-circular) data structure and confirm that
# it does not fail
$_ = eval {
my $a = { alpha => 1 };
my $ref = { a => $a, b => $a };
XMLout($ref);
};
ok(defined($_), 'repetitive (non-circular) data structure not fatal');
like($_, qr{^
(
\s*
|
\s*
){2}
\s*
}xs, 'and encodes as expected');
# Try encoding a non array/hash blessed reference and confirm that it fails
$_ = eval { my $ref = bless \*STDERR, 'BogoClass'; XMLout($ref) };
is($_, undef, 'caught blessed non array/hash reference in data structure');
like($@, qr/Can't encode a value of type: /, 'with correct error message');
# Repeat some of the above tests with named root element
# Try encoding a scalar value
$xml = XMLout("scalar", rootname => 'TOM');
ok(defined($xml), 'generated document with named root element');
is(XMLin($xml), 'scalar', 'parsed it back correctly');
like($xml, qr/^\s*scalar<\/TOM>\s*$/si, 'XML as expected');
# Next try encoding a hash
# Expect:
#
$_ = XMLout($hashref1, rootname => 'DICK');
is_deeply(XMLin($_), $hashref1, 'same again but encoded a hash');
ok(s/one="1"//, 'first key encoded OK');
ok(s/two="II"//, 'second key encoded OK');
ok(s/three="..."//, 'third key encoded OK');
like($_, qr/^/, 'XML looks OK');
# Now try encoding a hash with a nested array
$ref = {array => [qw(one two three)]};
# Expect:
#
# one
# two
# three
#
$_ = XMLout($ref, rootname => 'LARRY');
is_deeply(XMLin($_), $ref, 'same again but with array in hash');
ok(s{one\s*
two\s*
three}{}sx, 'array encoded in correct order');
like($_, qr/^<(LARRY)\s*>\s*<\/\1>\s*$/s, 'only expected root element left');
# Now try encoding a nested hash
$ref = { value => '555 1234',
hash1 => { one => 1 },
hash2 => { two => 2 } };
# Expect:
#
#
#
#
$_ = XMLout($ref, rootname => 'CURLY');
is_deeply(XMLin($_), $ref, 'same again but with nested hashes');
ok(s{\s*}{}s, 'hash 1 encoded OK');
ok(s{\s*}{}s, 'hash 2 encoded OK');
like($_, qr{^<(CURLY)\s+value="555 1234"\s*>\s*\1>\s*$}s, 'document OK');
# Now try encoding an anonymous array
$ref = [ qw(1 two III) ];
# Expect:
#
# 1
# two
# III
#
$_ = XMLout($ref, rootname => 'MOE');
is_deeply(XMLin($_), $ref, 'same again but with nested anonymous array');
like($_, qr{
^<(MOE)\s*>
\s*1
\s*two
\s*III
\s*\1>\s*$}sx, 'document OK');
# Test again, this time with no root element
# Try encoding a scalar value
like(XMLout("scalar", rootname => ''), qr/scalar\s+/s,
'encoded scalar with no root element');
like(XMLout("scalar", rootname => undef), qr/scalar\s+/s,
'same again but with rootname = undef');
# Next try encoding a hash
# Expect:
# 1
# II
# ...
$_ = XMLout($hashref1, rootname => '');
is_deeply(XMLin("$_"), $hashref1,
'generated doc with no root element from hash');
ok(s/1<\/one>//, 'first key encoded OK');
ok(s/II<\/two>//, 'second key encoded OK');
ok(s/...<\/three>//, 'third key encoded OK');
like($_, qr/^\s*$/, 'document OK');
# Now try encoding a nested hash
$ref = { value => '555 1234',
hash1 => { one => 1 },
hash2 => { two => 2 } };
# Expect:
# 555 1234
#
#
$_ = XMLout($ref, rootname => '');
is_deeply(XMLin("$_"), $ref,
'generated docucment with no root element from nested hashes');
ok(s{555 1234<\/value>\s*}{}s, 'first element OK');
ok(s{\s*}{}s, 'second element OK');
ok(s{\s*}{}s, 'third element OK');
like($_, qr{^\s*$}s, 'document OK');
# Now try encoding an anonymous array
$ref = [ qw(1 two III) ];
# Expect:
# 1
# two
# III
$_ = XMLout($ref, rootname => '');
is_deeply(XMLin("$_"), $ref,
'generated doc with no root name from array');
like($_, qr{
^\s*1
\s*two
\s*III
\s*$}sx, 'document OK');
# Test option error handling
$_ = eval { XMLout($hashref1, searchpath => []) }; # only valid for XMLin()
ok(!defined($_), 'caught attempt to specify searchpath on XMLout');
like($@, qr/Unrecognised option:/, 'with correct error message');
$_ = eval { XMLout($hashref1, 'bogus') };
ok(!defined($_), 'caught attempt to specify odd number of option args');
like($@, qr/Options must be name=>value pairs \(odd number supplied\)/,
'with correct error message');
# Test output to file
my $TestFile = 'testoutput.xml';
unlink($TestFile);
ok(!-e $TestFile, 'output file does not exist');
$xml = XMLout($hashref1);
XMLout($hashref1, outputfile => $TestFile);
ok(-e $TestFile, 'created xml output file');
is(ReadFile($TestFile), $xml, 'Contents match expectations');
unlink($TestFile);
# Test output to an IO handle
ok(!-e $TestFile);
open my $fh, '>', $TestFile or die "open(>$TestFile): $!";
XMLout($hashref1, outputfile => $fh);
$fh->close() or die "close() failed: $!";
ok(-e $TestFile, 'create XML output file via IO::File');
is(ReadFile($TestFile), $xml, 'Contents match expectations');
unlink($TestFile);
# After all that, confirm that the original hashref we supplied has not
# been corrupted.
is_deeply($hashref1, $hashref2, 'original data not corrupted');
# Confirm that hash keys with leading '-' are skipped
$ref = {
'a' => 'one',
'-b' => 'two',
'-c' => {
'one' => 1,
'two' => 2
}
};
$_ = XMLout($ref, rootname => 'opt');
like($_, qr{^\s*\s*$}s, "skipped hashkeys with '-' prefix");
# Try a more complex unfolding with key attributes named in a hash
$ref = {
'car' => {
'LW1804' => {
'option' => {
'9926543-1167' => { 'key' => 1, 'desc' => 'Steering Wheel' }
},
'id' => 2,
'make' => 'GM'
},
'SH6673' => {
'option' => {
'6389733317-12' => { 'key' => 2, 'desc' => 'Electric Windows' },
'3735498158-01' => { 'key' => 3, 'desc' => 'Leather Seats' },
'5776155953-25' => { 'key' => 4, 'desc' => 'Sun Roof' },
},
'id' => 1,
'make' => 'Ford'
}
}
};
# Expect:
#
#
#
#
#
#
#
#
#
#
$_ = XMLout($ref, keyattr => { 'car' => 'license', 'option' => 'pn' });
is_deeply(XMLin($_,
forcearray => 1,
keyattr => { 'car' => 'license', 'option' => 'pn' }
), $ref, 'generated document from complex nested hash with unfolding');
ok(s{\s*make="GM"}{gm}s, 'element 1 attribute 1 OK');
ok(s{\s*id="2"}{gm}s, 'element 1 attribute 2 OK');
ok(s{\s*license="LW1804"}{gm}s, 'element 1 attribute 3 OK');
ok(s{\s*desc="Steering Wheel"}{opt}s, 'element 1.1 attribute 1 OK');
ok(s{\s*pn="9926543-1167"}{opt}s, 'element 1.1 attribute 2 OK');
ok(s{\s*key="1"}{opt}s, 'element 1.1 attribute 3 OK');
ok(s{\s*\s*\s*}{CAR}s,
'elements 1 and 1.1 OK');
ok(s{\s*make="Ford"}{ford}s, 'element 2 attribute 1 OK');
ok(s{\s*id="1"}{ford}s, 'element 2 attribute 2 OK');
ok(s{\s*license="SH6673"}{ford}s, 'element 2 attribute 3 OK');
ok(s{\s*desc="Electric Windows"}{1}s, 'element 2.1 attribute 1 OK');
ok(s{\s*pn="6389733317-12"}{1}s, 'element 2.1 attribute 2 OK');
ok(s{\s*key="2"}{1}s, 'element 2.1 attribute 3 OK');
ok(s{\s*\s*(\s*){3}}{CAR}s, 'element 2 OK');
ok(s{^<(\w+)\s*>\s*CAR\s*CAR\s*\1>$}{}s, 'document OK');
# Check that empty hashes translate to empty tags
$ref = {
'one' => {
'attr1' => 'avalue1',
'nest1' => [ 'nvalue1' ],
'nest2' => {}
},
two => {}
};
$_ = XMLout($ref);
ok(s{\s*}{}, 'nested empty hash OK');
ok(s{nvalue1\s*}{}, 'array OK');
ok(s{\s*}{}, 'scalar OK');
ok(s{\s*\s*\s*}{}, 'nesting OK');
ok(s{\s*}{}, 'empty hash OK');
like($_, qr{^\s*<(\w+)\s*>\s*\s*\s*\1\s*>\s*$}, 'document OK');
# Check undefined values generate warnings
{
my $warn = '';
local $SIG{__WARN__} = sub { $warn = $_[0] };
$ref = { 'one' => 1, 'two' => undef };
my $expect = qr/^<\w+(\s+one="1"|\s+two=""){2}/;
$_ = XMLout($ref);
like($warn, qr/Use of uninitialized value/,
'caught warning re uninitialised value');
like($_, $expect, 'undef maps to any empty attribute by default');
}
{
# unless warnings are disabled
no warnings;
my $warn = '';
local $SIG{__WARN__} = sub { $warn = $_[0] };
my $expect = qr/^<\w+(\s+one="1"|\s+two=""){2}/;
$_ = XMLout($ref);
is($warn, '', 'no warning re uninitialised value if warnings off');
like($_, $expect, 'undef still maps to any empty attribute');
}
# Unless undef is mapped to empty elements
$ref = { 'tag' => undef };
$_ = XMLout($ref, suppressempty => undef);
like($_, qr{^\s*<(\w*)\s*>\s*\s*\1\s*>\s*$}s,
'uninitialiased values successfully mapped to empty elements');
# Set suppressempty to 1 to not output anything for undef
$ref = { 'one' => 1, 'two' => undef };
$_ = XMLout($ref, suppressempty => 1, noattr => 1);
like($_, qr{^\s*<(\w*)\s*>\s*1\s*\1\s*>\s*$}s,
'uninitialiased values successfully skipped');
# Try undef in an array
$ref = { a => [ 'one', undef, 'three' ] };
$_ = XMLout($ref);
like($_,
qr{
^\s*<(\w*)\s*>
\s*one
\s*
\s*three
\s*\1\s*>\s*$
}xs,
'uninitialiased value in array is empty element');
# And again with SuppressEmpty enabled
$_ = XMLout($ref, SuppressEmpty => 1);
like($_,
qr{
^\s*<(\w*)\s*>
\s*one
\s*three
\s*\1\s*>\s*$
}xs,
'uninitialiased value in array is skipped');
# Test the keeproot option
$ref = {
'seq' => {
'name' => 'alpha',
'alpha' => [ 1, 2, 3 ]
}
};
my $xml1 = XMLout($ref, rootname => 'sequence');
my $xml2 = XMLout({ 'sequence' => $ref }, keeproot => 1);
is_deeply($xml1, $xml2, 'keeproot works as expected');
# Test that items with text content are output correctly
# Expect: text
$ref = { 'one' => 1, 'content' => 'text' };
$_ = XMLout($ref);
like($_, qr{^\s*text\s*$}s, 'content keys mapped OK');
# Even if we change the default value for the 'contentkey' option
$ref = { 'one' => 1, 'text_content' => 'text' };
$_ = XMLout($ref, contentkey => 'text_content');
like($_, qr{^\s*text\s*$}s, 'even when name changed');
# and also if we add the '-' prefix
$_ = XMLout($ref, contentkey => '-text_content');
like($_, qr{^\s*text\s*$}s, 'even with "-" prefix');
# Confirm content key works with undef values (and no warnings)
{
my $warn = '';
local $SIG{__WARN__} = sub { $warn = $_[0] };
$ref = {
column => [
{ name => 'title', content => 'A Title' },
{ name => 'sponsor', content => undef },
],
};
$_ = XMLout($ref, suppress_empty => undef, content_key => 'content');
ok(!$warn, 'no warnings with suppress_empty => undef');
like($_, qr{^<(\w+)>
\s*A\sTitle
\s*
\s*
\1>$
}sx, "undef does not cause content tags in output"
);
}
# Check 'noattr' option
$ref = {
attr1 => 'value1',
attr2 => 'value2',
nest => [ qw(one two three) ]
};
# Expect:
#
#
# value1
# value2
# one
# two
# three
#
#
$_ = XMLout($ref, noattr => 1);
unlike($_, qr{=}s, 'generated document with no attributes');
is_deeply(XMLin($_), $ref, 'parses ok');
ok(s{\s*<(attr1)>value1\1>\s*}{NEST}s, 'scalar 1 mapped ok');
ok(s{\s*<(attr2)>value2\1>\s*}{NEST}s, 'scalar 2 mapped ok');
ok(s{\s*<(nest)>one\1>\s*<\1>two\1>\s*<\1>three\1>}{NEST}s,
'array mapped ok');
like($_, qr{^<(\w+)\s*>(NEST\s*){3}\1>$}s, 'document OK');
# Check noattr doesn't screw up keyattr
$ref = { number => {
'twenty one' => { dec => 21, hex => '0x15' },
'thirty two' => { dec => 32, hex => '0x20' }
}
};
# Expect:
#
#
#
# 21
# twenty one
# 0x15
#
#
# 32
# thirty two
# 0x20
#
#
#
$_ = XMLout($ref, noattr => 1, keyattr => [ 'word' ]);
unlike($_, qr{=}s, 'same again but with unfolding too');
is_deeply(XMLin($_, keyattr => [ 'word' ]), $ref, 'parsed OK');
ok(s{\s*<(dec)>21\1>\s*}{21}s, 'scalar 1.1 mapped OK');
ok(s{\s*<(hex)>0x15\1>\s*}{21}s, 'scalar 1.2 mapped OK');
ok(s{\s*<(word)>twenty one\1>\s*}{21}s, 'scalar 1.3 mapped OK');
ok(s{\s*<(number)>212121\1>\s*}{NUM}s, 'element 1 OK');
ok(s{\s*<(dec)>32\1>\s*}{32}s, 'scalar 2.1 mapped OK');
ok(s{\s*<(hex)>0x20\1>\s*}{32}s, 'scalar 2.1 mapped OK');
ok(s{\s*<(word)>thirty two\1>\s*}{32}s, 'scalar 2.1 mapped OK');
ok(s{\s*<(number)>323232\1>\s*}{NUM}s, 'element 2 OK');
like($_, qr{^<(\w+)\s*>NUMNUM\1>$}, 'document OK');
# Check grouped tags get ungrouped correctly
$ref = {
prefix => 'before',
dirs => [ '/usr/bin', '/usr/local/bin' ],
suffix => 'after',
};
# Expect:
#
#
# before
#
# /usr/bin
# /usr/local/bin
#
# after
#
#
$@ = '';
$_ = eval { XMLout($ref, grouptags => {dirs => 'dirs'}, noattr => 1); };
ok($@, 'bad GroupTags value was caught');
like("$@", qr{Bad value in GroupTags: 'dirs' => 'dirs'},
'error message looks good');
$@ = '';
$_ = eval { XMLout($ref, grouptags => {dirs => 'dir'}, noattr => 1); };
ok(!$@, 'good GroupTags value caused no error');
ok(s{\s*<(prefix)>before\1>\s*}{ELEM}s, 'prefix OK');
ok(s{\s*<(suffix)>after\1>\s*}{ELEM}s, 'suffix OK');
ok(s{\s*/usr/bin\s*/usr/local/bin\s*}{LIST}s, 'list OK');
ok(s{\s*LIST\s*}{ELEM}s, 'group OK');
like($_, qr{^<(\w+)\s*>ELEMELEMELEM\1>$}, 'document OK');
is_deeply($ref, {
prefix => 'before',
dirs => [ '/usr/bin', '/usr/local/bin' ],
suffix => 'after',
}, 'original ref is not messed with');
# Try again with multiple groupings
$ref = {
dirs => [ '/usr/bin', '/usr/local/bin' ],
terms => [ 'vt100', 'xterm' ],
};
# Expect:
#
#
#
# /usr/bin
# /usr/local/bin
#
#
# vt100
# xterm
#
#
#
$_ = XMLout($ref, grouptags => {dirs => 'dir', terms => 'term'}, noattr => 1);
ok(s{\s*/usr/bin\s*/usr/local/bin\s*}{LIST}s, 'list 1 OK');
ok(s{\s*LIST\s*}{ELEM}s, 'group 1 OK');
ok(s{\s*vt100\s*xterm\s*}{LIST}s, 'list 2 OK');
ok(s{\s*LIST\s*}{ELEM}s, 'group 2 OK');
like($_, qr{^<(\w+)\s*>ELEMELEM\1>$}, 'document OK');
# Confirm unfolding and grouping work together
$ref = {
dirs => {
first => { content => '/usr/bin' },
second => { content => '/usr/local/bin' },
},
};
# Expect:
#
#
#
# /usr/bin
# /usr/local/bin
#
#
#
$_ = XMLout($ref,
grouptags => {dirs => 'dir'}, keyattr => {dir => 'name'},
);
ok(s{\s*/usr/bin\s*}{ITEM}s, 'item 1 OK');
ok(s{\s*/usr/local/bin\s*}{ITEM}s, 'item 2 OK');
ok(s{\s*ITEMITEM\s*}{GROUP}s, 'group OK');
like($_, qr{^<(\w+)\s*>GROUP\1>$}, 'document OK');
# Combine unfolding, grouping and stripped content - watch it fail :-(
$ref = {
dirs => {
first => '/usr/bin',
second => '/usr/local/bin'
},
};
# Expect:
#
#
#
#
#
$_ = XMLout($ref,
grouptags => {dirs => 'dir'}, keyattr => {dir => 'name'},
contentkey => '-content'
);
like($_, qr{
^<(\w+)>\s*
\s*
\s*
\s*
\1>$
}x, 'Failed to unwrap/group stripped content - as expected');
# Check 'NoIndent' option
$ref = {
nest => [ qw(one two three) ]
};
# Expect:
#
# onetwothree
#
$_ = XMLout($ref, NoIndent => 1);
is_deeply(XMLin($_), $ref, 'parses ok');
is($_, 'onetwothree',
'NoIndent worked ok');
# Check 'NoIndent' works with KeyAttr
$ref = {
person => {
bob => { age => 25 },
kate => { age => 22 },
},
};
# Expect:
#
#
#
$_ = XMLout($ref, NoIndent => 1, KeyAttr => {person => 'name'});
is_deeply(XMLin($_), $ref, 'parses ok');
like($_, qr{
(
|
){2}
}sx,
'NoIndent worked ok with KeyAttr');
# Try the 'AttrIndent' option (assume NoSort defaults to off)
$ref = {
beta => '2',
gamma => '3',
alpha => '1',
colours => {
red => '#ff0000',
green => '#00ff00',
}
};
$_ = XMLout($ref, AttrIndent => 1, RootName => 'opt');
is($_, '
', 'AttrIndent seems to work');
# Test the attribute/element sorting algorithm
$xml = q{
};
$ref = XMLin($xml);
$_ = XMLout($ref, RootName => 'opt');
is($_, qq(\n) .
qq( \n) .
qq( \n) .
qq( \n) .
qq( \n) .
qq( \n) .
qq(\n),
'sorting by default key attribute works');
# Try again but with specific key fields:
$ref = XMLin($xml, KeyAttr => {test => 'vegetable', box => 'size'});
$_ = XMLout($ref,
RootName => 'opt',
KeyAttr => {test => 'vegetable', box => 'size'}
);
is($_, qq(\n) .
qq( \n) .
qq( \n) .
qq( \n) .
qq( \n) .
qq( \n) .
qq(\n),
'sorting by specified key attributes works');
# Try again but with no key fields:
$ref = XMLin($xml, KeyAttr => {});
$_ = XMLout($ref, RootName => 'opt', KeyAttr => {});
like($_, qr{^\s*
(
(
\s*
\s*
\s*
)
|(
\s*
\s*
)
){2}
\s*
$}sx, 'sorting with no key attribute works');
# Check that sorting can be disabled
$@ = '';
SKIP: {
eval { require Tie::IxHash };
skip "Tie::IxHash not installed", 1 if $@;
my(%hash1, %hash2);
tie %hash1, 'Tie::IxHash', Jan => 1, Feb => 2, Mar => 3, Apr => 4, May => 5;
tie %hash2, 'Tie::IxHash', X => { b => 2 }, A => { c => 3 }, Z => { a => 1 },
M => { f => 6 }, K => { e => 4 }, O => { d => 5 };
$hash1{func} = \%hash2;
$_ = XMLout(\%hash1, NoSort => 1, KeyAttr => {func => 'name'});
like($_, qr{
^\s+
\s+
\s+
\s+
\s+
\s+
\s+
\s*$
}sx, 'Suppressing sort worked');
}
# Check ValueAttr => {} can expand the relevant records
$ref = { one => 1, two => 2, six => 6 };
$xml = XMLout($ref, ValueAttr => { one => 'value', six => 'num' });
like($xml, qr{
^
(
\s*
| \s*
){2}
\s*$
}sx, 'Correct attributes inserted when ValueAttr specified'
);
# Try out the NumericEscape option
SKIP: {
skip "Perl 5.6 or better required", 4 unless($] >= 5.006);
$ref = { euro => "\x{20AC}", nbsp => "\x{A0}" };
$xml = XMLout($ref); # Default: no numeric escaping
my $ents = join ',', sort ($xml =~ m{(\d+);}g);
is($ents, '', "No numeric escaping by default");
$xml = XMLout($ref, NumericEscape => 0);
$ents = join ',', sort ($xml =~ m{(\d+);}g);
is($ents, '', "No numeric escaping: explicit");
$xml = XMLout($ref, NumericEscape => 2);
$ents = join ',', sort ($xml =~ m{(\d+);}g);
is($ents, '160,8364', "Level 2 numeric escaping looks good");
$xml = XMLout($ref, NumericEscape => 1);
$ents = join ',', sort ($xml =~ m{(\d+);}g);
is($ents, '8364', "Level 1 numeric escaping looks good");
}
# 'Stress test' with a data structure that maps to several thousand elements.
# Unfold elements with XMLout() and fold them up again with XMLin()
my $opt1 = {};
foreach my $i (0..40) {
foreach my $j (0..$i) {
$opt1->{TypeA}->{$i}->{Record}->{$j} = { Hex => sprintf("0x%04X", $j) };
$opt1->{TypeB}->{$i}->{Record}->{$j} = { Oct => sprintf("%04o", $j) };
$opt1->{List}->[$i]->[$j] = "$i:$j";
}
}
$xml = XMLout($opt1, keyattr => { TypeA => 'alpha', TypeB => 'beta', Record => 'id' });
my $opt2 = XMLin($xml, keyattr => { TypeA => 'alpha', TypeB => 'beta', Record => 'id' }, forcearray => 1);
is_deeply($opt1, $opt2, 'large datastructure mapped to XML and back OK');
exit(0);
XML-Simple-2.24/lib/ 0000775 0001750 0001750 00000000000 13075040223 013104 5 ustar grant grant XML-Simple-2.24/lib/XML/ 0000775 0001750 0001750 00000000000 13075040223 013544 5 ustar grant grant XML-Simple-2.24/lib/XML/Simple.pm 0000644 0001750 0001750 00000304154 13075040223 015340 0 ustar grant grant package XML::Simple;
$XML::Simple::VERSION = '2.24';
=head1 NAME
XML::Simple - An API for simple XML files
=head1 SYNOPSIS
You really don't want to use this module in new code. If you ignore this
warning and use it anyway, the C mode will save you a little pain.
use XML::Simple qw(:strict);
my $ref = XMLin([] [, ]);
my $xml = XMLout($hashref [, ]);
Or the object oriented way:
require XML::Simple qw(:strict);
my $xs = XML::Simple->new([]);
my $ref = $xs->XMLin([] [, ]);
my $xml = $xs->XMLout($hashref [, ]);
(or see L<"SAX SUPPORT"> for 'the SAX way').
Note, in these examples, the square brackets are used to denote optional items
not to imply items should be supplied in arrayrefs.
=cut
# See after __END__ for more POD documentation
# Load essentials here, other modules loaded on demand later
use strict;
use warnings;
use warnings::register;
use Carp;
use Scalar::Util qw();
require Exporter;
##############################################################################
# Define some constants
#
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $PREFERRED_PARSER);
@ISA = qw(Exporter);
@EXPORT = qw(XMLin XMLout);
@EXPORT_OK = qw(xml_in xml_out);
my %StrictMode = ();
my @KnownOptIn = qw(keyattr keeproot forcecontent contentkey noattr
searchpath forcearray cache suppressempty parseropts
grouptags nsexpand datahandler varattr variables
normalisespace normalizespace valueattr strictmode);
my @KnownOptOut = qw(keyattr keeproot contentkey noattr
rootname xmldecl outputfile noescape suppressempty
grouptags nsexpand handler noindent attrindent nosort
valueattr numericescape strictmode);
my @DefKeyAttr = qw(name key id);
my $DefRootName = qq(opt);
my $DefContentKey = qq(content);
my $DefXmlDecl = qq();
my $xmlns_ns = 'http://www.w3.org/2000/xmlns/';
my $bad_def_ns_jcn = '{' . $xmlns_ns . '}'; # LibXML::SAX workaround
##############################################################################
# Globals for use by caching routines
#
my %MemShareCache = ();
my %MemCopyCache = ();
##############################################################################
# Wrapper for Exporter - handles ':strict'
#
sub import {
# Handle the :strict tag
my($calling_package) = caller();
_strict_mode_for_caller(1) if grep(/^:strict$/, @_);
# Pass everything else to Exporter.pm
@_ = grep(!/^:strict$/, @_);
goto &Exporter::import;
}
##############################################################################
# Constructor for optional object interface.
#
sub new {
my $class = shift;
if(@_ % 2) {
croak "Default options must be name=>value pairs (odd number supplied)";
}
my %known_opt;
@known_opt{@KnownOptIn, @KnownOptOut} = ();
my %raw_opt = @_;
$raw_opt{strictmode} = _strict_mode_for_caller()
unless exists $raw_opt{strictmode};
my %def_opt;
while(my($key, $val) = each %raw_opt) {
my $lkey = lc($key);
$lkey =~ s/_//g;
croak "Unrecognised option: $key" unless(exists($known_opt{$lkey}));
$def_opt{$lkey} = $val;
}
my $self = { def_opt => \%def_opt };
return(bless($self, $class));
}
##############################################################################
# Sub: _strict_mode_for_caller()
#
# Gets or sets the XML::Simple :strict mode flag for the calling namespace.
# Walks back through call stack to find the calling namespace and sets the
# :strict mode flag for that namespace if an argument was supplied and returns
# the flag value if not.
#
sub _strict_mode_for_caller {
my $set_mode = @_;
my $frame = 1;
while(my($package) = caller($frame++)) {
next if $package eq 'XML::Simple';
$StrictMode{$package} = 1 if $set_mode;
return $StrictMode{$package};
}
return(0);
}
##############################################################################
# Sub: _get_object()
#
# Helper routine called from XMLin() and XMLout() to create an object if none
# was provided. Note, this routine does mess with the caller's @_ array.
#
sub _get_object {
my $self;
if($_[0] and UNIVERSAL::isa($_[0], 'XML::Simple')) {
$self = shift;
}
else {
$self = XML::Simple->new();
}
return $self;
}
##############################################################################
# Sub/Method: XMLin()
#
# Exported routine for slurping XML into a hashref - see pod for info.
#
# May be called as object method or as a plain function.
#
# Expects one arg for the source XML, optionally followed by a number of
# name => value option pairs.
#
sub XMLin {
my $self = &_get_object; # note, @_ is passed implicitly
my $target = shift;
# Work out whether to parse a string, a file or a filehandle
if(not defined $target) {
return $self->parse_file(undef, @_);
}
elsif($target eq '-') {
local($/) = undef;
$target = ;
return $self->parse_string(\$target, @_);
}
elsif(my $type = ref($target)) {
if($type eq 'SCALAR') {
return $self->parse_string($target, @_);
}
else {
return $self->parse_fh($target, @_);
}
}
elsif($target =~ m{<.*?>}s) {
return $self->parse_string(\$target, @_);
}
else {
return $self->parse_file($target, @_);
}
}
##############################################################################
# Sub/Method: parse_file()
#
# Same as XMLin, but only parses from a named file.
#
sub parse_file {
my $self = &_get_object; # note, @_ is passed implicitly
my $filename = shift;
$self->handle_options('in', @_);
$filename = $self->default_config_file if not defined $filename;
$filename = $self->find_xml_file($filename, @{$self->{opt}->{searchpath}});
# Check cache for previous parse
if($self->{opt}->{cache}) {
foreach my $scheme (@{$self->{opt}->{cache}}) {
my $method = 'cache_read_' . $scheme;
my $opt = $self->$method($filename);
return($opt) if($opt);
}
}
my $ref = $self->build_simple_tree($filename, undef);
if($self->{opt}->{cache}) {
my $method = 'cache_write_' . $self->{opt}->{cache}->[0];
$self->$method($ref, $filename);
}
return $ref;
}
##############################################################################
# Sub/Method: parse_fh()
#
# Same as XMLin, but only parses from a filehandle.
#
sub parse_fh {
my $self = &_get_object; # note, @_ is passed implicitly
my $fh = shift;
croak "Can't use " . (defined $fh ? qq{string ("$fh")} : 'undef') .
" as a filehandle" unless ref $fh;
$self->handle_options('in', @_);
return $self->build_simple_tree(undef, $fh);
}
##############################################################################
# Sub/Method: parse_string()
#
# Same as XMLin, but only parses from a string or a reference to a string.
#
sub parse_string {
my $self = &_get_object; # note, @_ is passed implicitly
my $string = shift;
$self->handle_options('in', @_);
return $self->build_simple_tree(undef, ref $string ? $string : \$string);
}
##############################################################################
# Method: default_config_file()
#
# Returns the name of the XML file to parse if no filename (or XML string)
# was provided.
#
sub default_config_file {
my $self = shift;
require File::Basename;
my($basename, $script_dir, $ext) = File::Basename::fileparse($0, '\.[^\.]+');
# Add script directory to searchpath
if($script_dir) {
unshift(@{$self->{opt}->{searchpath}}, $script_dir);
}
return $basename . '.xml';
}
##############################################################################
# Method: build_simple_tree()
#
# Builds a 'tree' data structure as provided by XML::Parser and then
# 'simplifies' it as specified by the various options in effect.
#
sub build_simple_tree {
my $self = shift;
my $tree = eval {
$self->build_tree(@_);
};
Carp::croak("$@XML::Simple called") if $@;
return $self->{opt}->{keeproot}
? $self->collapse({}, @$tree)
: $self->collapse(@{$tree->[1]});
}
##############################################################################
# Method: build_tree()
#
# This routine will be called if there is no suitable pre-parsed tree in a
# cache. It parses the XML and returns an XML::Parser 'Tree' style data
# structure (summarised in the comments for the collapse() routine below).
#
# XML::Simple requires the services of another module that knows how to parse
# XML. If XML::SAX is installed, the default SAX parser will be used,
# otherwise XML::Parser will be used.
#
# This routine expects to be passed a filename as argument 1 or a 'string' as
# argument 2. The 'string' might be a string of XML (passed by reference to
# save memory) or it might be a reference to an IO::Handle. (This
# non-intuitive mess results in part from the way XML::Parser works but that's
# really no excuse).
#
sub build_tree {
my $self = shift;
my $filename = shift;
my $string = shift;
my $preferred_parser = $PREFERRED_PARSER;
unless(defined($preferred_parser)) {
$preferred_parser = $ENV{XML_SIMPLE_PREFERRED_PARSER} || '';
}
if($preferred_parser eq 'XML::Parser') {
return($self->build_tree_xml_parser($filename, $string));
}
eval { require XML::SAX; }; # We didn't need it until now
if($@) { # No XML::SAX - fall back to XML::Parser
if($preferred_parser) { # unless a SAX parser was expressly requested
croak "XMLin() could not load XML::SAX";
}
return($self->build_tree_xml_parser($filename, $string));
}
$XML::SAX::ParserPackage = $preferred_parser if($preferred_parser);
my $sp = XML::SAX::ParserFactory->parser(Handler => $self);
$self->{nocollapse} = 1;
my($tree);
if($filename) {
$tree = $sp->parse_uri($filename);
}
else {
if(ref($string) && ref($string) ne 'SCALAR') {
$tree = $sp->parse_file($string);
}
else {
$tree = $sp->parse_string($$string);
}
}
return($tree);
}
##############################################################################
# Method: build_tree_xml_parser()
#
# This routine will be called if XML::SAX is not installed, or if XML::Parser
# was specifically requested. It takes the same arguments as build_tree() and
# returns the same data structure (XML::Parser 'Tree' style).
#
sub build_tree_xml_parser {
my $self = shift;
my $filename = shift;
my $string = shift;
eval {
local($^W) = 0; # Suppress warning from Expat.pm re File::Spec::load()
require XML::Parser; # We didn't need it until now
};
if($@) {
croak "XMLin() requires either XML::SAX or XML::Parser";
}
if($self->{opt}->{nsexpand}) {
carp "'nsexpand' option requires XML::SAX";
}
my $xp = XML::Parser->new(Style => 'Tree', @{$self->{opt}->{parseropts}});
my($tree);
if($filename) {
# $tree = $xp->parsefile($filename); # Changed due to prob w/mod_perl
open(my $xfh, '<', $filename) || croak qq($filename - $!);
$tree = $xp->parse($xfh);
}
else {
$tree = $xp->parse($$string);
}
return($tree);
}
##############################################################################
# Method: cache_write_storable()
#
# Wrapper routine for invoking Storable::nstore() to cache a parsed data
# structure.
#
sub cache_write_storable {
my($self, $data, $filename) = @_;
my $cachefile = $self->storable_filename($filename);
require Storable; # We didn't need it until now
if ('VMS' eq $^O) {
Storable::nstore($data, $cachefile);
}
else {
# If the following line fails for you, your Storable.pm is old - upgrade
Storable::lock_nstore($data, $cachefile);
}
}
##############################################################################
# Method: cache_read_storable()
#
# Wrapper routine for invoking Storable::retrieve() to read a cached parsed
# data structure. Only returns cached data if the cache file exists and is
# newer than the source XML file.
#
sub cache_read_storable {
my($self, $filename) = @_;
my $cachefile = $self->storable_filename($filename);
return unless(-r $cachefile);
return unless((stat($cachefile))[9] > (stat($filename))[9]);
require Storable; # We didn't need it until now
if ('VMS' eq $^O) {
return(Storable::retrieve($cachefile));
}
else {
return(Storable::lock_retrieve($cachefile));
}
}
##############################################################################
# Method: storable_filename()
#
# Translates the supplied source XML filename into a filename for the storable
# cached data. A '.stor' suffix is added after stripping an optional '.xml'
# suffix.
#
sub storable_filename {
my($self, $cachefile) = @_;
$cachefile =~ s{(\.xml)?$}{.stor};
return $cachefile;
}
##############################################################################
# Method: cache_write_memshare()
#
# Takes the supplied data structure reference and stores it away in a global
# hash structure.
#
sub cache_write_memshare {
my($self, $data, $filename) = @_;
$MemShareCache{$filename} = [time(), $data];
}
##############################################################################
# Method: cache_read_memshare()
#
# Takes a filename and looks in a global hash for a cached parsed version.
#
sub cache_read_memshare {
my($self, $filename) = @_;
return unless($MemShareCache{$filename});
return unless($MemShareCache{$filename}->[0] > (stat($filename))[9]);
return($MemShareCache{$filename}->[1]);
}
##############################################################################
# Method: cache_write_memcopy()
#
# Takes the supplied data structure and stores a copy of it in a global hash
# structure.
#
sub cache_write_memcopy {
my($self, $data, $filename) = @_;
require Storable; # We didn't need it until now
$MemCopyCache{$filename} = [time(), Storable::dclone($data)];
}
##############################################################################
# Method: cache_read_memcopy()
#
# Takes a filename and looks in a global hash for a cached parsed version.
# Returns a reference to a copy of that data structure.
#
sub cache_read_memcopy {
my($self, $filename) = @_;
return unless($MemCopyCache{$filename});
return unless($MemCopyCache{$filename}->[0] > (stat($filename))[9]);
return(Storable::dclone($MemCopyCache{$filename}->[1]));
}
##############################################################################
# Sub/Method: XMLout()
#
# Exported routine for 'unslurping' a data structure out to XML.
#
# Expects a reference to a data structure and an optional list of option
# name => value pairs.
#
sub XMLout {
my $self = &_get_object; # note, @_ is passed implicitly
croak "XMLout() requires at least one argument" unless(@_);
my $ref = shift;
$self->handle_options('out', @_);
# If namespace expansion is set, XML::NamespaceSupport is required
if($self->{opt}->{nsexpand}) {
require XML::NamespaceSupport;
$self->{nsup} = XML::NamespaceSupport->new();
$self->{ns_prefix} = 'aaa';
}
# Wrap top level arrayref in a hash
if(UNIVERSAL::isa($ref, 'ARRAY')) {
$ref = { anon => $ref };
}
# Extract rootname from top level hash if keeproot enabled
if($self->{opt}->{keeproot}) {
my(@keys) = keys(%$ref);
if(@keys == 1) {
$ref = $ref->{$keys[0]};
$self->{opt}->{rootname} = $keys[0];
}
}
# Ensure there are no top level attributes if we're not adding root elements
elsif($self->{opt}->{rootname} eq '') {
if(UNIVERSAL::isa($ref, 'HASH')) {
my $refsave = $ref;
$ref = {};
foreach (keys(%$refsave)) {
if(ref($refsave->{$_})) {
$ref->{$_} = $refsave->{$_};
}
else {
$ref->{$_} = [ $refsave->{$_} ];
}
}
}
}
# Encode the hashref and write to file if necessary
$self->{_ancestors} = {};
my $xml = $self->value_to_xml($ref, $self->{opt}->{rootname}, '');
delete $self->{_ancestors};
if($self->{opt}->{xmldecl}) {
$xml = $self->{opt}->{xmldecl} . "\n" . $xml;
}
if($self->{opt}->{outputfile}) {
if(ref($self->{opt}->{outputfile})) {
my $fh = $self->{opt}->{outputfile};
if(UNIVERSAL::isa($fh, 'GLOB') and !UNIVERSAL::can($fh, 'print')) {
eval { require IO::Handle; };
croak $@ if $@;
}
return($fh->print($xml));
}
else {
open(my $out, '>', "$self->{opt}->{outputfile}") ||
croak "open($self->{opt}->{outputfile}): $!";
binmode($out, ':utf8') if($] >= 5.008);
print $out $xml or croak "print: $!";
close $out or croak "close: $!";
}
}
elsif($self->{opt}->{handler}) {
require XML::SAX;
my $sp = XML::SAX::ParserFactory->parser(
Handler => $self->{opt}->{handler}
);
return($sp->parse_string($xml));
}
else {
return($xml);
}
}
##############################################################################
# Method: handle_options()
#
# Helper routine for both XMLin() and XMLout(). Both routines handle their
# first argument and assume all other args are options handled by this routine.
# Saves a hash of options in $self->{opt}.
#
# If default options were passed to the constructor, they will be retrieved
# here and merged with options supplied to the method call.
#
# First argument should be the string 'in' or the string 'out'.
#
# Remaining arguments should be name=>value pairs. Sets up default values
# for options not supplied. Unrecognised options are a fatal error.
#
sub handle_options {
my $self = shift;
my $dirn = shift;
# Determine valid options based on context
my %known_opt;
if($dirn eq 'in') {
@known_opt{@KnownOptIn} = @KnownOptIn;
}
else {
@known_opt{@KnownOptOut} = @KnownOptOut;
}
# Store supplied options in hashref and weed out invalid ones
if(@_ % 2) {
croak "Options must be name=>value pairs (odd number supplied)";
}
my %raw_opt = @_;
my $opt = {};
$self->{opt} = $opt;
while(my($key, $val) = each %raw_opt) {
my $lkey = lc($key);
$lkey =~ s/_//g;
croak "Unrecognised option: $key" unless($known_opt{$lkey});
$opt->{$lkey} = $val;
}
# Merge in options passed to constructor
foreach (keys(%known_opt)) {
unless(exists($opt->{$_})) {
if(exists($self->{def_opt}->{$_})) {
$opt->{$_} = $self->{def_opt}->{$_};
}
}
}
# Set sensible defaults if not supplied
if(exists($opt->{rootname})) {
unless(defined($opt->{rootname})) {
$opt->{rootname} = '';
}
}
else {
$opt->{rootname} = $DefRootName;
}
if($opt->{xmldecl} and $opt->{xmldecl} eq '1') {
$opt->{xmldecl} = $DefXmlDecl;
}
if(exists($opt->{contentkey})) {
if($opt->{contentkey} =~ m{^-(.*)$}) {
$opt->{contentkey} = $1;
$opt->{collapseagain} = 1;
}
}
else {
$opt->{contentkey} = $DefContentKey;
}
unless(exists($opt->{normalisespace})) {
$opt->{normalisespace} = $opt->{normalizespace};
}
$opt->{normalisespace} = 0 unless(defined($opt->{normalisespace}));
# Cleanups for values assumed to be arrays later
if($opt->{searchpath}) {
unless(ref($opt->{searchpath})) {
$opt->{searchpath} = [ $opt->{searchpath} ];
}
}
else {
$opt->{searchpath} = [ ];
}
if($opt->{cache} and !ref($opt->{cache})) {
$opt->{cache} = [ $opt->{cache} ];
}
if($opt->{cache}) {
$_ = lc($_) foreach (@{$opt->{cache}});
foreach my $scheme (@{$opt->{cache}}) {
my $method = 'cache_read_' . $scheme;
croak "Unsupported caching scheme: $scheme"
unless($self->can($method));
}
}
if(exists($opt->{parseropts})) {
if(warnings::enabled()) {
carp "Warning: " .
"'ParserOpts' is deprecated, contact the author if you need it";
}
}
else {
$opt->{parseropts} = [ ];
}
# Special cleanup for {forcearray} which could be regex, arrayref or boolean
# or left to default to 0
if(exists($opt->{forcearray})) {
if(ref($opt->{forcearray}) eq 'Regexp') {
$opt->{forcearray} = [ $opt->{forcearray} ];
}
if(ref($opt->{forcearray}) eq 'ARRAY') {
my @force_list = @{$opt->{forcearray}};
if(@force_list) {
$opt->{forcearray} = {};
foreach my $tag (@force_list) {
if(ref($tag) eq 'Regexp') {
push @{$opt->{forcearray}->{_regex}}, $tag;
}
else {
$opt->{forcearray}->{$tag} = 1;
}
}
}
else {
$opt->{forcearray} = 0;
}
}
else {
$opt->{forcearray} = ( $opt->{forcearray} ? 1 : 0 );
}
}
else {
if($opt->{strictmode} and $dirn eq 'in') {
croak "No value specified for 'ForceArray' option in call to XML$dirn()";
}
$opt->{forcearray} = 0;
}
# Special cleanup for {keyattr} which could be arrayref or hashref or left
# to default to arrayref
if(exists($opt->{keyattr})) {
if(ref($opt->{keyattr})) {
if(ref($opt->{keyattr}) eq 'HASH') {
# Make a copy so we can mess with it
$opt->{keyattr} = { %{$opt->{keyattr}} };
# Convert keyattr => { elem => '+attr' }
# to keyattr => { elem => [ 'attr', '+' ] }
foreach my $el (keys(%{$opt->{keyattr}})) {
if($opt->{keyattr}->{$el} =~ /^(\+|-)?(.*)$/) {
$opt->{keyattr}->{$el} = [ $2, ($1 ? $1 : '') ];
if($opt->{strictmode} and $dirn eq 'in') {
next if($opt->{forcearray} == 1);
next if(ref($opt->{forcearray}) eq 'HASH'
and $opt->{forcearray}->{$el});
croak "<$el> set in KeyAttr but not in ForceArray";
}
}
else {
delete($opt->{keyattr}->{$el}); # Never reached (famous last words?)
}
}
}
else {
if(@{$opt->{keyattr}} == 0) {
delete($opt->{keyattr});
}
}
}
else {
$opt->{keyattr} = [ $opt->{keyattr} ];
}
}
else {
if($opt->{strictmode}) {
croak "No value specified for 'KeyAttr' option in call to XML$dirn()";
}
$opt->{keyattr} = [ @DefKeyAttr ];
}
# Special cleanup for {valueattr} which could be arrayref or hashref
if(exists($opt->{valueattr})) {
if(ref($opt->{valueattr}) eq 'ARRAY') {
$opt->{valueattrlist} = {};
$opt->{valueattrlist}->{$_} = 1 foreach(@{ delete $opt->{valueattr} });
}
}
# make sure there's nothing weird in {grouptags}
if($opt->{grouptags}) {
croak "Illegal value for 'GroupTags' option - expected a hashref"
unless UNIVERSAL::isa($opt->{grouptags}, 'HASH');
while(my($key, $val) = each %{$opt->{grouptags}}) {
next if $key ne $val;
croak "Bad value in GroupTags: '$key' => '$val'";
}
}
# Check the {variables} option is valid and initialise variables hash
if($opt->{variables} and !UNIVERSAL::isa($opt->{variables}, 'HASH')) {
croak "Illegal value for 'Variables' option - expected a hashref";
}
if($opt->{variables}) {
$self->{_var_values} = { %{$opt->{variables}} };
}
elsif($opt->{varattr}) {
$self->{_var_values} = {};
}
}
##############################################################################
# Method: find_xml_file()
#
# Helper routine for XMLin().
# Takes a filename, and a list of directories, attempts to locate the file in
# the directories listed.
# Returns a full pathname on success; croaks on failure.
#
sub find_xml_file {
my $self = shift;
my $file = shift;
my @search_path = @_;
require File::Basename;
require File::Spec;
my($filename, $filedir) = File::Basename::fileparse($file);
if($filename ne $file) { # Ignore searchpath if dir component
return($file) if(-e $file);
}
else {
my($path);
foreach $path (@search_path) {
my $fullpath = File::Spec->catfile($path, $file);
return($fullpath) if(-e $fullpath);
}
}
# If user did not supply a search path, default to current directory
if(!@search_path) {
return($file) if(-e $file);
croak "File does not exist: $file";
}
croak "Could not find $file in ", join(':', @search_path);
}
##############################################################################
# Method: collapse()
#
# Helper routine for XMLin(). This routine really comprises the 'smarts' (or
# value add) of this module.
#
# Takes the parse tree that XML::Parser produced from the supplied XML and
# recurses through it 'collapsing' unnecessary levels of indirection (nested
# arrays etc) to produce a data structure that is easier to work with.
#
# Elements in the original parser tree are represented as an element name
# followed by an arrayref. The first element of the array is a hashref
# containing the attributes. The rest of the array contains a list of any
# nested elements as name+arrayref pairs:
#
# , [ { }, , [ ... ], ... ]
#
# The special element name '0' (zero) flags text content.
#
# This routine cuts down the noise by discarding any text content consisting of
# only whitespace and then moves the nested elements into the attribute hash
# using the name of the nested element as the hash key and the collapsed
# version of the nested element as the value. Multiple nested elements with
# the same name will initially be represented as an arrayref, but this may be
# 'folded' into a hashref depending on the value of the keyattr option.
#
sub collapse {
my $self = shift;
# Start with the hash of attributes
my $attr = shift;
if($self->{opt}->{noattr}) { # Discard if 'noattr' set
$attr = $self->new_hashref;
}
elsif($self->{opt}->{normalisespace} == 2) {
while(my($key, $value) = each %$attr) {
$attr->{$key} = $self->normalise_space($value)
}
}
# Do variable substitutions
if(my $var = $self->{_var_values}) {
while(my($key, $val) = each(%$attr)) {
$val =~ s^\$\{([\w.]+)\}^ $self->get_var($1) ^ge;
$attr->{$key} = $val;
}
}
# Roll up 'value' attributes (but only if no nested elements)
if(!@_ and keys %$attr == 1) {
my($k) = keys %$attr;
if($self->{opt}->{valueattrlist} and $self->{opt}->{valueattrlist}->{$k}) {
return $attr->{$k};
}
}
# Add any nested elements
my($key, $val);
while(@_) {
$key = shift;
$val = shift;
$val = '' if not defined $val;
if(ref($val)) {
$val = $self->collapse(@$val);
next if(!defined($val) and $self->{opt}->{suppressempty});
}
elsif($key eq '0') {
next if($val =~ m{^\s*$}s); # Skip all whitespace content
$val = $self->normalise_space($val)
if($self->{opt}->{normalisespace} == 2);
# do variable substitutions
if(my $var = $self->{_var_values}) {
$val =~ s^\$\{(\w+)\}^ $self->get_var($1) ^ge;
}
# look for variable definitions
if(my $var = $self->{opt}->{varattr}) {
if(exists $attr->{$var}) {
$self->set_var($attr->{$var}, $val);
}
}
# Collapse text content in element with no attributes to a string
if(!%$attr and !@_) {
return($self->{opt}->{forcecontent} ?
{ $self->{opt}->{contentkey} => $val } : $val
);
}
$key = $self->{opt}->{contentkey};
}
# Combine duplicate attributes into arrayref if required
if(exists($attr->{$key})) {
if(UNIVERSAL::isa($attr->{$key}, 'ARRAY')) {
push(@{$attr->{$key}}, $val);
}
else {
$attr->{$key} = [ $attr->{$key}, $val ];
}
}
elsif(defined($val) and UNIVERSAL::isa($val, 'ARRAY')) {
$attr->{$key} = [ $val ];
}
else {
if( $key ne $self->{opt}->{contentkey}
and (
($self->{opt}->{forcearray} == 1)
or (
(ref($self->{opt}->{forcearray}) eq 'HASH')
and (
$self->{opt}->{forcearray}->{$key}
or (grep $key =~ $_, @{$self->{opt}->{forcearray}->{_regex}})
)
)
)
) {
$attr->{$key} = [ $val ];
}
else {
$attr->{$key} = $val;
}
}
}
# Turn arrayrefs into hashrefs if key fields present
if($self->{opt}->{keyattr}) {
while(($key,$val) = each %$attr) {
if(defined($val) and UNIVERSAL::isa($val, 'ARRAY')) {
$attr->{$key} = $self->array_to_hash($key, $val);
}
}
}
# disintermediate grouped tags
if($self->{opt}->{grouptags}) {
while(my($key, $val) = each(%$attr)) {
next unless(UNIVERSAL::isa($val, 'HASH') and (keys %$val == 1));
next unless(exists($self->{opt}->{grouptags}->{$key}));
my($child_key, $child_val) = %$val;
if($self->{opt}->{grouptags}->{$key} eq $child_key) {
$attr->{$key}= $child_val;
}
}
}
# Fold hashes containing a single anonymous array up into just the array
my $count = scalar keys %$attr;
if($count == 1
and exists $attr->{anon}
and UNIVERSAL::isa($attr->{anon}, 'ARRAY')
) {
return($attr->{anon});
}
# Do the right thing if hash is empty, otherwise just return it
if(!%$attr and exists($self->{opt}->{suppressempty})) {
if(defined($self->{opt}->{suppressempty}) and
$self->{opt}->{suppressempty} eq '') {
return('');
}
return(undef);
}
# Roll up named elements with named nested 'value' attributes
if($self->{opt}->{valueattr}) {
while(my($key, $val) = each(%$attr)) {
next unless($self->{opt}->{valueattr}->{$key});
next unless(UNIVERSAL::isa($val, 'HASH') and (keys %$val == 1));
my($k) = keys %$val;
next unless($k eq $self->{opt}->{valueattr}->{$key});
$attr->{$key} = $val->{$k};
}
}
return($attr)
}
##############################################################################
# Method: set_var()
#
# Called when a variable definition is encountered in the XML. (A variable
# definition looks like value where attrname
# matches the varattr setting).
#
sub set_var {
my($self, $name, $value) = @_;
$self->{_var_values}->{$name} = $value;
}
##############################################################################
# Method: get_var()
#
# Called during variable substitution to get the value for the named variable.
#
sub get_var {
my($self, $name) = @_;
my $value = $self->{_var_values}->{$name};
return $value if(defined($value));
return '${' . $name . '}';
}
##############################################################################
# Method: normalise_space()
#
# Strips leading and trailing whitespace and collapses sequences of whitespace
# characters to a single space.
#
sub normalise_space {
my($self, $text) = @_;
$text =~ s/^\s+//s;
$text =~ s/\s+$//s;
$text =~ s/\s\s+/ /sg;
return $text;
}
##############################################################################
# Method: array_to_hash()
#
# Helper routine for collapse().
# Attempts to 'fold' an array of hashes into an hash of hashes. Returns a
# reference to the hash on success or the original array if folding is
# not possible. Behaviour is controlled by 'keyattr' option.
#
sub array_to_hash {
my $self = shift;
my $name = shift;
my $arrayref = shift;
my $hashref = $self->new_hashref;
my($i, $key, $val, $flag);
# Handle keyattr => { .... }
if(ref($self->{opt}->{keyattr}) eq 'HASH') {
return($arrayref) unless(exists($self->{opt}->{keyattr}->{$name}));
($key, $flag) = @{$self->{opt}->{keyattr}->{$name}};
for($i = 0; $i < @$arrayref; $i++) {
if(UNIVERSAL::isa($arrayref->[$i], 'HASH') and
exists($arrayref->[$i]->{$key})
) {
$val = $arrayref->[$i]->{$key};
if(ref($val)) {
$self->die_or_warn("<$name> element has non-scalar '$key' key attribute");
return($arrayref);
}
$val = $self->normalise_space($val)
if($self->{opt}->{normalisespace} == 1);
$self->die_or_warn("<$name> element has non-unique value in '$key' key attribute: $val")
if(exists($hashref->{$val}));
$hashref->{$val} = $self->new_hashref( %{$arrayref->[$i]} );
$hashref->{$val}->{"-$key"} = $hashref->{$val}->{$key} if($flag eq '-');
delete $hashref->{$val}->{$key} unless($flag eq '+');
}
else {
$self->die_or_warn("<$name> element has no '$key' key attribute");
return($arrayref);
}
}
}
# Or assume keyattr => [ .... ]
else {
my $default_keys =
join(',', @DefKeyAttr) eq join(',', @{$self->{opt}->{keyattr}});
ELEMENT: for($i = 0; $i < @$arrayref; $i++) {
return($arrayref) unless(UNIVERSAL::isa($arrayref->[$i], 'HASH'));
foreach $key (@{$self->{opt}->{keyattr}}) {
if(defined($arrayref->[$i]->{$key})) {
$val = $arrayref->[$i]->{$key};
if(ref($val)) {
$self->die_or_warn("<$name> element has non-scalar '$key' key attribute")
if not $default_keys;
return($arrayref);
}
$val = $self->normalise_space($val)
if($self->{opt}->{normalisespace} == 1);
$self->die_or_warn("<$name> element has non-unique value in '$key' key attribute: $val")
if(exists($hashref->{$val}));
$hashref->{$val} = $self->new_hashref( %{$arrayref->[$i]} );
delete $hashref->{$val}->{$key};
next ELEMENT;
}
}
return($arrayref); # No keyfield matched
}
}
# collapse any hashes which now only have a 'content' key
if($self->{opt}->{collapseagain}) {
$hashref = $self->collapse_content($hashref);
}
return($hashref);
}
##############################################################################
# Method: die_or_warn()
#
# Takes a diagnostic message and does one of three things:
# 1. dies if strict mode is enabled
# 2. warns if warnings are enabled but strict mode is not
# 3. ignores message and returns silently if neither strict mode nor warnings
# are enabled
#
sub die_or_warn {
my $self = shift;
my $msg = shift;
croak $msg if($self->{opt}->{strictmode});
if(warnings::enabled()) {
carp "Warning: $msg";
}
}
##############################################################################
# Method: new_hashref()
#
# This is a hook routine for overriding in a sub-class. Some people believe
# that using Tie::IxHash here will solve order-loss problems.
#
sub new_hashref {
my $self = shift;
return { @_ };
}
##############################################################################
# Method: collapse_content()
#
# Helper routine for array_to_hash
#
# Arguments expected are:
# - an XML::Simple object
# - a hashref
# the hashref is a former array, turned into a hash by array_to_hash because
# of the presence of key attributes
# at this point collapse_content avoids over-complicated structures like
# dir => { libexecdir => { content => '$exec_prefix/libexec' },
# localstatedir => { content => '$prefix' },
# }
# into
# dir => { libexecdir => '$exec_prefix/libexec',
# localstatedir => '$prefix',
# }
sub collapse_content {
my $self = shift;
my $hashref = shift;
my $contentkey = $self->{opt}->{contentkey};
# first go through the values,checking that they are fit to collapse
foreach my $val (values %$hashref) {
return $hashref unless ( (ref($val) eq 'HASH')
and (keys %$val == 1)
and (exists $val->{$contentkey})
);
}
# now collapse them
foreach my $key (keys %$hashref) {
$hashref->{$key}= $hashref->{$key}->{$contentkey};
}
return $hashref;
}
##############################################################################
# Method: value_to_xml()
#
# Helper routine for XMLout() - recurses through a data structure building up
# and returning an XML representation of that structure as a string.
#
# Arguments expected are:
# - the data structure to be encoded (usually a reference)
# - the XML tag name to use for this item
# - a string of spaces for use as the current indent level
#
sub value_to_xml {
my $self = shift;;
# Grab the other arguments
my($ref, $name, $indent) = @_;
my $named = (defined($name) and $name ne '' ? 1 : 0);
my $nl = "\n";
my $is_root = $indent eq '' ? 1 : 0; # Warning, dirty hack!
if($self->{opt}->{noindent}) {
$indent = '';
$nl = '';
}
# Convert to XML
my $refaddr = Scalar::Util::refaddr($ref);
if($refaddr) {
croak "circular data structures not supported"
if $self->{_ancestors}->{$refaddr};
$self->{_ancestors}->{$refaddr} = $ref; # keep ref alive until we delete it
}
else {
if($named) {
return(join('',
$indent, '<', $name, '>',
($self->{opt}->{noescape} ? $ref : $self->escape_value($ref)),
'', $name, ">", $nl
));
}
else {
return("$ref$nl");
}
}
# Unfold hash to array if possible
if(UNIVERSAL::isa($ref, 'HASH') # It is a hash
and keys %$ref # and it's not empty
and $self->{opt}->{keyattr} # and folding is enabled
and !$is_root # and its not the root element
) {
$ref = $self->hash_to_array($name, $ref);
}
my @result = ();
my($key, $value);
# Handle hashrefs
if(UNIVERSAL::isa($ref, 'HASH')) {
# Reintermediate grouped values if applicable
if($self->{opt}->{grouptags}) {
$ref = $self->copy_hash($ref);
while(my($key, $val) = each %$ref) {
if($self->{opt}->{grouptags}->{$key}) {
$ref->{$key} = $self->new_hashref(
$self->{opt}->{grouptags}->{$key} => $val
);
}
}
}
# Scan for namespace declaration attributes
my $nsdecls = '';
my $default_ns_uri;
if($self->{nsup}) {
$ref = $self->copy_hash($ref);
$self->{nsup}->push_context();
# Look for default namespace declaration first
if(exists($ref->{xmlns})) {
$self->{nsup}->declare_prefix('', $ref->{xmlns});
$nsdecls .= qq( xmlns="$ref->{xmlns}");
delete($ref->{xmlns});
}
$default_ns_uri = $self->{nsup}->get_uri('');
# Then check all the other keys
foreach my $qname (keys(%$ref)) {
my($uri, $lname) = $self->{nsup}->parse_jclark_notation($qname);
if($uri) {
if($uri eq $xmlns_ns) {
$self->{nsup}->declare_prefix($lname, $ref->{$qname});
$nsdecls .= qq( xmlns:$lname="$ref->{$qname}");
delete($ref->{$qname});
}
}
}
# Translate any remaining Clarkian names
foreach my $qname (keys(%$ref)) {
my($uri, $lname) = $self->{nsup}->parse_jclark_notation($qname);
if($uri) {
if($default_ns_uri and $uri eq $default_ns_uri) {
$ref->{$lname} = $ref->{$qname};
delete($ref->{$qname});
}
else {
my $prefix = $self->{nsup}->get_prefix($uri);
unless($prefix) {
# $self->{nsup}->declare_prefix(undef, $uri);
# $prefix = $self->{nsup}->get_prefix($uri);
$prefix = $self->{ns_prefix}++;
$self->{nsup}->declare_prefix($prefix, $uri);
$nsdecls .= qq( xmlns:$prefix="$uri");
}
$ref->{"$prefix:$lname"} = $ref->{$qname};
delete($ref->{$qname});
}
}
}
}
my @nested = ();
my $text_content = undef;
if($named) {
push @result, $indent, '<', $name, $nsdecls;
}
if(keys %$ref) {
my $first_arg = 1;
foreach my $key ($self->sorted_keys($name, $ref)) {
my $value = $ref->{$key};
next if(substr($key, 0, 1) eq '-');
if(!defined($value)) {
next if $self->{opt}->{suppressempty};
unless(exists($self->{opt}->{suppressempty})
and !defined($self->{opt}->{suppressempty})
) {
carp 'Use of uninitialized value' if warnings::enabled();
}
if($key eq $self->{opt}->{contentkey}) {
$text_content = '';
}
else {
$value = exists($self->{opt}->{suppressempty}) ? {} : '';
}
}
if(!ref($value)
and $self->{opt}->{valueattr}
and $self->{opt}->{valueattr}->{$key}
) {
$value = $self->new_hashref(
$self->{opt}->{valueattr}->{$key} => $value
);
}
if(ref($value) or $self->{opt}->{noattr}) {
push @nested,
$self->value_to_xml($value, $key, "$indent ");
}
else {
if($key eq $self->{opt}->{contentkey}) {
$value = $self->escape_value($value) unless($self->{opt}->{noescape});
$text_content = $value;
}
else {
$value = $self->escape_attr($value) unless($self->{opt}->{noescape});
push @result, "\n$indent " . ' ' x length($name)
if($self->{opt}->{attrindent} and !$first_arg);
push @result, ' ', $key, '="', $value , '"';
$first_arg = 0;
}
}
}
}
else {
$text_content = '';
}
if(@nested or defined($text_content)) {
if($named) {
push @result, ">";
if(defined($text_content)) {
push @result, $text_content;
$nested[0] =~ s/^\s+// if(@nested);
}
else {
push @result, $nl;
}
if(@nested) {
push @result, @nested, $indent;
}
push @result, '', $name, ">", $nl;
}
else {
push @result, @nested; # Special case if no root elements
}
}
else {
push @result, " />", $nl;
}
$self->{nsup}->pop_context() if($self->{nsup});
}
# Handle arrayrefs
elsif(UNIVERSAL::isa($ref, 'ARRAY')) {
foreach $value (@$ref) {
next if !defined($value) and $self->{opt}->{suppressempty};
if(!ref($value)) {
push @result,
$indent, '<', $name, '>',
($self->{opt}->{noescape} ? $value : $self->escape_value($value)),
'', $name, ">$nl";
}
elsif(UNIVERSAL::isa($value, 'HASH')) {
push @result, $self->value_to_xml($value, $name, $indent);
}
else {
push @result,
$indent, '<', $name, ">$nl",
$self->value_to_xml($value, 'anon', "$indent "),
$indent, '', $name, ">$nl";
}
}
}
else {
croak "Can't encode a value of type: " . ref($ref);
}
delete $self->{_ancestors}->{$refaddr};
return(join('', @result));
}
##############################################################################
# Method: sorted_keys()
#
# Returns the keys of the referenced hash sorted into alphabetical order, but
# with the 'key' key (as in KeyAttr) first, if there is one.
#
sub sorted_keys {
my($self, $name, $ref) = @_;
return keys %$ref if $self->{opt}->{nosort};
my %hash = %$ref;
my $keyattr = $self->{opt}->{keyattr};
my @key;
if(ref $keyattr eq 'HASH') {
if(exists $keyattr->{$name} and exists $hash{$keyattr->{$name}->[0]}) {
push @key, $keyattr->{$name}->[0];
delete $hash{$keyattr->{$name}->[0]};
}
}
elsif(ref $keyattr eq 'ARRAY') {
foreach (@{$keyattr}) {
if(exists $hash{$_}) {
push @key, $_;
delete $hash{$_};
last;
}
}
}
return(@key, sort keys %hash);
}
##############################################################################
# Method: escape_value()
#
# Helper routine for automatically escaping values for XMLout().
# Expects a scalar data value. Returns escaped version.
#
sub escape_value {
my($self, $data) = @_;
return '' unless(defined($data));
$data =~ s/&/&/sg;
$data =~ s/</sg;
$data =~ s/>/>/sg;
$data =~ s/"/"/sg;
my $level = $self->{opt}->{numericescape} or return $data;
return $self->numeric_escape($data, $level);
}
sub numeric_escape {
my($self, $data, $level) = @_;
if($self->{opt}->{numericescape} eq '2') {
$data =~ s/([^\x00-\x7F])/'' . ord($1) . ';'/gse;
}
else {
$data =~ s/([^\x00-\xFF])/'' . ord($1) . ';'/gse;
}
return $data;
}
##############################################################################
# Method: escape_attr()
#
# Helper routine for escaping attribute values. Defaults to escape_value(),
# but may be overridden by a subclass to customise behaviour.
#
sub escape_attr {
my $self = shift;
return $self->escape_value(@_);
}
##############################################################################
# Method: hash_to_array()
#
# Helper routine for value_to_xml().
# Attempts to 'unfold' a hash of hashes into an array of hashes. Returns a
# reference to the array on success or the original hash if unfolding is
# not possible.
#
sub hash_to_array {
my $self = shift;
my $parent = shift;
my $hashref = shift;
my $arrayref = [];
my($key, $value);
my @keys = $self->{opt}->{nosort} ? keys %$hashref : sort keys %$hashref;
foreach $key (@keys) {
$value = $hashref->{$key};
return($hashref) unless(UNIVERSAL::isa($value, 'HASH'));
if(ref($self->{opt}->{keyattr}) eq 'HASH') {
return($hashref) unless(defined($self->{opt}->{keyattr}->{$parent}));
push @$arrayref, $self->copy_hash(
$value, $self->{opt}->{keyattr}->{$parent}->[0] => $key
);
}
else {
push(@$arrayref, { $self->{opt}->{keyattr}->[0] => $key, %$value });
}
}
return($arrayref);
}
##############################################################################
# Method: copy_hash()
#
# Helper routine for hash_to_array(). When unfolding a hash of hashes into
# an array of hashes, we need to copy the key from the outer hash into the
# inner hash. This routine makes a copy of the original hash so we don't
# destroy the original data structure. You might wish to override this
# method if you're using tied hashes and don't want them to get untied.
#
sub copy_hash {
my($self, $orig, @extra) = @_;
return { @extra, %$orig };
}
##############################################################################
# Methods required for building trees from SAX events
##############################################################################
sub start_document {
my $self = shift;
$self->handle_options('in') unless($self->{opt});
$self->{lists} = [];
$self->{curlist} = $self->{tree} = [];
}
sub start_element {
my $self = shift;
my $element = shift;
my $name = $element->{Name};
if($self->{opt}->{nsexpand}) {
$name = $element->{LocalName} || '';
if($element->{NamespaceURI}) {
$name = '{' . $element->{NamespaceURI} . '}' . $name;
}
}
my $attributes = {};
if($element->{Attributes}) { # Might be undef
foreach my $attr (values %{$element->{Attributes}}) {
if($self->{opt}->{nsexpand}) {
my $name = $attr->{LocalName} || '';
if($attr->{NamespaceURI}) {
$name = '{' . $attr->{NamespaceURI} . '}' . $name
}
$name = 'xmlns' if($name eq $bad_def_ns_jcn);
$attributes->{$name} = $attr->{Value};
}
else {
$attributes->{$attr->{Name}} = $attr->{Value};
}
}
}
my $newlist = [ $attributes ];
push @{ $self->{lists} }, $self->{curlist};
push @{ $self->{curlist} }, $name => $newlist;
$self->{curlist} = $newlist;
}
sub characters {
my $self = shift;
my $chars = shift;
my $text = $chars->{Data};
my $clist = $self->{curlist};
my $pos = $#$clist;
if ($pos > 0 and $clist->[$pos - 1] eq '0') {
$clist->[$pos] .= $text;
}
else {
push @$clist, 0 => $text;
}
}
sub end_element {
my $self = shift;
$self->{curlist} = pop @{ $self->{lists} };
}
sub end_document {
my $self = shift;
delete($self->{curlist});
delete($self->{lists});
my $tree = $self->{tree};
delete($self->{tree});
# Return tree as-is to XMLin()
return($tree) if($self->{nocollapse});
# Or collapse it before returning it to SAX parser class
if($self->{opt}->{keeproot}) {
$tree = $self->collapse({}, @$tree);
}
else {
$tree = $self->collapse(@{$tree->[1]});
}
if($self->{opt}->{datahandler}) {
return($self->{opt}->{datahandler}->($self, $tree));
}
return($tree);
}
*xml_in = \&XMLin;
*xml_out = \&XMLout;
1;
__END__
=head1 STATUS OF THIS MODULE
The use of this module in new code is discouraged. Other modules are available
which provide more straightforward and consistent interfaces. In particular,
L is highly recommended and L is an excellent
alternative.
The major problems with this module are the large number of options (some of
which have unfortunate defaults) and the arbitrary ways in which these options
interact - often producing unexpected results.
Patches with bug fixes and documentation fixes are welcome, but new features
are unlikely to be added.
=head1 QUICK START
Say you have a script called B and a file of configuration options
called B containing the following:
10.0.0.101
10.0.1.101
10.0.0.102
10.0.0.103
10.0.1.103
The following lines of code in B:
use XML::Simple qw(:strict);
my $config = XMLin(undef, KeyAttr => { server => 'name' }, ForceArray => [ 'server', 'address' ]);
will 'slurp' the configuration options into the hashref $config (because no
filename or XML string was passed as the first argument to C the name
and location of the XML file will be inferred from name and location of the
script). You can dump out the contents of the hashref using Data::Dumper:
use Data::Dumper;
print Dumper($config);
which will produce something like this (formatting has been adjusted for
brevity):
{
'logdir' => '/var/log/foo/',
'debugfile' => '/tmp/foo.debug',
'server' => {
'sahara' => {
'osversion' => '2.6',
'osname' => 'solaris',
'address' => [ '10.0.0.101', '10.0.1.101' ]
},
'gobi' => {
'osversion' => '6.5',
'osname' => 'irix',
'address' => [ '10.0.0.102' ]
},
'kalahari' => {
'osversion' => '2.0.34',
'osname' => 'linux',
'address' => [ '10.0.0.103', '10.0.1.103' ]
}
}
}
Your script could then access the name of the log directory like this:
print $config->{logdir};
similarly, the second address on the server 'kalahari' could be referenced as:
print $config->{server}->{kalahari}->{address}->[1];
Note: If the mapping between the output of Data::Dumper and the print
statements above is not obvious to you, then please refer to the 'references'
tutorial (AKA: "Mark's very short tutorial about references") at L.
In this example, the C<< ForceArray >> option was used to list elements that
might occur multiple times and should therefore be represented as arrayrefs
(even when only one element is present).
The C<< KeyAttr >> option was used to indicate that each C<< >>
element has a unique identifier in the C<< name >> attribute. This allows you
to index directly to a particular server record using the name as a hash key
(as shown above).
For simple requirements, that's really all there is to it. If you want to
store your XML in a different directory or file, or pass it in as a string or
even pass it in via some derivative of an IO::Handle, you'll need to check out
L<"OPTIONS">. If you want to turn off or tweak the array folding feature (that
neat little transformation that produced $config->{server}) you'll find options
for that as well.
If you want to generate XML (for example to write a modified version of
$config back out as XML), check out C.
If your needs are not so simple, this may not be the module for you. In that
case, you might want to read L<"WHERE TO FROM HERE?">.
=head1 DESCRIPTION
The XML::Simple module provides a simple API layer on top of an underlying XML
parsing module (either XML::Parser or one of the SAX2 parser modules). Two
functions are exported: C and C. Note: you can explicitly
request the lower case versions of the function names: C and
C.
The simplest approach is to call these two functions directly, but an
optional object oriented interface (see L<"OPTIONAL OO INTERFACE"> below)
allows them to be called as methods of an B object. The object
interface can also be used at either end of a SAX pipeline.
=head2 XMLin()
Parses XML formatted data and returns a reference to a data structure which
contains the same information in a more readily accessible form. (Skip
down to L<"EXAMPLES"> below, for more sample code).
C accepts an optional XML specifier followed by zero or more 'name =>
value' option pairs. The XML specifier can be one of the following:
=over 4
=item A filename
If the filename contains no directory components C will look for the
file in each directory in the SearchPath (see L<"OPTIONS"> below) or in the
current directory if the SearchPath option is not defined. eg:
$ref = XMLin('/etc/params.xml');
Note, the filename '-' can be used to parse from STDIN.
=item undef
If there is no XML specifier, C will check the script directory and
each of the SearchPath directories for a file with the same name as the script
but with the extension '.xml'. Note: if you wish to specify options, you
must specify the value 'undef'. eg:
$ref = XMLin(undef, ForceArray => 1);
=item A string of XML
A string containing XML (recognised by the presence of '<' and '>' characters)
will be parsed directly. eg:
$ref = XMLin('');
=item An IO::Handle object
An IO::Handle object will be read to EOF and its contents parsed. eg:
$fh = IO::File->new('/etc/params.xml');
$ref = XMLin($fh);
=back
=head2 XMLout()
Takes a data structure (generally a hashref) and returns an XML encoding of
that structure. If the resulting XML is parsed using C, it should
return a data structure equivalent to the original (see caveats below).
The C function can also be used to output the XML as SAX events
see the C option and L<"SAX SUPPORT"> for more details).
When translating hashes to XML, hash keys which have a leading '-' will be
silently skipped. This is the approved method for marking elements of a
data structure which should be ignored by C. (Note: If these items
were not skipped the key names would be emitted as element or attribute names
with a leading '-' which would not be valid XML).
=head2 Caveats
Some care is required in creating data structures which will be passed to
C. Hash keys from the data structure will be encoded as either XML
element names or attribute names. Therefore, you should use hash key names
which conform to the relatively strict XML naming rules:
Names in XML must begin with a letter. The remaining characters may be
letters, digits, hyphens (-), underscores (_) or full stops (.). It is also
allowable to include one colon (:) in an element name but this should only be
used when working with namespaces (B can only usefully work with
namespaces when teamed with a SAX Parser).
You can use other punctuation characters in hash values (just not in hash
keys) however B does not support dumping binary data.
If you break these rules, the current implementation of C will
simply emit non-compliant XML which will be rejected if you try to read it
back in. (A later version of B might take a more proactive
approach).
Note also that although you can nest hashes and arrays to arbitrary levels,
circular data structures are not supported and will cause C to die.
If you wish to 'round-trip' arbitrary data structures from Perl to XML and back
to Perl, then you should probably disable array folding (using the KeyAttr
option) both with C and with C. If you still don't get the
expected results, you may prefer to use L which is designed for
exactly that purpose.
Refer to L<"WHERE TO FROM HERE?"> if C is too simple for your needs.
=head1 OPTIONS
B supports a number of options (in fact as each release of
B adds more options, the module's claim to the name 'Simple'
becomes increasingly tenuous). If you find yourself repeatedly having to
specify the same options, you might like to investigate L<"OPTIONAL OO
INTERFACE"> below.
If you can't be bothered reading the documentation, refer to
L<"STRICT MODE"> to automatically catch common mistakes.
Because there are so many options, it's hard for new users to know which ones
are important, so here are the two you really need to know about:
=over 4
=item *
check out C because you'll almost certainly want to turn it on
=item *
make sure you know what the C option does and what its default value is
because it may surprise you otherwise (note in particular that 'KeyAttr'
affects both C and C)
=back
The option name headings below have a trailing 'comment' - a hash followed by
two pieces of metadata:
=over 4
=item *
Options are marked with 'I' if they are recognised by C and
'I' if they are recognised by C.
=item *
Each option is also flagged to indicate whether it is:
'important' - don't use the module until you understand this one
'handy' - you can skip this on the first time through
'advanced' - you can skip this on the second time through
'SAX only' - don't worry about this unless you're using SAX (or
alternatively if you need this, you also need SAX)
'seldom used' - you'll probably never use this unless you were the
person that requested the feature
=back
The options are listed alphabetically:
Note: option names are no longer case sensitive so you can use the mixed case
versions shown here; all lower case as required by versions 2.03 and earlier;
or you can add underscores between the words (eg: key_attr).
=head2 AttrIndent => 1 I<# out - handy>
When you are using C, enable this option to have attributes printed
one-per-line with sensible indentation rather than all on one line.
=head2 Cache => [ cache schemes ] I<# in - advanced>
Because loading the B module and parsing an XML file can consume a
significant number of CPU cycles, it is often desirable to cache the output of
C for later reuse.
When parsing from a named file, B supports a number of caching
schemes. The 'Cache' option may be used to specify one or more schemes (using
an anonymous array). Each scheme will be tried in turn in the hope of finding
a cached pre-parsed representation of the XML file. If no cached copy is
found, the file will be parsed and the first cache scheme in the list will be
used to save a copy of the results. The following cache schemes have been
implemented:
=over 4
=item storable
Utilises B to read/write a cache file with the same name as the
XML file but with the extension .stor
=item memshare
When a file is first parsed, a copy of the resulting data structure is retained
in memory in the B module's namespace. Subsequent calls to parse
the same file will return a reference to this structure. This cached version
will persist only for the life of the Perl interpreter (which in the case of
mod_perl for example, may be some significant time).
Because each caller receives a reference to the same data structure, a change
made by one caller will be visible to all. For this reason, the reference
returned should be treated as read-only.
=item memcopy
This scheme works identically to 'memshare' (above) except that each caller
receives a reference to a new data structure which is a copy of the cached
version. Copying the data structure will add a little processing overhead,
therefore this scheme should only be used where the caller intends to modify
the data structure (or wishes to protect itself from others who might). This
scheme uses B to perform the copy.
=back
Warning! The memory-based caching schemes compare the timestamp on the file to
the time when it was last parsed. If the file is stored on an NFS filesystem
(or other network share) and the clock on the file server is not exactly
synchronised with the clock where your script is run, updates to the source XML
file may appear to be ignored.
=head2 ContentKey => 'keyname' I<# in+out - seldom used>
When text content is parsed to a hash value, this option lets you specify a
name for the hash key to override the default 'content'. So for example:
XMLin('Text', ContentKey => 'text')
will parse to:
{ 'one' => 1, 'text' => 'Text' }
instead of:
{ 'one' => 1, 'content' => 'Text' }
C will also honour the value of this option when converting a hashref
to XML.
You can also prefix your selected key name with a '-' character to have
C try a little harder to eliminate unnecessary 'content' keys after
array folding. For example:
XMLin(
'FirstSecond',
KeyAttr => {item => 'name'},
ForceArray => [ 'item' ],
ContentKey => '-content'
)
will parse to:
{
'item' => {
'one' => 'First'
'two' => 'Second'
}
}
rather than this (without the '-'):
{
'item' => {
'one' => { 'content' => 'First' }
'two' => { 'content' => 'Second' }
}
}
=head2 DataHandler => code_ref I<# in - SAX only>
When you use an B object as a SAX handler, it will return a
'simple tree' data structure in the same format as C would return. If
this option is set (to a subroutine reference), then when the tree is built the
subroutine will be called and passed two arguments: a reference to the
B object and a reference to the data tree. The return value from
the subroutine will be returned to the SAX driver. (See L<"SAX SUPPORT"> for
more details).
=head2 ForceArray => 1 I<# in - important>
This option should be set to '1' to force nested elements to be represented
as arrays even when there is only one. Eg, with ForceArray enabled, this
XML:
value
would parse to this:
{
'name' => [
'value'
]
}
instead of this (the default):
{
'name' => 'value'
}
This option is especially useful if the data structure is likely to be written
back out as XML and the default behaviour of rolling single nested elements up
into attributes is not desirable.
If you are using the array folding feature, you should almost certainly enable
this option. If you do not, single nested elements will not be parsed to
arrays and therefore will not be candidates for folding to a hash. (Given that
the default value of 'KeyAttr' enables array folding, the default value of this
option should probably also have been enabled too - sorry).
=head2 ForceArray => [ names ] I<# in - important>
This alternative (and preferred) form of the 'ForceArray' option allows you to
specify a list of element names which should always be forced into an array
representation, rather than the 'all or nothing' approach above.
It is also possible (since version 2.05) to include compiled regular
expressions in the list - any element names which match the pattern will be
forced to arrays. If the list contains only a single regex, then it is not
necessary to enclose it in an arrayref. Eg:
ForceArray => qr/_list$/
=head2 ForceContent => 1 I<# in - seldom used>
When C parses elements which have text content as well as attributes,
the text content must be represented as a hash value rather than a simple
scalar. This option allows you to force text content to always parse to
a hash value even when there are no attributes. So for example:
XMLin('text1text2', ForceContent => 1)
will parse to:
{
'x' => { 'content' => 'text1' },
'y' => { 'a' => 2, 'content' => 'text2' }
}
instead of:
{
'x' => 'text1',
'y' => { 'a' => 2, 'content' => 'text2' }
}
=head2 GroupTags => { grouping tag => grouped tag } I<# in+out - handy>
You can use this option to eliminate extra levels of indirection in your Perl
data structure. For example this XML:
/usr/bin/usr/local/bin/usr/X11/bin
Would normally be read into a structure like this:
{
searchpath => {
dir => [ '/usr/bin', '/usr/local/bin', '/usr/X11/bin' ]
}
}
But when read in with the appropriate value for 'GroupTags':
my $opt = XMLin($xml, GroupTags => { searchpath => 'dir' });
It will return this simpler structure:
{
searchpath => [ '/usr/bin', '/usr/local/bin', '/usr/X11/bin' ]
}
The grouping element (C<< >> in the example) must not contain any
attributes or elements other than the grouped element.
You can specify multiple 'grouping element' to 'grouped element' mappings in
the same hashref. If this option is combined with C, the array
folding will occur first and then the grouped element names will be eliminated.
C will also use the grouptag mappings to re-introduce the tags around
the grouped elements. Beware though that this will occur in all places that
the 'grouping tag' name occurs - you probably don't want to use the same name
for elements as well as attributes.
=head2 Handler => object_ref I<# out - SAX only>
Use the 'Handler' option to have C generate SAX events rather than
returning a string of XML. For more details see L<"SAX SUPPORT"> below.
Note: the current implementation of this option generates a string of XML
and uses a SAX parser to translate it into SAX events. The normal encoding
rules apply here - your data must be UTF8 encoded unless you specify an
alternative encoding via the 'XMLDecl' option; and by the time the data reaches
the handler object, it will be in UTF8 form regardless of the encoding you
supply. A future implementation of this option may generate the events
directly.
=head2 KeepRoot => 1 I<# in+out - handy>
In its attempt to return a data structure free of superfluous detail and
unnecessary levels of indirection, C normally discards the root
element name. Setting the 'KeepRoot' option to '1' will cause the root element
name to be retained. So after executing this code:
$config = XMLin('', KeepRoot => 1)
You'll be able to reference the tempdir as
C<$config-E{config}-E{tempdir}> instead of the default
C<$config-E{tempdir}>.
Similarly, setting the 'KeepRoot' option to '1' will tell C that the
data structure already contains a root element name and it is not necessary to
add another.
=head2 KeyAttr => [ list ] I<# in+out - important>
This option controls the 'array folding' feature which translates nested
elements from an array to a hash. It also controls the 'unfolding' of hashes
to arrays.
For example, this XML:
would, by default, parse to this:
{
'user' => [
{
'login' => 'grep',
'fullname' => 'Gary R Epstein'
},
{
'login' => 'stty',
'fullname' => 'Simon T Tyson'
}
]
}
If the option 'KeyAttr => "login"' were used to specify that the 'login'
attribute is a key, the same XML would parse to:
{
'user' => {
'stty' => {
'fullname' => 'Simon T Tyson'
},
'grep' => {
'fullname' => 'Gary R Epstein'
}
}
}
The key attribute names should be supplied in an arrayref if there is more
than one. C will attempt to match attribute names in the order
supplied. C will use the first attribute name supplied when
'unfolding' a hash into an array.
Note 1: The default value for 'KeyAttr' is ['name', 'key', 'id']. If you do
not want folding on input or unfolding on output you must set this option
to an empty list to disable the feature.
Note 2: If you wish to use this option, you should also enable the
C option. Without 'ForceArray', a single nested element will be
rolled up into a scalar rather than an array and therefore will not be folded
(since only arrays get folded).
=head2 KeyAttr => { list } I<# in+out - important>
This alternative (and preferred) method of specifying the key attributes
allows more fine grained control over which elements are folded and on which
attributes. For example the option 'KeyAttr => { package => 'id' } will cause
any package elements to be folded on the 'id' attribute. No other elements
which have an 'id' attribute will be folded at all.
Note: C will generate a warning (or a fatal error in L<"STRICT MODE">)
if this syntax is used and an element which does not have the specified key
attribute is encountered (eg: a 'package' element without an 'id' attribute, to
use the example above). Warnings can be suppressed with the lexical
C pragma or C.
Two further variations are made possible by prefixing a '+' or a '-' character
to the attribute name:
The option 'KeyAttr => { user => "+login" }' will cause this XML:
to parse to this data structure:
{
'user' => {
'stty' => {
'fullname' => 'Simon T Tyson',
'login' => 'stty'
},
'grep' => {
'fullname' => 'Gary R Epstein',
'login' => 'grep'
}
}
}
The '+' indicates that the value of the key attribute should be copied rather
than moved to the folded hash key.
A '-' prefix would produce this result:
{
'user' => {
'stty' => {
'fullname' => 'Simon T Tyson',
'-login' => 'stty'
},
'grep' => {
'fullname' => 'Gary R Epstein',
'-login' => 'grep'
}
}
}
As described earlier, C will ignore hash keys starting with a '-'.
=head2 NoAttr => 1 I<# in+out - handy>
When used with C, the generated XML will contain no attributes.
All hash key/values will be represented as nested elements instead.
When used with C, any attributes in the XML will be ignored.
=head2 NoEscape => 1 I<# out - seldom used>
By default, C will translate the characters 'E', 'E', '&' and
'"' to '<', '>', '&' and '"' respectively. Use this option to
suppress escaping (presumably because you've already escaped the data in some
more sophisticated manner).
=head2 NoIndent => 1 I<# out - seldom used>
Set this option to 1 to disable C's default 'pretty printing' mode.
With this option enabled, the XML output will all be on one line (unless there
are newlines in the data) - this may be easier for downstream processing.
=head2 NoSort => 1 I<# out - seldom used>
Newer versions of XML::Simple sort elements and attributes alphabetically (*),
by default. Enable this option to suppress the sorting - possibly for
backwards compatibility.
* Actually, sorting is alphabetical but 'key' attribute or element names (as in
'KeyAttr') sort first. Also, when a hash of hashes is 'unfolded', the elements
are sorted alphabetically by the value of the key field.
=head2 NormaliseSpace => 0 | 1 | 2 I<# in - handy>
This option controls how whitespace in text content is handled. Recognised
values for the option are:
=over 4
=item *
0 = (default) whitespace is passed through unaltered (except of course for the
normalisation of whitespace in attribute values which is mandated by the XML
recommendation)
=item *
1 = whitespace is normalised in any value used as a hash key (normalising means
removing leading and trailing whitespace and collapsing sequences of whitespace
characters to a single space)
=item *
2 = whitespace is normalised in all text content
=back
Note: you can spell this option with a 'z' if that is more natural for you.
=head2 NSExpand => 1 I<# in+out handy - SAX only>
This option controls namespace expansion - the translation of element and
attribute names of the form 'prefix:name' to '{uri}name'. For example the
element name 'xsl:template' might be expanded to:
'{http://www.w3.org/1999/XSL/Transform}template'.
By default, C will return element names and attribute names exactly as
they appear in the XML. Setting this option to 1 will cause all element and
attribute names to be expanded to include their namespace prefix.
I.
This option also controls whether C performs the reverse translation
from '{uri}name' back to 'prefix:name'. The default is no translation. If
your data contains expanded names, you should set this option to 1 otherwise
C will emit XML which is not well formed.
I to translate URIs back to prefixes>.
=head2 NumericEscape => 0 | 1 | 2 I<# out - handy>
Use this option to have 'high' (non-ASCII) characters in your Perl data
structure converted to numeric entities (eg: €) in the XML output. Three
levels are possible:
0 - default: no numeric escaping (OK if you're writing out UTF8)
1 - only characters above 0xFF are escaped (ie: characters in the 0x80-FF range are not escaped), possibly useful with ISO8859-1 output
2 - all characters above 0x7F are escaped (good for plain ASCII output)
=head2 OutputFile => I<# out - handy>
The default behaviour of C is to return the XML as a string. If you
wish to write the XML to a file, simply supply the filename using the
'OutputFile' option.
This option also accepts an IO handle object - especially useful in Perl 5.8.0
and later for output using an encoding other than UTF-8, eg:
open my $fh, '>:encoding(iso-8859-1)', $path or die "open($path): $!";
XMLout($ref, OutputFile => $fh);
Note, XML::Simple does not require that the object you pass in to the
OutputFile option inherits from L - it simply assumes the object
supports a C method.
=head2 ParserOpts => [ XML::Parser Options ] I<# in - don't use this>
I.
This option allows you to pass parameters to the constructor of the underlying
XML::Parser object (which of course assumes you're not using SAX).
=head2 RootName => 'string' I<# out - handy>
By default, when C generates XML, the root element will be named
'opt'. This option allows you to specify an alternative name.
Specifying either undef or the empty string for the RootName option will
produce XML with no root elements. In most cases the resulting XML fragment
will not be 'well formed' and therefore could not be read back in by C.
Nevertheless, the option has been found to be useful in certain circumstances.
=head2 SearchPath => [ list ] I<# in - handy>
If you pass C a filename, but the filename include no directory
component, you can use this option to specify which directories should be
searched to locate the file. You might use this option to search first in the
user's home directory, then in a global directory such as /etc.
If a filename is provided to C but SearchPath is not defined, the
file is assumed to be in the current directory.
If the first parameter to C is undefined, the default SearchPath
will contain only the directory in which the script itself is located.
Otherwise the default SearchPath will be empty.
=head2 StrictMode => 1 | 0 I<# in+out seldom used>
This option allows you to turn L on or off for a particular call,
regardless of whether it was enabled at the time XML::Simple was loaded.
=head2 SuppressEmpty => 1 | '' | undef I<# in+out - handy>
This option controls what C should do with empty elements (no
attributes and no content). The default behaviour is to represent them as
empty hashes. Setting this option to a true value (eg: 1) will cause empty
elements to be skipped altogether. Setting the option to 'undef' or the empty
string will cause empty elements to be represented as the undefined value or
the empty string respectively. The latter two alternatives are a little
easier to test for in your code than a hash with no keys.
The option also controls what C does with undefined values. Setting
the option to undef causes undefined values to be output as empty elements
(rather than empty attributes), it also suppresses the generation of warnings
about undefined values. Setting the option to a true value (eg: 1) causes
undefined values to be skipped altogether on output.
=head2 ValueAttr => [ names ] I<# in - handy>
Use this option to deal elements which always have a single attribute and no
content. Eg:
Setting C<< ValueAttr => [ 'value' ] >> will cause the above XML to parse to:
{
colour => 'red',
size => 'XXL'
}
instead of this (the default):
{
colour => { value => 'red' },
size => { value => 'XXL' }
}
Note: This form of the ValueAttr option is not compatible with C -
since the attribute name is discarded at parse time, the original XML cannot be
reconstructed.
=head2 ValueAttr => { element => attribute, ... } I<# in+out - handy>
This (preferred) form of the ValueAttr option requires you to specify both
the element and the attribute names. This is not only safer, it also allows
the original XML to be reconstructed by C.
Note: You probably don't want to use this option and the NoAttr option at the
same time.
=head2 Variables => { name => value } I<# in - handy>
This option allows variables in the XML to be expanded when the file is read.
(there is no facility for putting the variable names back if you regenerate
XML using C).
A 'variable' is any text of the form C<${name}> which occurs in an attribute
value or in the text content of an element. If 'name' matches a key in the
supplied hashref, C<${name}> will be replaced with the corresponding value from
the hashref. If no matching key is found, the variable will not be replaced.
Names must match the regex: C<[\w.]+> (ie: only 'word' characters and dots are
allowed).
=head2 VarAttr => 'attr_name' I<# in - handy>
In addition to the variables defined using C, this option allows
variables to be defined in the XML. A variable definition consists of an
element with an attribute called 'attr_name' (the value of the C
option). The value of the attribute will be used as the variable name and the
text content of the element will be used as the value. A variable defined in
this way will override a variable defined using the C option. For
example:
XMLin( '/usr/local/apache${prefix}${exec_prefix}/bin',
VarAttr => 'name', ContentKey => '-content'
);
produces the following data structure:
{
dir => {
prefix => '/usr/local/apache',
exec_prefix => '/usr/local/apache',
bindir => '/usr/local/apache/bin',
}
}
=head2 XMLDecl => 1 or XMLDecl => 'string' I<# out - handy>
If you want the output from C to start with the optional XML
declaration, simply set the option to '1'. The default XML declaration is:
If you want some other string (for example to declare an encoding value), set
the value of this option to the complete string you require.
=head1 OPTIONAL OO INTERFACE
The procedural interface is both simple and convenient however there are a
couple of reasons why you might prefer to use the object oriented (OO)
interface:
=over 4
=item *
to define a set of default values which should be used on all subsequent calls
to C or C
=item *
to override methods in B to provide customised behaviour
=back
The default values for the options described above are unlikely to suit
everyone. The OO interface allows you to effectively override B's
defaults with your preferred values. It works like this:
First create an XML::Simple parser object with your preferred defaults:
my $xs = XML::Simple->new(ForceArray => 1, KeepRoot => 1);
then call C or C as a method of that object:
my $ref = $xs->XMLin($xml);
my $xml = $xs->XMLout($ref);
You can also specify options when you make the method calls and these values
will be merged with the values specified when the object was created. Values
specified in a method call take precedence.
Note: when called as methods, the C and C routines may be
called as C or C. The method names are aliased so the
only difference is the aesthetics.
=head2 Parsing Methods
You can explicitly call one of the following methods rather than rely on the
C method automatically determining whether the target to be parsed is
a string, a file or a filehandle:
=over 4
=item parse_string(text)
Works exactly like the C method but assumes the first argument is
a string of XML (or a reference to a scalar containing a string of XML).
=item parse_file(filename)
Works exactly like the C method but assumes the first argument is
the name of a file containing XML.
=item parse_fh(file_handle)
Works exactly like the C method but assumes the first argument is
a filehandle which can be read to get XML.
=back
=head2 Hook Methods
You can make your own class which inherits from XML::Simple and overrides
certain behaviours. The following methods may provide useful 'hooks' upon
which to hang your modified behaviour. You may find other undocumented methods
by examining the source, but those may be subject to change in future releases.
=over 4
=item handle_options(direction, name => value ...)
This method will be called when one of the parsing methods or the C
method is called. The initial argument will be a string (either 'in' or 'out')
and the remaining arguments will be name value pairs.
=item default_config_file()
Calculates and returns the name of the file which should be parsed if no
filename is passed to C (default: C<$0.xml>).
=item build_simple_tree(filename, string)
Called from C or any of the parsing methods. Takes either a file name
as the first argument or C followed by a 'string' as the second
argument. Returns a simple tree data structure. You could override this
method to apply your own transformations before the data structure is returned
to the caller.
=item new_hashref()
When the 'simple tree' data structure is being built, this method will be
called to create any required anonymous hashrefs.
=item sorted_keys(name, hashref)
Called when C is translating a hashref to XML. This routine returns
a list of hash keys in the order that the corresponding attributes/elements
should appear in the output.
=item escape_value(string)
Called from C, takes a string and returns a copy of the string with
XML character escaping rules applied.
=item escape_attr(string)
Called from C, to handle attribute values. By default, just calls
C, but you can override this method if you want attributes
escaped differently than text content.
=item numeric_escape(string)
Called from C, to handle non-ASCII characters (depending on the
value of the NumericEscape option).
=item copy_hash(hashref, extra_key => value, ...)
Called from C, when 'unfolding' a hash of hashes into an array of
hashes. You might wish to override this method if you're using tied hashes and
don't want them to get untied.
=back
=head2 Cache Methods
XML::Simple implements three caching schemes ('storable', 'memshare' and
'memcopy'). You can implement a custom caching scheme by implementing
two methods - one for reading from the cache and one for writing to it.
For example, you might implement a new 'dbm' scheme that stores cached data
structures using the L module. First, you would add a
C method which accepted a filename for use as a lookup key
and returned a data structure on success, or undef on failure. Then, you would
implement a C method which accepted a data structure and a
filename.
You would use this caching scheme by specifying the option:
Cache => [ 'dbm' ]
=head1 STRICT MODE
If you import the B routines like this:
use XML::Simple qw(:strict);
the following common mistakes will be detected and treated as fatal errors
=over 4
=item *
Failing to explicitly set the C option - if you can't be bothered
reading about this option, turn it off with: KeyAttr => [ ]
=item *
Failing to explicitly set the C option - if you can't be bothered
reading about this option, set it to the safest mode with: ForceArray => 1
=item *
Setting ForceArray to an array, but failing to list all the elements from the
KeyAttr hash.
=item *
Data error - KeyAttr is set to say { part => 'partnum' } but the XML contains
one or more EpartE elements without a 'partnum' attribute (or nested
element). Note: if strict mode is not set but C