| 1 | package Serge::Engine::Plugin::parse_properties; | ||||||||||||||||||||
| 2 | 1 | ▶ | use parent Serge::Engine::Plugin::Base::Parser; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | # spec: http://java.sun.com/javase/6/docs/api/java/util/Properties.html#load(java.io.Reader) | ||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | sub name { | ||||||||||||||||||||
| 9 | 0 | ▶ | return 'Java JDK .properties (resource bundle) parser plugin'; | ||||||||||||||||||
|
Subroutine
name: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 10 | } | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | sub init { | ||||||||||||||||||||
| 13 | 0 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
init: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 0 | $self->SUPER::init(@_); | |||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 0 | $self->merge_schema({ | |||||||||||||||||||
| 18 | escaped_quotes => 'BOOLEAN', | ||||||||||||||||||||
| 19 | }); | ||||||||||||||||||||
| 20 | } | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | # TODO: | ||||||||||||||||||||
| 23 | # - Support ``key = value'' pairs that span multiple lines | ||||||||||||||||||||
| 24 | # - Better handling of comments | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | sub parse { | ||||||||||||||||||||
| 27 | 0 | ▶ | my ($self, $textref, $callbackref, $lang) = @_; | ||||||||||||||||||
|
Subroutine
parse: not called CC=23 SCAR=63.1
Pod
undocumented
| |||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | 0 | ▶ | die 'callbackref not specified' unless $callbackref; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 0 | my $translated_text; | |||||||||||||||||||
| 32 | my $skip; | ||||||||||||||||||||
| 33 | 0 | my $context_line; | |||||||||||||||||||
| 34 | 0 | my %flags; | |||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | # Finding translatable strings in file | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | # Format is: | ||||||||||||||||||||
| 39 | # | ||||||||||||||||||||
| 40 | # #.flag | ||||||||||||||||||||
| 41 | # #.flag2=flag 2 value | ||||||||||||||||||||
| 42 | # key = value | ||||||||||||||||||||
| 43 | # | ||||||||||||||||||||
| 44 | # Special flags are: | ||||||||||||||||||||
| 45 | # `internal` (the string is skipped) | ||||||||||||||||||||
| 46 | # `context` (sets the context for the string) | ||||||||||||||||||||
| 47 | # `comment` (appends a line to the hint message) | ||||||||||||||||||||
| 48 | # All other flags are added as hashtags to the hint message | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | # Make a copy of the string as we will change it | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 0 | my $source_text = $$textref; | |||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | 0 | $source_text =~ s/\\\n//sg; # merge multi-line strings | |||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | 0 | foreach my $line (split(/\n/, $source_text)) { | |||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | 0 | my $hint; | |||||||||||||||||||
| 59 | my $property_key; | ||||||||||||||||||||
| 60 | 0 | my $context; | |||||||||||||||||||
| 61 | 0 | my $orig_str; | |||||||||||||||||||
| 62 | 0 | my $translated_str; | |||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 0 | ▶ | if ($line =~ m/^[\t ]*[\#\!]/) { # a comment line | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 65 | 0 | my $comment = $line; | |||||||||||||||||||
| 66 | 0 | $comment =~ s/^\s+//s; # trim left | |||||||||||||||||||
| 67 | 0 | $comment =~ s/\s+$//s; # trim right | |||||||||||||||||||
| 68 | 0 | ▶ | if ($comment =~ m/^#\.([^\s=:]+)([=:](.*))?$/) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 69 | 0 | my $param = $1; | |||||||||||||||||||
| 70 | 0 | my $value = $3; | |||||||||||||||||||
| 71 | 0 | ▶ | $flags{$param} = [] unless exists $flags{$param}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 72 | 0 | push @{$flags{$param}}, $value; | |||||||||||||||||||
| 73 | } | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | # check if there is a special comment line '#.context:string' which will | ||||||||||||||||||||
| 76 | # set the context for the next translatable line | ||||||||||||||||||||
| 77 | 0 | ▶ | ($line =~ m/^[\t ]*[\#\!]\.context\:(.*?)[\t ]*$/) && ($context_line = $1); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | } else { | ||||||||||||||||||||
| 80 | 0 | ▶ | if ($line =~ m/^[\t ]*(.*?[^\\])(?:[\t ]*[=:][\t ]*| )[\t ]*(.*)$/) { # a parameter line | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 81 | 0 | ▶ | if (!exists $flags{internal}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 82 | 0 | $property_key = $hint = $1; | |||||||||||||||||||
| 83 | 0 | $hint =~ s/[\t ]+$//; # trim from the right | |||||||||||||||||||
| 84 | 0 | $orig_str = $2; | |||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | # convert value from the array to a string representation | ||||||||||||||||||||
| 87 | # (or `undef` if all array items are undefined) | ||||||||||||||||||||
| 88 | 0 | foreach my $key (keys %flags) { | |||||||||||||||||||
| 89 | 0 | my $value = undef; | |||||||||||||||||||
| 90 | map { | ||||||||||||||||||||
| 91 | 0 | ▶ | $value = 1 if defined $_; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 92 | 0 | } @{$flags{$key}}; | |||||||||||||||||||
| 93 | 0 | ▶ | if (defined $value) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 94 | 0 | $value = join("\n", @{$flags{$key}}); | |||||||||||||||||||
| 95 | # trim the value and remove blank lines | ||||||||||||||||||||
| 96 | 0 | $value =~ s/^\s+//s; | |||||||||||||||||||
| 97 | 0 | $value =~ s/\s+$//s; | |||||||||||||||||||
| 98 | 0 | $value =~ s/\n{2,}$/\n/sg; | |||||||||||||||||||
| 99 | } | ||||||||||||||||||||
| 100 | # save the value back | ||||||||||||||||||||
| 101 | 0 | $flags{$key} = $value; | |||||||||||||||||||
| 102 | } | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | 0 | ▶ | if (exists $flags{comment}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 105 | 0 | ▶ | $hint .= "\n" if $hint ne ''; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 106 | 0 | $hint .= $flags{comment}; | |||||||||||||||||||
| 107 | 0 | delete $flags{comment}; | |||||||||||||||||||
| 108 | } | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | 0 | ▶ | if (exists $flags{context}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 111 | 0 | $context = $flags{context}; | |||||||||||||||||||
| 112 | 0 | delete $flags{context}; | |||||||||||||||||||
| 113 | } | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | # append flags as hashtags | ||||||||||||||||||||
| 116 | 0 | my @tags; | |||||||||||||||||||
| 117 | 0 | foreach my $key (sort keys %flags) { | |||||||||||||||||||
| 118 | 0 | my $tag = '#'.$key; | |||||||||||||||||||
| 119 | 0 | ▶ | $tag .= '='.$flags{$key} if defined $flags{$key}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 120 | 0 | push @tags, $tag; | |||||||||||||||||||
| 121 | } | ||||||||||||||||||||
| 122 | 0 | ▶ | if (scalar @tags > 0) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 123 | 0 | ▶ | $hint .= "\n" if $hint ne ''; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 124 | 0 | $hint .= join(' ', @tags); | |||||||||||||||||||
| 125 | } | ||||||||||||||||||||
| 126 | } | ||||||||||||||||||||
| 127 | } | ||||||||||||||||||||
| 128 | 0 | $context_line = undef; # resetting the context line | |||||||||||||||||||
| 129 | 0 | undef %flags; # reset the flags hash | |||||||||||||||||||
| 130 | } | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | 0 | ▶ | if ($orig_str) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 133 | 0 | my $str = $orig_str; | |||||||||||||||||||
| 134 | # decode \uXXXX | ||||||||||||||||||||
| 135 | 0 | $str =~ s/\\u([0-9A-Fa-f]{4})/chr(hex($1))/ge; | |||||||||||||||||||
| 136 | 0 | $str =~ s/''/'/g; # unescape single quotes (always, disregarding the 'escaped_quotes' parameter) | |||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | 0 | $translated_str = &$callbackref($str, $context, $hint, undef, $lang, $property_key); | |||||||||||||||||||
| 139 | } | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | 0 | ▶ | if ($lang) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 142 | # Per prior research: turns out Java/JSP is inconsistent. | ||||||||||||||||||||
| 143 | # We can probably say that anything that has {#} will have '' | ||||||||||||||||||||
| 144 | # Others will have ' | ||||||||||||||||||||
| 145 | 0 | ▶ | my $need_escape = $self->{data}->{escaped_quotes} || ($orig_str =~ m/\{\d+\}/); | ||||||||||||||||||
|
Condition0%0/3
$self->{'data'}{'escaped_quotes'} || $orig_str =~ /\{\d+\}/
MC/DC0%0/2
$self->{'data'}{'escaped_quotes'} || $orig_str =~ /\{\d+\}/
$self->{'data'}{'escaped_quotes'}
$orig_str =~ /\{\d+\}/
Truth table0%0/3
A || B
A= $self->{'data'}{'escaped_quotes'}B= $orig_str =~ /\{\d+\}/
| |||||||||||||||||||||
| 146 | 0 | ▶ | if ($need_escape) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 147 | 0 | $translated_str =~ s/'/''/g; # escape single quotes | |||||||||||||||||||
| 148 | } | ||||||||||||||||||||
| 149 | 0 | $translated_str =~ s/\n/\\n/g; | |||||||||||||||||||
| 150 | 0 | $line =~ s/\Q$orig_str\E$/$translated_str/; | |||||||||||||||||||
| 151 | 0 | $translated_text .= $line."\n"; | |||||||||||||||||||
| 152 | } | ||||||||||||||||||||
| 153 | } | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | 0 | return $translated_text; | |||||||||||||||||||
| 156 | } | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | 1 | 1; | |||||||||||||||||||