| 1 | package Catmandu::Fix::xpath_map; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use Catmandu::Sane; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use XML::LibXML::XPathContext; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use Catmandu::Fix::Has; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 1 | with 'Catmandu::Fix::Base'; | |||||||||
| 9 | |||||||||||
| 10 | 1 | our $VERSION = '0.05'; | |||||||||
| 11 | |||||||||||
| 12 | 1 | has old_path => (fix_arg => 1); | |||||||||
| 13 | 1 | has xpath => (fix_arg => 1); | |||||||||
| 14 | 1 | has new_path => (fix_arg => 1); | |||||||||
| 15 | 1 | has namespaces => (fix_opt => 'collect'); | |||||||||
| 16 | |||||||||||
| 17 | 1 | ▶ | sub emit { | ||||||||
|
Subroutine
emit: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 18 | 1 | my ($self, $fixer) = @_; | |||||||||
| 19 | |||||||||||
| 20 | 1 | my $old_path = $fixer->split_path($self->old_path); | |||||||||
| 21 | 1 | my $old_key = pop @$old_path; | |||||||||
| 22 | 1 | my $new_path = $fixer->split_path($self->new_path); | |||||||||
| 23 | 1 | my $xpath = $fixer->capture($self->xpath); | |||||||||
| 24 | |||||||||||
| 25 | 1 | my $vals = $fixer->generate_var; | |||||||||
| 26 | 1 | my $current_val = $fixer->generate_var; | |||||||||
| 27 | 1 | my $perl = ""; | |||||||||
| 28 | 1 | $perl .= $fixer->emit_declare_vars($vals, '[]'); | |||||||||
| 29 | 1 | $perl .= $fixer->emit_declare_vars($current_val); | |||||||||
| 30 | |||||||||||
| 31 | $perl .= $fixer->emit_walk_path( | ||||||||||
| 32 | $fixer->var, | ||||||||||
| 33 | $old_path, | ||||||||||
| 34 | 2 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 35 | 2 | my $var = shift; | |||||||||
| 36 | $fixer->emit_get_key( | ||||||||||
| 37 | $var, $old_key, | ||||||||||
| 38 | 2 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 39 | 2 | my $var = shift; | |||||||||
| 40 | 2 | "push(\@{${vals}}, ${var});"; | |||||||||
| 41 | } | ||||||||||
| 42 | 2 | ); | |||||||||
| 43 | } | ||||||||||
| 44 | 1 | ); | |||||||||
| 45 | |||||||||||
| 46 | 1 | my $my_self = $fixer->capture($self); | |||||||||
| 47 | |||||||||||
| 48 | $perl | ||||||||||
| 49 | .= "while (\@{${vals}}) {" | ||||||||||
| 50 | . "${current_val} = ${my_self}->xpath_map(shift(\@{${vals}}),${xpath});" | ||||||||||
| 51 | . $fixer->emit_create_path( | ||||||||||
| 52 | $fixer->var, | ||||||||||
| 53 | $new_path, | ||||||||||
| 54 | 1 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 55 | 1 | my $var = shift; | |||||||||
| 56 | 1 | "${var} = ${current_val};"; | |||||||||
| 57 | } | ||||||||||
| 58 | 1 | ) . "}"; | |||||||||
| 59 | |||||||||||
| 60 | 1 | $perl; | |||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | 1 | ▶ | sub xpath_map { | ||||||||
|
Subroutine
xpath_map: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||
| 64 | 1 | my ($self, $data, $xpath) = @_; | |||||||||
| 65 | |||||||||||
| 66 | 1 | ▶ | unless (ref($data) =~ /^XML::LibXML/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 67 | 1 | my ($key) = keys %$data; | |||||||||
| 68 | 1 | $data = $data->{$key}->[0]; | |||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | 1 | my $xc = XML::LibXML::XPathContext->new($data); | |||||||||
| 72 | |||||||||||
| 73 | 1 | ▶ | if ($self->namespaces) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | 1 | for (keys %{$self->namespaces}) { | |||||||||
| 75 | 1 | $xc->registerNs($_,$self->namespaces->{$_}); | |||||||||
| 76 | } | ||||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | 1 | $xc->findvalue($xpath); | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | 1 | 1; | |||||||||
| 83 | |||||||||||
| 84 | __END__ | ||||||||||