| 1 | package Cikl::Smrt::Parsers::ParseXPath; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Mouse; | ||
| 7 | use Cikl::Smrt::Parser; | ||
| 8 | extends 'Cikl::Smrt::Parser'; | ||
| 9 | use namespace::autoclean; | ||
| 10 | use Cikl::Smrt::ParserHelpers::XPathMapping; | ||
| 11 | use Cikl::Smrt::ParserHelpers::XPathRegexMapping; | ||
| 12 | |||
| 13 | use XML::LibXML::Reader; | ||
| 14 | |||
| 15 | use constant NAME => 'xpath'; | ||
| 16 | sub name { return NAME; } | ||
| 17 | |||
| 18 | has 'node_xpath' => ( | ||
| 19 | is => 'ro', | ||
| 20 | isa => 'Str', | ||
| 21 | required => 1 | ||
| 22 | ); | ||
| 23 | |||
| 24 | has 'xpath_map' => ( | ||
| 25 | is => 'ro', | ||
| 26 | isa => 'ArrayRef[Cikl::Smrt::ParserHelpers::XPathMapping]', | ||
| 27 | required => 0 | ||
| 28 | ); | ||
| 29 | |||
| 30 | has 'xpathregex_map' => ( | ||
| 31 | is => 'ro', | ||
| 32 | isa => 'ArrayRef[Cikl::Smrt::ParserHelpers::XPathRegexMapping]', | ||
| 33 | required => 0 | ||
| 34 | ); | ||
| 35 | |||
| 36 | around BUILDARGS => sub { | ||
| 37 | my $orig_method = shift; | ||
| 38 | my $class = shift; | ||
| 39 | my %args = @_; | ||
| 40 | |||
| 41 | if (!exists($args{xpath_map})) { | ||
| 42 | my @xpath_map; | ||
| 43 | foreach my $key (keys %args) { | ||
| 44 | if ($key =~ /^xpath\d+$/) { | ||
| 45 | my ($xpath, $event_field) = @{$args{$key}}; | ||
| 46 | my $m = Cikl::Smrt::ParserHelpers::XPathMapping->new( | ||
| 47 | event_field => $event_field, | ||
| 48 | xpath => $xpath | ||
| 49 | ); | ||
| 50 | push(@xpath_map, $m); | ||
| 51 | } | ||
| 52 | } | ||
| 53 | |||
| 54 | $args{xpath_map} = \@xpath_map; | ||
| 55 | } | ||
| 56 | |||
| 57 | if (!exists($args{xpathregex_map})) { | ||
| 58 | my @xpathregex_map; | ||
| 59 | foreach my $key (keys %args) { | ||
| 60 | if ($key =~ /^xpathregex\d+$/) { | ||
| 61 | my ($xpath, $regex, @event_fields) = @{$args{$key}}; | ||
| 62 | my $m = Cikl::Smrt::ParserHelpers::XPathRegexMapping->new( | ||
| 63 | xpath => $xpath, | ||
| 64 | regex => qr/$regex/, | ||
| 65 | event_fields => \@event_fields, | ||
| 66 | ); | ||
| 67 | push(@xpathregex_map, $m); | ||
| 68 | } | ||
| 69 | } | ||
| 70 | $args{xpathregex_map} = \@xpathregex_map; | ||
| 71 | } | ||
| 72 | |||
| 73 | return $class->$orig_method(%args); | ||
| 74 | }; | ||
| 75 | |||
| 76 | sub BUILD { | ||
| 77 | my $self = shift; | ||
| 78 | |||
| 79 | if ( $#{$self->xpath_map} == -1 && $#{$self->xpathregex_map} == -1) { | ||
| 80 | die 'at least one XPathMapping or XPathRegexMapping is required!'; | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | sub parse { | ||
| 85 | my $self = shift; | ||
| 86 | my $fh = shift; | ||
| 87 | my $broker = shift; | ||
| 88 | |||
| 89 | my $reader = XML::LibXML::Reader->new(IO => $fh); | ||
| 90 | my $pattern = XML::LibXML::Pattern->new($self->node_xpath); | ||
| 91 | while ($reader->read()) { | ||
| 92 | next unless ($reader->nodeType == XML_READER_TYPE_ELEMENT && | ||
| 93 | $reader->matchesPattern($pattern)); | ||
| 94 | |||
| 95 | my $node = $reader->copyCurrentNode(1); | ||
| 96 | |||
| 97 | my $h = {}; | ||
| 98 | foreach my $x (@{$self->xpath_map}) { | ||
| 99 | my $value = $node->findvalue($x->xpath); | ||
| 100 | goto SKIPIT if (!defined($value)); | ||
| 101 | $h->{$x->event_field} = $value; | ||
| 102 | } | ||
| 103 | |||
| 104 | foreach my $x (@{$self->xpathregex_map}) { | ||
| 105 | my $value = $node->findvalue($x->xpath); | ||
| 106 | goto SKIPIT if (!defined($value)); | ||
| 107 | |||
| 108 | my @matches = ($value =~ $x->regex); | ||
| 109 | goto SKIPIT if ($#matches == -1); | ||
| 110 | |||
| 111 | my $i = 0; | ||
| 112 | foreach my $field_name (@{$x->event_fields}) { | ||
| 113 | $h->{$field_name} = $matches[$i]; | ||
| 114 | $i++; | ||
| 115 | } | ||
| 116 | |||
| 117 | } | ||
| 118 | $broker->emit($h); | ||
| 119 | SKIPIT: | ||
| 120 | } | ||
| 121 | return(undef); | ||
| 122 | } | ||
| 123 | |||
| 124 | __PACKAGE__->meta->make_immutable; | ||
| 125 | |||
| 126 | 1; | ||
| 127 |