File Coverage

blib/lib/Catmandu/Importer/LIDO.pm
Criterion Covered Total %
statement 16 16 100.0
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 22 22 100.0


line stmt bran cond sub pod time code
1             package Catmandu::Importer::LIDO;
2              
3 1     1   168517 use Catmandu::Sane;
  1         177162  
  1         6  
4 1     1   732 use Lido::XML;
  1         249506  
  1         42  
5 1     1   505 use XML::LibXML::Reader;
  1         1885  
  1         125  
6              
7             our $VERSION = '0.10';
8              
9 1     1   8 use Moo;
  1         3  
  1         7  
10 1     1   403 use namespace::clean;
  1         3  
  1         11  
11              
12             with 'Catmandu::Importer';
13              
14             has 'lido' => (is => 'lazy');
15              
16             sub _build_lido {
17 1     1   21 return Lido::XML->new;
18             }
19              
20             sub generator {
21             my ($self) = @_;
22             my $file = $self->file;
23             my %opts;
24              
25             if (ref($file)) {
26             %opts = ('IO' , $file);
27             }
28             else {
29             %opts = ('location' , $file);
30             }
31              
32             sub {
33             state $reader = XML::LibXML::Reader->new(%opts);
34              
35             my $match = $reader->nextPatternMatch(
36             XML::LibXML::Pattern->new(
37             '//lido:lido', { lido => 'http://www.lido-schema.org' }
38             )
39             );
40              
41             return undef unless $match == 1;
42              
43             my $xml = $reader->readOuterXml();
44              
45             return undef unless length $xml;
46              
47             $reader->nextSibling();
48              
49             return $self->lido->parse($xml);
50             };
51             }
52              
53             1;
54              
55             __END__
56              
57             =pod
58              
59             =head1 NAME
60              
61             Catmandu::Importer::LIDO - A LIDO XML importer
62              
63             =head1 SYNOPSIS
64              
65             # From the command line
66             $ catmandu convert LIDO to YAML < ex/lido.xml
67              
68             # From Perl
69             use Catmandu;
70              
71             my $importer = Catmandu->importer('LIDO',file => 'ex/lido.xml');
72              
73             my $n = $importer->each(sub {
74             my $hashref = $_[0];
75             # ...
76             });
77              
78             =head1 DESCRIPTION
79              
80             This is a L<Catmandu::Importer> for converting LIDO data (an XML Schema for
81             Contributing Content to Cultural Heritage Repositories).
82              
83             =head1 CONFIGURATION
84              
85             =over
86              
87             =item file
88              
89             Read input from a local file given by its path. Alternatively a scalar
90             reference can be passed to read from a string.
91              
92             =item fh
93              
94             Read input from an L<IO::Handle>. If not specified, L<Catmandu::Util::io> is used to
95             create the input stream from the C<file> argument or by using STDIN.
96              
97             =item encoding
98              
99             Binmode of the input stream C<fh>. Set to C<:utf8> by default.
100              
101             =item fix
102              
103             An ARRAY of one or more fixes or file scripts to be applied to imported items.
104              
105             =item size
106              
107             Number of items. If not set, an endless stream is imported.
108              
109             =back
110              
111             =head1 METHODS
112              
113             Every L<Catmandu::Importer> is a L<Catmandu::Iterable> all its methods are
114             inherited.
115              
116             =head1 SEE ALSO
117              
118             L<Catmandu::Importer>, L<Lido::XML>
119              
120             =cut