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