Compile.pm

filter: Statementstmt 74.5% 82 / 110 Branchbran 40.3% 25 / 62 Conditioncond 31.2% 5 / 16 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 84.2% 16 / 19 Podpod 87.5% 7 / 8 totaltotal 60.4% 136 / 225 CC 42 SCAR 34.0
CC
42
Cov
60%
dataToXML
93.8
_parsedNode
9.2
addSchemaDirs
7.4
CRAP 30.0
« Compile/Translate/Template.pm ↑ Summary Compile/Translate.pm »
1 # Copyrights 2006-2024 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.03.
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 our $VERSION = '1.64';
11 }
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 52 ▶{ my $class = shift;
Subroutine new: called CC=3 SCAR=11.4
Pod documented
41 52 ▶ my $top = @_ % 2 ? shift : undef;
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 ? : 52 0 52
42  
43 52 ▶ $class ne __PACKAGE__
Branch: 1 branch
BranchTrueFalseTotal
$class ne __PACKAGE__ or &panic("you should instantiate a sub-class, $class is base only") 52 0 52
44 or panic "you should instantiate a sub-class, $class is base only";
45  
46 52 (bless {}, $class)->init( {top => $top, @_} );
47 }
48  
49 sub init($)
50 52 ▶{ my ($self, $args) = @_;
Subroutine init: called CC=3 SCAR=11.3
Pod undocumented
51  
52 52 ▶ my $popts = $args->{parser_options} || [];
Condition50%1/2
$args->{'parser_options'} || []
l!l
052
MC/DC0%0/1
$args->{'parser_options'} || []
$args->{'parser_options'}
Truth table50%1/2
A
A= $args->{'parser_options'}
Aresult
11
00
53 52 ▶ $self->initParser(ref $popts eq 'HASH' ? %$popts : @$popts);
Branch: 1 branch
BranchTrueFalseTotal
ref $popts eq 'HASH' ? : 0 52 52
54  
55 52 $self->addSchemaDirs($args->{schema_dirs});
56 52 $self;
57 }
58  
59 #-------------------
60  
61 50 my @schema_dirs;
62 sub addSchemaDirs(@)
63 152 ▶{ my $thing = shift;
Subroutine addSchemaDirs: called CC=7 SCAR=20.0
Pod documented
64 152 foreach (@_)
65 152 { my $dir = shift;
66 152 ▶ my @dirs = grep {defined} ref $dir eq 'ARRAY' ? @$dir : $dir;
Branch: 1 branch
BranchTrueFalseTotal
ref $dir eq 'ARRAY' ? : 0 152 152
67 152 ▶ my $sep = $^O eq 'MSWin32' ? qr/\;/ : qr/\:/;
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 152 152
68 152 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 152 ▶ defined wantarray ? @schema_dirs : ();
Branch: 1 branch
BranchTrueFalseTotal
defined wantarray ? : 0 152 152
75 }
76  
77 #----------------------
78  
79  
80 sub initParser(@)
81 52 ▶{ 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 52 );
92 }
93  
94  
95 sub dataToXML($)
96 455 ▶{ my ($thing, $raw) = @_;
Subroutine dataToXML: called CC=17 SCAR=45.4
Pod documented
97 455 ▶ defined $raw or return;
Branch: 1 branch
BranchTrueFalseTotal
defined $raw or return 454 1 455
98  
99 454 ▶ $parser ||= $thing->initParser;
Condition33%1/3
$parser ||= $thing->initParser
l!l&&r!l&&!r
45400
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 454 my ($xml, %details);
102 454 ▶ if(ref $raw && UNIVERSAL::isa($raw, 'XML::LibXML::Node'))
Branch: 8 branches
BranchTrueFalseTotal
if (ref $raw and UNIVERSAL::isa($raw, 'XML::LibXML::Node')) { } 1 453 454
elsif (ref $raw eq 'SCALAR') { } 0 453 453
elsif (ref $raw eq 'GLOB') { } 0 453 453
elsif ($raw =~ /^[\s\x{FFFE}\x{FEFF}]*\</) { } 453 0 453
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
45301
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\x{FFFE}\x{FEFF}]*\</)
112 { # XML starts with '<', rare for files
113 453 ($xml, %details) = $thing->_parseScalar(\$raw);
114 }
115 elsif(my $known = $thing->knownNamespace($raw))
116 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
117 or error __x"cannot find pre-installed name-space file named {path} for {name}"
118 , path => $known, name => $raw;
119  
120 0 ($xml, %details) = $thing->_parseFile($fn);
121 0 $details{source} = "known namespace $raw";
122 }
123 elsif(my $fn = $thing->findSchemaFile($raw))
124 0 { ($xml, %details) = $thing->_parseFile($fn);
125 0 $details{source} = "filename in schema-dir $raw";
126 }
127 elsif(-f $raw)
128 0 { ($xml, %details) = $thing->_parseFile($raw);
129 }
130 ▶ 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
131 0 { error __x"file {fn} does not exist", fn => $raw;
132 }
133 else
134 0 { my $data = "$raw";
135 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
136 if length($data) > 60 && $data =~ m/\</;
137  
138 0 error __x"don't known how to interpret XML data\n {data}"
139 , data => $data;
140 }
141  
142 454 ▶ wantarray ? ($xml, %details) : $xml;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 54 400 454
143 }
144  
145 sub _parsedNode($)
146 1 ▶{ my ($thing, $node) = @_;
Subroutine _parsedNode: called CC=5 SCAR=22.1
147 1 my $top = $node;
148  
149 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
150 1 { $top = $node->documentElement;
151 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
152 1 trace "using preparsed XML document with element <$eltype>";
153 }
154 elsif($node->isa('XML::LibXML::Element'))
155 0 { trace 'using preparsed XML node <'.type_of_node($node).'>';
156 }
157 else
158 0 { my $text = $node->toString;
159 0 $text =~ s/\s+/ /gs;
160 0 ▶ substr($text, 70, -1, '...')
Branch: 1 branch
BranchTrueFalseTotal
if length $text > 75 0 0 0
161 if length $text > 75;
162 0 error __x"dataToXML() accepts pre-parsed document or element\n {got}"
163 , got => $text;
164 }
165  
166 1 ($top, source => ref $node);
167 }
168  
169 sub _parseScalar($)
170 453 ▶{ my ($thing, $data) = @_;
Subroutine _parseScalar: called CC=2 SCAR=7.0
171 453 trace "parsing XML from string $data";
172 453 my $xml = $parser->parse_string($$data);
173  
174 453 ▶ ( (defined $xml ? $xml->documentElement : undef)
Branch: 1 branch
BranchTrueFalseTotal
defined $xml ? : 453 0 453
175 , source => ref $data
176 );
177 }
178  
179 sub _parseFile($)
180 0 ▶{ my ($thing, $fn) = @_;
Subroutine _parseFile: not called CC=2 SCAR=17.9
181 0 trace "parsing XML from file $fn";
182 0 my $xml = $parser->parse_file($fn);
183  
184 0 ▶ ( (defined $xml ? $xml->documentElement : undef)
Branch: 1 branch
BranchTrueFalseTotal
defined $xml ? : 0 0 0
185 , source => 'file'
186 , filename => $fn
187 );
188 }
189  
190 sub _parseFileHandle($)
191 0 ▶{ my ($thing, $fh) = @_;
Subroutine _parseFileHandle: not called CC=2 SCAR=17.9
192 0 trace "parsing XML from open file $fh";
193 0 my $xml = $parser->parse_fh($fh);
194  
195 0 ▶ ( (defined $xml ? $xml->documentElement : undef)
Branch: 1 branch
BranchTrueFalseTotal
defined $xml ? : 0 0 0
196 , source => ref $thing
197 );
198 }
199  
200 #--------------------------
201  
202  
203 sub walkTree($$)
204 76 ▶{ my ($self, $node, $code) = @_;
Subroutine walkTree: called CC=3 SCAR=11.0
Pod documented
205 76 ▶ if($code->($node))
Branch: 1 branch
BranchTrueFalseTotal
if (&$code($node)) 18 58 76
206 { $self->walkTree($_, $code)
207 18 for $node->getChildNodes;
208 }
209 }
210  
211  
212 my %namespace_file;
213 sub knownNamespace($;@)
214 50 ▶{ my $thing = shift;
Subroutine knownNamespace: called CC=3 SCAR=11.0
Pod documented
215 50 ▶ return $namespace_file{ $_[0] } if @_==1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 0 50 50
216  
217 50 ▶ while(@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 300 50 350
218 300 { my $ns = shift;
219 300 $namespace_file{$ns} = shift;
220 }
221 50 undef;
222 }
223  
224  
225 sub findSchemaFile($)
226 1 ▶{ my ($thing, $fn) = @_;
Subroutine findSchemaFile: called CC=5 SCAR=19.2
Pod documented
227  
228 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
229 if File::Spec->file_name_is_absolute($fn);
230  
231 1 foreach my $dir (@schema_dirs)
232 1 { my $full = File::Spec->catfile($dir, $fn);
233 1 ▶ return $full if -f $full;
Branch: 1 branch
BranchTrueFalseTotal
if -f $full 1 0 1
234 }
235  
236 0 undef;
237 }
238  
239  
240 50 1;
« Compile/Translate/Template.pm ↑ Summary Compile/Translate.pm »