| 1 | package Catmandu::Importer::XSD; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 1 | ▶ | use Catmandu::Sane; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 1 | ▶ | use Catmandu::Util qw(:is); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 1 | ▶ | use XML::LibXML::Reader; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 1 | ▶ | use Catmandu::XSD; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 1 | ▶ | use feature 'state'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | 1 | our $VERSION = '0.05'; | |||||||||||||
| 10 | |||||||||||||||
| 11 | 1 | ▶ | use Moo; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | 1 | ▶ | use namespace::clean; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | |||||||||||||||
| 14 | 1 | with 'Catmandu::Importer'; | |||||||||||||
| 15 | |||||||||||||||
| 16 | 1 | has 'root' => (is => 'ro' , required => 1); | |||||||||||||
| 17 | 1 | has 'schemas' => (is => 'ro' , required => 1); | |||||||||||||
| 18 | 2 | ▶ | has 'mixed' => (is => 'ro' , default => sub { 'ATTRIBUTES' }); | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 19 | 2 | ▶ | has 'any_element' => (is => 'ro' , default => sub { 'TAKE_ALL' }); | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 20 | 2 | ▶ | has 'prefixes' => (is => 'ro' , default => sub { [] }); | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 21 | 1 | has 'files' => (is => 'ro'); | |||||||||||||
| 22 | 1 | ▶ | has 'xpath' => (is => 'ro' , default => sub { '*' }); | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 23 | 1 | has 'example' => (is => 'ro'); | |||||||||||||
| 24 | |||||||||||||||
| 25 | 1 | has 'xsd' => (is => 'lazy'); | |||||||||||||
| 26 | |||||||||||||||
| 27 | 2 | ▶ | sub _build_xsd { | ||||||||||||
|
Subroutine
_build_xsd: called CC=1 SCAR=0.0
| |||||||||||||||
| 28 | 2 | my $self = $_[0]; | |||||||||||||
| 29 | 2 | return Catmandu::XSD->new( | |||||||||||||
| 30 | root => $self->root , | ||||||||||||||
| 31 | schemas => $self->schemas , | ||||||||||||||
| 32 | mixed => $self->mixed , | ||||||||||||||
| 33 | any_element => $self->any_element , | ||||||||||||||
| 34 | prefixes => $self->prefixes , | ||||||||||||||
| 35 | ); | ||||||||||||||
| 36 | } | ||||||||||||||
| 37 | |||||||||||||||
| 38 | 2 | ▶ | sub generator { | ||||||||||||
|
Subroutine
generator: called CC=3 SCAR=11.3
Pod
undocumented
| |||||||||||||||
| 39 | 2 | my $self = $_[0]; | |||||||||||||
| 40 | |||||||||||||||
| 41 | 2 | ▶ | if ($self->example) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 42 | 0 | $self->example_generator | |||||||||||||
| 43 | } | ||||||||||||||
| 44 | elsif ($self->files) { | ||||||||||||||
| 45 | 1 | $self->multi_file_generator | |||||||||||||
| 46 | } | ||||||||||||||
| 47 | else { | ||||||||||||||
| 48 | 1 | $self->single_file_generator | |||||||||||||
| 49 | } | ||||||||||||||
| 50 | } | ||||||||||||||
| 51 | |||||||||||||||
| 52 | 0 | ▶ | sub example_generator { | ||||||||||||
|
Subroutine
example_generator: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 53 | 0 | my $self = $_[0]; | |||||||||||||
| 54 | |||||||||||||||
| 55 | 0 | my $count = 0; | |||||||||||||
| 56 | |||||||||||||||
| 57 | 0 | ▶ | sub { | ||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||
| 58 | 0 | ▶ | $count++ ? undef : $self->xsd->template; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 59 | 0 | }; | |||||||||||||
| 60 | } | ||||||||||||||
| 61 | |||||||||||||||
| 62 | 1 | ▶ | sub multi_file_generator { | ||||||||||||
|
Subroutine
multi_file_generator: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 63 | 1 | my $self = $_[0]; | |||||||||||||
| 64 | |||||||||||||||
| 65 | 1 | my @files = glob($self->files); | |||||||||||||
| 66 | |||||||||||||||
| 67 | 3 | ▶ | sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||
| 68 | 3 | my $file = shift @files; | |||||||||||||
| 69 | |||||||||||||||
| 70 | 3 | ▶ | return undef unless $file; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 71 | 2 | my $xml = XML::LibXML->load_xml(location => $file); | |||||||||||||
| 72 | 2 | $self->xsd->parse($xml); | |||||||||||||
| 73 | 1 | }; | |||||||||||||
| 74 | } | ||||||||||||||
| 75 | |||||||||||||||
| 76 | 1 | ▶ | sub single_file_generator { | ||||||||||||
|
Subroutine
single_file_generator: called CC=5 SCAR=17.1
Pod
undocumented
| |||||||||||||||
| 77 | 1 | my $self = $_[0]; | |||||||||||||
| 78 | |||||||||||||||
| 79 | 1 | my $prefixes = {}; | |||||||||||||
| 80 | |||||||||||||||
| 81 | 1 | ▶ | if ($self->prefixes) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 82 | 1 | ▶ | if (is_array_ref $self->prefixes) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 83 | 1 | for (@{$self->prefixes}) { | |||||||||||||
| 84 | 0 | my ($key,$val) = each %$_; | |||||||||||||
| 85 | 0 | $prefixes->{$key} = $val; | |||||||||||||
| 86 | } | ||||||||||||||
| 87 | } | ||||||||||||||
| 88 | else { | ||||||||||||||
| 89 | 0 | for (split(/,/,$self->prefixes)) { | |||||||||||||
| 90 | 0 | my ($key,$val) = split(/:/,$_,2); | |||||||||||||
| 91 | 0 | $prefixes->{$key} = $val; | |||||||||||||
| 92 | } | ||||||||||||||
| 93 | } | ||||||||||||||
| 94 | } | ||||||||||||||
| 95 | |||||||||||||||
| 96 | # Drop all PerlIO layers possibly created by a use open pragma | ||||||||||||||
| 97 | # requirement for XML::LibXML parsing | ||||||||||||||
| 98 | # See: https://metacpan.org/pod/distribution/XML-LibXML/LibXML.pod | ||||||||||||||
| 99 | 1 | binmode $self->fh; | |||||||||||||
| 100 | |||||||||||||||
| 101 | 3 | ▶ | sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||
| 102 | 3 | state $reader = XML::LibXML::Reader->new(IO => $self->fh); | |||||||||||||
| 103 | |||||||||||||||
| 104 | 3 | my $match = $reader->nextPatternMatch( | |||||||||||||
| 105 | XML::LibXML::Pattern->new($self->xpath , $prefixes) | ||||||||||||||
| 106 | ); | ||||||||||||||
| 107 | |||||||||||||||
| 108 | 3 | ▶ | return undef unless $match == 1; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 109 | |||||||||||||||
| 110 | 2 | my $xml = $reader->readOuterXml(); | |||||||||||||
| 111 | |||||||||||||||
| 112 | 2 | ▶ | return undef unless length $xml; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 113 | |||||||||||||||
| 114 | 2 | $reader->nextSibling(); | |||||||||||||
| 115 | |||||||||||||||
| 116 | 2 | my $data = $self->xsd->parse($xml); | |||||||||||||
| 117 | |||||||||||||||
| 118 | 2 | return $data; | |||||||||||||
| 119 | 1 | }; | |||||||||||||
| 120 | } | ||||||||||||||
| 121 | |||||||||||||||
| 122 | 1 | 1; | |||||||||||||
| 123 | |||||||||||||||
| 124 | __END__ | ||||||||||||||