blib/lib/ETR/XML/SAX/FilterHandler.pm

filter: Statementstmt 18.5% 10 / 54 Branchbran 0.0% 0 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 27.2% 3 / 11 Podpod 12.5% 1 / 8 totaltotal 15.0% 14 / 93 SCAR 48.1
CC
13
Cov
14%
end_element
30.0
characters
20.0
start_element
12.0
CRAP 122.3
↑ Summary
1 package ETR::XML::SAX::FilterHandler;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 ETR::XML::SAX::FilterHandler - A handler to filter large XML files or streams
10  
11 =head1 VERSION
12  
13 Version 0.01
14  
15 =cut
16  
17 1 our $VERSION = '0.01';
18  
19  
20 =head1 SYNOPSIS
21  
22 use ETR::XML::SAX::FilterHandler;
23  
24 use XML::SAX::ParserFactory;
25  
26 my $hnd = ETR::XML::SAX::FilterHandler->new({
27  
28 root => books,
29 record => {
30 entry => 1,
31 other_entry => 1
32 },
33 find_data => {
34 "Title 1" => 1,
35 "Title 3" => 1,
36 Oth => 1
37 }
38 });
39  
40 my $str=<<EOS;
41  
42 <books>
43  
44 <entry>
45 <title>Title 1</title>
46 </entry>
47 <other_entry>
48 <title>Other title</title>
49 </other_entry>
50 <entry>
51 <title>Title 2</title>
52 </entry>
53 <entry>
54 <title>Title 3</title>
55 </entry>
56 </books>
57 EOS
58  
59  
60 my $factory = XML::SAX::ParserFactory->parser(Handler => $hnd);
61  
62 # XML source:
63  
64 # from string
65  
66 print "\nfrom string:\n======\n";
67  
68 $factory->parse_string($str);
69  
70 # from file
71  
72 print "\nfrom file:\n======\n";
73  
74 $factory->parse_file("books.xml");
75  
76 # from standard input
77  
78 print "\nfrom standard input:\n======\n";
79  
80 $factory->parse_file(*STDIN);
81  
82 Every time you should receive the following result:
83  
84 <books>
85  
86 <entry>
87  
88 <title>Title 1</title>
89  
90 </entry>
91  
92 <other_entry>
93  
94 <title>Other title</title>
95  
96 </other_entry>
97  
98 <entry>
99  
100 <title>Title 3</title>
101  
102 </entry>
103  
104 </books>
105  
106  
107 =head1 DESCRIPTION
108  
109 Parse large XML files or streams without being load into memory and print
110 to the standard output only the fragments that match to the filtering rules
111 specified by the two parameters: record and find_data.
112  
113 =head1 METHODS
114  
115 =head2
116 new({
117 root => document_root,
118 record => {
119 entry1 => 1,
120 entry2 => 1,
121 ...
122 entrym => 1
123 }
124 find_data => {
125 str1 => 1,
126 str2 => 1,
127 ...
128 strn => 1
129 }
130 })
131  
132 =head3
133 Parameters:
134  
135 root: a string used to enclose the whole document;
136  
137 record: is a hash with element names considered as being record
138 delimiters;
139  
140 find_data: the record is printed out if at least one string from this hash
141 matches to the xml data.
142  
143 Note: The values from the two hashes have to be set to 1, just to force key to be
144 defined.
145  
146 =cut
147  
148 sub new {
149 0 ▶ my ($type, $arg) = @_;
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
150 #print Dumper($arg->{tag}{entry});
151 #print "...filter\n";
152 return bless {
153 buf => "",
154 open => 0,
155 level => 0,
156 found => 0,
157 isdata => 0,
158 indent => " ",
159 record => $arg->{record},
160 find_data => $arg->{find_data},
161 root => $arg->{root}
162 0 }, $type;
163 }
164  
165 sub start_document{
166 0 ▶ my $self = shift;
Subroutine start_document: not called CC=2 SCAR=17.9
Pod undocumented
167 0 ▶ print "<$self->{root}>\n" if defined $self->{root};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"root"} 0 0 0
168 }
169  
170 sub end_document{
171 0 ▶ my $self = shift;
Subroutine end_document: not called CC=2 SCAR=17.9
Pod undocumented
172 #print "End doc\n";
173 0 ▶ print "</$self->{root}>\n" if defined $self->{root};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"root"} 0 0 0
174 }
175  
176 sub start_element{
177 0 ▶ my ($self, $el) = @_;
Subroutine start_element: not called CC=3 SCAR=24.8
Pod undocumented
178 #print "Starting el $el->{Name} \n";
179 #print "...record","\n" if defined $self->{record}{$el->{Name}};
180 0 ▶ if (defined $self->{record}{$el->{Name}}){
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->{'record'}{$el->{'Name'}}) { } 0 0 0
elsif ($self->{'open'}) { } 0 0 0
181 0 $self->{buf} = "<$el->{Name}";
182 #print Dumper $el->{Attributes};
183 0 $self->add_attr($el->{Attributes});
184 0 $self->{open} = 1;
185 0 $self->{level} = 0;
186 0 $self->{found} = 0;
187 0 $self->{isdata} = 0;
188 }
189 elsif($self->{open}){
190 0 $self->{level}++;
191 #chomp($el->{Name});
192 #print $el->{Name},"\n";
193 #print $self->{level},"\n";
194 0 $self->indent;
195 0 $self->{buf} .= "<$el->{Name}" ;
196 0 $self->add_attr($el->{Attributes});
197 }
198 }
199  
200 sub end_element{
201 0 ▶ my ($self, $el) = @_;
Subroutine end_element: not called CC=5 SCAR=34.0
Pod undocumented
202 #print "Ending element $el->{Name}\n";
203 0 ▶ if (defined $self->{record}{$el->{Name}}){
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->{'record'}{$el->{'Name'}}) { } 0 0 0
elsif ($self->{'open'}) { } 0 0 0
204 0 $self->{buf} .= "</$el->{Name}>\n";
205 0 $self->{open} = 0;
206 #$self->{buf} =~ s/^\s*\n//mg;
207 0 ▶ print $self->{buf} if $self->{found};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"found"} 0 0 0
208 }
209 elsif ($self->{open}){
210 0 ▶ if($self->{isdata}){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'isdata'}) { } 0 0 0
211 0 $self->{isdata} = 0;
212 }
213 else{
214 0 $self->{level}--;
215 0 $self->indent;
216 }
217 0 $self->{level}--;
218 0 $self->{buf} .= "</$el->{Name}>\n";
219 }
220 }
221  
222 sub characters{
223 0 ▶ my($self, $char) = @_;
Subroutine characters: not called CC=4 SCAR=30.0
Pod undocumented
224 0 chomp($char->{Data});
225 0 ▶ if($char->{Data} ne ""){
Branch: 1 branch
BranchTrueFalseTotal
if ($char->{"Data"} ne "") 0 0 0
226 0 chomp($self->{buf});
227 0 $self->{isdata} = 1;
228 }
229  
230 0 $self->{buf} .= $char->{Data};
231 0 my $find = $self->{find_data};
232 0 foreach(keys (%$find)){
233 #print "key=$_","\n";
234 0 ▶ $self->{found} = 1 if $char->{Data} =~ $_;
Branch: 1 branch
BranchTrueFalseTotal
if $char->{"Data"} =~ /$_/ 0 0 0
235 }
236 }
237  
238 sub indent{
239 0 ▶ my $self = shift;
Subroutine indent: not called CC=1 SCAR=6.9
Pod undocumented
240 0 $self->{buf} .= ($self->{indent} x $self->{level});
241 }
242 sub add_attr{
243 0 ▶ my ($self, $attr) = @_;
Subroutine add_attr: not called CC=2 SCAR=17.9
Pod undocumented
244 #print Dumper($attr);
245 0 foreach (keys( %$attr)){
246 0 $self->{buf} .= " $attr->{$_}{Name}=\"$attr->{$_}{Value}\"";
247 }
248 0 $self->{buf} .= ">\n";
249 }
250  
251  
252  
253  
254 =head1 AUTHOR
255  
256 Daniel Necsoiu, Ericsson, C<< <daniel.necsoiu@gmail.com; daniel.necsoiu@ericsson.com> >>
257  
258 =head1 BUGS
259  
260 Please report any bugs or feature requests to C<bug-etr-xml-sax-filterhandler at rt.cpan.org>, or through
261 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=ETR-XML-SAX-FilterHandler>. I will be notified, and then you'll
262 automatically be notified of progress on your bug as I make changes.
263  
264  
265  
266  
267 =head1 SUPPORT
268  
269 You can find documentation for this module with the perldoc command.
270  
271 perldoc ETR::XML::SAX::FilterHandler
272  
273  
274 You can also look for information at:
275  
276 =over 4
277  
278 =item * RT: CPAN's request tracker (report bugs here)
279  
280 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=ETR-XML-SAX-FilterHandler>
281  
282 =item * AnnoCPAN: Annotated CPAN documentation
283  
284 L<http://annocpan.org/dist/ETR-XML-SAX-FilterHandler>
285  
286 =item * CPAN Ratings
287  
288 L<http://cpanratings.perl.org/d/ETR-XML-SAX-FilterHandler>
289  
290 =item * Search CPAN
291  
292 L<http://search.cpan.org/dist/ETR-XML-SAX-FilterHandler/>
293  
294 =back
295  
296  
297 =head1 ACKNOWLEDGEMENTS
298  
299  
300 =head1 LICENSE AND COPYRIGHT
301  
302 Copyright 2015 Daniel Necsoiu, Ericsson.
303  
304 This program is free software; you can redistribute it and/or modify it
305 under the terms of either: the GNU General Public License as published
306 by the Free Software Foundation; or the Artistic License.
307  
308 See L<http://dev.perl.org/licenses/> for more information.
309  
310  
311 =cut
312  
313 1 1; # End of ETR::XML::SAX::FilterHandler
↑ Summary