source: osm/applications/editors/josm/i18n/convpreset.pl@ 26180

Last change on this file since 26180 was 26180, checked in by stoecker, 13 years ago

support ' in preset data

File size: 5.0 KB
Line 
1#! /usr/bin/perl -w
2
3# Written by Dirk Stöcker <openstreetmap@dstoecker.de>
4# Public domain, no rights reserved.
5
6use strict;
7
8my $item = "";
9my $group;
10my $combo_n;
11my @combo_values;
12my $combo_idx;
13my $result = 0;
14my $comment = 0;
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.
21print "class trans_preset { void tr(String s){} void f() {";
22
23sub fix($)
24{
25 my ($val) = @_;
26 $val =~ s/'/''/g;
27 return $val;
28}
29
30while(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 # first handle display values
114 elsif($line =~ /<(combo|multiselect).*\s+text=(".*?").*\s+display_values="(.*?)"/)
115 {
116 my ($type,$n,$vals) = ($1,fix($2),$3);
117 my $sp = ($type eq "combo" ? ",":";");
118 $combo_n = $n;
119 $combo_idx = 0;
120 my $vctx = ($line =~ /values_context=(".*?")/) ? $1 : undef;
121 if($line =~ /text_context=(".*?")/)
122 {
123 print "/* item $item $type $n */ trc($1,$n);";
124 }
125 else
126 {
127 print "/* item $item $type $n */ tr($n);";
128 }
129 $vals =~ s/\\$sp/\x91/g;
130 @combo_values = split $sp,$vals;
131 for (my $i=0; $i<@combo_values; ++$i)
132 {
133 my $val = $combo_values[$i];
134 $val =~ s/\x91/$sp/g;
135 next if $val =~ /^[0-9-]+$/; # search for non-numbers
136 $val = fix($val);
137 print "/* item $item $type $n display value */" . ($vctx ? " trc($vctx, \"$val\");" : " tr(\"$val\");");
138 }
139 print "\n";
140 }
141 elsif($line =~ /<(combo|multiselect).*\s+text=(".*?").*\s+values="(.*?)"/)
142 {
143 my ($type,$n,$vals) = ($1,fix($2),$3);
144 my $sp = ($type eq "combo" ? ",":";");
145 $combo_n = $n;
146 $combo_idx = 0;
147 my $vctx = ($line =~ /values_context=(".*?")/) ? $1 : undef;
148 if($line =~ /text_context=(".*?")/)
149 {
150 print "/* item $item $type $n */ trc($1,$n);";
151 }
152 else
153 {
154 print "/* item $item $type $n */ tr($n);";
155 }
156 @combo_values = split $sp,$vals;
157 foreach my $val (@combo_values)
158 {
159 next if $val =~ /^[0-9-]+$/; # search for non-numbers
160 $val = fix($val);
161 print "/* item $item $type $n display value */" . ($vctx ? " trc($vctx, \"$val\");" : " tr(\"$val\");");
162 }
163 print "\n";
164 }
165 elsif(!$comment && $line =~ /<short_description>(.*?)<\/short_description>/)
166 {
167 my $n = fix($1);
168 print "/* item $item combo $combo_n item \"$combo_values[$combo_idx]\" short description */ tr(\"$n\");\n";
169 $combo_idx++;
170 }
171 elsif($line =~ /<\/group>/)
172 {
173 $group = 0 if !($group =~ s/(.*\/).*?$//);
174 print "\n";
175 }
176 elsif($line =~ /<\/item>/)
177 {
178 $item = "";
179 print "\n";
180 }
181 elsif($line =~ /<\/combo/)
182 {
183 $combo_n = "";
184 $combo_idx = 0;
185 }
186 elsif(!$line)
187 {
188 print "\n";
189 }
190 elsif($line =~ /^\s*$/
191 || $line =~ /<separator *\/>/
192 || $line =~ /<space *\/>/
193 || $line =~ /<\/?optional>/
194 || $line =~ /<key/
195 || $line =~ /<presets/
196 || $line =~ /<\/presets/
197 || $line =~ /roles/
198 || $line =~ /href=/
199 || $line =~ /<!--/
200 || $line =~ /<\?xml/
201 || $line =~ /-->/
202 || $comment)
203 {
204 print "// $line\n";
205 }
206 else
207 {
208 print "/* unparsed line $line */\n";
209 $result = 20
210 }
211
212 # note, these two must be in this order ore oneliners aren't handled
213 $comment = 1 if($line =~ /<!--/);
214 $comment = 0 if($line =~ /-->/);
215}
216
217print "}}\n";
218return $result if $result;
Note: See TracBrowser for help on using the repository browser.