[11529] | 1 | #! /usr/bin/perl -w
|
---|
| 2 |
|
---|
| 3 | # Written by Dirk Stöcker <openstreetmap@dstoecker.de>
|
---|
| 4 | # Public domain, no rights reserved.
|
---|
| 5 |
|
---|
| 6 | use strict;
|
---|
| 7 |
|
---|
| 8 | my $item = "";
|
---|
[32241] | 9 | my $chunk = "";
|
---|
[11529] | 10 | my $group;
|
---|
[24764] | 11 | my $combo_n;
|
---|
[28250] | 12 | my $combo_type;
|
---|
[25155] | 13 | my $result = 0;
|
---|
[11529] | 14 | my $comment = 0;
|
---|
[28250] | 15 | my $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.
|
---|
| 22 | print "class trans_preset { void tr(String s){} void f() {";
|
---|
| 23 |
|
---|
[26180] | 24 | sub fix($)
|
---|
| 25 | {
|
---|
| 26 | my ($val) = @_;
|
---|
| 27 | $val =~ s/'/''/g;
|
---|
[31076] | 28 | $val =~ s/</</g;
|
---|
| 29 | $val =~ s/>/>/g;
|
---|
| 30 | $val =~ s/&/&/g;
|
---|
[26180] | 31 | return $val;
|
---|
| 32 | }
|
---|
| 33 |
|
---|
[32241] | 34 | sub 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] | 45 | my $linenr = 0;
|
---|
[11529] | 46 | while(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 | }
|
---|
[35422] | 135 | elsif($line =~ /<(optional|preset_link).*\s+text=(".*?")/)
|
---|
[29933] | 136 | {
|
---|
[35422] | 137 | my ($type,$n) = ($1,fix($2));
|
---|
[29933] | 138 | if($line =~ /text_context=(".*?")/)
|
---|
| 139 | {
|
---|
[35422] | 140 | print infoblock("$type $n") . "trc($1,$n);\n";
|
---|
[29933] | 141 | }
|
---|
| 142 | else
|
---|
| 143 | {
|
---|
[35422] | 144 | print infoblock("$type $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 | }
|
---|
[35737] | 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 | }
|
---|
[13469] | 218 | elsif(!$line)
|
---|
| 219 | {
|
---|
| 220 | print "\n";
|
---|
| 221 | }
|
---|
[11529] | 222 | elsif($line =~ /^\s*$/
|
---|
[13140] | 223 | || $line =~ /<separator *\/>/
|
---|
| 224 | || $line =~ /<space *\/>/
|
---|
| 225 | || $line =~ /<\/?optional>/
|
---|
[11529] | 226 | || $line =~ /<key/
|
---|
[25155] | 227 | || $line =~ /<presets/
|
---|
[29933] | 228 | || $line =~ /<checkgroup/
|
---|
| 229 | || $line =~ /<\/checkgroup/
|
---|
[25155] | 230 | || $line =~ /<\/presets/
|
---|
[13469] | 231 | || $line =~ /roles/
|
---|
[34809] | 232 | || $line =~ /<link wiki=/
|
---|
[13469] | 233 | || $line =~ /href=/
|
---|
[11529] | 234 | || $line =~ /<!--/
|
---|
[25155] | 235 | || $line =~ /<\?xml/
|
---|
[11529] | 236 | || $line =~ /-->/
|
---|
[30159] | 237 | || $line =~ /<\/?chunk/
|
---|
| 238 | || $line =~ /<reference/
|
---|
| 239 | || $line =~ /<preset_link/
|
---|
[31139] | 240 | || $line =~ /<item_separator\/>/
|
---|
[11529] | 241 | || $comment)
|
---|
| 242 | {
|
---|
[31111] | 243 | $line =~ s/[ \t]+((?:short)?description) *= *"([^"]+)/*\/ \/* $1 *\/ tr("$2"); \/*/g;
|
---|
| 244 | print "/* $line */\n";
|
---|
[11529] | 245 | }
|
---|
| 246 | else
|
---|
| 247 | {
|
---|
| 248 | print "/* unparsed line $line */\n";
|
---|
[29933] | 249 | print STDERR "/* unparsed line $linenr $line */\n";
|
---|
[25155] | 250 | $result = 20
|
---|
[11529] | 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 | }
|
---|
[13392] | 257 |
|
---|
| 258 | print "}}\n";
|
---|
[29208] | 259 | exit($result) if $result;
|
---|