Essex/Base.pm

filter: Statementstmt 89.7% 70 / 78 Branchbran 68.7% 22 / 32 Conditioncond 45.4% 5 / 11 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 92.3% 12 / 13 Podpod 100% 6 / 6 totaltotal 79.5% 117 / 147 CC 22 SCAR 20.6
CC
22
Cov
80%
import
13.3
namespace_map
12.0
execute
6.8
CRAP 7.8
« Essex.pm ↑ Summary Handler/Essex.pm »
1 package XML::Essex::Base;
2  
3 8 $VERSION = 0.000_1;
4  
5 =head1 NAME
6  
7 XML::Essex::Base - class for Essex SAX components
8  
9 =head1 SYNOPSIS
10  
11 ## Not for external use.
12  
13 =head1 DESCRIPTION
14  
15 All Essex generators, filters and handlers must inherit from this
16 class among others. This class provides methods common to all
17 three and specialized export semantics so that exports may be
18 inherited from base classes.
19  
20 =for test_scripts XML-Generator-Essex.t XML-Filter-Essex.t
21  
22 =cut
23  
24 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 8 ▶use Carp (); # keep from acquiring Croak's exported subs as methods.
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 8 our $self;
29  
30 =head1 METHODS
31  
32 =over
33  
34 =cut
35  
36 =item new
37  
38 Creates and initializes an instance.
39  
40 =cut
41  
42 35 ▶sub new {
Subroutine new: called CC=2 SCAR=7.4
Pod documented
43 35 my $proto = shift;
44 35 ▶ my $self = bless { @_ }, ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0350
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
45 35 $self->_init; ## These must use NEXT::, it's a diamond hierarchy at
46 ## times (eq XML::Filter::Dispatcher).
47 35 return $self;
48 }
49  
50 43 ▶sub _classes {
Subroutine _classes: called CC=2 SCAR=6.9
51 8 ▶ no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
52 43 ▶ return ( $_ ) unless exists ${"${_}::"}{ISA};
Branch: 1 branch
BranchTrueFalseTotal
unless exists do { "${_}::" }->{"ISA"} 19 24 43
53 24 return ( $_, map _classes( $_ ), @{"${_}::ISA"} );
54 }
55  
56 =item import
57  
58 Uses C<@EXPORT> and C<@EXPORT_OK> arrays like Exporter.pm, but
59 implements inheritence on it. Understands the meaning of the tags
60 ":all" and ":default", which are hardcoded (C<%EXPORT_TAGS> is ignored
61 thus far), but does not emulate Exporter's other, rarely used syntaxes.
62  
63 =cut
64  
65 8 ▶use vars qw( $self );
Subroutine BEGIN: called CC=1 SCAR=0.0
66  
67 13 ▶sub import {
Subroutine import: called CC=13 SCAR=25.9
68 13 my $class = shift;
69 13 my $caller = caller;
70  
71 13 my $no_params = ! @_;
72  
73 8 ▶ no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
74  
75 13 my @classes = do {
76 13 local $_ = $class;
77 13 my %seen;
78 13 grep !$seen{$_}++, _classes;
79 };
80  
81 13 my %tags;
82 @_ = grep
83 13 ▶ substr( $_, 0, 1 ) eq ":" ? $tags{$_} = undef : 1,
Branch: 1 branch
BranchTrueFalseTotal
substr($_, 0, 1) eq ':' ? : 2 0 2
84 @_;
85  
86 my %default_exports = (
87 137 ▶ map { ( $_ => undef ) }
Branch: 1 branch
BranchTrueFalseTotal
exists do { "${_}::" }->{'EXPORT'} ? : 17 23 40
88 map
89 40 exists ${"${_}::"}{EXPORT}
90 13 ? @{"${_}::EXPORT"}
91 : (),
92 @classes
93 );
94  
95 13 ▶ push @_, keys %default_exports if exists $tags{":default"};
Branch: 1 branch
BranchTrueFalseTotal
if exists $tags{":default"} 1 12 13
96  
97 my %all_exports = (
98 %default_exports,
99 ( exists $tags{":all"} || @_ )
100 ? (
101 4 ▶ map { ( $_ => undef ) }
Branch: 1 branch
BranchTrueFalseTotal
exists do { "${_}::" }->{'EXPORT_OK'} ? : 4 2 6
102 map
103 6 exists ${"${_}::"}{EXPORT_OK}
104 13 ▶ ? @{"${_}::EXPORT_OK"}
Branch: 1 branch
BranchTrueFalseTotal
exists $tags{':all'} || @_ ? : 2 11 13
Condition100%3/3
exists $tags{":all"} or @_
l!l&&r!l&&!r
1111
MC/DC100%2/2
exists $tags{":all"} or @_
exists $tags{":all"} @_
Truth table100%3/3
A or B
A= exists $tags{":all"}B= @_
ABresult
1X1
011
000
105 : (),
106 @classes
107 )
108 : ()
109 );
110 13 ▶ push @_, keys %all_exports if exists $tags{":all"};
Branch: 1 branch
BranchTrueFalseTotal
if exists $tags{":all"} 1 12 13
111  
112 13 ▶ @_= keys %default_exports if $no_params;
Branch: 1 branch
BranchTrueFalseTotal
if $no_params 11 2 13
113  
114 13 my @not_exported;
115  
116 my %seen;
117 13 for ( grep !$seen{$_}++, @_ ) {
118 139 ▶ unless ( exists $all_exports{$_} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $all_exports{$_}) 0 139 139
119 0 push @not_exported, $_;
120 0 next;
121 }
122  
123 0 ▶ *{"${caller}::$_"} = ( $class->can( $_ ) || \&{"${class}::$_"} );
Condition50%1/2
$class->can($_) || \&{"${class}::$_";}
l!l
1390
MC/DC0%0/1
$class->can($_) || \&{"${class}::$_";}
$class->can($_)
Truth table50%1/2
A
A= $class->can($_)
Aresult
11
00
124 }
125  
126 Carp::croak
127 13 ▶ "functions ",
Branch: 1 branch
BranchTrueFalseTotal
if @not_exported 0 13 13
128 join( " ", @not_exported ),
129 " not exported by $class"
130 if @not_exported;
131 }
132  
133 =item main
134  
135 The main routine. Overload this or pass in a code ref
136 to C<new( Main => \&foo )> or C<set_main( \&foo )> to set this.
137  
138 =cut
139  
140 49 ▶sub main {
Subroutine main: called CC=1 SCAR=0.0
Pod documented
141 49 goto &{$_[0]->{Main}};
142 }
143  
144 =item set_main
145  
146 Sets the main routines to a code reference.
147  
148 =cut
149  
150 11 ▶sub set_main {
Subroutine set_main: called CC=1 SCAR=0.0
Pod documented
151 11 my $self = shift;
152 11 $self->{Main} = shift;
153 }
154  
155 =item reset
156  
157 Called before the main routine is called.
158  
159 =cut
160  
161 85 ▶sub reset {
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
162 85 my $self = shift;
163  
164 85 $self->{NamespaceMaps} = [];
165  
166 85 $self->NEXT::reset;
167 }
168  
169 =item finish
170  
171 Called after the main routine is called.
172  
173 =cut
174  
175 =item execute
176  
177 Prepares the runtime environment, calls C<<$self->main( @_ )>>, cleans
178 up afterwards and runs sanity checks.
179  
180 This is called automatically in filters and handlers, must be
181 called manually in generators.
182  
183 Calls reset() before and finish() after main().
184  
185 =cut
186  
187 52 ▶sub execute {
Subroutine execute: called CC=6 SCAR=19.2
Pod documented
188 52 local $self = shift;
189  
190 52 ▶ return if $self->{NoExecute}; ## Used by XML::Essex
Branch: 1 branch
BranchTrueFalseTotal
if $XML::Essex::Base::self->{"NoExecute"} 3 49 52
191  
192 ## Don't save a reference to the output_monitor in case some whacko
193 ## manages to alter $self->{Handler} somehow.
194 49 $self->reset;
195  
196 49 local $_; ## get() explicitly sets $_ for the convenience of
197 ## main() programmers. In unthreaded mode, we want
198 ## to be sure not to perturb the caller's sense of $_.
199  
200 49 my $r;
201 my @r;
202 49 my $ok = eval {
203 wantarray
204 49 ▶ ? @r = $self->main( @_ )
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 0 49 49
defined wantarray ? : 49 0 49
205 : defined wantarray
206 ? $r = $self->main( @_ )
207 : $self->main( @_ );
208 32 1;
209 };
210  
211 49 my ( $result_set, $result ) = $self->finish( $ok, $@ );
212  
213 49 ▶ return $result if $result_set;
Branch: 1 branch
BranchTrueFalseTotal
if $result_set 49 0 49
214 0 ▶ return wantarray ? @r : $r;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
215 }
216  
217  
218 =item namespace_map
219  
220 aka: ns_map
221  
222 $self->ns_map(
223 $ns1 => $prefix1,
224 $ns2 => $prefix2,
225 ...
226 );
227  
228 Creates a new set of mappings in addition to any that are already in
229 effect. If a namespace is mapped to multiple prefixes, the last one
230 created is used. The mappings stay in effect until the map objected
231 referred to by C<$map> is destroyed.
232  
233 NOTE: the namespace prefixes from the source document override the
234 namespace prefixes set here when events are transmitted downstream.
235 This is so that namespace prefixes are not altered arbitrarily; the
236 philosophy is to make as few changes to the source document as possible
237 and remapping prefixes to match what happens to be declared in the
238 filter would not be proper.
239  
240 For names in namespaces that are introduced by the filter and are not in
241 the source document, the prefixes from the filter are used. This is a
242 bit dangerous: some other namespace in the source document may use the
243 same prefix and the result could be catastrophic. Some future version
244 will try to detect these collisions, and there may even be a nice way to
245 avoid them.
246  
247 Source document prefixes are generally invisible in the Essex
248 environment (aside from the start_prefix_mapping and end_prefix_mapping
249 events) because they could be anything. If you root around inside essex
250 objects enough, though, you can ferret them out. Trying to do that is a
251 pretty good indication that something's wrong.
252  
253 =cut
254  
255 0 ▶sub namespace_map {
Subroutine namespace_map: not called CC=3 SCAR=24.8
Pod documented
256 0 ▶ local $self = shift if @_ && UNIVERSAL::isa( $_[0], __PACKAGE__ );
Branch: 1 branch
BranchTrueFalseTotal
if @_ and UNIVERSAL::isa($_[0], __PACKAGE__) 0 0 0
Condition0%0/3
@_ and UNIVERSAL::isa($_[0], __PACKAGE__)
!ll&&!rl&&r
000
MC/DC0%0/2
@_ and UNIVERSAL::isa($_[0], __PACKAGE__)
@_ UNIVERSAL::isa($_[0], __PACKAGE__)
Truth table0%0/3
A and B
A= @_B= UNIVERSAL::isa($_[0], __PACKAGE__)
ABresult
0X0
100
111
257 0 require XML::Essex::NamespaceMap;
258 0 push @{$self->{Namespaces}},
259 XML::Essex::NamespaceMap->new( $self, @_ );
260 }
261  
262 8 *ns_map = \&namespace_map;
263  
264  
265 =back
266  
267 =head1 LIMITATIONS
268  
269 Does not support other Exporter features like exporting past several calling
270 modules.
271  
272 =head1 COPYRIGHT
273  
274 Copyright 2002, R. Barrie Slaymaker, Jr., All Rights Reserved
275  
276 =head1 LICENSE
277  
278 You may use this module under the terms of the BSD, Artistic, oir GPL licenses,
279 any version.
280  
281 =head1 AUTHOR
282  
283 Barrie Slaymaker <barries@slaysys.com>
284  
285 =cut
286  
287 8 1;
« Essex.pm ↑ Summary Handler/Essex.pm »