| 1 | package PYX::XMLNorm; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # Pragmas. | ||||||||||||||||||||||||||||
| 4 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | # Modules. | ||||||||||||||||||||||||||||
| 8 | 7 | ▶ | use Class::Utils qw(set_params); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 7 | ▶ | use Error::Pure qw(err); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 7 | ▶ | use PYX qw(end_element); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 7 | ▶ | use PYX::Parser; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | # Version. | ||||||||||||||||||||||||||||
| 14 | 7 | our $VERSION = 0.03; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | # Constructor. | ||||||||||||||||||||||||||||
| 17 | sub new { | ||||||||||||||||||||||||||||
| 18 | 7 | ▶ | my ($class, @params) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 19 | 7 | my $self = bless {}, $class; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | # Flush stack on finalization. | ||||||||||||||||||||||||||||
| 22 | 7 | $self->{'flush_stack'} = 0; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | # Output handler. | ||||||||||||||||||||||||||||
| 25 | 7 | $self->{'output_handler'} = \*STDOUT; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | # XML normalization rules. | ||||||||||||||||||||||||||||
| 28 | 7 | $self->{'rules'} = {}; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | # Process params. | ||||||||||||||||||||||||||||
| 31 | 7 | set_params($self, @params); | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | # Check to rules. | ||||||||||||||||||||||||||||
| 34 | 5 | ▶ | if (! keys %{$self->{'rules'}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 1 | err 'Cannot exist XML normalization rules.'; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | # PYX::Parser object. | ||||||||||||||||||||||||||||
| 39 | $self->{'pyx_parser'} = PYX::Parser->new( | ||||||||||||||||||||||||||||
| 40 | 'callbacks' => { | ||||||||||||||||||||||||||||
| 41 | 'data' => \&_end_element_simple, | ||||||||||||||||||||||||||||
| 42 | 'end_element' => \&_end_element, | ||||||||||||||||||||||||||||
| 43 | 'final' => \&_final, | ||||||||||||||||||||||||||||
| 44 | 'start_element' => \&_start_element, | ||||||||||||||||||||||||||||
| 45 | }, | ||||||||||||||||||||||||||||
| 46 | 'non_parser_options' => { | ||||||||||||||||||||||||||||
| 47 | 'flush_stack' => $self->{'flush_stack'}, | ||||||||||||||||||||||||||||
| 48 | 'rules' => $self->{'rules'}, | ||||||||||||||||||||||||||||
| 49 | 'stack' => [], | ||||||||||||||||||||||||||||
| 50 | }, | ||||||||||||||||||||||||||||
| 51 | 4 | 'output_handler' => $self->{'output_handler'}, | |||||||||||||||||||||||||||
| 52 | 'output_rewrite' => 1, | ||||||||||||||||||||||||||||
| 53 | ); | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | # Object. | ||||||||||||||||||||||||||||
| 56 | 4 | return $self; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | # Parse pyx text or array of pyx text. | ||||||||||||||||||||||||||||
| 60 | sub parse { | ||||||||||||||||||||||||||||
| 61 | 1 | ▶ | my ($self, $pyx, $out) = @_; | ||||||||||||||||||||||||||
|
Subroutine
parse: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 62 | 1 | $self->{'pyx_parser'}->parse($pyx, $out); | |||||||||||||||||||||||||||
| 63 | 1 | return; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | # Parse file with pyx text. | ||||||||||||||||||||||||||||
| 67 | sub parse_file { | ||||||||||||||||||||||||||||
| 68 | 7 | ▶ | my ($self, $file) = @_; | ||||||||||||||||||||||||||
|
Subroutine
parse_file: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 69 | 7 | $self->{'pyx_parser'}->parse_file($file); | |||||||||||||||||||||||||||
| 70 | 7 | return; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | # Parse from handler. | ||||||||||||||||||||||||||||
| 74 | sub parse_handler { | ||||||||||||||||||||||||||||
| 75 | 1 | ▶ | my ($self, $input_file_handler, $out) = @_; | ||||||||||||||||||||||||||
|
Subroutine
parse_handler: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 76 | 1 | $self->{'pyx_parser'}->parse_handler($input_file_handler, $out); | |||||||||||||||||||||||||||
| 77 | 1 | return; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | # Process start of element. | ||||||||||||||||||||||||||||
| 81 | sub _start_element { | ||||||||||||||||||||||||||||
| 82 | 55 | ▶ | my ($pyx_parser, $tag) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_start_element: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||
| 83 | 55 | my $out = $pyx_parser->{'output_handler'}; | |||||||||||||||||||||||||||
| 84 | 55 | my $rules = $pyx_parser->{'non_parser_options'}->{'rules'}; | |||||||||||||||||||||||||||
| 85 | 55 | my $stack = $pyx_parser->{'non_parser_options'}->{'stack'}; | |||||||||||||||||||||||||||
| 86 | 55 | ▶ | if (exists $rules->{'*'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 55 | foreach my $tmp (@{$rules->{'*'}}) { | |||||||||||||||||||||||||||
| 88 | 275 | ▶ | if (@{$stack} > 0 && lc($stack->[-1]) eq $tmp) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@{$stack;} > 0 and lc $stack->[-1] eq $tmp
MC/DC100%2/2
@{$stack;} > 0 and lc $stack->[-1] eq $tmp
@{$stack;} > 0
lc $stack->[-1] eq $tmp
Truth table100%3/3
A and B
A= @{$stack;} > 0B= lc $stack->[-1] eq $tmp
| |||||||||||||||||||||||||||||
| 89 | 15 | print {$out} end_element(pop @{$stack}), "\n"; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | 55 | ▶ | if (exists $rules->{lc($tag)}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 21 | foreach my $tmp (@{$rules->{lc($tag)}}) { | |||||||||||||||||||||||||||
| 95 | 34 | ▶ | if (@{$stack} > 0 && lc($stack->[-1]) eq $tmp) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@{$stack;} > 0 and lc $stack->[-1] eq $tmp
MC/DC100%2/2
@{$stack;} > 0 and lc $stack->[-1] eq $tmp
@{$stack;} > 0
lc $stack->[-1] eq $tmp
Truth table100%3/3
A and B
A= @{$stack;} > 0B= lc $stack->[-1] eq $tmp
| |||||||||||||||||||||||||||||
| 96 | 7 | print {$out} end_element(pop @{$stack}), "\n"; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | 55 | push @{$stack}, $tag; | |||||||||||||||||||||||||||
| 101 | 55 | print {$out} $pyx_parser->line, "\n"; | |||||||||||||||||||||||||||
| 102 | 55 | return; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | # Add implicit end_element. | ||||||||||||||||||||||||||||
| 106 | sub _end_element_simple { | ||||||||||||||||||||||||||||
| 107 | 11 | ▶ | my $pyx_parser = shift; | ||||||||||||||||||||||||||
|
Subroutine
_end_element_simple: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 108 | 11 | my $rules = $pyx_parser->{'non_parser_options'}->{'rules'}; | |||||||||||||||||||||||||||
| 109 | 11 | my $stack = $pyx_parser->{'non_parser_options'}->{'stack'}; | |||||||||||||||||||||||||||
| 110 | 11 | my $out = $pyx_parser->{'output_handler'}; | |||||||||||||||||||||||||||
| 111 | 11 | ▶ | if (exists $rules->{'*'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 11 | foreach my $tmp (@{$rules->{'*'}}) { | |||||||||||||||||||||||||||
| 113 | 55 | ▶ | if (@{$stack} && lc $stack->[-1] eq $tmp) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@{$stack;} and lc $stack->[-1] eq $tmp
MC/DC100%2/2
@{$stack;} and lc $stack->[-1] eq $tmp
@{$stack;}
lc $stack->[-1] eq $tmp
Truth table100%3/3
A and B
A= @{$stack;}B= lc $stack->[-1] eq $tmp
| |||||||||||||||||||||||||||||
| 114 | 2 | print {$out} end_element(pop @{$stack}), "\n"; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | 11 | print {$out} $pyx_parser->line, "\n"; | |||||||||||||||||||||||||||
| 119 | 11 | return; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | # Process end of element | ||||||||||||||||||||||||||||
| 123 | sub _end_element { | ||||||||||||||||||||||||||||
| 124 | 14 | ▶ | my ($pyx_parser, $tag) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_end_element: called CC=10 SCAR=23.1
| |||||||||||||||||||||||||||||
| 125 | 14 | my $out = $pyx_parser->{'output_handler'}; | |||||||||||||||||||||||||||
| 126 | 14 | my $rules = $pyx_parser->{'non_parser_options'}->{'rules'}; | |||||||||||||||||||||||||||
| 127 | 14 | my $stack = $pyx_parser->{'non_parser_options'}->{'stack'}; | |||||||||||||||||||||||||||
| 128 | 14 | ▶ | if (exists $rules->{'*'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 14 | foreach my $tmp (@{$rules->{'*'}}) { | |||||||||||||||||||||||||||
| 130 | 70 | ▶ | if (lc($tag) ne $tmp && lc($stack->[-1]) eq $tmp) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
lc $tag ne $tmp and lc $stack->[-1] eq $tmp
MC/DC100%2/2
lc $tag ne $tmp and lc $stack->[-1] eq $tmp
lc $tag ne $tmp
lc $stack->[-1] eq $tmp
Truth table100%3/3
A and B
A= lc $tag ne $tmpB= lc $stack->[-1] eq $tmp
| |||||||||||||||||||||||||||||
| 131 | 7 | print {$out} end_element(pop @{$stack}), "\n"; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | # XXX Myslim, ze tenhle blok je spatne. | ||||||||||||||||||||||||||||
| 136 | 14 | ▶ | if (exists $rules->{$tag}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 8 | foreach my $tmp (@{$rules->{$tag}}) { | |||||||||||||||||||||||||||
| 138 | 11 | ▶ | if (lc($tag) ne $tmp && lc($stack->[-1]) eq $tmp) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
lc $tag ne $tmp and lc $stack->[-1] eq $tmp
MC/DC50%1/2
lc $tag ne $tmp and lc $stack->[-1] eq $tmp
lc $tag ne $tmp
lc $stack->[-1] eq $tmp
Truth table66%2/3
A and B
A= lc $tag ne $tmpB= lc $stack->[-1] eq $tmp
| |||||||||||||||||||||||||||||
| 139 | 10 | print {$out} end_element(pop @{$stack}), "\n"; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | 14 | ▶ | if (lc($stack->[-1]) eq lc($tag)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 14 | pop @{$stack}; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | 14 | print {$out} $pyx_parser->line, "\n"; | |||||||||||||||||||||||||||
| 147 | 14 | return; | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | # Process final. | ||||||||||||||||||||||||||||
| 151 | sub _final { | ||||||||||||||||||||||||||||
| 152 | 9 | ▶ | my $pyx_parser = shift; | ||||||||||||||||||||||||||
|
Subroutine
_final: called CC=4 SCAR=18.6
| |||||||||||||||||||||||||||||
| 153 | 9 | my $stack = $pyx_parser->{'non_parser_options'}->{'stack'}; | |||||||||||||||||||||||||||
| 154 | 9 | my $out = $pyx_parser->{'output_handler'}; | |||||||||||||||||||||||||||
| 155 | 9 | ▶ | if (@{$stack} > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | # If set, than flush stack. | ||||||||||||||||||||||||||||
| 158 | 0 | ▶ | if ($pyx_parser->{'non_parser_options'}->{'flush_stack'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | 0 | foreach my $tmp (reverse @{$stack}) { | |||||||||||||||||||||||||||
| 160 | 0 | print {$out} end_element($tmp), "\n"; | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | 9 | return; | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 7 | 1; | |||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | __END__ | ||||||||||||||||||||||||||||