/usr/share/perl5/Magpie/ConfigReader/XML.pm is in libmagpie-perl 1.141660-1.
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 | package Magpie::ConfigReader::XML;
#ABSTRACT: Magpie Configuration via XML
$Magpie::ConfigReader::XML::VERSION = '1.141660';
use Moose;
use XML::LibXML;
use Magpie::Util;
use Magpie::Plugin::URITemplate;
use Class::Load;
#use Data::Printer;
sub make_token {
return '__MTOKEN__XMLCONF';
}
sub make_match_token {
return '__MATCH__' . int( rand(100000) );
}
has match_stack => (
traits => ['Array'],
is => 'rw',
isa => 'ArrayRef[ArrayRef]',
default => sub { [] },
handles => {
push_stack => 'push',
},
);
has assets => (
traits => ['Array'],
is => 'rw',
isa => 'ArrayRef[Object]',
default => sub { [] },
handles => {
add_asset => 'push',
},
);
has accept_matrix => (
traits => ['Array'],
is => 'rw',
isa => 'ArrayRef[ArrayRef]',
default => sub { [] },
handles => {
add_variant => 'push',
},
);
sub process {
my $self = shift;
my $xml_file = shift;
my $dom = XML::LibXML->load_xml( location => $xml_file );
my $root = $dom->documentElement;
if ( $root->exists('//accept_matrix')) {
$self->process_accept_matrix( $root->findnodes('//accept_matrix') );
}
if ( $root->exists('//assets')) {
$self->process_assets( $root->findnodes('//assets') );
}
# now process the handler pipeline
foreach my $pipe ($root->findnodes('//pipeline')) {
foreach my $pipe_child ($pipe->childNodes) {
my $pipe_child_name = $pipe_child->localname;
next unless $pipe_child_name; # skip txt nodes here
my ($match_type, $to_match, $input) = (undef, undef, []);
if ( $pipe_child_name eq 'add' ) {
$match_type = 'AUTO';
push @{$input}, process_add( $pipe_child );
}
elsif ( $pipe_child_name eq 'match' ) {
#push @{$input}, $self->process_match( $pipe_child );
$self->process_match( $pipe_child );
next;
}
else {
warn "Unknown child element '$pipe_child_name' in config.\n";
}
$self->push_stack( [$match_type, $to_match, $input, make_token, make_match_token] );
}
}
}
sub process_match {
my $self = shift;
my $node = shift;
my @input = ();
my $match_type = $node->findvalue('@type|./type/text()');
$match_type = uc $match_type;
my $to_match = $node->findvalue('@rule|./rule/text()');
foreach my $child ($node->childNodes) {
my $name = $child->localname;
next unless $name && length $name;
if ($name eq 'add') {
push @input, process_add($child);
}
elsif ($name eq 'match') {
push @input, $self->process_match($child);
}
elsif ($name eq 'reset') {
push @input, '__RESET__';
}
}
if ( $match_type eq 'REGEXP' ) {
$to_match = qr|$to_match|;
}
elsif ($match_type eq 'LITERAL') {
$match_type = 'STRING';
}
elsif ($match_type eq 'ENV') {
$match_type = 'HASH';
$to_match = {};
foreach my $rule ($node->findnodes('./rules/rule')) {
my $key = $rule->findvalue('@key|./key/text()');
my $val = $rule->findvalue('@value|./value/text()');
my $type = $rule->findvalue('@type|./value/@type|./value/type/text()');
if ( $type && $type eq 'regexp' ) {
$val = qr|$val|;
}
next unless $key && $val;
$to_match->{$key} = $val;
}
}
elsif ($match_type eq 'ACCEPT') {
$to_match = $node->findvalue('@variant_name|./variant_name/text()');
}
# NOTE: See comment in Plack::Middleware::Magpie re: this munging.
elsif ($match_type eq 'TEMPLATE') {
$match_type = 'REGEXP';
my $uri_template = $to_match;
# to_match becomes the compiled regexp here
my ($match_re, $names) = Magpie::Plugin::URITemplate::process_template($to_match);
$to_match = $match_re;
my @parameterized = ();
for (my $i = 0; $i < scalar @input; $i++ ) {
next if ref( $input[$i] ) eq 'HASH';
if ($input[$i] =~/__MATCH__/) {
push @parameterized, $input[$i];
next;
}
my $args = {};
if ( ref( $input[$i + 1 ]) eq 'HASH' ) {
$args = $input[$i + 1 ];
}
if (defined $args->{traits}) {
if (ref $args->{traits} eq 'ARRAY') {
push @{$args->{traits}}, '+Magpie::Plugin::URITemplate';
}
else {
my $existing = delete $args->{traits};
$args->{traits} = [$existing, '+Magpie::Plugin::URITemplate'];
}
}
else {
$args->{traits} = ['+Magpie::Plugin::URITemplate'];
}
$args->{uri_template} = $uri_template;
push @parameterized, ($input[$i], $args);
}
@input = @parameterized;
}
my $match_token = make_match_token;
$self->push_stack( [$match_type, $to_match, \@input, make_token, $match_token] );
return $match_token;
}
sub process_add {
my $node = shift;
my $class_name = $node->findvalue('@class|./class/text()');
my $params = {};
if ($node->exists('./parameters')) {
foreach my $param ($node->findnodes('./parameters/*')) {
my ($name, $value) = (undef, undef);
if ($param->localname eq 'parameter' ) {
$name = $param->findvalue('@name|./name/text()');
$value = $param->findvalue('@value|./value/text()');
}
else {
$name = $param->localname;
$value = $param->findvalue('./text()');
}
$value =~ s/^\s+//;
$value =~ s/\s+$//;
if( $name && $value ) {
if (defined $params->{$name}) {
if (ref ($params->{$name}) eq 'ARRAY') {
push @{$params->{$name}}, $value;
}
else {
my $existing = delete $params->{$name};
$params->{$name} = [$existing, $value];
}
}
else {
$params->{$name} = $value;
}
}
}
}
return ($class_name, $params);
}
sub trim_whitespace {
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;
}
sub process_accept_matrix {
my $self = shift;
my $node = shift;
foreach my $variant ($node->findnodes('./variant')) {
my $name = $variant->findvalue('@name|./name/text()');
next unless length $name;
my ($type, $lang, $qs, $encoding, $charset, $length);
if ($variant->exists('@type|./type')) {
$type = $variant->findvalue('@type|./type/text()');
}
if ($variant->exists('@language|./language|@lang|./lang')) {
$lang = $variant->findvalue('@language|./language/text()|@lang|./lang/text()');
}
if ($variant->exists('@qs|./qs')) {
$qs = $variant->findvalue('@qs|./qs/text()');
}
if ($variant->exists('@encoding|./encoding')) {
$encoding = $variant->findvalue('@encoding|./encoding/text()');
}
if ($variant->exists('@charset|./charset')) {
$charset = $variant->findvalue('@charset|./charset/text()');
}
if ($variant->exists('@length|./length')) {
$length = $variant->findvalue('@length|./length/text()');
}
$self->add_variant([$name, $qs, $type, $encoding, $charset, $lang, $length]);
}
}
sub process_assets {
Class::Load::load_class('Bread::Board');
my $self = shift;
my $node = shift;
foreach my $container ($node->findnodes('./container')) {
$self->process_asset_container($container);
}
foreach my $service ($node->findnodes('./service')) {
$self->process_asset_service($service);
}
foreach my $alias ($node->findnodes('./alias')) {
my $name = $alias->findvalue('@name|./name/text()');
my $path = $alias->findvalue('@path|./path/text()');
my $service_alias = Bread::Board::Service::Alias->new(
name => $name,
aliased_from_path => $path,
);
$self->add_asset($service_alias)
}
}
sub process_asset_container {
my ($self, $node, $parent) = @_;
my $name = $node->findvalue('@name|./name/text()') || '';
my $c = Bread::Board::Container->new( name => $name );
if (defined $parent) {
$parent->add_sub_container($c);
}
else {
$self->add_asset($c);
}
foreach my $child ($node->findnodes('./container')) {
$self->process_asset_container($child, $c);
}
foreach my $service ($node->findnodes('./service')) {
$self->process_asset_service($service, $c);
}
foreach my $alias ($node->findnodes('./alias')) {
my $name = $alias->findvalue('@name|./name/text()');
my $path = $alias->findvalue('@path|./path/text()');
my $service_alias = Bread::Board::Service::Alias->new(
name => $name,
aliased_from_path => $path,
);
$c->add_service($service_alias)
}
}
sub process_asset_service {
my ($self, $node, $container) = @_;
my %service_args = (
name => $node->findvalue('@name|./name/text()'),
);
if ($node->exists('@class|./class')) {
$service_args{class} = $node->findvalue('@class|./class/text()');
}
my $injector_type = $node->findvalue('@type|./type/text()');
$injector_type ||= 'literal';
my $injector_subclass;
if ($injector_type eq 'literal') {
$injector_subclass = 'Bread::Board::Literal';
$service_args{value} = $node->findvalue('@value|./value/text()|./text()');
}
elsif ($injector_type eq 'constructor') {
$injector_subclass = 'Bread::Board::ConstructorInjection';
}
elsif ($injector_type eq 'setter') {
$injector_subclass = 'Bread::Board::SetterInjection';
}
elsif ($injector_type eq 'block') {
$injector_subclass = 'Bread::Board::BlockInjection';
my %deps = ();
if ($node->exists('@class|./class')) {
my $dep = $node->findvalue('@class|./class/text()');
$deps{$dep} = 1;
}
foreach my $classnode ($node->findnodes('./requires/class')) {
my $dep = $classnode->findvalue('.');
$deps{$dep} = 1;
}
my $dep_string = join "\n", map { "use $_;" } keys %deps;
my $block = $node->findvalue('./block/text()');
my $pkg_name = random_string();
my $subname = random_string();
my $full_name = $pkg_name . '::' . $subname;
my $pkg = 'package ' . $pkg_name .'; ' . $dep_string . ' sub ' . $subname . '{' . $block . '} 1;';
eval "$pkg";
$service_args{block} = \&$full_name;
}
if ($node->exists('@lifecycle|./lifecycle')) {
$service_args{lifecycle} = $node->findvalue('@lifecycle|./lifecycle');
}
if ($node->exists('./dependencies')) {
my $deps = {};
foreach my $d ($node->findnodes('./dependencies/dependency')) {
my $dep_type = $d->findvalue('@type|./type/text()');
if ($dep_type && $dep_type eq 'literal') {
my $dep_name = $d->findvalue('@name|./name/text()');
my $dep_val = $d->findvalue('@value|./value/text()');
my $dep_key = $d->findvalue('@key|./key/text()') || $dep_name;
$deps->{$dep_key} = Bread::Board::Literal->new( name => $dep_name, value => $dep_val);
}
else {
my $dep_name = $d->findvalue('@name|./name/text()');
my $dep_path = $d->findvalue('@service_path|./service_path/text()');
$deps->{$dep_name} = Bread::Board::Dependency->new( service_path => $dep_path );
}
}
$service_args{dependencies} = $deps;
}
my $s = $injector_subclass->new(%service_args);
if (defined $container) {
$container->add_service($s);
}
else {
$self->add_asset($s);
}
}
sub random_string {
my $length = shift || 10;
my $ret = '';
my @chars = ('a'..'z', 'A'..'Z');
for (0..$length) {
$ret .= $chars[ rand @chars ];
}
return $ret;
}
# SEEALSO: Magpie
1;
__END__
=pod
=head1 NAME
Magpie::ConfigReader::XML - Magpie Configuration via XML
=head1 VERSION
version 1.141660
=head1 AUTHORS
=over 4
=item *
Kip Hampton <kip.hampton@tamarou.com>
=item *
Chris Prather <chris.prather@tamarou.com>
=back
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2011 by Tamarou, LLC.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|