source: osm/applications/editors/josm/i18n/convpreset.pl@ 34839

Last change on this file since 34839 was 34809, checked in by simon04, 6 years ago

see #josm17173 - Update convpreset.pl

Add support for <link wiki="..." />

File size: 5.9 KB
RevLine 
[11529]1#! /usr/bin/perl -w
2
3# Written by Dirk Stöcker <openstreetmap@dstoecker.de>
4# Public domain, no rights reserved.
5
6use strict;
7
8my $item = "";
[32241]9my $chunk = "";
[11529]10my $group;
[24764]11my $combo_n;
[28250]12my $combo_type;
[25155]13my $result = 0;
[11529]14my $comment = 0;
[28250]15my $vctx;
[11529]16
17# This is a simple conversion and in no way a complete XML parser
18# but it works with a default Perl installation
19
[13392]20# Print a header to write valid Java code. No line break,
21# so that the input and output line numbers will match.
22print "class trans_preset { void tr(String s){} void f() {";
23
[26180]24sub fix($)
25{
26 my ($val) = @_;
27 $val =~ s/'/''/g;
[31076]28 $val =~ s/&lt;/</g;
29 $val =~ s/&gt;/>/g;
30 $val =~ s/&amp;/&/g;
[26180]31 return $val;
32}
33
[32241]34sub infoblock
35{
36 my $r = "";
37 $r .= " item $item" if $item;
38 $r .= " chunk $chunk" if $chunk;
39 $r .= " group $group" if $group;
[33841]40 $r .= " combo $combo_type $combo_n" if $combo_type;
[32241]41 $r .= " $_[0]" if $_[0];
42 return $r ? "/* $r */ " : "";
43}
44
[29933]45my $linenr = 0;
[11529]46while(my $line = <>)
47{
[29933]48 ++$linenr;
[11529]49 chomp($line);
[31901]50 print "tr(\"---DUMMY-MARKER---\"); ";
[33841]51 if($line =~ /<item\s+name=(".*?")/ || $line =~ /<item.* name=(".*?")/)
[11529]52 {
[26180]53 my $val = fix($1);
[17969]54 $item = $group ? "$group$val" : $val;
[11529]55 $item =~ s/""/\//;
[17969]56 if($line =~ /name_context=(".*?")/)
57 {
[32241]58 print infoblock() . "trc($1, $val);\n";
[17969]59 }
60 else
61 {
[32241]62 print infoblock() . "tr($val);\n";
[17969]63 }
[11529]64 }
[32280]65 elsif($line =~ /<chunk\s+id=(".*?")/)
[32241]66 {
67 $chunk = fix($1);
68 }
[17969]69 elsif($line =~ /<group.*\s+name=(".*?")/)
[11529]70 {
[26180]71 my $gr = fix($1);
[17973]72 $group = $group ? "$group$gr" : $gr;
[17969]73 $group =~ s/\"\"/\//;
74 if($line =~ /name_context=(".*?")/)
75 {
[32241]76 print infoblock() . "trc($1,$gr);\n";
[17969]77 }
78 else
79 {
[32241]80 print infoblock() . "tr($gr);\n";
[17969]81 }
[11529]82 }
[17969]83 elsif($line =~ /<label.*\s+text=" "/)
[11529]84 {
[32241]85 print infoblock("empty label") . "\n";
[11529]86 }
[17969]87 elsif($line =~ /<label.*\s+text=(".*?")/)
[11529]88 {
[26180]89 my $text = fix($1);
[17969]90 if($line =~ /text_context=(".*?")/)
91 {
[32241]92 print infoblock("label $text") ."trc($1,$text);\n";
[17969]93 }
94 else
95 {
[32241]96 print infoblock("label $text") . "tr($text);\n";
[17969]97 }
[11529]98 }
[17969]99 elsif($line =~ /<text.*\s+text=(".*?")/)
[11529]100 {
[26180]101 my $n = fix($1);
[17969]102 if($line =~ /text_context=(".*?")/)
103 {
[32241]104 print infoblock("text $n") . "trc($1,$n);\n";
[17969]105 }
106 else
107 {
[32241]108 print infoblock("text $n") . "tr($n);\n";
[17969]109 }
[11529]110 }
[17969]111 elsif($line =~ /<check.*\s+text=(".*?")/)
[11529]112 {
[26180]113 my $n = fix($1);
[17969]114 if($line =~ /text_context=(".*?")/)
115 {
[32241]116 print infoblock("check $n") . "trc($1,$n);\n";
[17969]117 }
118 else
119 {
[32241]120 print infoblock("check $n") . "tr($n);\n";
[17969]121 }
[11529]122 }
[17969]123 elsif($line =~ /<role.*\s+text=(".*?")/)
[13469]124 {
[26180]125 my $n = fix($1);
[17969]126 if($line =~ /text_context=(".*?")/)
127 {
[32241]128 print infoblock("role $n") . "trc($1,$n);\n";
[17969]129 }
130 else
131 {
[32241]132 print infoblock("role $n") . "tr($n);\n";
[17969]133 }
[13469]134 }
[29933]135 elsif($line =~ /<optional.*\s+text=(".*?")/)
136 {
137 my $n = fix($1);
138 if($line =~ /text_context=(".*?")/)
139 {
[32241]140 print infoblock("optional $n") . "trc($1,$n);\n";
[29933]141 }
142 else
143 {
[32241]144 print infoblock("optional $n") . "tr($n);\n";
[29933]145 }
146 }
[28250]147 elsif($line =~ /<(combo|multiselect).*\s+text=(".*?")/)
[11529]148 {
[28250]149 my ($type,$n) = ($1,fix($2));
150 $combo_n = $n;
151 $combo_type = $type;
152 $vctx = ($line =~ /values_context=(".*?")/) ? $1 : undef;
153 # text
154 my $tctx = ($line =~ /text_context=(".*?")/) ? $1 : undef;
[32241]155 print infoblock("$type $n") . ($tctx ? " trc($tctx, $n);" : " tr($n);");
[28250]156 # display_values / values
[30038]157 my $sp = ($line =~ /delimiter="(.*?)"/) ? $1 : ($type eq "combo" ? ",":";");
[31111]158 my $vals = ($line =~ / display_values="(.*?)"/) ? $1 : ($line =~ /values="(.*?)"/) ? $1 : undef;
[31634]159 $vals = undef if ($line =~ /values_no_i18n="true"/);
[28250]160 if($vals)
[17969]161 {
[30038]162 my @combo_values = split "\Q$sp\E" ,$vals;
[28250]163 foreach my $val (@combo_values)
164 {
165 next if $val =~ /^[0-9-]+$/; # search for non-numbers
166 $val = fix($val);
[32241]167 print infoblock("$type $n display value") . ($vctx ? " trc($vctx, \"$val\");" : " tr(\"$val\");");
[28250]168 }
[17969]169 }
[11529]170 print "\n";
171 }
[28250]172 elsif(!$comment && $line =~ /<list_entry/)
[11529]173 {
[28250]174 my $vctxi = ($line =~ /value_context=(".*?")/) ? $1 : $vctx;
175 my $value = ($line =~ /value=(".*?")/) ? $1 : undef;
[33841]176 if($line =~ /[^.]display_value=(".*?")/)
[17969]177 {
[28250]178 my $val = fix($1);
[33841]179 print infoblock("entry $value display value") . ($vctxi ? " trc($vctxi, $val);" : " tr($val);");
[17969]180 }
181 else
182 {
[28250]183 my $val = fix($value);
[33841]184 print infoblock("entry $value display value") . ($vctxi ? " trc($vctxi, $val);" : " tr($val);");
[17969]185 }
[28250]186 if($line =~ /short_description=(".*?")/)
[11529]187 {
[28250]188 my $val = fix($1);
[33841]189 print infoblock("entry $value short description") . "tr($val);";
[11529]190 }
[28252]191 print "\n";
[11529]192 }
193 elsif($line =~ /<\/group>/)
194 {
[17969]195 $group = 0 if !($group =~ s/(.*\/).*?$//);
[11529]196 print "\n";
197 }
198 elsif($line =~ /<\/item>/)
199 {
200 $item = "";
201 print "\n";
202 }
[32241]203 elsif($line =~ /<\/chunk>/)
204 {
205 $chunk = "";
206 }
[31111]207 elsif($line =~ /<\/(combo|multiselect)/)
[24764]208 {
209 $combo_n = "";
[29999]210 print "\n";
[24764]211 }
[13469]212 elsif(!$line)
213 {
214 print "\n";
215 }
[11529]216 elsif($line =~ /^\s*$/
[13140]217 || $line =~ /<separator *\/>/
218 || $line =~ /<space *\/>/
219 || $line =~ /<\/?optional>/
[11529]220 || $line =~ /<key/
[25155]221 || $line =~ /<presets/
[29933]222 || $line =~ /<checkgroup/
223 || $line =~ /<\/checkgroup/
[25155]224 || $line =~ /<\/presets/
[13469]225 || $line =~ /roles/
[34809]226 || $line =~ /<link wiki=/
[13469]227 || $line =~ /href=/
[11529]228 || $line =~ /<!--/
[25155]229 || $line =~ /<\?xml/
[11529]230 || $line =~ /-->/
[30159]231 || $line =~ /<\/?chunk/
232 || $line =~ /<reference/
233 || $line =~ /<preset_link/
[31139]234 || $line =~ /<item_separator\/>/
[11529]235 || $comment)
236 {
[31111]237 $line =~ s/[ \t]+((?:short)?description) *= *"([^"]+)/*\/ \/* $1 *\/ tr("$2"); \/*/g;
238 print "/* $line */\n";
[11529]239 }
240 else
241 {
242 print "/* unparsed line $line */\n";
[29933]243 print STDERR "/* unparsed line $linenr $line */\n";
[25155]244 $result = 20
[11529]245 }
246
247 # note, these two must be in this order ore oneliners aren't handled
248 $comment = 1 if($line =~ /<!--/);
249 $comment = 0 if($line =~ /-->/);
250}
[13392]251
252print "}}\n";
[29208]253exit($result) if $result;
Note: See TracBrowser for help on using the repository browser.