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