lib/MKDoc/XML/Stripper.pm

filter: Statementstmt 84.2% 80 / 95 Branchbran 92.8% 26 / 28 Conditioncond 82.3% 14 / 17 MC/DCmcdc 72.7% 8 / 11 Subroutinesub 84.6% 11 / 13 Podpod 85.7% 6 / 7 totaltotal 84.8% 145 / 171 CC 25 SCAR 16.9
CC
25
Cov
86%
_node_to_tag
9.0
load_def
6.4
strip
6.0
CRAP 5.4
« lib/MKDoc/XML/Dumper.pm ↑ Summary lib/MKDoc/XML/TreeBuilder.pm »
1 # -------------------------------------------------------------------------------------
2 # MKDoc::XML::Stripper
3 # -------------------------------------------------------------------------------------
4 # Author : Jean-Michel Hiver.
5 # Copyright : (c) MKDoc Holdings Ltd, 2003
6 #
7 # This module removes user-defined markup from an existing XML file / variable.
8 #
9 # This module is distributed under the same license as Perl itself.
10 # -------------------------------------------------------------------------------------
11 package MKDoc::XML::Stripper;
12 3 ▶use MKDoc::XML::Tokenizer;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 ##
19 # $class->new();
20 # --------------
21 # Returns a new MKDoc::XML::Stripper object.
22 ##
23 sub new
24 3 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod documented
25 3 my $class = shift;
26 3 my $self = bless { @_ }, $class;
27 3 return $self;
28 }
29  
30  
31 sub load_def
32 1 ▶{
Subroutine load_def: called CC=6 SCAR=18.6
Pod documented
33 1 my $self = shift;
34 1 my $file = shift;
35
36 1 ▶ $file =~ /\// and return $self->_load_def ($file);
Branch: 1 branch
BranchTrueFalseTotal
$file =~ m[/] and return $self->_load_def($file) 0 1 1
37 1 ▶ $file =~ /\./ and return $self->_load_def ($file);
Branch: 1 branch
BranchTrueFalseTotal
$file =~ /\./ and return $self->_load_def($file) 0 1 1
38
39 1 $file .= '.txt';
40 1 for (@INC)
41 {
42 2 my $path = File::Spec->catfile ($_, qw /MKDoc XML Stripper/, $file);
43 2 ▶ -e $path and -f $path and return $self->_load_def ($path);
Branch: 1 branch
BranchTrueFalseTotal
-e $path and -f $path and return $self->_load_def($path) 1 1 2
Condition66%2/3
-e $path and -f $path
!ll&&!rl&&r
101
MC/DC50%1/2
-e $path and -f $path
-e $path -f $path
Truth table66%2/3
A and B
A= -e $pathB= -f $path
ABresult
0X0
100
111
44 }
45
46 0 warn "Cannot read-open $file. Reason: Doesn't seem to be anywhere in \@INC";
47 }
48  
49  
50 sub _load_def
51 1 ▶{
Subroutine _load_def: called CC=5 SCAR=16.5
52 1 my $self = shift;
53 1 my $file = shift;
54
55 1 ▶ open FP, "<$file" || do {
Condition33%1/3
"<$file" || do { warn "Cannot read-open $file. Reason: $!"; return }
l!l&&r!l&&!r
100
MC/DC0%0/2
"<$file" || do { warn "Cannot read-open $file. Reason: $!"; return }
"<$file" do { warn "Cannot read-open $file. Reason: $!"; return }
Truth table33%1/3
A || B
A= "<$file"B= do { warn "Cannot read-open $file. Reason: $!"; return }
ABresult
1X1
011
000
56 0 warn "Cannot read-open $file. Reason: $!";
57 0 return;
58 };
59  
60 # clean $self
61 0 for (keys %{$self}) { delete $self->{$_} }
62 1 ▶ while (<FP>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline FP)) 893 1 894
63 893 chomp();
64 893 s/\#.*$//;
65 893 s/^\s*//;
66 893 s/\s*$//;
67 893 ▶ next unless ($_ ne '');
Branch: 1 branch
BranchTrueFalseTotal
unless $_ ne "" 350 543 893
68
69 543 my @l = split /\s+/, $_;
70 543 $self->allow (@l);
71 }
72
73 1 close FP;
74 }
75  
76  
77 ##
78 # $self->allow ($tag, @attributes);
79 # ---------------------------------
80 # Allows the tag $tag to be present along with a list of attributes,
81 # i.e.
82 #
83 # $self->allow (qw /p class id/);
84 ##
85 sub allow
86 545 ▶{
Subroutine allow: called CC=3 SCAR=11.0
Pod documented
87 545 my $self = shift;
88 545 my $tag = shift;
89 545 ▶ $self->{$tag} ||= {};
Condition100%2/2
$self->{$tag} ||= {}
l!l
47273
MC/DC100%1/1
$self->{$tag} ||= {}
$self->{$tag}
Truth table100%2/2
A
A= $self->{$tag}
Aresult
11
00
90 545 for (@_) { $self->{$tag}->{$_} = 1 };
91 }
92  
93  
94 ##
95 # $self->disallow ($tag, @attributes);
96 # ------------------------------------
97 # Disallows the tag $tag to be present.
98 ##
99 sub disallow
100 0 ▶{
Subroutine disallow: not called CC=1 SCAR=6.9
Pod documented
101 0 my $self = shift;
102 0 my $tag = shift;
103 0 delete $self->{$tag};
104 }
105  
106  
107 ##
108 # $self->process_data ($data);
109 # ----------------------------
110 # Strips tags on $data and returns the stripped result.
111 ##
112 sub process_data
113 1 ▶{
Subroutine process_data: called CC=1 SCAR=0.0
Pod documented
114 1 my $self = shift;
115 1 my $data = shift;
116 1 my $tokens = MKDoc::XML::Tokenizer->process_data ($data);
117 1 my @result = map { $self->strip ($_) } @{$tokens};
118 1 return join '', map { $$_ } @result;
119 }
120  
121  
122 ##
123 # $self->process_file ($file);
124 # ----------------------------
125 # Strips tags on $file and returns the stripped result.
126 ##
127 sub process_file
128 0 ▶{
Subroutine process_file: not called CC=1 SCAR=6.9
Pod documented
129 0 my $self = shift;
130 0 my $file = shift;
131 0 my $tokens = MKDoc::XML::Tokenizer->process_file ($file);
132 0 my @result = map { $self->strip ($_) } @{$tokens};
133 0 return join '', map { $$_ } @result;
134 }
135  
136  
137 ##
138 # $self->strip ($token);
139 # ----------------------
140 # Returns this token stripped out of the stuff which we don't want.
141 # Returns an empty list if the token is not allowed.
142 ##
143 sub strip
144 18 ▶{
Subroutine strip: called CC=6 SCAR=17.9
Pod undocumented
145 18 my $self = shift;
146 18 my $token = shift;
147 18 my $node = $token->tag();
148 18 ▶ defined $node || return $token;
Branch: 1 branch
BranchTrueFalseTotal
defined $node or return $token 13 5 18
149
150 13 my $tag = $node->{_tag};
151 13 ▶ return unless ( $self->{$tag} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{$tag} 9 4 13
152
153 4 for (keys %{$node})
154 {
155 17 ▶ /^_/ and next;
Branch: 1 branch
BranchTrueFalseTotal
/^_/ and next 12 5 17
156 5 ▶ delete $node->{$_} unless $self->{$tag}->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{$tag}{$_} 2 3 5
157 }
158
159 4 return new MKDoc::XML::Token ( _node_to_tag ($node) );
160 }
161  
162  
163 sub _node_to_tag
164 16 ▶{
Subroutine _node_to_tag: called CC=9 SCAR=22.0
165 16 my $node = shift;
166 16 my $tag = $node->{_tag};
167 16 my $open = $node->{_open};
168 16 my $close = $node->{_close};
169 16 ▶ my %attr = map { /^_/ ? () : ($_ => $node->{$_}) } keys %{$node};
Branch: 1 branch
BranchTrueFalseTotal
/^_/ ? : 40 12 52
170 my $attr = join ' ', map {
171 16 my $key = $_;
172 12 my $val = $attr{$key};
173 12 ▶ ($val =~ /\"/) ? "$key='$val'" : "$key=\"$val\""
Branch: 1 branch
BranchTrueFalseTotal
$val =~ /\"/ ? : 3 9 12
174 } keys %attr;
175
176 16 my $res = '<';
177 16 ▶ $res .= '/' if ($close and not $open);
Branch: 1 branch
BranchTrueFalseTotal
if $close and not $open 5 11 16
Condition100%3/3
$close and not $open
!ll&&!rl&&r
745
MC/DC100%2/2
$close and not $open
$close not $open
Truth table100%3/3
A and not(B)
A= $closeB= not $open
ABresult
0X0
100
111
178 16 $res .= $tag;
179 16 ▶ $res .= " $attr" if ($attr and $open);
Branch: 1 branch
BranchTrueFalseTotal
if $attr and $open 8 8 16
Condition100%3/3
$attr and $open
!ll&&!rl&&r
538
MC/DC100%2/2
$attr and $open
$attr $open
Truth table100%3/3
A and B
A= $attrB= $open
ABresult
0X0
100
111
180 16 ▶ $res .= ' /' if ($open and $close);
Branch: 1 branch
BranchTrueFalseTotal
if $open and $close 4 12 16
Condition100%3/3
$open and $close
!ll&&!rl&&r
574
MC/DC100%2/2
$open and $close
$open $close
Truth table100%3/3
A and B
A= $openB= $close
ABresult
0X0
100
111
181 16 $res .= '>';
182 16 return $res;
183 }
184  
185  
186 3 1;
187  
188  
189 __END__
« lib/MKDoc/XML/Dumper.pm ↑ Summary lib/MKDoc/XML/TreeBuilder.pm »