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

Last change on this file since 34775 was 33841, checked in by stoecker, 7 years ago

fix recent parsing errors

File size: 5.9 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.*\s+text=(".*?")/)
136 {
137 my $n = fix($1);
138 if($line =~ /text_context=(".*?")/)
139 {
140 print infoblock("optional $n") . "trc($1,$n);\n";
141 }
142 else
143 {
144 print infoblock("optional $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 elsif(!$line)
213 {
214 print "\n";
215 }
216 elsif($line =~ /^\s*$/
217 || $line =~ /<separator *\/>/
218 || $line =~ /<space *\/>/
219 || $line =~ /<\/?optional>/
220 || $line =~ /<key/
221 || $line =~ /<presets/
222 || $line =~ /<checkgroup/
223 || $line =~ /<\/checkgroup/
224 || $line =~ /<\/presets/
225 || $line =~ /roles/
226 || $line =~ /href=/
227 || $line =~ /<!--/
228 || $line =~ /<\?xml/
229 || $line =~ /-->/
230 || $line =~ /<\/?chunk/
231 || $line =~ /<reference/
232 || $line =~ /<preset_link/
233 || $line =~ /<item_separator\/>/
234 || $comment)
235 {
236 $line =~ s/[ \t]+((?:short)?description) *= *"([^"]+)/*\/ \/* $1 *\/ tr("$2"); \/*/g;
237 print "/* $line */\n";
238 }
239 else
240 {
241 print "/* unparsed line $line */\n";
242 print STDERR "/* unparsed line $linenr $line */\n";
243 $result = 20
244 }
245
246 # note, these two must be in this order ore oneliners aren't handled
247 $comment = 1 if($line =~ /<!--/);
248 $comment = 0 if($line =~ /-->/);
249}
250
251print "}}\n";
252exit($result) if $result;
Note: See TracBrowser for help on using the repository browser.