| 1 | package Catmandu::Importer::XML; | ||
| 2 | |||
| 3 | our $VERSION = '0.17'; | ||
| 4 | |||
| 5 | 1 | ▶ | use namespace::clean; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use Catmandu::Sane; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use Moo; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use XML::Struct::Reader; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 0 | ▶ | use Catmandu::XML::Transformer; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 10 | |||
| 11 | with 'Catmandu::Importer'; | ||
| 12 | |||
| 13 | has type => (is => 'ro', default => sub { 'simple' }); | ||
| 14 | has path => (is => 'ro'); | ||
| 15 | has root => (is => 'lazy', default => sub { defined $_[0]->path ? 1 : 0 }); | ||
| 16 | has depth => (is => 'ro'); | ||
| 17 | has ns => (is => 'ro', default => sub { '' }); | ||
| 18 | has attributes => (is => 'ro', default => sub { 1 }); | ||
| 19 | has whitespace => (is => 'ro', default => sub { 0 }); | ||
| 20 | has xslt => ( | ||
| 21 | is => 'ro', | ||
| 22 | lazy => 1, | ||
| 23 | coerce => sub { | ||
| 24 | Catmandu::XML::Transformer->new( stylesheet => $_[0] ) if defined $_[0] | ||
| 25 | }, | ||
| 26 | builder => sub { | ||
| 27 | $_[0]->transform | ||
| 28 | } | ||
| 29 | ); | ||
| 30 | has transform => ( | ||
| 31 | is => 'ro', | ||
| 32 | coerce => sub { | ||
| 33 | warn "Catmandu::Importer::XML option 'transform' renamed to 'xslt'\n"; | ||
| 34 | $_[0]; | ||
| 35 | } | ||
| 36 | ); | ||
| 37 | |||
| 38 | sub generator { | ||
| 39 | my ($self) = @_; | ||
| 40 | sub { | ||
| 41 | state $reader = do { | ||
| 42 | my %options = ( | ||
| 43 | from => ($self->file || $self->fh), | ||
| 44 | whitespace => $self->whitespace, | ||
| 45 | attributes => $self->attributes, | ||
| 46 | depth => $self->depth, | ||
| 47 | ns => $self->ns, | ||
| 48 | ); | ||
| 49 | $options{path} = $self->path if defined $self->path; | ||
| 50 | if ($self->type eq 'simple') { | ||
| 51 | $options{simple} = 1; | ||
| 52 | $options{root} = $self->root; | ||
| 53 | } elsif ($self->type ne 'ordered') { | ||
| 54 | return; | ||
| 55 | } | ||
| 56 | XML::Struct::Reader->new(%options); | ||
| 57 | }; | ||
| 58 | |||
| 59 | my $item = $reader->readNext; | ||
| 60 | |||
| 61 | # TODO: transformation should be done earlier for efficiency | ||
| 62 | # and because simple format modifies the XML document (bug) | ||
| 63 | return $self->xslt ? | ||
| 64 | $self->xslt->transform($item) : $item | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | 1; | ||
| 69 | __END__ | ||