| 1 | package Catmandu::Fix::xml_simple; | ||
| 2 | |||
| 3 | 1 | our $VERSION = '0.17'; | |
| 4 | |||
| 5 | 1 | ▶ | use Catmandu::Sane; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use Moo; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use XML::Struct::Reader; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use XML::LibXML::Reader; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 1 | ▶ | use Catmandu::Fix::Has; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | |||
| 11 | 1 | with 'Catmandu::Fix::Base'; | |
| 12 | |||
| 13 | # TODO: avoid code duplication with xml_read | ||
| 14 | |||
| 15 | 1 | has field => ( fix_arg => 1 ); | |
| 16 | 1 | has attributes => ( fix_opt => 1 ); | |
| 17 | 1 | has ns => ( fix_opt => 1 ); | |
| 18 | 1 | has content => ( fix_opt => 1 ); | |
| 19 | 1 | has root => ( fix_opt => 1 ); | |
| 20 | 1 | has depth => ( fix_opt => 1 ); | |
| 21 | 1 | has path => ( fix_opt => 1 ); | |
| 22 | 1 | has whitespace => ( fix_opt => 1 ); | |
| 23 | |||
| 24 | 2 | ▶ | sub simple { 1 } |
|
Subroutine
simple: called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 25 | |||
| 26 | has _reader => ( | ||
| 27 | is => 'ro', | ||
| 28 | lazy => 1, | ||
| 29 | 1 | ▶ | builder => sub { |
|
Subroutine
_build__reader: called CC=1 SCAR=0.0
| |||
| 30 | XML::Struct::Reader->new( | ||
| 31 | 1 | map { $_ => $_[0]->$_ } | |
| 32 | 1 | grep { defined $_[0]->$_ } | |
| 33 | qw(attributes ns simple root depth content whitespace) | ||
| 34 | ); | ||
| 35 | } | ||
| 36 | 1 | ); | |
| 37 | |||
| 38 | 2 | ▶ | sub emit { |
|
Subroutine
emit: called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 39 | 2 | my ( $self, $fixer ) = @_; | |
| 40 | |||
| 41 | 2 | my $path = $fixer->split_path( $self->field ); | |
| 42 | 2 | my $key = pop @$path; | |
| 43 | |||
| 44 | 2 | my $reader = $fixer->capture( $self->_reader ); | |
| 45 | 2 | my $xpath = $fixer->capture( $self->path ); | |
| 46 | 2 | my $attributes = $fixer->capture( $self->attributes ); | |
| 47 | |||
| 48 | # TODO: use XML::Struct::Simple instead | ||
| 49 | my $options = $fixer->capture( | ||
| 50 | { | ||
| 51 | 0 | map { $_ => $self->$_ } | |
| 52 | 2 | grep { defined $self->$_ } qw(root depth attributes) | |
| 53 | } | ||
| 54 | ); | ||
| 55 | |||
| 56 | return $fixer->emit_walk_path( | ||
| 57 | $fixer->var, | ||
| 58 | $path, | ||
| 59 | 2 | ▶ | sub { |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 60 | 2 | my $var = $_[0]; | |
| 61 | $fixer->emit_get_key( | ||
| 62 | $var, $key, | ||
| 63 | 2 | ▶ | sub { |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 64 | 2 | my $var = $_[0]; | |
| 65 | 2 | return <<PERL; | |
| 66 | if (ref(${var}) and ref(${var}) =~ /^ARRAY/) { | ||
| 67 | ${var} = XML::Struct::simpleXML( ${var}, %{${options}} ); | ||
| 68 | } else { | ||
| 69 | # TODO: code duplication with xml_read | ||
| 70 | my \$stream = XML::LibXML::Reader->new( string => ${var} ); | ||
| 71 | ${var} = ${xpath} | ||
| 72 | ? [ ${reader}->readDocument(\$stream, ${xpath}) ] | ||
| 73 | : ${reader}->readDocument(\$stream); | ||
| 74 | } | ||
| 75 | PERL | ||
| 76 | } | ||
| 77 | 2 | ); | |
| 78 | } | ||
| 79 | 2 | ); | |
| 80 | } | ||
| 81 | |||
| 82 | 1 | 1; | |
| 83 | __END__ | ||