Skip to content

Commit

Permalink
Get basic XML config reader working. Still need to integrate with exi…
Browse files Browse the repository at this point in the history
…sting machine() and pipeline().
  • Loading branch information
ubu committed Jul 24, 2011
1 parent 418ff08 commit 04ce507
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 55 deletions.
32 changes: 27 additions & 5 deletions temp/lib/Magpie/Config/XML.pm → lib/Magpie/ConfigReader/XML.pm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Magpie::Config::XML;
package Magpie::ConfigReader::XML;
use Moose;
use XML::LibXML;
use Data::Dumper::Concise;
Expand All @@ -14,21 +14,41 @@ sub process {
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' ) {
process_add( $pipe_child );
$match_type = 'AUTO';
push @{$input}, process_add( $pipe_child );
}
elsif ( $pipe_child_name eq 'match' ) {
warn Dumper( $pipe_child );
($match_type, $to_match, $input) = process_match( $pipe_child );
}
else {
warn "Unknown child element '$pipe_child_name' in config.\n";
}
push @stack, [$match_type, $to_match, $input, '####'];
}
}

return @stack;
}

sub process_match {
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()');
if ( $match_type eq 'REGEXP' ) {
$to_match = qr|$to_match|;
}
elsif ($match_type eq 'LITERAL' ) {
$match_type = 'STRING';
}
foreach my $add ($node->findnodes('./add')) {
push @{$input}, process_add( $add );
}
return ($match_type, $to_match, $input);
}

sub process_add {
my $node = shift;
my $class_name = $node->findvalue('@class|./class/text()');
Expand Down Expand Up @@ -61,7 +81,9 @@ sub process_add {

sub trim_whitespace {
my $string = shift;

$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;
}

1;
15 changes: 13 additions & 2 deletions lib/Plack/Middleware/Magpie.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package Plack::Middleware::Magpie;
use strict;
use warnings;
use parent qw( Exporter Plack::Middleware);
use Plack::Util::Accessor qw(pipeline resource assets context);
use Plack::Util::Accessor qw(pipeline resource assets context conf);
our @EXPORT = qw( machine match match_env );
use Scalar::Util qw(reftype);
use Magpie::Machine;
use HTTP::Throwable::Factory;
use Magpie::ConfigReader::XML;
use Data::Dumper::Concise;

my @STACK = ();
Expand Down Expand Up @@ -81,6 +82,9 @@ sub build_machine {
}
push @{$out->{$token}}, @{$frame->[2]} if $matched == scalar keys %{$rules};
}
elsif ($match_type eq 'AUTO') {
push @{$out->{$token}}, @{$frame->[2]};
}
else {
warn "I don't know how to match '$match_type', skipping.\n"
}
Expand All @@ -94,6 +98,13 @@ sub call {

my @resource_handlers = ();
my $req = Plack::Request->new($env);

my $conf_file = $self->conf;
if ($conf_file) {
my $reader = Magpie::ConfigReader::XML->new;
@STACK = $reader->process($conf_file);
}

my $machine_map = build_machine($req) || {};
my $pipeline = $self->pipeline || [];
my @tokens = keys( %{$machine_map} );
Expand All @@ -111,7 +122,7 @@ sub call {
$pipeline = \@temp;
}

#warn "pipe " . Dumper( $pipeline );
warn "pipe " . Dumper( $pipeline );

my $m = Magpie::Machine->new(
plack_request => $req,
Expand Down
37 changes: 37 additions & 0 deletions t/core/xml_conf.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use strict;
use warnings;
use Test::More;
use FindBin;
use lib "$FindBin::Bin/../lib";

use Plack::Test;
use Plack::Builder;
use HTTP::Request::Common;
use Plack::Middleware::Magpie;

my $context = {
is => 'everything',
actually => 'matters',
is_frequently => [qw(ignored misunderstood)],
};

my $handler = builder {
enable "Magpie", context => $context, conf => 't/data/simple_conf.xml'
};

test_psgi
app => $handler,
client => sub {
my $cb = shift;
{
my $res = $cb->(GET "http://localhost/stooges");
like $res->content, qr/_moebaz__moebar__curlyfoo_RIGHT_actually__is__is_frequently__larryfoo__larrybar_/;
}
{
my $res = $cb->(GET "http://localhost/");
like $res->content, qr/_moebaz__moebar__curlyfoo_RIGHT\b/;
}

};

done_testing();
File renamed without changes.
48 changes: 0 additions & 48 deletions temp/xml_conf.t

This file was deleted.

0 comments on commit 04ce507

Please sign in to comment.