| 1 | package Serge::Engine::Plugin::apply_xslt; | ||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use parent Serge::Engine::Plugin::if; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | no warnings qw(uninitialized); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Encode qw(decode_utf8); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Serge::Util qw(subst_macros_strref xml_escape_strref xml_unescape_strref); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | sub name { | ||||||||||||||||||||||||||||
| 13 | 0 | ▶ | return 'Generic xslt (https://www.w3.org/Style/XSL/) replacement plugin'; | ||||||||||||||||||||||||||
|
Subroutine
name: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 14 | } | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | sub init { | ||||||||||||||||||||||||||||
| 17 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 1 | $self->SUPER::init(@_); | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 1 | $self->merge_schema({ | |||||||||||||||||||||||||||
| 22 | apply => {'' => 'LIST', | ||||||||||||||||||||||||||||
| 23 | '*' => 'STRING', | ||||||||||||||||||||||||||||
| 24 | }, | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | params => { | ||||||||||||||||||||||||||||
| 27 | '*' => 'STRING' | ||||||||||||||||||||||||||||
| 28 | }, | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | if => { | ||||||||||||||||||||||||||||
| 31 | '*' => { | ||||||||||||||||||||||||||||
| 32 | then => { | ||||||||||||||||||||||||||||
| 33 | apply => {'' => 'LIST', | ||||||||||||||||||||||||||||
| 34 | '*' => 'STRING', | ||||||||||||||||||||||||||||
| 35 | }, | ||||||||||||||||||||||||||||
| 36 | params => { | ||||||||||||||||||||||||||||
| 37 | '*' => 'STRING' | ||||||||||||||||||||||||||||
| 38 | }, | ||||||||||||||||||||||||||||
| 39 | }, | ||||||||||||||||||||||||||||
| 40 | }, | ||||||||||||||||||||||||||||
| 41 | }, | ||||||||||||||||||||||||||||
| 42 | }); | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 1 | $self->add({ | |||||||||||||||||||||||||||
| 45 | after_load_file => \&check, | ||||||||||||||||||||||||||||
| 46 | before_save_localized_file => \&check, | ||||||||||||||||||||||||||||
| 47 | }); | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 1 | $self->{stylesheets} = {}; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | sub validate_data { | ||||||||||||||||||||||||||||
| 53 | 1 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
validate_data: called CC=8 SCAR=30.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 1 | $self->SUPER::validate_data; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 1 | ▶ | die "'apply' parameter is not specified and no 'if' blocks found" if !exists $self->{data}->{if} && !$self->{data}->{apply}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $self->{"data"}{"if"} or $self->{"data"}{"apply"}
MC/DC0%0/2
exists $self->{"data"}{"if"} or $self->{"data"}{"apply"}
exists $self->{"data"}{"if"}
$self->{"data"}{"apply"}
Truth table33%1/3
A or B
A= exists $self->{"data"}{"if"}B= $self->{"data"}{"apply"}
| |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 1 | ▶ | if (exists $self->{data}->{if}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 0 | foreach my $block (@{$self->{data}->{if}}) { | |||||||||||||||||||||||||||
| 61 | 0 | ▶ | die "'apply' parameter is not specified inside if/then block" if !$block->{then}->{apply}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 1 | ▶ | eval('use XML::LibXSLT;'); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 66 | 1 | ▶ | die "ERROR: To use the apply_xslt plugin, please install XML::LibXSLT module (run 'cpan XML::LibXSLT')\n" if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | eval('use XML::LibXML;'); | |||||||||||||||||||||||||||
| 69 | 0 | ▶ | die "ERROR: To use the apply_xslt plugin, please install XML::LibXML module (run 'cpan XML::LibXML')\n" if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | sub adjust_phases { | ||||||||||||||||||||||||||||
| 73 | 0 | ▶ | my ($self, $phases) = @_; | ||||||||||||||||||||||||||
|
Subroutine
adjust_phases: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | $self->SUPER::adjust_phases($phases); | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | # this plugin makes sense only when applied to a single phase | ||||||||||||||||||||||||||||
| 78 | # (in addition to 'before_job' phase inherited from Serge::Engine::Plugin::if plugin) | ||||||||||||||||||||||||||||
| 79 | 0 | ▶ | die "This plugin needs to be attached to only one phase at a time" unless @$phases == 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | sub process_then_block { | ||||||||||||||||||||||||||||
| 83 | 0 | ▶ | my ($self, $phase, $block, $file, $lang, $strref) = @_; | ||||||||||||||||||||||||||
|
Subroutine
process_then_block: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | #print "::process_then_block(), phase=[$phase], block=[$block], file=[$file], lang=[$lang], strref=[$strref]\n"; | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | my $parser = XML::LibXML->new(); | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | my $source = $parser->parse_string($$strref); | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 0 | my $apply_list = $block->{apply}; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 0 | foreach my $xslt_filepath (@$apply_list) { | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 0 | ▶ | if (not exists $self->{stylesheets}->{$xslt_filepath}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 0 | my $xslt = XML::LibXSLT->new(); | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 0 | my $style_doc = $parser->parse_file($xslt_filepath); | |||||||||||||||||||||||||||
| 99 | 0 | my $stylesheet = $xslt->parse_stylesheet($style_doc); | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | $self->{stylesheets}->{$xslt_filepath} = $stylesheet; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 0 | my $stylesheet = $self->{stylesheets}->{$xslt_filepath}; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 0 | ▶ | if (defined $block->{params}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 0 | my $params_list = $block->{params}; | |||||||||||||||||||||||||||
| 108 | 0 | my %params = $self->to_xslt_params($params_list); | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 0 | $source = $stylesheet->transform($source, %params); | |||||||||||||||||||||||||||
| 111 | } else { | ||||||||||||||||||||||||||||
| 112 | 0 | $source = $stylesheet->transform($source); | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 0 | my $source_as_string = $stylesheet->output_string($source); | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | $source_as_string = decode_utf8($source_as_string); | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 0 | $$strref = $source_as_string; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 0 | return $self->SUPER::process_then_block(shift @_); | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | sub to_xslt_params { | ||||||||||||||||||||||||||||
| 126 | 0 | ▶ | my ($self, $params) = @_; | ||||||||||||||||||||||||||
|
Subroutine
to_xslt_params: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 0 | my %xslt_params; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 0 | foreach my $key (keys %{$params}) { | |||||||||||||||||||||||||||
| 131 | 0 | my $value = $params->{$key}; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 0 | $value = $self->subst_captures($value); | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 0 | $xslt_params{$key} = $value; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 0 | my %transformed_params = XML::LibXSLT::xpath_to_string(%xslt_params); | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 0 | return %transformed_params; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | sub check { | ||||||||||||||||||||||||||||
| 144 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
check: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 145 | 0 | return $self->SUPER::check(@_); | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 2 | 1; | |||||||||||||||||||||||||||