| 1 | package XML::LibXML::LazyMatcher; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use XML::LibXML; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | XML::LibXML::LazyMatcher - A simple XML matcher with lazy evaluation. | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =head1 VERSION | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | Version 0.02 | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =cut | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 3 | our $VERSION = '0.02'; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | my $dom = XML::LibXML->load_xml (string => "<root><c1><c2>hello</c2><c3>world</c3></c1></root>"); | ||||||||||||||||||||||||||||
| 24 | my $matcher; | ||||||||||||||||||||||||||||
| 25 | my ($c2content, $c3content); | ||||||||||||||||||||||||||||
| 26 | { | ||||||||||||||||||||||||||||
| 27 | package XML::LibXML::LazyMatcher; | ||||||||||||||||||||||||||||
| 28 | $matcher = M (root => | ||||||||||||||||||||||||||||
| 29 | C (M (c1 => | ||||||||||||||||||||||||||||
| 30 | C (M (c2 => | ||||||||||||||||||||||||||||
| 31 | sub { | ||||||||||||||||||||||||||||
| 32 | $c2content = $_[0]->textContent; | ||||||||||||||||||||||||||||
| 33 | return 1; | ||||||||||||||||||||||||||||
| 34 | }), | ||||||||||||||||||||||||||||
| 35 | M (c3 => | ||||||||||||||||||||||||||||
| 36 | sub { | ||||||||||||||||||||||||||||
| 37 | $c3content = $_[0]->textContent; | ||||||||||||||||||||||||||||
| 38 | return 1; | ||||||||||||||||||||||||||||
| 39 | }))))); | ||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | $matcher->($dom->documentElement); | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | =head1 EXPORT | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | None. | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | =head1 SUBROUTINES/METHODS | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =head2 M (tagname => [sub_matcher, ...]) | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | Returns a matcher function. This returned function takes an | ||||||||||||||||||||||||||||
| 52 | XML::LibXML::Node object as an argument. First, The matcher checks if | ||||||||||||||||||||||||||||
| 53 | the tag name of the passed node is correct, then, applies the node to | ||||||||||||||||||||||||||||
| 54 | all C<sub_matcher>s. If all C<sub_matcher>s return true value then | ||||||||||||||||||||||||||||
| 55 | the C<M()> returns 1. Otherwise returns 0. | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | You can define some action as a sub_matcher. A typical C<sub_matcher> | ||||||||||||||||||||||||||||
| 58 | may be like this: | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | sub { | ||||||||||||||||||||||||||||
| 61 | my $node = shift; # $node should be a XML::LibXML::Node. | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | return 0 unless is_valid($node); | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | do_some_action($node); | ||||||||||||||||||||||||||||
| 66 | return 1; | ||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | =cut | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 9 | ▶ | sub M { | ||||||||||||||||||||||||||
|
Subroutine
M: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 72 | 9 | my $tagname = shift; | |||||||||||||||||||||||||||
| 73 | 9 | my @matchers = @_; | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 17 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.3
| |||||||||||||||||||||||||||||
| 76 | 17 | my $elem = shift; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | # warn "matching $tagname", $elem->nodeName; | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 17 | ▶ | return 0 unless ($elem->nodeName eq $tagname); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | # warn "eating $tagname"; | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 14 | for my $m (@matchers) { | |||||||||||||||||||||||||||
| 85 | 14 | ▶ | if (ref ($m) eq "CODE") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 14 | ▶ | return 0 unless ($m->($elem)); # failure | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | } else { | ||||||||||||||||||||||||||||
| 88 | 0 | die "invalid matcher"; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 10 | return 1; | |||||||||||||||||||||||||||
| 93 | 9 | }; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | =head2 C (sub_matcher, ...) | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | Creates a matcher function which tests all child nodes. If a | ||||||||||||||||||||||||||||
| 99 | sub_matcher returns true value, then the C<C()> returns 1. Otherwise | ||||||||||||||||||||||||||||
| 100 | returns 0. | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =cut | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 5 | ▶ | sub C { | ||||||||||||||||||||||||||
|
Subroutine
C: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 105 | 9 | ▶ | my $alternate = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 106 | 9 | my @children = @_; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 11 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 109 | 11 | my $elem = shift; | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 11 | for my $m (@children) { | |||||||||||||||||||||||||||
| 112 | 12 | ▶ | return 1 if ($m->($elem)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | 4 | return 0; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | 5 | }; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 5 | my @children = @_; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 9 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 121 | 9 | my $parent = shift; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 9 | my $m = $alternate->(@children); | |||||||||||||||||||||||||||
| 124 | 9 | ▶ | for (my $c = $parent->firstChild; $c; $c = $c->nextSibling) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 11 | ▶ | return 0 unless $m->($c); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 5 | return 1; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | 5 | } | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | =head2 S (sub_matcher, ...) | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | Creates a matcher function which test all child nodes sequentially. | ||||||||||||||||||||||||||||
| 135 | Every child nodes is tested by the appropriate C<sub_matcher> | ||||||||||||||||||||||||||||
| 136 | accordingly. The returned matcher fails if one of C<sub_matcher>s | ||||||||||||||||||||||||||||
| 137 | fails. | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | Also, this matcher ignores empty text node for convenience. | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | =cut | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 0 | ▶ | sub S { | ||||||||||||||||||||||||||
|
Subroutine
S: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 144 | 0 | my @children = @_; | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 0 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||
| 147 | 0 | my $parent = shift; | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 0 | ▶ | for (my $c = $parent->firstChild; $c; $c = $c->nextSibling) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | 0 | ▶ | next if ($c->nodeType == 3 && $c->textContent =~ /\s*/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$c->nodeType == 3 and $c->textContent =~ /\s*/
MC/DC0%0/2
$c->nodeType == 3 and $c->textContent =~ /\s*/
$c->nodeType == 3
$c->textContent =~ /\s*/
Truth table0%0/3
A and B
A= $c->nodeType == 3B= $c->textContent =~ /\s*/
| |||||||||||||||||||||||||||||
| 151 | 0 | ▶ | return 0 unless $#children >= 0 && shift (@children)->($c); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$#children >= 0 and shift(@children)->($c)
MC/DC0%0/2
$#children >= 0 and shift(@children)->($c)
$#children >= 0
shift(@children)->($c)
Truth table0%0/3
A and B
A= $#children >= 0B= shift(@children)->($c)
| |||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 0 | ▶ | return 0 if $#children >= 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 0 | return 1; | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | 0 | } | |||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | Toru Hisai, C<< <toru at torus.jp> >> | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | =head1 BUGS | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | Please report any bugs or feature requests to C<bug-xml-libxml-lazymatcher at rt.cpan.org>, or through | ||||||||||||||||||||||||||||
| 167 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=XML-LibXML-LazyMatcher>. I will be notified, and then you'll | ||||||||||||||||||||||||||||
| 168 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | perldoc XML::LibXML::LazyMatcher | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | You can also look for information at: | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | =over 4 | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | =item * RT: CPAN's request tracker | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=XML-LibXML-LazyMatcher> | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | L<http://annocpan.org/dist/XML-LibXML-LazyMatcher> | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | =item * CPAN Ratings | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | L<http://cpanratings.perl.org/d/XML-LibXML-LazyMatcher> | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | =item * Search CPAN | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | L<http://search.cpan.org/dist/XML-LibXML-LazyMatcher/> | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | =back | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | Copyright 2010 Toru Hisai. | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 211 | under the terms of either: the GNU General Public License as published | ||||||||||||||||||||||||||||
| 212 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | See http://dev.perl.org/licenses/ for more information. | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | =cut | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 3 | 1; # End of XML::LibXML::LazyMatcher | |||||||||||||||||||||||||||