[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 = "";
|
---|
| 9 | my $group;
|
---|
[24764] | 10 | my $combo_n;
|
---|
[28250] | 11 | my $combo_type;
|
---|
[25155] | 12 | my $result = 0;
|
---|
[11529] | 13 | my $comment = 0;
|
---|
[28250] | 14 | my $vctx;
|
---|
[11529] | 15 |
|
---|
| 16 | # This is a simple conversion and in no way a complete XML parser
|
---|
| 17 | # but it works with a default Perl installation
|
---|
| 18 |
|
---|
[13392] | 19 | # Print a header to write valid Java code. No line break,
|
---|
| 20 | # so that the input and output line numbers will match.
|
---|
| 21 | print "class trans_preset { void tr(String s){} void f() {";
|
---|
| 22 |
|
---|
[26180] | 23 | sub fix($)
|
---|
| 24 | {
|
---|
| 25 | my ($val) = @_;
|
---|
| 26 | $val =~ s/'/''/g;
|
---|
[31076] | 27 | $val =~ s/</</g;
|
---|
| 28 | $val =~ s/>/>/g;
|
---|
| 29 | $val =~ s/&/&/g;
|
---|
[26180] | 30 | return $val;
|
---|
| 31 | }
|
---|
| 32 |
|
---|
[29933] | 33 | my $linenr = 0;
|
---|
[11529] | 34 | while(my $line = <>)
|
---|
| 35 | {
|
---|
[29933] | 36 | ++$linenr;
|
---|
[11529] | 37 | chomp($line);
|
---|
[29943] | 38 | print "tr(\"\"); ";
|
---|
[11529] | 39 | if($line =~ /<item\s+name=(".*?")/)
|
---|
| 40 | {
|
---|
[26180] | 41 | my $val = fix($1);
|
---|
[17969] | 42 | $item = $group ? "$group$val" : $val;
|
---|
[11529] | 43 | $item =~ s/""/\//;
|
---|
[17969] | 44 | if($line =~ /name_context=(".*?")/)
|
---|
| 45 | {
|
---|
[23091] | 46 | print "/* item $item */ trc($1, $val);\n";
|
---|
[17969] | 47 | }
|
---|
| 48 | else
|
---|
| 49 | {
|
---|
[23091] | 50 | print "/* item $item */ tr($val);\n";
|
---|
[17969] | 51 | }
|
---|
[11529] | 52 | }
|
---|
[17969] | 53 | elsif($line =~ /<group.*\s+name=(".*?")/)
|
---|
[11529] | 54 | {
|
---|
[26180] | 55 | my $gr = fix($1);
|
---|
[17973] | 56 | $group = $group ? "$group$gr" : $gr;
|
---|
[17969] | 57 | $group =~ s/\"\"/\//;
|
---|
| 58 | if($line =~ /name_context=(".*?")/)
|
---|
| 59 | {
|
---|
[23091] | 60 | print "/* group $group */ trc($1,$gr);\n";
|
---|
[17969] | 61 | }
|
---|
| 62 | else
|
---|
| 63 | {
|
---|
[23091] | 64 | print "/* group $group */ tr($gr);\n";
|
---|
[17969] | 65 | }
|
---|
[11529] | 66 | }
|
---|
[17969] | 67 | elsif($line =~ /<label.*\s+text=" "/)
|
---|
[11529] | 68 | {
|
---|
| 69 | print "/* item $item empty label */\n";
|
---|
| 70 | }
|
---|
[17969] | 71 | elsif($line =~ /<label.*\s+text=(".*?")/)
|
---|
[11529] | 72 | {
|
---|
[26180] | 73 | my $text = fix($1);
|
---|
[17969] | 74 | if($line =~ /text_context=(".*?")/)
|
---|
| 75 | {
|
---|
[23091] | 76 | print "/* item $item label $text */ trc($1,$text);\n";
|
---|
[17969] | 77 | }
|
---|
| 78 | else
|
---|
| 79 | {
|
---|
[23091] | 80 | print "/* item $item label $text */ tr($text);\n";
|
---|
[17969] | 81 | }
|
---|
[11529] | 82 | }
|
---|
[17969] | 83 | elsif($line =~ /<text.*\s+text=(".*?")/)
|
---|
[11529] | 84 | {
|
---|
[26180] | 85 | my $n = fix($1);
|
---|
[17969] | 86 | if($line =~ /text_context=(".*?")/)
|
---|
| 87 | {
|
---|
[23091] | 88 | print "/* item $item text $n */ trc($1,$n);\n";
|
---|
[17969] | 89 | }
|
---|
| 90 | else
|
---|
| 91 | {
|
---|
[23091] | 92 | print "/* item $item text $n */ tr($n);\n";
|
---|
[17969] | 93 | }
|
---|
[11529] | 94 | }
|
---|
[17969] | 95 | elsif($line =~ /<check.*\s+text=(".*?")/)
|
---|
[11529] | 96 | {
|
---|
[26180] | 97 | my $n = fix($1);
|
---|
[17969] | 98 | if($line =~ /text_context=(".*?")/)
|
---|
| 99 | {
|
---|
[23091] | 100 | print "/* item $item check $n */ trc($1,$n);\n";
|
---|
[17969] | 101 | }
|
---|
| 102 | else
|
---|
| 103 | {
|
---|
[23091] | 104 | print "/* item $item check $n */ tr($n);\n";
|
---|
[17969] | 105 | }
|
---|
[11529] | 106 | }
|
---|
[17969] | 107 | elsif($line =~ /<role.*\s+text=(".*?")/)
|
---|
[13469] | 108 | {
|
---|
[26180] | 109 | my $n = fix($1);
|
---|
[17969] | 110 | if($line =~ /text_context=(".*?")/)
|
---|
| 111 | {
|
---|
[23091] | 112 | print "/* item $item role $n */ trc($1,$n);\n";
|
---|
[17969] | 113 | }
|
---|
| 114 | else
|
---|
| 115 | {
|
---|
[23091] | 116 | print "/* item $item role $n */ tr($n);\n";
|
---|
[17969] | 117 | }
|
---|
[13469] | 118 | }
|
---|
[29933] | 119 | elsif($line =~ /<optional.*\s+text=(".*?")/)
|
---|
| 120 | {
|
---|
| 121 | my $n = fix($1);
|
---|
| 122 | if($line =~ /text_context=(".*?")/)
|
---|
| 123 | {
|
---|
| 124 | print "/* item $item optional $n */ trc($1,$n);\n";
|
---|
| 125 | }
|
---|
| 126 | else
|
---|
| 127 | {
|
---|
| 128 | print "/* item $item optional $n */ tr($n);\n";
|
---|
| 129 | }
|
---|
| 130 | }
|
---|
[28250] | 131 | elsif($line =~ /<(combo|multiselect).*\s+text=(".*?")/)
|
---|
[11529] | 132 | {
|
---|
[28250] | 133 | my ($type,$n) = ($1,fix($2));
|
---|
| 134 | $combo_n = $n;
|
---|
| 135 | $combo_type = $type;
|
---|
| 136 | $vctx = ($line =~ /values_context=(".*?")/) ? $1 : undef;
|
---|
| 137 | # text
|
---|
| 138 | my $tctx = ($line =~ /text_context=(".*?")/) ? $1 : undef;
|
---|
| 139 | print "/* item $item $type $n */" . ($tctx ? " trc($tctx, $n);" : " tr($n);");
|
---|
| 140 | # display_values / values
|
---|
[30038] | 141 | my $sp = ($line =~ /delimiter="(.*?)"/) ? $1 : ($type eq "combo" ? ",":";");
|
---|
[31111] | 142 | my $vals = ($line =~ / display_values="(.*?)"/) ? $1 : ($line =~ /values="(.*?)"/) ? $1 : undef;
|
---|
[31634] | 143 | $vals = undef if ($line =~ /values_no_i18n="true"/);
|
---|
[28250] | 144 | if($vals)
|
---|
[17969] | 145 | {
|
---|
[30038] | 146 | my @combo_values = split "\Q$sp\E" ,$vals;
|
---|
[28250] | 147 | foreach my $val (@combo_values)
|
---|
| 148 | {
|
---|
| 149 | next if $val =~ /^[0-9-]+$/; # search for non-numbers
|
---|
| 150 | $val = fix($val);
|
---|
| 151 | print "/* item $item $type $n display value */" . ($vctx ? " trc($vctx, \"$val\");" : " tr(\"$val\");");
|
---|
| 152 | }
|
---|
[17969] | 153 | }
|
---|
[11529] | 154 | print "\n";
|
---|
| 155 | }
|
---|
[28250] | 156 | elsif(!$comment && $line =~ /<list_entry/)
|
---|
[11529] | 157 | {
|
---|
[28250] | 158 | my $vctxi = ($line =~ /value_context=(".*?")/) ? $1 : $vctx;
|
---|
| 159 | my $value = ($line =~ /value=(".*?")/) ? $1 : undef;
|
---|
| 160 | if($line =~ /display_value=(".*?")/)
|
---|
[17969] | 161 | {
|
---|
[28250] | 162 | my $val = fix($1);
|
---|
[28252] | 163 | print "/* item $item $combo_type $combo_n entry $value display value */" . ($vctxi ? " trc($vctxi, $val);" : " tr($val);");
|
---|
[17969] | 164 | }
|
---|
| 165 | else
|
---|
| 166 | {
|
---|
[28250] | 167 | my $val = fix($value);
|
---|
[28252] | 168 | print "/* item $item $combo_type $combo_n entry $value display value */" . ($vctxi ? " trc($vctxi, $val);" : " tr($val);");
|
---|
[17969] | 169 | }
|
---|
[28250] | 170 | if($line =~ /short_description=(".*?")/)
|
---|
[11529] | 171 | {
|
---|
[28250] | 172 | my $val = fix($1);
|
---|
[28252] | 173 | print "/* item $item $combo_type $combo_n entry $value short description */ tr($val);";
|
---|
[11529] | 174 | }
|
---|
[28252] | 175 | print "\n";
|
---|
[11529] | 176 | }
|
---|
| 177 | elsif($line =~ /<\/group>/)
|
---|
| 178 | {
|
---|
[17969] | 179 | $group = 0 if !($group =~ s/(.*\/).*?$//);
|
---|
[11529] | 180 | print "\n";
|
---|
| 181 | }
|
---|
| 182 | elsif($line =~ /<\/item>/)
|
---|
| 183 | {
|
---|
| 184 | $item = "";
|
---|
| 185 | print "\n";
|
---|
| 186 | }
|
---|
[31111] | 187 | elsif($line =~ /<\/(combo|multiselect)/)
|
---|
[24764] | 188 | {
|
---|
| 189 | $combo_n = "";
|
---|
[29999] | 190 | print "\n";
|
---|
[24764] | 191 | }
|
---|
[13469] | 192 | elsif(!$line)
|
---|
| 193 | {
|
---|
| 194 | print "\n";
|
---|
| 195 | }
|
---|
[11529] | 196 | elsif($line =~ /^\s*$/
|
---|
[13140] | 197 | || $line =~ /<separator *\/>/
|
---|
| 198 | || $line =~ /<space *\/>/
|
---|
| 199 | || $line =~ /<\/?optional>/
|
---|
[11529] | 200 | || $line =~ /<key/
|
---|
[25155] | 201 | || $line =~ /<presets/
|
---|
[29933] | 202 | || $line =~ /<checkgroup/
|
---|
| 203 | || $line =~ /<\/checkgroup/
|
---|
[25155] | 204 | || $line =~ /<\/presets/
|
---|
[13469] | 205 | || $line =~ /roles/
|
---|
| 206 | || $line =~ /href=/
|
---|
[11529] | 207 | || $line =~ /<!--/
|
---|
[25155] | 208 | || $line =~ /<\?xml/
|
---|
[11529] | 209 | || $line =~ /-->/
|
---|
[30159] | 210 | || $line =~ /<\/?chunk/
|
---|
| 211 | || $line =~ /<reference/
|
---|
| 212 | || $line =~ /<preset_link/
|
---|
[31139] | 213 | || $line =~ /<item_separator\/>/
|
---|
[11529] | 214 | || $comment)
|
---|
| 215 | {
|
---|
[31111] | 216 | $line =~ s/[ \t]+((?:short)?description) *= *"([^"]+)/*\/ \/* $1 *\/ tr("$2"); \/*/g;
|
---|
| 217 | print "/* $line */\n";
|
---|
[11529] | 218 | }
|
---|
| 219 | else
|
---|
| 220 | {
|
---|
| 221 | print "/* unparsed line $line */\n";
|
---|
[29933] | 222 | print STDERR "/* unparsed line $linenr $line */\n";
|
---|
[25155] | 223 | $result = 20
|
---|
[11529] | 224 | }
|
---|
| 225 |
|
---|
| 226 | # note, these two must be in this order ore oneliners aren't handled
|
---|
| 227 | $comment = 1 if($line =~ /<!--/);
|
---|
| 228 | $comment = 0 if($line =~ /-->/);
|
---|
| 229 | }
|
---|
[13392] | 230 |
|
---|
| 231 | print "}}\n";
|
---|
[29208] | 232 | exit($result) if $result;
|
---|