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

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

fix UTF8 issue with launchpad

File size: 5.1 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 $combo_values[$i] = $val;
136 next if $val =~ /^[0-9-]+$/; # search for non-numbers
137 $val = fix($val);
138 print "/* item $item $type $n display value */" . ($vctx ? " trc($vctx, \"$val\");" : " tr(\"$val\");");
139 }
140 print "\n";
141 }
142 elsif($line =~ /<(combo|multiselect).*\s+text=(".*?").*\s+values="(.*?)"/)
143 {
144 my ($type,$n,$vals) = ($1,fix($2),$3);
145 my $sp = ($type eq "combo" ? ",":";");
146 $combo_n = $n;
147 $combo_idx = 0;
148 my $vctx = ($line =~ /values_context=(".*?")/) ? $1 : undef;
149 if($line =~ /text_context=(".*?")/)
150 {
151 print "/* item $item $type $n */ trc($1,$n);";
152 }
153 else
154 {
155 print "/* item $item $type $n */ tr($n);";
156 }
157 @combo_values = split $sp,$vals;
158 foreach my $val (@combo_values)
159 {
160 next if $val =~ /^[0-9-]+$/; # search for non-numbers
161 $val = fix($val);
162 print "/* item $item $type $n display value */" . ($vctx ? " trc($vctx, \"$val\");" : " tr(\"$val\");");
163 }
164 print "\n";
165 }
166 elsif(!$comment && $line =~ /<short_description>(.*?)<\/short_description>/)
167 {
168 my $n = fix($1);
169 print "/* item $item combo $combo_n item \"$combo_values[$combo_idx]\" short description */ tr(\"$n\");\n";
170 $combo_idx++;
171 }
172 elsif($line =~ /<\/group>/)
173 {
174 $group = 0 if !($group =~ s/(.*\/).*?$//);
175 print "\n";
176 }
177 elsif($line =~ /<\/item>/)
178 {
179 $item = "";
180 print "\n";
181 }
182 elsif($line =~ /<\/combo/)
183 {
184 $combo_n = "";
185 $combo_idx = 0;
186 }
187 elsif(!$line)
188 {
189 print "\n";
190 }
191 elsif($line =~ /^\s*$/
192 || $line =~ /<separator *\/>/
193 || $line =~ /<space *\/>/
194 || $line =~ /<\/?optional>/
195 || $line =~ /<key/
196 || $line =~ /<presets/
197 || $line =~ /<\/presets/
198 || $line =~ /roles/
199 || $line =~ /href=/
200 || $line =~ /<!--/
201 || $line =~ /<\?xml/
202 || $line =~ /-->/
203 || $comment)
204 {
205 print "// $line\n";
206 }
207 else
208 {
209 print "/* unparsed line $line */\n";
210 $result = 20
211 }
212
213 # note, these two must be in this order ore oneliners aren't handled
214 $comment = 1 if($line =~ /<!--/);
215 $comment = 0 if($line =~ /-->/);
216}
217
218print "}}\n";
219return $result if $result;
Note: See TracBrowser for help on using the repository browser.