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

Last change on this file since 33691 was 32280, checked in by stoecker, 9 years ago

fix typo

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;
40 $r .= " $_[0]" if $_[0];
41 return $r ? "/* $r */ " : "";
42}
43
[29933]44my $linenr = 0;
[11529]45while(my $line = <>)
46{
[29933]47 ++$linenr;
[11529]48 chomp($line);
[31901]49 print "tr(\"---DUMMY-MARKER---\"); ";
[11529]50 if($line =~ /<item\s+name=(".*?")/)
51 {
[26180]52 my $val = fix($1);
[17969]53 $item = $group ? "$group$val" : $val;
[11529]54 $item =~ s/""/\//;
[17969]55 if($line =~ /name_context=(".*?")/)
56 {
[32241]57 print infoblock() . "trc($1, $val);\n";
[17969]58 }
59 else
60 {
[32241]61 print infoblock() . "tr($val);\n";
[17969]62 }
[11529]63 }
[32280]64 elsif($line =~ /<chunk\s+id=(".*?")/)
[32241]65 {
66 $chunk = fix($1);
67 }
[17969]68 elsif($line =~ /<group.*\s+name=(".*?")/)
[11529]69 {
[26180]70 my $gr = fix($1);
[17973]71 $group = $group ? "$group$gr" : $gr;
[17969]72 $group =~ s/\"\"/\//;
73 if($line =~ /name_context=(".*?")/)
74 {
[32241]75 print infoblock() . "trc($1,$gr);\n";
[17969]76 }
77 else
78 {
[32241]79 print infoblock() . "tr($gr);\n";
[17969]80 }
[11529]81 }
[17969]82 elsif($line =~ /<label.*\s+text=" "/)
[11529]83 {
[32241]84 print infoblock("empty label") . "\n";
[11529]85 }
[17969]86 elsif($line =~ /<label.*\s+text=(".*?")/)
[11529]87 {
[26180]88 my $text = fix($1);
[17969]89 if($line =~ /text_context=(".*?")/)
90 {
[32241]91 print infoblock("label $text") ."trc($1,$text);\n";
[17969]92 }
93 else
94 {
[32241]95 print infoblock("label $text") . "tr($text);\n";
[17969]96 }
[11529]97 }
[17969]98 elsif($line =~ /<text.*\s+text=(".*?")/)
[11529]99 {
[26180]100 my $n = fix($1);
[17969]101 if($line =~ /text_context=(".*?")/)
102 {
[32241]103 print infoblock("text $n") . "trc($1,$n);\n";
[17969]104 }
105 else
106 {
[32241]107 print infoblock("text $n") . "tr($n);\n";
[17969]108 }
[11529]109 }
[17969]110 elsif($line =~ /<check.*\s+text=(".*?")/)
[11529]111 {
[26180]112 my $n = fix($1);
[17969]113 if($line =~ /text_context=(".*?")/)
114 {
[32241]115 print infoblock("check $n") . "trc($1,$n);\n";
[17969]116 }
117 else
118 {
[32241]119 print infoblock("check $n") . "tr($n);\n";
[17969]120 }
[11529]121 }
[17969]122 elsif($line =~ /<role.*\s+text=(".*?")/)
[13469]123 {
[26180]124 my $n = fix($1);
[17969]125 if($line =~ /text_context=(".*?")/)
126 {
[32241]127 print infoblock("role $n") . "trc($1,$n);\n";
[17969]128 }
129 else
130 {
[32241]131 print infoblock("role $n") . "tr($n);\n";
[17969]132 }
[13469]133 }
[29933]134 elsif($line =~ /<optional.*\s+text=(".*?")/)
135 {
136 my $n = fix($1);
137 if($line =~ /text_context=(".*?")/)
138 {
[32241]139 print infoblock("optional $n") . "trc($1,$n);\n";
[29933]140 }
141 else
142 {
[32241]143 print infoblock("optional $n") . "tr($n);\n";
[29933]144 }
145 }
[28250]146 elsif($line =~ /<(combo|multiselect).*\s+text=(".*?")/)
[11529]147 {
[28250]148 my ($type,$n) = ($1,fix($2));
149 $combo_n = $n;
150 $combo_type = $type;
151 $vctx = ($line =~ /values_context=(".*?")/) ? $1 : undef;
152 # text
153 my $tctx = ($line =~ /text_context=(".*?")/) ? $1 : undef;
[32241]154 print infoblock("$type $n") . ($tctx ? " trc($tctx, $n);" : " tr($n);");
[28250]155 # display_values / values
[30038]156 my $sp = ($line =~ /delimiter="(.*?)"/) ? $1 : ($type eq "combo" ? ",":";");
[31111]157 my $vals = ($line =~ / display_values="(.*?)"/) ? $1 : ($line =~ /values="(.*?)"/) ? $1 : undef;
[31634]158 $vals = undef if ($line =~ /values_no_i18n="true"/);
[28250]159 if($vals)
[17969]160 {
[30038]161 my @combo_values = split "\Q$sp\E" ,$vals;
[28250]162 foreach my $val (@combo_values)
163 {
164 next if $val =~ /^[0-9-]+$/; # search for non-numbers
165 $val = fix($val);
[32241]166 print infoblock("$type $n display value") . ($vctx ? " trc($vctx, \"$val\");" : " tr(\"$val\");");
[28250]167 }
[17969]168 }
[11529]169 print "\n";
170 }
[28250]171 elsif(!$comment && $line =~ /<list_entry/)
[11529]172 {
[28250]173 my $vctxi = ($line =~ /value_context=(".*?")/) ? $1 : $vctx;
174 my $value = ($line =~ /value=(".*?")/) ? $1 : undef;
175 if($line =~ /display_value=(".*?")/)
[17969]176 {
[28250]177 my $val = fix($1);
[32241]178 print infoblock("$combo_type $combo_n entry $value display value") . ($vctxi ? " trc($vctxi, $val);" : " tr($val);");
[17969]179 }
180 else
181 {
[28250]182 my $val = fix($value);
[32241]183 print infoblock("$combo_type $combo_n entry $value display value") . ($vctxi ? " trc($vctxi, $val);" : " tr($val);");
[17969]184 }
[28250]185 if($line =~ /short_description=(".*?")/)
[11529]186 {
[28250]187 my $val = fix($1);
[32241]188 print infoblock("$combo_type $combo_n entry $value short description") . "tr($val);";
[11529]189 }
[28252]190 print "\n";
[11529]191 }
192 elsif($line =~ /<\/group>/)
193 {
[17969]194 $group = 0 if !($group =~ s/(.*\/).*?$//);
[11529]195 print "\n";
196 }
197 elsif($line =~ /<\/item>/)
198 {
199 $item = "";
200 print "\n";
201 }
[32241]202 elsif($line =~ /<\/chunk>/)
203 {
204 $chunk = "";
205 }
[31111]206 elsif($line =~ /<\/(combo|multiselect)/)
[24764]207 {
208 $combo_n = "";
[29999]209 print "\n";
[24764]210 }
[13469]211 elsif(!$line)
212 {
213 print "\n";
214 }
[11529]215 elsif($line =~ /^\s*$/
[13140]216 || $line =~ /<separator *\/>/
217 || $line =~ /<space *\/>/
218 || $line =~ /<\/?optional>/
[11529]219 || $line =~ /<key/
[25155]220 || $line =~ /<presets/
[29933]221 || $line =~ /<checkgroup/
222 || $line =~ /<\/checkgroup/
[25155]223 || $line =~ /<\/presets/
[13469]224 || $line =~ /roles/
225 || $line =~ /href=/
[11529]226 || $line =~ /<!--/
[25155]227 || $line =~ /<\?xml/
[11529]228 || $line =~ /-->/
[30159]229 || $line =~ /<\/?chunk/
230 || $line =~ /<reference/
231 || $line =~ /<preset_link/
[31139]232 || $line =~ /<item_separator\/>/
[11529]233 || $comment)
234 {
[31111]235 $line =~ s/[ \t]+((?:short)?description) *= *"([^"]+)/*\/ \/* $1 *\/ tr("$2"); \/*/g;
236 print "/* $line */\n";
[11529]237 }
238 else
239 {
240 print "/* unparsed line $line */\n";
[29933]241 print STDERR "/* unparsed line $linenr $line */\n";
[25155]242 $result = 20
[11529]243 }
244
245 # note, these two must be in this order ore oneliners aren't handled
246 $comment = 1 if($line =~ /<!--/);
247 $comment = 0 if($line =~ /-->/);
248}
[13392]249
250print "}}\n";
[29208]251exit($result) if $result;
Note: See TracBrowser for help on using the repository browser.