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

Last change on this file since 36027 was 35737, checked in by stoecker, 4 years ago

see #20467 - support more relation types details

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