[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;
|
---|
| 10 | my $comment = 0;
|
---|
| 11 |
|
---|
| 12 | # This is a simple conversion and in no way a complete XML parser
|
---|
| 13 | # but it works with a default Perl installation
|
---|
| 14 |
|
---|
[13392] | 15 | # Print a header to write valid Java code. No line break,
|
---|
| 16 | # so that the input and output line numbers will match.
|
---|
| 17 | print "class trans_preset { void tr(String s){} void f() {";
|
---|
| 18 |
|
---|
[11529] | 19 | while(my $line = <>)
|
---|
| 20 | {
|
---|
| 21 | chomp($line);
|
---|
| 22 | if($line =~ /<item\s+name=(".*?")/)
|
---|
| 23 | {
|
---|
[17969] | 24 | my $val = $1;
|
---|
| 25 | $item = $group ? "$group$val" : $val;
|
---|
[11529] | 26 | $item =~ s/""/\//;
|
---|
[17969] | 27 | if($line =~ /name_context=(".*?")/)
|
---|
| 28 | {
|
---|
[23091] | 29 | print "/* item $item */ trc($1, $val);\n";
|
---|
[17969] | 30 | }
|
---|
| 31 | else
|
---|
| 32 | {
|
---|
[23091] | 33 | print "/* item $item */ tr($val);\n";
|
---|
[17969] | 34 | }
|
---|
[11529] | 35 | }
|
---|
[17969] | 36 | elsif($line =~ /<group.*\s+name=(".*?")/)
|
---|
[11529] | 37 | {
|
---|
[17973] | 38 | my $gr = $1;
|
---|
| 39 | $group = $group ? "$group$gr" : $gr;
|
---|
[17969] | 40 | $group =~ s/\"\"/\//;
|
---|
| 41 | if($line =~ /name_context=(".*?")/)
|
---|
| 42 | {
|
---|
[23091] | 43 | print "/* group $group */ trc($1,$gr);\n";
|
---|
[17969] | 44 | }
|
---|
| 45 | else
|
---|
| 46 | {
|
---|
[23091] | 47 | print "/* group $group */ tr($gr);\n";
|
---|
[17969] | 48 | }
|
---|
[11529] | 49 | }
|
---|
[17969] | 50 | elsif($line =~ /<label.*\s+text=" "/)
|
---|
[11529] | 51 | {
|
---|
| 52 | print "/* item $item empty label */\n";
|
---|
| 53 | }
|
---|
[17969] | 54 | elsif($line =~ /<label.*\s+text=(".*?")/)
|
---|
[11529] | 55 | {
|
---|
[17969] | 56 | my $text = $1;
|
---|
| 57 | if($line =~ /text_context=(".*?")/)
|
---|
| 58 | {
|
---|
[23091] | 59 | print "/* item $item label $text */ trc($1,$text);\n";
|
---|
[17969] | 60 | }
|
---|
| 61 | else
|
---|
| 62 | {
|
---|
[23091] | 63 | print "/* item $item label $text */ tr($text);\n";
|
---|
[17969] | 64 | }
|
---|
[11529] | 65 | }
|
---|
[17969] | 66 | elsif($line =~ /<text.*\s+text=(".*?")/)
|
---|
[11529] | 67 | {
|
---|
| 68 | my $n = $1;
|
---|
[17969] | 69 | if($line =~ /text_context=(".*?")/)
|
---|
| 70 | {
|
---|
[23091] | 71 | print "/* item $item text $n */ trc($1,$n);\n";
|
---|
[17969] | 72 | }
|
---|
| 73 | else
|
---|
| 74 | {
|
---|
[23091] | 75 | print "/* item $item text $n */ tr($n);\n";
|
---|
[17969] | 76 | }
|
---|
[11529] | 77 | }
|
---|
[17969] | 78 | elsif($line =~ /<check.*\s+text=(".*?")/)
|
---|
[11529] | 79 | {
|
---|
| 80 | my $n = $1;
|
---|
[17969] | 81 | if($line =~ /text_context=(".*?")/)
|
---|
| 82 | {
|
---|
[23091] | 83 | print "/* item $item check $n */ trc($1,$n);\n";
|
---|
[17969] | 84 | }
|
---|
| 85 | else
|
---|
| 86 | {
|
---|
[23091] | 87 | print "/* item $item check $n */ tr($n);\n";
|
---|
[17969] | 88 | }
|
---|
[11529] | 89 | }
|
---|
[17969] | 90 | elsif($line =~ /<role.*\s+text=(".*?")/)
|
---|
[13469] | 91 | {
|
---|
| 92 | my $n = $1;
|
---|
[17969] | 93 | if($line =~ /text_context=(".*?")/)
|
---|
| 94 | {
|
---|
[23091] | 95 | print "/* item $item role $n */ trc($1,$n);\n";
|
---|
[17969] | 96 | }
|
---|
| 97 | else
|
---|
| 98 | {
|
---|
[23091] | 99 | print "/* item $item role $n */ tr($n);\n";
|
---|
[17969] | 100 | }
|
---|
[13469] | 101 | }
|
---|
[11529] | 102 | # first handle display values
|
---|
[17969] | 103 | elsif($line =~ /<combo.*\s+text=(".*?").*\s+display_values="(.*?)"/)
|
---|
[11529] | 104 | {
|
---|
| 105 | my ($n,$vals) = ($1,$2);
|
---|
[17969] | 106 | my $vctx = ($line =~ /values_context=(".*?")/) ? $1 : undef;
|
---|
| 107 | if($line =~ /text_context=(".*?")/)
|
---|
| 108 | {
|
---|
[23091] | 109 | print "/* item $item combo $n */ trc($1,$n);";
|
---|
[17969] | 110 | }
|
---|
| 111 | else
|
---|
| 112 | {
|
---|
[23091] | 113 | print "/* item $item combo $n */ tr($n);";
|
---|
[17969] | 114 | }
|
---|
[11529] | 115 | foreach my $val (split ",",$vals)
|
---|
| 116 | {
|
---|
| 117 | next if $val =~ /^[0-9-]+$/; # search for non-numbers
|
---|
[17969] | 118 | print $vctx ? " trc($vctx, \"$val\");" : " tr(\"$val\");";
|
---|
[11529] | 119 | }
|
---|
| 120 | print "\n";
|
---|
| 121 | }
|
---|
[17969] | 122 | elsif($line =~ /<combo.*\s+text=(".*?").*\s+values="(.*?)"/)
|
---|
[11529] | 123 | {
|
---|
| 124 | my ($n,$vals) = ($1,$2);
|
---|
[17969] | 125 | my $vctx = ($line =~ /values_context=(".*?")/) ? $1 : undef;
|
---|
| 126 | if($line =~ /text_context=(".*?")/)
|
---|
| 127 | {
|
---|
[23091] | 128 | print "/* item $item combo $n */ trc($1,$n);";
|
---|
[17969] | 129 | }
|
---|
| 130 | else
|
---|
| 131 | {
|
---|
[23091] | 132 | print "/* item $item combo $n */ tr($n);";
|
---|
[17969] | 133 | }
|
---|
[11529] | 134 | foreach my $val (split ",",$vals)
|
---|
| 135 | {
|
---|
| 136 | next if $val =~ /^[0-9-]+$/; # search for non-numbers
|
---|
[17969] | 137 | print $vctx ? " trc($vctx, \"$val\");" : " tr(\"$val\");";
|
---|
[11529] | 138 | }
|
---|
| 139 | print "\n";
|
---|
| 140 | }
|
---|
| 141 | elsif($line =~ /<\/group>/)
|
---|
| 142 | {
|
---|
[17969] | 143 | $group = 0 if !($group =~ s/(.*\/).*?$//);
|
---|
[11529] | 144 | print "\n";
|
---|
| 145 | }
|
---|
| 146 | elsif($line =~ /<\/item>/)
|
---|
| 147 | {
|
---|
| 148 | $item = "";
|
---|
| 149 | print "\n";
|
---|
| 150 | }
|
---|
[13469] | 151 | elsif(!$line)
|
---|
| 152 | {
|
---|
| 153 | print "\n";
|
---|
| 154 | }
|
---|
[11529] | 155 | elsif($line =~ /^\s*$/
|
---|
[13140] | 156 | || $line =~ /<separator *\/>/
|
---|
| 157 | || $line =~ /<space *\/>/
|
---|
| 158 | || $line =~ /<\/?optional>/
|
---|
[11529] | 159 | || $line =~ /<key/
|
---|
| 160 | || $line =~ /annotations/
|
---|
[13469] | 161 | || $line =~ /roles/
|
---|
| 162 | || $line =~ /href=/
|
---|
[11529] | 163 | || $line =~ /<!--/
|
---|
| 164 | || $line =~ /-->/
|
---|
| 165 | || $comment)
|
---|
| 166 | {
|
---|
[13469] | 167 | print "// $line\n";
|
---|
[11529] | 168 | }
|
---|
| 169 | else
|
---|
| 170 | {
|
---|
| 171 | print "/* unparsed line $line */\n";
|
---|
[13140] | 172 | # print STDERR "Unparsed line $line\n";
|
---|
[11529] | 173 | }
|
---|
| 174 |
|
---|
| 175 | # note, these two must be in this order ore oneliners aren't handled
|
---|
| 176 | $comment = 1 if($line =~ /<!--/);
|
---|
| 177 | $comment = 0 if($line =~ /-->/);
|
---|
| 178 | }
|
---|
[13392] | 179 |
|
---|
| 180 | print "}}\n";
|
---|