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