/usr/share/perl5/XML/Struct/Simple.pm is in libxml-struct-perl 0.23-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | package XML::Struct::Simple;
# ABSTRACT: Transform MicroXML data structures into simple (unordered) form
our $VERSION = '0.23'; # VERSION
use strict;
use Moo;
use List::Util qw(first);
has root => (is => 'rw', default => sub { 0 });
has attributes => (is => 'rw', default => sub { 1 });
has content => (is => 'rw', default => sub { 'content' });
has depth => (is => 'rw');
sub transform {
my ($self, $element) = @_;
my $attributes = (!defined $self->attributes or $self->attributes);
if ($attributes eq 'remove') {
$element = removeXMLAttr($element);
$attributes = 0;
}
if (defined $self->depth and $self->depth !~ /^\+?\d+/) {
$self->depth(undef);
}
if ($self->root) {
my $root = $self->root;
$root = $element->[0] if $root =~ /^[+-]?[0-9]+$/;
$self->depth($self->depth - 1) if defined $self->depth;
my $hash = $attributes
? $self->_simple(1, [ dummy => {}, [$element] ], $self->depth)
: $self->_simple(0, [ dummy => [$element] ], $self->depth);
return { $root => values %$hash };
}
my $hash = $self->_simple($attributes, $element, $self->depth);
$hash = { } unless ref $hash;
return $hash;
}
sub removeXMLAttr {
my $node = shift;
ref $node
? ( $node->[2]
? [ $node->[0], [ map { removeXMLAttr($_) } @{$node->[2]} ] ]
: [ $node->[0] ] ) # empty element
: $node; # text node
}
# hashifies attributes and child elements
sub _simple {
my $self = shift;
my $with_attributes = $_[0];
my ($children, $attributes) = $with_attributes ? ($_[1]->[2], $_[1]->[1]) : ($_[1]->[1]);
my $depth = defined $_[2] ? $_[2] - 1 : undef;
my $text_only = (first { ref $_ } @$children) ? undef : join("",@$children);
# empty element or characters only
if (!($attributes and %$attributes) and defined $text_only) {
return $text_only ne "" ? $text_only : { };
}
my $hash = { $attributes ? %$attributes : () };
$hash->{ $self->content } = $text_only if defined $text_only and @$children;
foreach my $child ( @$children ) {
next unless ref $child; # skip mixed content text
if (defined $depth and $depth < 0) {
_push_hash( $hash, $child->[0], $child, 1 );
} else {
_push_hash( $hash, $child->[0] => $self->_simple($with_attributes, $child, $depth) );
}
}
return $hash;
}
sub _push_hash {
my ($hash, $key, $value, $force) = @_;
if ( exists $hash->{$key} ) {
if ((ref $hash->{$key} || '') ne 'ARRAY') {
$hash->{$key} = [ $hash->{$key} ];
}
push @{$hash->{$key}}, $value;
} elsif ( $force ) {
$hash->{$key} = [ $value ];
} else {
$hash->{$key} = $value;
}
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
XML::Struct::Simple - Transform MicroXML data structures into simple (unordered) form
=head1 VERSION
version 0.23
=head1 SYNOPSIS
my $converter = XML::Struct::Simple->new( root => 'record' );
my $struct = [ root => { xmlns => 'http://example.org/' },
[ '!', [ x => {}, [42] ] ] ];
my $simple = $converter->transform( $xml );
# { record => { xmlns => 'http://example.org/', x => 42 } }
=head1 DESCRIPTION
This module implements a transformation from structured XML (MicroXML) to
simple key-value format as known from L<XML::Simple>: Attributes and child
elements are treated as hash keys with their content as value. Text elements
without attributes are converted to text and empty elements without attributes
are converted to empty hashes.
L<XML::Struct> can export the function C<simpleXML> for easy use.
=head1 CONFIGURATION
=over
=item root
Keep the root element instead of removing. This corresponds to option
C<KeepRoot> in L<XML::Simple>. In addition one can set the name of the root
element if a non-numeric value is passed.
=item content
With option C<simple> enable, text content at elements with attributes is
parsed into a hash value with this key. Set to "C<content> by default.
Corresponds to option C<ContentKey> in L<XML::Simple>.
=item attributes
Assume input without attributes if set to a true value. The special value
C<remove> will first remove attributes, so the following three are equivalent:
my @children = (['a'],['b']);
simpleXML( [ $name => \@children ], attributes => 0 );
simpleXML( removeXMLAttr( [ $name => \%attributes, \@children ] ), attributes => 0 );
simpleXML( [ $name => \%attributes, \@children ], attributes => 'remove' );
=back
Option C<KeyAttr>, C<ForceArray>, and other fetures of L<XML::Simple> not
supported (yet).
=head1 AUTHOR
Jakob Voß
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2014 by Jakob Voß.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|