Debian Codegraph @

i18n/convpreset.pl

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