| 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
| |||||||||||||||||||||||||||||
| 37 | 1 | ▶ | $file =~ /\./ and return $self->_load_def ($file); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
-e $path and -f $path
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
| |||||||||||||||||||||||||||||
| 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
}
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
}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 63 | 893 | chomp(); | |||||||||||||||||||||||||||
| 64 | 893 | s/\#.*$//; | |||||||||||||||||||||||||||
| 65 | 893 | s/^\s*//; | |||||||||||||||||||||||||||
| 66 | 893 | s/\s*$//; | |||||||||||||||||||||||||||
| 67 | 893 | ▶ | next unless ($_ ne ''); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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} ||= {}
MC/DC100%1/1
$self->{$tag} ||= {}
$self->{$tag}
Truth table100%2/2
A
A= $self->{$tag}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 13 | my $tag = $node->{_tag}; | |||||||||||||||||||||||||||
| 151 | 13 | ▶ | return unless ( $self->{$tag} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 4 | for (keys %{$node}) | |||||||||||||||||||||||||||
| 154 | { | ||||||||||||||||||||||||||||
| 155 | 17 | ▶ | /^_/ and next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 5 | ▶ | delete $node->{$_} unless $self->{$tag}->{$_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 174 | } keys %attr; | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 16 | my $res = '<'; | |||||||||||||||||||||||||||
| 177 | 16 | ▶ | $res .= '/' if ($close and not $open); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$close and not $open
MC/DC100%2/2
$close and not $open
$close
not $open
Truth table100%3/3
A and not(B)
A= $closeB= not $open
| |||||||||||||||||||||||||||||
| 178 | 16 | $res .= $tag; | |||||||||||||||||||||||||||
| 179 | 16 | ▶ | $res .= " $attr" if ($attr and $open); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$attr and $open
MC/DC100%2/2
$attr and $open
$attr
$open
Truth table100%3/3
A and B
A= $attrB= $open
| |||||||||||||||||||||||||||||
| 180 | 16 | ▶ | $res .= ' /' if ($open and $close); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$open and $close
MC/DC100%2/2
$open and $close
$open
$close
Truth table100%3/3
A and B
A= $openB= $close
| |||||||||||||||||||||||||||||
| 181 | 16 | $res .= '>'; | |||||||||||||||||||||||||||
| 182 | 16 | return $res; | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 3 | 1; | |||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | __END__ | ||||||||||||||||||||||||||||