lib/XML/Compile.pm

filter: Statementstmt 75.0% 84 / 112 Branchbran 40.3% 25 / 62 Conditioncond 31.2% 5 / 16 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 85.0% 17 / 20 Podpod 87.5% 7 / 8 totaltotal 60.9% 139 / 228 CC 42 SCAR 33.9
CC
42
Cov
60%
dataToXML
93.8
_parsedNode
9.2
addSchemaDirs
7.4
CRAP 29.5
« blib/lib/XML/Compile/Schema/Specs.pm ↑ Summary lib/XML/Compile/Translate.pm »
1 # Copyrights 2006-2018 by [Mark Overmeer <markov@cpan.org>].
2 # For other contributors see ChangeLog.
3 # See the manual pages for details on the licensing terms.
4 # Pod stripped from pm file by OODoc 2.02.
5 # This code is part of distribution XML-Compile. Meta-POD processed with
6 # OODoc into POD and HTML manual-pages. See README.md
7 # Copyright Mark Overmeer. Licensed under the same terms as Perl itself.
8  
9 package XML::Compile;
10 50 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 50 $VERSION = '1.60';
12  
13  
14 50 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 50 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 50 ▶use Log::Report 'xml-compile';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 50 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 50 ▶use XML::Compile::Util qw/:constants type_of_node/;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 50 ▶use File::Spec qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 50 my $parser;
24  
25 50 __PACKAGE__->knownNamespace
26 ( &XMLNS => '1998-namespace.xsd'
27 , &SCHEMA1999 => '1999-XMLSchema.xsd'
28 , &SCHEMA2000 => '2000-XMLSchema.xsd'
29 , &SCHEMA2001 => '2001-XMLSchema.xsd'
30 , &SCHEMA2001i => '2001-XMLSchema-instance.xsd'
31 , 'http://www.w3.org/1999/part2.xsd'
32 => '1999-XMLSchema-part2.xsd'
33 );
34  
35 50 __PACKAGE__->addSchemaDirs($ENV{SCHEMA_DIRECTORIES});
36 50 __PACKAGE__->addSchemaDirs(__FILE__);
37  
38  
39 sub new(@)
40 51 ▶{ my $class = shift;
Subroutine new: called CC=3 SCAR=11.4
Pod documented
41 51 ▶ my $top = @_ % 2 ? shift : undef;
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 ? : 51 0 51
42  
43 51 ▶ $class ne __PACKAGE__
Branch: 1 branch
BranchTrueFalseTotal
$class ne __PACKAGE__ or &panic("you should instantiate a sub-class, $class is base only") 0 51 51
44 or panic "you should instantiate a sub-class, $class is base only";
45  
46 51 (bless {}, $class)->init( {top => $top, @_} );
47 }
48  
49 sub init($)
50 51 ▶{ my ($self, $args) = @_;
Subroutine init: called CC=3 SCAR=11.3
Pod undocumented
51  
52 51 ▶ my $popts = $args->{parser_options} || [];
Condition50%1/2
$args->{'parser_options'} || []
l!l
051
MC/DC0%0/1
$args->{'parser_options'} || []
$args->{'parser_options'}
Truth table50%1/2
A
A= $args->{'parser_options'}
Aresult
11
00
53 51 ▶ $self->initParser(ref $popts eq 'HASH' ? %$popts : @$popts);
Branch: 1 branch
BranchTrueFalseTotal
ref $popts eq 'HASH' ? : 0 51 51
54  
55 51 $self->addSchemaDirs($args->{schema_dirs});
56 51 $self;
57 }
58  
59 #-------------------
60  
61 50 my @schema_dirs;
62 sub addSchemaDirs(@)
63 151 ▶{ my $thing = shift;
Subroutine addSchemaDirs: called CC=7 SCAR=20.0
Pod documented
64 151 foreach (@_)
65 151 { my $dir = shift;
66 151 ▶ my @dirs = grep {defined} ref $dir eq 'ARRAY' ? @$dir : $dir;
Branch: 1 branch
BranchTrueFalseTotal
ref $dir eq 'ARRAY' ? : 0 151 151
67 151 ▶ my $sep = $^O eq 'MSWin32' ? qr/\;/ : qr/\:/;
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 151 151
68 151 foreach (map { split $sep } @dirs)
69 50 { my $el = $_;
70 50 ▶ $el = File::Spec->catfile($el, 'xsd') if $el =~ s/\.pm$//i;
Branch: 1 branch
BranchTrueFalseTotal
if $el =~ s/\.pm$//i 50 0 50
71 50 push @schema_dirs, $el;
72 }
73 }
74 151 ▶ defined wantarray ? @schema_dirs : ();
Branch: 1 branch
BranchTrueFalseTotal
defined wantarray ? : 0 151 151
75 }
76  
77 #----------------------
78  
79  
80 sub initParser(@)
81 51 ▶{ my $thing = shift;
Subroutine initParser: called CC=1 SCAR=1.2
Pod documented
82 $parser = XML::LibXML->new
83 ( line_numbers => 1
84 , no_network => 1
85 , expand_xinclude => 0
86 , expand_entities => 1
87 , load_ext_dtd => 0
88 , ext_ent_handler =>
89 0 ▶ sub { alert __x"parsing external entities disabled"; '' }
Subroutine __ANON__: not called CC=1 SCAR=6.9
90 , @_
91 51 );
92 }
93  
94  
95 sub dataToXML($)
96 432 ▶{ my ($thing, $raw) = @_;
Subroutine dataToXML: called CC=17 SCAR=45.4
Pod documented
97 432 ▶ defined $raw or return;
Branch: 1 branch
BranchTrueFalseTotal
defined $raw or return 1 431 432
98  
99 431 ▶ $parser ||= $thing->initParser;
Condition33%1/3
$parser ||= $thing->initParser
l!l&&r!l&&!r
43100
MC/DC0%0/2
$parser ||= $thing->initParser
$parser $thing->initParser
Truth table33%1/3
A ||= B
A= $parserB= $thing->initParser
ABresult
1X1
011
000
100  
101 431 my ($xml, %details);
102 431 ▶ if(ref $raw && UNIVERSAL::isa($raw, 'XML::LibXML::Node'))
Branch: 8 branches
BranchTrueFalseTotal
if (ref $raw and UNIVERSAL::isa($raw, 'XML::LibXML::Node')) { } 1 430 431
elsif (ref $raw eq 'SCALAR') { } 0 430 430
elsif (ref $raw eq 'GLOB') { } 0 430 430
elsif ($raw =~ /^\s*\</) { } 430 0 430
elsif (my $known = $thing->knownNamespace($raw)) { } 0 0 0
elsif (my $fn = $thing->findSchemaFile($raw)) { } 0 0 0
elsif (-f $raw) { } 0 0 0
elsif (not $raw =~ /[\n\r<]/ and $raw =~ m([/\\]|\.xsd$|\.wsdl$)i) { } 0 0 0
Condition66%2/3
ref $raw and UNIVERSAL::isa($raw, "XML::LibXML::Node")
!ll&&!rl&&r
43001
MC/DC50%1/2
ref $raw and UNIVERSAL::isa($raw, "XML::LibXML::Node")
ref $raw UNIVERSAL::isa($raw, "XML::LibXML::Node")
Truth table66%2/3
A and B
A= ref $rawB= UNIVERSAL::isa($raw, "XML::LibXML::Node")
ABresult
0X0
100
111
103 1 { ($xml, %details) = $thing->_parsedNode($raw);
104 }
105 elsif(ref $raw eq 'SCALAR') # XML string as ref
106 0 { ($xml, %details) = $thing->_parseScalar($raw);
107 }
108 elsif(ref $raw eq 'GLOB') # from file-handle
109 0 { ($xml, %details) = $thing->_parseFileHandle($raw);
110 }
111 elsif($raw =~ m/^\s*\</) # XML starts with '<', rare for files
112 430 { ($xml, %details) = $thing->_parseScalar(\$raw);
113 }
114 elsif(my $known = $thing->knownNamespace($raw))
115 0 ▶ { my $fn = $thing->findSchemaFile($known)
Branch: 1 branch
BranchTrueFalseTotal
my $fn = $thing->findSchemaFile($known) or &error(&__x("cannot find pre-installed name-space file named {path} for {name}", "path", $known, "name", $raw)) 0 0 0
116 or error __x"cannot find pre-installed name-space file named {path} for {name}"
117 , path => $known, name => $raw;
118  
119 0 ($xml, %details) = $thing->_parseFile($fn);
120 0 $details{source} = "known namespace $raw";
121 }
122 elsif(my $fn = $thing->findSchemaFile($raw))
123 0 { ($xml, %details) = $thing->_parseFile($fn);
124 0 $details{source} = "filename in schema-dir $raw";
125 }
126 elsif(-f $raw)
127 0 { ($xml, %details) = $thing->_parseFile($raw);
128 }
129 ▶ elsif($raw !~ /[\n\r<]/ && $raw =~ m![/\\]|\.xsd$|\.wsdl$!i)
Condition0%0/3
not $raw =~ /[\n\r<]/ and $raw =~ m([/\\]|\.xsd$|\.wsdl$)i
!ll&&!rl&&r
000
MC/DC0%0/2
not $raw =~ /[\n\r<]/ and $raw =~ m([/\\]|\.xsd$|\.wsdl$)i
not $raw =~ /[\n\r<]/ $raw =~ m([/\\]|\.xsd$|\.wsdl$)i
Truth table0%0/3
not(A) and B
A= not $raw =~ /[\n\r<]/B= $raw =~ m([/\\]|\.xsd$|\.wsdl$)i
ABresult
0X0
100
111
130 0 { error __x"file {fn} does not exist", fn => $raw;
131 }
132 else
133 0 { my $data = "$raw";
134 0 ▶ $data = substr($data, 0, 59) . '...'
Branch: 1 branch
BranchTrueFalseTotal
if length $data > 60 and $data =~ /\</ 0 0 0
Condition0%0/3
length $data > 60 and $data =~ /\</
!ll&&!rl&&r
000
MC/DC0%0/2
length $data > 60 and $data =~ /\</
length $data > 60 $data =~ /\</
Truth table0%0/3
A and B
A= length $data > 60B= $data =~ /\</
ABresult
0X0
100
111
135 if length($data) > 60 && $data =~ m/\</;
136  
137 0 error __x"don't known how to interpret XML data\n {data}"
138 , data => $data;
139 }
140  
141 431 ▶ wantarray ? ($xml, %details) : $xml;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 53 378 431
142 }
143  
144 sub _parsedNode($)
145 1 ▶{ my ($thing, $node) = @_;
Subroutine _parsedNode: called CC=5 SCAR=22.1
146 1 my $top = $node;
147  
148 1 ▶ if($node->isa('XML::LibXML::Document'))
Branch: 2 branches
BranchTrueFalseTotal
if ($node->isa('XML::LibXML::Document')) { } 1 0 1
elsif ($node->isa('XML::LibXML::Element')) { } 0 0 0
149 1 { $top = $node->documentElement;
150 1 ▶ my $eltype = type_of_node($top || '(none)');
Condition50%1/2
$top || '(none)'
l!l
10
MC/DC0%0/1
$top || '(none)'
$top
Truth table50%1/2
A
A= $top
Aresult
11
00
151 1 trace "using preparsed XML document with element <$eltype>";
152 }
153 elsif($node->isa('XML::LibXML::Element'))
154 0 { trace 'using preparsed XML node <'.type_of_node($node).'>';
155 }
156 else
157 0 { my $text = $node->toString;
158 0 $text =~ s/\s+/ /gs;
159 0 ▶ substr($text, 70, -1, '...')
Branch: 1 branch
BranchTrueFalseTotal
if length $text > 75 0 0 0
160 if length $text > 75;
161 0 error __x"dataToXML() accepts pre-parsed document or element\n {got}"
162 , got => $text;
163 }
164  
165 1 ($top, source => ref $node);
166 }
167  
168 sub _parseScalar($)
169 430 ▶{ my ($thing, $data) = @_;
Subroutine _parseScalar: called CC=2 SCAR=7.0
170 430 trace "parsing XML from string $data";
171 430 my $xml = $parser->parse_string($$data);
172  
173 430 ▶ ( (defined $xml ? $xml->documentElement : undef)
Branch: 1 branch
BranchTrueFalseTotal
defined $xml ? : 430 0 430
174 , source => ref $data
175 );
176 }
177  
178 sub _parseFile($)
179 0 ▶{ my ($thing, $fn) = @_;
Subroutine _parseFile: not called CC=2 SCAR=17.9
180 0 trace "parsing XML from file $fn";
181 0 my $xml = $parser->parse_file($fn);
182  
183 0 ▶ ( (defined $xml ? $xml->documentElement : undef)
Branch: 1 branch
BranchTrueFalseTotal
defined $xml ? : 0 0 0
184 , source => 'file'
185 , filename => $fn
186 );
187 }
188  
189 sub _parseFileHandle($)
190 0 ▶{ my ($thing, $fh) = @_;
Subroutine _parseFileHandle: not called CC=2 SCAR=17.9
191 0 trace "parsing XML from open file $fh";
192 0 my $xml = $parser->parse_fh($fh);
193  
194 0 ▶ ( (defined $xml ? $xml->documentElement : undef)
Branch: 1 branch
BranchTrueFalseTotal
defined $xml ? : 0 0 0
195 , source => ref $thing
196 );
197 }
198  
199 #--------------------------
200  
201  
202 sub walkTree($$)
203 75 ▶{ my ($self, $node, $code) = @_;
Subroutine walkTree: called CC=3 SCAR=11.0
Pod documented
204 75 ▶ if($code->($node))
Branch: 1 branch
BranchTrueFalseTotal
if (&$code($node)) 18 57 75
205 { $self->walkTree($_, $code)
206 18 for $node->getChildNodes;
207 }
208 }
209  
210  
211 my %namespace_file;
212 sub knownNamespace($;@)
213 50 ▶{ my $thing = shift;
Subroutine knownNamespace: called CC=3 SCAR=11.0
Pod documented
214 50 ▶ return $namespace_file{ $_[0] } if @_==1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 0 50 50
215  
216 50 ▶ while(@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 300 50 350
217 300 { my $ns = shift;
218 300 $namespace_file{$ns} = shift;
219 }
220 50 undef;
221 }
222  
223  
224 sub findSchemaFile($)
225 1 ▶{ my ($thing, $fn) = @_;
Subroutine findSchemaFile: called CC=5 SCAR=19.2
Pod documented
226  
227 1 ▶ return (-f $fn ? $fn : undef)
Branch: 2 branches
BranchTrueFalseTotal
if "File::Spec"->file_name_is_absolute($fn) 0 1 1
-f $fn ? : 0 0 0
228 if File::Spec->file_name_is_absolute($fn);
229  
230 1 foreach my $dir (@schema_dirs)
231 1 { my $full = File::Spec->catfile($dir, $fn);
232 1 ▶ return $full if -f $full;
Branch: 1 branch
BranchTrueFalseTotal
if -f $full 1 0 1
233 }
234  
235 0 undef;
236 }
237  
238  
239 50 1;
« blib/lib/XML/Compile/Schema/Specs.pm ↑ Summary lib/XML/Compile/Translate.pm »