| 1 | package WWW::Scraper::Opcode; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use vars qw($VERSION); | ||
| 5 | $VERSION = sprintf("%d.%02d", q$Revision: 1.1 $ =~ /(\d+)\.(\d+)/); | ||
| 6 | |||
| 7 | # Default new() simply assumes each parameter of the operation is a field name, | ||
| 8 | # unless: | ||
| 9 | # 1. it is a reference, | ||
| 10 | # 2. it is a number, (??gdw.2003.03.14??) or | ||
| 11 | # 3. it begins with '#'. | ||
| 12 | |||
| 13 | sub new { | ||
| 14 | my ($cls, $scaffold, $params) = @_; | ||
| 15 | my $self = bless {},$cls; | ||
| 16 | my @scfld = @$scaffold; | ||
| 17 | shift @scfld; | ||
| 18 | my @fields; | ||
| 19 | map { push @fields, $_ unless ref($_) || m{^#} } @scfld; | ||
| 20 | $self->{'fieldsCaptured'} = \@fields; | ||
| 21 | |||
| 22 | return $self; | ||
| 23 | } | ||
| 24 | |||
| 25 | # Base scrape() method performs the following - | ||
| 26 | # 1. getMarkedTextAndAttributes(OpcodeName) | ||
| 27 | # 2. Walking along your Opcode scraper frame paramater . . . | ||
| 28 | # 3. Executes any function you list there. | ||
| 29 | # 4. Put attributes into same named fields in the list if they | ||
| 30 | # appear in your 'CaptureAttributes' array. | ||
| 31 | # 5. Puts the (current) content of the element into any otherwise | ||
| 32 | # named field (by current meaning after previous processing by | ||
| 33 | # any functions you've put in the parameter list.) | ||
| 34 | # | ||
| 35 | sub scrape { | ||
| 36 | my ($self, $scraper, $scaffold, $TidyXML, $hit) = @_; | ||
| 37 | |||
| 38 | my $tag = ref($self); $tag =~ s{^.*::(\w[\w\d]*)$}{$1}; | ||
| 39 | |||
| 40 | my ($sub_string, $attributes) = $TidyXML->getMarkedTextAndAttributes($tag); | ||
| 41 | return undef unless defined($sub_string); | ||
| 42 | my @ary = @$scaffold; | ||
| 43 | shift @ary; | ||
| 44 | for ( @ary ) | ||
| 45 | { | ||
| 46 | if ( ! defined $_ ) { # "if ( $_ eq '' )" reports "use of uninitialized variable" under diagnostics. | ||
| 47 | } | ||
| 48 | elsif ( ref($_) eq 'CODE' ) { | ||
| 49 | $sub_string = &$_($scraper,$hit,$sub_string); | ||
| 50 | } | ||
| 51 | elsif ( $self->isCapturedField($_) ) { | ||
| 52 | $self->plug_elem($_, $attributes->{$_}, $TidyXML); | ||
| 53 | } | ||
| 54 | elsif ( $_ eq 'url' ) | ||
| 55 | { | ||
| 56 | my $url = new URI::URL($sub_string, $scraper->{_base_url}); | ||
| 57 | $url = $url->abs(); | ||
| 58 | $hit->plug_url($url); | ||
| 59 | } | ||
| 60 | else { | ||
| 61 | $hit->plug_elem($_, $sub_string, $TidyXML) if defined $sub_string; | ||
| 62 | } | ||
| 63 | } | ||
| 64 | return ($self->_next_scaffold($scaffold), $sub_string, $attributes); | ||
| 65 | } | ||
| 66 | |||
| 67 | sub isCapturedField { | ||
| 68 | my ($self, $parm) = @_; | ||
| 69 | for ( @{$self->{'CaptureAttributes'}} ) { | ||
| 70 | return 1 if ( uc $parm eq uc $_ ); | ||
| 71 | } | ||
| 72 | return 0; | ||
| 73 | } | ||
| 74 | |||
| 75 | # Replace string Opcodes with their respective objects. | ||
| 76 | sub InitiateScaffold { | ||
| 77 | my ($scaffold) = @_; | ||
| 78 | my @fields = (); | ||
| 79 | my $next_scaffold; | ||
| 80 | SCAFFOLD: for my $scaffold ( @$scaffold ) { | ||
| 81 | |||
| 82 | my $tag = $$scaffold[0]; | ||
| 83 | if ( $tag =~ m/^(HIT|HIT\*|HTML|TidyXML|TRX|DL|FOR|DATA|DL|DT|DD|DIV|SPAN|RESIDUE|TAG|AN|AQ|F|SNIP|CALLBACK|XPath|COUNT|TRACE)$/ ) | ||
| 84 | { | ||
| 85 | my @scfld = @$scaffold; | ||
| 86 | $next_scaffold = $scfld[$#scfld] if ref($scfld[$#scfld]) eq 'ARRAY'; | ||
| 87 | } else | ||
| 88 | { | ||
| 89 | my @fieldsCaptured; | ||
| 90 | my ($op,$params,$opmod); | ||
| 91 | if ( ref($tag) ) { | ||
| 92 | $op = $tag; | ||
| 93 | } | ||
| 94 | else { | ||
| 95 | ($op,$params) = ($tag =~ m{^(\w+)(?:\((.*)\))?$}); | ||
| 96 | my @params = split /\s*,\s*/, $params if $params; | ||
| 97 | eval "\$opmod = new WWW::Scraper::Opcode::$op(\$scaffold, \\\@params)"; | ||
| 98 | unless ( ref($opmod) ) { | ||
| 99 | eval "require WWW::Scraper::Opcode::$op; \$opmod = new WWW::Scraper::Opcode::$op(\$scaffold, \\\@params)"; | ||
| 100 | } | ||
| 101 | die "WWW::Scraper::Response - - no $tag Scraper opcode class: $@" if $@ || !ref($opmod); | ||
| 102 | $$scaffold[0] = $opmod; | ||
| 103 | } | ||
| 104 | push @fields, @{$opmod->{'fieldsCaptured'}} if $opmod->{'fieldsCaptured'}; | ||
| 105 | my @scfld = @$scaffold; | ||
| 106 | $next_scaffold = $scfld[$#scfld] if ref($scfld[$#scfld]) eq 'ARRAY'; | ||
| 107 | } | ||
| 108 | push @fields, InitiateScaffold($next_scaffold) if $next_scaffold; | ||
| 109 | } | ||
| 110 | return @fields; | ||
| 111 | } | ||
| 112 | |||
| 113 | # Calculate the next scaffold element of the give scaffold. | ||
| 114 | sub _next_scaffold { | ||
| 115 | my $scaffold = $_[1]; | ||
| 116 | return $scaffold->[$#$scaffold] if ref($scaffold->[$#$scaffold]) eq 'ARRAY'; | ||
| 117 | return undef; | ||
| 118 | } | ||
| 119 | 1; | ||
| 120 | |||
| 121 | __END__ |