blib/lib/XML/Compile.pm untested

filter: Statementstmt 0.0% 0 / 118 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 24 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 227 CC 28 SCAR 37.0
CC
28
Cov
0%
dataToXML
132.0
addSchemaDirs
30.0
_parsedNode
20.0
CRAP 40.5
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 use vars '$VERSION';
11 $VERSION = '1.60';
12  
13  
14 use warnings;
15 use strict;
16  
17 use Log::Report 'xml-compile';
18 use XML::LibXML;
19 use XML::Compile::Util qw/:constants type_of_node/;
20  
21 use File::Spec qw();
22  
23 my $parser;
24  
25 __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 __PACKAGE__->addSchemaDirs($ENV{SCHEMA_DIRECTORIES});
36 __PACKAGE__->addSchemaDirs(__FILE__);
37  
38  
39 sub new(@)
40 { my $class = shift;
41 my $top = @_ % 2 ? shift : undef;
42  
43 $class ne __PACKAGE__
44 or panic "you should instantiate a sub-class, $class is base only";
45  
46 (bless {}, $class)->init( {top => $top, @_} );
47 }
48  
49 sub init($)
50 { my ($self, $args) = @_;
51  
52 my $popts = $args->{parser_options} || [];
53 $self->initParser(ref $popts eq 'HASH' ? %$popts : @$popts);
54  
55 $self->addSchemaDirs($args->{schema_dirs});
56 $self;
57 }
58  
59 #-------------------
60  
61 my @schema_dirs;
62 sub addSchemaDirs(@)
63 { my $thing = shift;
64 foreach (@_)
65 { my $dir = shift;
66 my @dirs = grep {defined} ref $dir eq 'ARRAY' ? @$dir : $dir;
67 my $sep = $^O eq 'MSWin32' ? qr/\;/ : qr/\:/;
68 foreach (map { split $sep } @dirs)
69 { my $el = $_;
70 $el = File::Spec->catfile($el, 'xsd') if $el =~ s/\.pm$//i;
71 push @schema_dirs, $el;
72 }
73 }
74 defined wantarray ? @schema_dirs : ();
75 }
76  
77 #----------------------
78  
79  
80 sub initParser(@)
81 { my $thing = shift;
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 sub { alert __x"parsing external entities disabled"; '' }
90 , @_
91 );
92 }
93  
94  
95 sub dataToXML($)
96 { my ($thing, $raw) = @_;
97 defined $raw or return;
98  
99 $parser ||= $thing->initParser;
100  
101 my ($xml, %details);
102 if(ref $raw && UNIVERSAL::isa($raw, 'XML::LibXML::Node'))
103 { ($xml, %details) = $thing->_parsedNode($raw);
104 }
105 elsif(ref $raw eq 'SCALAR') # XML string as ref
106 { ($xml, %details) = $thing->_parseScalar($raw);
107 }
108 elsif(ref $raw eq 'GLOB') # from file-handle
109 { ($xml, %details) = $thing->_parseFileHandle($raw);
110 }
111 elsif($raw =~ m/^\s*\</) # XML starts with '<', rare for files
112 { ($xml, %details) = $thing->_parseScalar(\$raw);
113 }
114 elsif(my $known = $thing->knownNamespace($raw))
115 { my $fn = $thing->findSchemaFile($known)
116 or error __x"cannot find pre-installed name-space file named {path} for {name}"
117 , path => $known, name => $raw;
118  
119 ($xml, %details) = $thing->_parseFile($fn);
120 $details{source} = "known namespace $raw";
121 }
122 elsif(my $fn = $thing->findSchemaFile($raw))
123 { ($xml, %details) = $thing->_parseFile($fn);
124 $details{source} = "filename in schema-dir $raw";
125 }
126 elsif(-f $raw)
127 { ($xml, %details) = $thing->_parseFile($raw);
128 }
129 elsif($raw !~ /[\n\r<]/ && $raw =~ m![/\\]|\.xsd$|\.wsdl$!i)
130 { error __x"file {fn} does not exist", fn => $raw;
131 }
132 else
133 { my $data = "$raw";
134 $data = substr($data, 0, 59) . '...'
135 if length($data) > 60 && $data =~ m/\</;
136  
137 error __x"don't known how to interpret XML data\n {data}"
138 , data => $data;
139 }
140  
141 wantarray ? ($xml, %details) : $xml;
142 }
143  
144 sub _parsedNode($)
145 { my ($thing, $node) = @_;
146 my $top = $node;
147  
148 if($node->isa('XML::LibXML::Document'))
149 { $top = $node->documentElement;
150 my $eltype = type_of_node($top || '(none)');
151 trace "using preparsed XML document with element <$eltype>";
152 }
153 elsif($node->isa('XML::LibXML::Element'))
154 { trace 'using preparsed XML node <'.type_of_node($node).'>';
155 }
156 else
157 { my $text = $node->toString;
158 $text =~ s/\s+/ /gs;
159 substr($text, 70, -1, '...')
160 if length $text > 75;
161 error __x"dataToXML() accepts pre-parsed document or element\n {got}"
162 , got => $text;
163 }
164  
165 ($top, source => ref $node);
166 }
167  
168 sub _parseScalar($)
169 { my ($thing, $data) = @_;
170 trace "parsing XML from string $data";
171 my $xml = $parser->parse_string($$data);
172  
173 ( (defined $xml ? $xml->documentElement : undef)
174 , source => ref $data
175 );
176 }
177  
178 sub _parseFile($)
179 { my ($thing, $fn) = @_;
180 trace "parsing XML from file $fn";
181 my $xml = $parser->parse_file($fn);
182  
183 ( (defined $xml ? $xml->documentElement : undef)
184 , source => 'file'
185 , filename => $fn
186 );
187 }
188  
189 sub _parseFileHandle($)
190 { my ($thing, $fh) = @_;
191 trace "parsing XML from open file $fh";
192 my $xml = $parser->parse_fh($fh);
193  
194 ( (defined $xml ? $xml->documentElement : undef)
195 , source => ref $thing
196 );
197 }
198  
199 #--------------------------
200  
201  
202 sub walkTree($$)
203 { my ($self, $node, $code) = @_;
204 if($code->($node))
205 { $self->walkTree($_, $code)
206 for $node->getChildNodes;
207 }
208 }
209  
210  
211 my %namespace_file;
212 sub knownNamespace($;@)
213 { my $thing = shift;
214 return $namespace_file{ $_[0] } if @_==1;
215  
216 while(@_)
217 { my $ns = shift;
218 $namespace_file{$ns} = shift;
219 }
220 undef;
221 }
222  
223  
224 sub findSchemaFile($)
225 { my ($thing, $fn) = @_;
226  
227 return (-f $fn ? $fn : undef)
228 if File::Spec->file_name_is_absolute($fn);
229  
230 foreach my $dir (@schema_dirs)
231 { my $full = File::Spec->catfile($dir, $fn);
232 return $full if -f $full;
233 }
234  
235 undef;
236 }
237  
238  
239 1;