| 1 | ##---------------------------------------------------------------------------- | ||
| 2 | ## HTML Object - ~/lib/HTML/Object/XPath/LocationPath.pm | ||
| 3 | ## Version v0.2.0 | ||
| 4 | ## Copyright(c) 2021 DEGUEST Pte. Ltd. | ||
| 5 | ## Author: Jacques Deguest <jack@deguest.jp> | ||
| 6 | ## Created 2021/12/05 | ||
| 7 | ## Modified 2022/09/18 | ||
| 8 | ## All rights reserved | ||
| 9 | ## | ||
| 10 | ## | ||
| 11 | ## This program is free software; you can redistribute it and/or modify it | ||
| 12 | ## under the same terms as Perl itself. | ||
| 13 | ##---------------------------------------------------------------------------- | ||
| 14 | package HTML::Object::XPath::LocationPath; | ||
| 15 | BEGIN | ||
| 16 | { | ||
| 17 | use strict; | ||
| 18 | use warnings; | ||
| 19 | warnings::register_categories( 'HTML::Object' ); | ||
| 20 | use parent qw( Module::Generic ); | ||
| 21 | use vars qw( $BASE_CLASS $DEBUG $VERSION ); | ||
| 22 | our $BASE_CLASS = 'HTML::Object::XPath'; | ||
| 23 | our $DEBUG = 0; | ||
| 24 | our $VERSION = 'v0.2.0'; | ||
| 25 | }; | ||
| 26 | |||
| 27 | use strict; | ||
| 28 | use warnings; | ||
| 29 | |||
| 30 | sub new | ||
| 31 | { | ||
| 32 | my $this = shift( @_ ); | ||
| 33 | return( bless( [] => ( ref( $this ) || $this ) ) ); | ||
| 34 | } | ||
| 35 | |||
| 36 | sub as_string | ||
| 37 | { | ||
| 38 | my $self = shift( @_ ); | ||
| 39 | my $string = ''; | ||
| 40 | for( my $i = 0; $i < @$self; $i++ ) | ||
| 41 | { | ||
| 42 | $string .= ( $self->[ $i ]->as_string // '' ); | ||
| 43 | $string .= '/' if( $self->[ $i + 1 ] ); | ||
| 44 | } | ||
| 45 | return( $string ); | ||
| 46 | } | ||
| 47 | |||
| 48 | sub as_xml | ||
| 49 | { | ||
| 50 | my $self = shift( @_ ); | ||
| 51 | my $string = "<LocationPath>\n"; | ||
| 52 | |||
| 53 | for (my $i = 0; $i < @$self; $i++ ) | ||
| 54 | { | ||
| 55 | $string .= $self->[ $i ]->as_xml; | ||
| 56 | } | ||
| 57 | $string .= "</LocationPath>\n"; | ||
| 58 | return( $string ); | ||
| 59 | } | ||
| 60 | |||
| 61 | sub evaluate | ||
| 62 | { | ||
| 63 | my $self = shift( @_ ); | ||
| 64 | # context _MUST_ be a single node | ||
| 65 | my $context = shift( @_ ); | ||
| 66 | die( "No context" ) unless( $context ); | ||
| 67 | |||
| 68 | # I _think_ this is how it should work :) | ||
| 69 | my $nodeset = $self->new_nodeset(); | ||
| 70 | $nodeset->push( $context ); | ||
| 71 | |||
| 72 | foreach my $step ( @$self ) | ||
| 73 | { | ||
| 74 | # For each step | ||
| 75 | # evaluate the step with the nodeset | ||
| 76 | my $pos = 1; | ||
| 77 | # die( "Looping !\n" ) if( ref( $step ) eq ref( $self ) ); | ||
| 78 | die( "Looping !\n" ) if( $step eq $self ); | ||
| 79 | $nodeset = $step->evaluate( $nodeset ); | ||
| 80 | } | ||
| 81 | return( $nodeset ); | ||
| 82 | } | ||
| 83 | |||
| 84 | sub new_nodeset { return( shift->_class_for( 'NodeSet' )->new( @_ ) ); } | ||
| 85 | |||
| 86 | sub new_root { return( shift->_class_for( 'Root' )->new( @_ ) ); } | ||
| 87 | |||
| 88 | # sub push { return( CORE::push( @{$_[0]}, @_ ) ); } | ||
| 89 | sub push | ||
| 90 | { | ||
| 91 | my $self = shift( @_ ); | ||
| 92 | if( $self->debug ) | ||
| 93 | { | ||
| 94 | for( @_ ) | ||
| 95 | { | ||
| 96 | if( ref( $_ ) eq ref( $self ) ) | ||
| 97 | { | ||
| 98 | die( "A LocationPath object was added to its own stack!\n" ); | ||
| 99 | } | ||
| 100 | } | ||
| 101 | } | ||
| 102 | return( CORE::push( @$self, @_ ) ); | ||
| 103 | } | ||
| 104 | |||
| 105 | sub set_root | ||
| 106 | { | ||
| 107 | my $self = shift( @_ ); | ||
| 108 | return( unshift( @$self, $self->new_root ) ); | ||
| 109 | } | ||
| 110 | |||
| 111 | sub _class_for | ||
| 112 | { | ||
| 113 | my( $self, $mod ) = @_; | ||
| 114 | eval( "require ${BASE_CLASS}\::${mod};" ); | ||
| 115 | die( $@ ) if( $@ ); | ||
| 116 | # ${"${BASE_CLASS}\::${mod}\::DEBUG"} = $DEBUG; | ||
| 117 | eval( "\$${BASE_CLASS}\::${mod}\::DEBUG = " . ( $DEBUG // 0 ) ); | ||
| 118 | return( "${BASE_CLASS}::${mod}" ); | ||
| 119 | } | ||
| 120 | |||
| 121 | 1; | ||
| 122 | # NOTE: POD | ||
| 123 | __END__ | ||
| 124 | |||
| 125 | =encoding utf-8 | ||
| 126 | |||
| 127 | =head1 NAME | ||
| 128 | |||
| 129 | HTML::Object::XPath::LocationPath - HTML Object XPath Location Path | ||
| 130 | |||
| 131 | =head1 SYNOPSIS | ||
| 132 | |||
| 133 | use HTML::Object::XPath::LocationPath; | ||
| 134 | my $this = HTML::Object::XPath::LocationPath->new || | ||
| 135 | die( HTML::Object::XPath::LocationPath->error, "\n" ); | ||
| 136 | |||
| 137 | =head1 VERSION | ||
| 138 | |||
| 139 | v0.2.0 | ||
| 140 | |||
| 141 | =head1 DESCRIPTION | ||
| 142 | |||
| 143 | This module represents a XML LocationPath. | ||
| 144 | |||
| 145 | =head1 CONSTRUCTOR | ||
| 146 | |||
| 147 | Takes no argument and returns a new array-based object. | ||
| 148 | |||
| 149 | =head1 METHODS | ||
| 150 | |||
| 151 | =head2 as_string | ||
| 152 | |||
| 153 | For each element in it the current object array, this will call C<as_string> and concatenate those strings separated by C</>. It returns the result as a regular perl string. | ||
| 154 | |||
| 155 | =head2 as_xml | ||
| 156 | |||
| 157 | Calls C<as_xml> on each elements in the current object array and returns the concatenated string enclosed in <LocationPath> and </LocationPath> | ||
| 158 | |||
| 159 | =head2 evaluate | ||
| 160 | |||
| 161 | Provided with a L<context|HTML::Object::Element>, and this will call C<evaluate> for each object in the stack. It returns a new L<HTML::Object::XPath::NodeSet> object containing the result with the C<context> initially provided as the first in the node set. | ||
| 162 | |||
| 163 | =head2 new_nodeset | ||
| 164 | |||
| 165 | Returns a new L<HTML::Object::XPath::NodeSet> passing it whatever argument was provided. | ||
| 166 | |||
| 167 | =head2 new_root | ||
| 168 | |||
| 169 | Returns a new L<HTML::Object::XPath::Root> passing it whatever argument was provided. | ||
| 170 | |||
| 171 | =head2 push | ||
| 172 | |||
| 173 | Add the elements provided to the object array. | ||
| 174 | |||
| 175 | =head2 set_root | ||
| 176 | |||
| 177 | Add a new L<root object|HTML::Object::XPath::Root> as the first element of our internal array. | ||
| 178 | |||
| 179 | =head1 AUTHOR | ||
| 180 | |||
| 181 | Jacques Deguest E<lt>F<jack@deguest.jp>E<gt> | ||
| 182 | |||
| 183 | =head1 SEE ALSO | ||
| 184 | |||
| 185 | L<HTML::Object::XPath>, L<HTML::Object::XPath::Boolean>, L<HTML::Object::XPath::Expr>, L<HTML::Object::XPath::Function>, L<HTML::Object::XPath::Literal>, L<HTML::Object::XPath::LocationPath>, L<HTML::Object::XPath::NodeSet>, L<HTML::Object::XPath::Number>, L<HTML::Object::XPath::Root>, L<HTML::Object::XPath::Step>, L<HTML::Object::XPath::Variable> | ||
| 186 | |||
| 187 | =head1 COPYRIGHT & LICENSE | ||
| 188 | |||
| 189 | Copyright(c) 2021 DEGUEST Pte. Ltd. | ||
| 190 | |||
| 191 | All rights reserved | ||
| 192 | |||
| 193 | This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||
| 194 | |||
| 195 | =cut |