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