/usr/share/perl5/XML/Compile/Schema.pm is in libxml-compile-perl 1.58-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 | # Copyrights 2006-2017 by [Mark Overmeer].
# For other contributors see ChangeLog.
# See the manual pages for details on the licensing terms.
# Pod stripped from pm file by OODoc 2.02.
package XML::Compile::Schema;
use vars '$VERSION';
$VERSION = '1.58';
use base 'XML::Compile';
use warnings;
use strict;
use Log::Report 'xml-compile', syntax => 'SHORT';
use List::Util qw/first/;
use XML::LibXML ();
use File::Spec ();
use File::Basename qw/basename/;
use Digest::MD5 qw/md5_hex/;
use XML::Compile::Schema::Specs;
use XML::Compile::Schema::Instance;
use XML::Compile::Schema::NameSpaces;
use XML::Compile::Util qw/SCHEMA2001 SCHEMA2001i unpack_type/;
use XML::Compile::Translate ();
sub init($)
{ my ($self, $args) = @_;
$self->{namespaces} = XML::Compile::Schema::NameSpaces->new;
$self->SUPER::init($args);
$self->importDefinitions($args->{top}, %$args)
if $args->{top};
$self->{hooks} = [];
if(my $h1 = $args->{hook})
{ $self->addHook(ref $h1 eq 'ARRAY' ? @$h1 : $h1);
}
if(my $h2 = $args->{hooks})
{ $self->addHook($_) for ref $h2 eq 'ARRAY' ? @$h2 : $h2;
}
$self->{key_rewrite} = [];
if(my $kr = $args->{key_rewrite})
{ $self->addKeyRewrite(ref $kr eq 'ARRAY' ? @$kr : $kr);
}
$self->{block_nss} = [];
$self->blockNamespace($args->{block_namespace});
$self->{typemap} = $args->{typemap} || {};
$self->{unused_tags} = $args->{ignore_unused_tags};
$self;
}
#--------------------------------------
sub addHook(@)
{ my $self = shift;
push @{$self->{hooks}}, @_>1 ? {@_} : defined $_[0] ? shift : ();
$self;
}
sub addHooks(@)
{ my $self = shift;
$self->addHook($_) for @_;
$self;
}
sub hooks(;$)
{ my $hooks = shift->{hooks};
my $dir = shift or return @$hooks;
grep +(!$_->{action} || $_->{action} eq $dir), @$hooks;
}
sub addTypemaps(@)
{ my $map = shift->{typemap};
while(@_ > 1)
{ my $k = shift;
$map->{$k} = shift;
}
$map;
}
*addTypemap = \&addTypemaps;
sub addSchemas($@)
{ my ($self, $node, %opts) = @_;
defined $node or return ();
my @nsopts;
foreach my $o (qw/source filename target_namespace
element_form_default attribute_form_default/)
{ push @nsopts, $o => delete $opts{$o} if exists $opts{$o};
}
UNIVERSAL::isa($node, __PACKAGE__)
and error __x"use useSchema(), not addSchemas() for a {got} object"
, got => ref $node;
UNIVERSAL::isa($node, 'XML::LibXML::Node')
or error __x"addSchema() requires an XML::LibXML::Node";
$node = $node->documentElement
if $node->isa('XML::LibXML::Document');
my $nss = $self->namespaces;
my @schemas;
$self->walkTree
( $node,
sub { my $this = shift;
return 1 unless $this->isa('XML::LibXML::Element')
&& $this->localName eq 'schema';
my $schema = XML::Compile::Schema::Instance->new($this, @nsopts)
or next;
$nss->add($schema);
push @schemas, $schema;
return 0;
}
);
@schemas;
}
sub useSchema(@)
{ my $self = shift;
foreach my $schema (@_)
{ error __x"useSchema() accepts only {pkg} extensions, not {got}"
, pkg => __PACKAGE__, got => (ref $schema || $schema);
$self->namespaces->use($schema);
}
$self;
}
sub addKeyRewrite(@)
{ my $self = shift;
unshift @{$self->{key_rewrite}}, @_;
defined wantarray ? $self->_key_rewrite(undef) : ();
}
sub _key_rewrite($)
{ my $self = shift;
my @more = map { ref $_ eq 'ARRAY' ? @$_ : defined $_ ? $_ : () } @_;
my ($pref_all, %pref, @other);
foreach my $rule (@more, @{$self->{key_rewrite}})
{ if($rule eq 'PREFIXED') { $pref_all++ }
elsif($rule =~ m/^PREFIXED\((.*)\)/) { $pref{$_}++ for split /\,/, $1 }
else { push @other, $rule }
}
( ( $pref_all ? 'PREFIXED'
: keys %pref ? 'PREFIXED('.join(',', sort keys %pref).')'
: ()), @other );
}
sub blockNamespace(@)
{ my $self = shift;
push @{$self->{block_nss}}, @_;
}
sub _block_nss(@)
{ my $self = shift;
grep defined, map {ref $_ eq 'ARRAY' ? @$_ : $_}
@_, @{$self->{block_nss}};
}
#--------------------------------------
sub compile($$@)
{ my ($self, $action, $type, %args) = @_;
defined $type or return ();
if(exists $args{validation})
{ $args{check_values} = $args{validation};
$args{check_occurs} = $args{validation};
$args{ignore_facets} = ! $args{validation};
}
else
{ exists $args{check_values} or $args{check_values} = 1;
exists $args{check_occurs} or $args{check_occurs} = 1;
}
my $iut = exists $args{ignore_unused_tags}
? $args{ignore_unused_tags} : $self->{unused_tags};
$args{ignore_unused_tags}
= !defined $iut ? undef : ref $iut eq 'Regexp' ? $iut : qr/^/;
exists $args{include_namespaces}
or $args{include_namespaces} = 1;
if($args{sloppy_integers} ||= 0)
{ eval "require Math::BigInt";
panic "require Math::BigInt or sloppy_integers:\n$@"
if $@;
}
if($args{sloppy_floats} ||= 0)
{ eval "require Math::BigFloat";
panic "require Math::BigFloat by sloppy_floats:\n$@" if $@;
}
if($args{json_friendly} ||= 0)
{ eval "require Types::Serialiser";
panic "require Types::Serialiser by json_friendly:\n$@" if $@;
}
$args{prefixes} = $self->_namespaceTable
(($args{prefixes} || $args{output_namespaces})
, $args{namespace_reset}
, !($args{use_default_namespace} || $args{use_default_prefix})
# use_default_prefix renamed in 0.90
);
my $nss = $self->namespaces;
my ($h1, $h2) = (delete $args{hook}, delete $args{hooks});
my @hooks = $self->hooks($action);
push @hooks, ref $h1 eq 'ARRAY' ? @$h1 : $h1 if $h1;
push @hooks, ref $h2 eq 'ARRAY' ? @$h2 : $h2 if $h2;
my %map = ( %{$self->{typemap}}, %{$args{typemap} || {}} );
trace "schema compile $action for $type";
my @rewrite = $self->_key_rewrite(delete $args{key_rewrite});
my @blocked = $self->_block_nss(delete $args{block_namespace});
$args{abstract_types} ||= 'ERROR';
$args{mixed_elements} ||= 'ATTRIBUTES';
$args{default_values} ||= $action eq 'READER' ? 'EXTEND' : 'IGNORE';
# Option rename in 0.88
$args{any_element} ||= delete $args{anyElement};
$args{any_attribute} ||= delete $args{anyAttribute};
if(my $xi = $args{xsi_type})
{ my $nss = $self->namespaces;
foreach (keys %$xi)
{ $xi->{$_} = $nss->autoexpand_xsi_type($_) if $xi->{$_} eq 'AUTO';
}
}
my $transl = XML::Compile::Translate->new
( $action
, nss => $self->namespaces
);
$transl->compile
( $type, %args
, hooks => \@hooks
, typemap => \%map
, rewrite => \@rewrite
, block_namespace => \@blocked
);
}
# also used in ::Cache init()
sub _namespaceTable($;$$)
{ my ($self, $table, $reset_count, $block_default) = @_;
$table = { reverse @$table }
if ref $table eq 'ARRAY';
$table->{$_} = { uri => $_, prefix => $table->{$_} }
for grep ref $table->{$_} ne 'HASH', keys %$table;
if($reset_count)
{ $_->{used} = 0 for values %$table;
}
$table->{''} = {uri => '', prefix => '', used => 0}
if $block_default && !grep $_->{prefix} eq '', values %$table;
# very strong preference for 'xsi'
$table->{&SCHEMA2001i} = {uri => SCHEMA2001i, prefix => 'xsi', used => 0};
$table;
}
sub compileType($$@)
{ my ($self, $action, $type, %args) = @_;
# translator can only create elements, not types.
my $elem = delete $args{element} || $type;
my ($ens, $elocal) = unpack_type $elem;
my ($ns, $local) = unpack_type $type;
my $SchemaNS = SCHEMA2001;
my $defs = $ns ? <<_DIRTY_TRICK1 : <<_DIRTY_TRICK2;
<schema xmlns="$SchemaNS"
targetNamespace="$ens"
xmlns:tns="$ns">
<element name="$elocal" type="tns:$local" />
</schema>
_DIRTY_TRICK1
<schema xmlns="$SchemaNS"
targetNamespace="$ens"
elementFormDefault="unqualified"
>
<element name="$elocal" type="$local" />
</schema>
_DIRTY_TRICK2
$self->importDefinitions($defs);
$self->compile($action, $elem, %args);
}
sub template($@)
{ my ($self, $action, $type, %args) = @_;
my ($to_perl, $to_xml)
= $action eq 'PERL' ? (1, 0)
: $action eq 'XML' ? (0, 1)
: $action eq 'TREE' ? (0, 0)
: error __x"template output is either in XML or PERL layout, not '{action}'"
, action => $action;
my $show
= exists $args{show_comments} ? $args{show_comments}
: exists $args{show} ? $args{show} # pre-0.79 option name
: 'ALL';
$show = 'struct,type,occur,facets' if $show eq 'ALL';
$show = '' if $show eq 'NONE';
my %show = map {("show_$_" => 1)} split m/\,/, $show;
my $nss = $self->namespaces;
my $indent = $args{indent} || " ";
$args{check_occurs} = 1;
$args{mixed_elements} ||= 'ATTRIBUTES';
$args{default_values} ||= 'EXTEND';
$args{abstract_types} ||= 'ERROR';
exists $args{include_namespaces}
or $args{include_namespaces} = 1;
# it could be used to add extra comment lines
error __x"typemaps not implemented for XML template examples"
if $to_xml && defined $args{typemap} && keys %{$args{typemap}};
my @rewrite = $self->_key_rewrite(delete $args{key_rewrite});
my @blocked = $self->_block_nss(delete $args{block_namespace});
my $table = $args{prefixes} = $self->_namespaceTable
(($args{prefixes} || $args{output_namespaces})
, $args{namespace_reset}
, !$args{use_default_namespace}
);
my $used = $to_xml && $show{show_type};
$table->{&SCHEMA2001}
||= +{prefix => 'xs', uri => SCHEMA2001, used => $used};
$table->{&SCHEMA2001i}
||= +{prefix => 'xsi', uri => SCHEMA2001i, used => $used};
my $transl = XML::Compile::Translate->new
( 'TEMPLATE'
, nss => $self->namespaces
);
my $compiled = $transl->compile
( $type
, %args
, rewrite => \@rewrite
, block_namespace => \@blocked # not yet supported
, output => $action
);
$compiled or return;
my $ast = $compiled->();
#use Data::Dumper; $Data::Dumper::Indent = 1; warn Dumper $ast;
if($to_perl)
{ return $transl->toPerl($ast, %show, indent => $indent
, skip_header => $args{skip_header})
}
if($to_xml)
{ my $doc = XML::LibXML::Document->new('1.1', 'UTF-8');
my $node = $transl->toXML($doc, $ast, %show
, indent => $indent, skip_header => $args{skip_header});
return $node->toString(1);
}
# return tree
$ast;
}
#------------------------------------------
sub namespaces() { shift->{namespaces} }
# The cache will certainly avoid penalties by the average module user,
# which does not understand the sharing schema definitions between objects
# especially in SOAP implementations.
my (%schemaByFilestamp, %schemaByChecksum);
sub importDefinitions($@)
{ my ($self, $frags, %options) = @_;
my @data = ref $frags eq 'ARRAY' ? @$frags : $frags;
# this is a horrible hack, but by far the simpelest solution to
# avoid dataToXML process the same info twice.
local $self->{_use_cache} = 1;
my @schemas;
foreach my $data (@data)
{ defined $data or next;
my ($xml, %details) = $self->dataToXML($data);
%details = %{delete $options{details}} if $options{details};
if(defined $xml)
{ my @added = $self->addSchemas($xml, %details, %options);
if(my $checksum = $details{checksum})
{ $self->{_cache_checksum}{$checksum} = \@added;
}
elsif(my $filestamp = $details{filestamp})
{ $self->{_cache_file}{$filestamp} = \@added;
}
push @schemas, @added;
}
elsif(my $filestamp = $details{filestamp})
{ my $cached = $self->{_cache_file}{$filestamp};
$self->namespaces->add(@$cached);
}
elsif(my $checksum = $details{checksum})
{ my $cached = $self->{_cache_checksum}{$checksum};
$self->namespaces->add(@$cached);
}
}
@schemas;
}
sub _parseScalar($)
{ my ($thing, $data) = @_;
ref $thing && $thing->{_use_cache}
or return $thing->SUPER::_parseScalar($data);
my $self = $thing;
my $checksum = md5_hex $$data;
if($self->{_cache_checksum}{$checksum})
{ trace "reusing string data with checksum $checksum";
return (undef, checksum => $checksum);
}
trace "cache parsed scalar with checksum $checksum";
( $self->SUPER::_parseScalar($data)
, checksum => $checksum
);
}
sub _parseFile($)
{ my ($thing, $fn) = @_;
ref $thing && $thing->{_use_cache}
or return $thing->SUPER::_parseFile($fn);
my $self = $thing;
my ($mtime, $size) = (stat $fn)[9,7];
my $filestamp = File::Spec->rel2abs($fn) . '-'. $mtime . '-' . $size;
if($self->{_cache_file}{$filestamp})
{ trace "reusing schemas from file $filestamp";
return (undef, filestamp => $filestamp);
}
trace "cache parsed file $filestamp";
( $self->SUPER::_parseFile($fn)
, filestamp => $filestamp
);
}
sub types()
{ my $nss = shift->namespaces;
sort map {$_->types}
map {$nss->schemas($_)}
$nss->list;
}
sub elements()
{ my $nss = shift->namespaces;
sort map {$_->elements}
map {$nss->schemas($_)}
$nss->list;
}
sub printIndex(@)
{ my $self = shift;
$self->namespaces->printIndex(@_);
}
sub doesExtend($$)
{ my $self = shift;
$self->namespaces->doesExtend(@_);
}
1;
|