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

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

fix UTF8 issue with launchpad

File size: 5.1 KB
RevLine 
[11529]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;
[24764]10my $combo_n;
11my @combo_values;
12my $combo_idx;
[25155]13my $result = 0;
[11529]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
[13392]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
[26180]23sub fix($)
24{
25 my ($val) = @_;
26 $val =~ s/'/''/g;
27 return $val;
28}
29
[11529]30while(my $line = <>)
31{
32 chomp($line);
33 if($line =~ /<item\s+name=(".*?")/)
34 {
[26180]35 my $val = fix($1);
[17969]36 $item = $group ? "$group$val" : $val;
[11529]37 $item =~ s/""/\//;
[17969]38 if($line =~ /name_context=(".*?")/)
39 {
[23091]40 print "/* item $item */ trc($1, $val);\n";
[17969]41 }
42 else
43 {
[23091]44 print "/* item $item */ tr($val);\n";
[17969]45 }
[11529]46 }
[17969]47 elsif($line =~ /<group.*\s+name=(".*?")/)
[11529]48 {
[26180]49 my $gr = fix($1);
[17973]50 $group = $group ? "$group$gr" : $gr;
[17969]51 $group =~ s/\"\"/\//;
52 if($line =~ /name_context=(".*?")/)
53 {
[23091]54 print "/* group $group */ trc($1,$gr);\n";
[17969]55 }
56 else
57 {
[23091]58 print "/* group $group */ tr($gr);\n";
[17969]59 }
[11529]60 }
[17969]61 elsif($line =~ /<label.*\s+text=" "/)
[11529]62 {
63 print "/* item $item empty label */\n";
64 }
[17969]65 elsif($line =~ /<label.*\s+text=(".*?")/)
[11529]66 {
[26180]67 my $text = fix($1);
[17969]68 if($line =~ /text_context=(".*?")/)
69 {
[23091]70 print "/* item $item label $text */ trc($1,$text);\n";
[17969]71 }
72 else
73 {
[23091]74 print "/* item $item label $text */ tr($text);\n";
[17969]75 }
[11529]76 }
[17969]77 elsif($line =~ /<text.*\s+text=(".*?")/)
[11529]78 {
[26180]79 my $n = fix($1);
[17969]80 if($line =~ /text_context=(".*?")/)
81 {
[23091]82 print "/* item $item text $n */ trc($1,$n);\n";
[17969]83 }
84 else
85 {
[23091]86 print "/* item $item text $n */ tr($n);\n";
[17969]87 }
[11529]88 }
[17969]89 elsif($line =~ /<check.*\s+text=(".*?")/)
[11529]90 {
[26180]91 my $n = fix($1);
[17969]92 if($line =~ /text_context=(".*?")/)
93 {
[23091]94 print "/* item $item check $n */ trc($1,$n);\n";
[17969]95 }
96 else
97 {
[23091]98 print "/* item $item check $n */ tr($n);\n";
[17969]99 }
[11529]100 }
[17969]101 elsif($line =~ /<role.*\s+text=(".*?")/)
[13469]102 {
[26180]103 my $n = fix($1);
[17969]104 if($line =~ /text_context=(".*?")/)
105 {
[23091]106 print "/* item $item role $n */ trc($1,$n);\n";
[17969]107 }
108 else
109 {
[23091]110 print "/* item $item role $n */ tr($n);\n";
[17969]111 }
[13469]112 }
[11529]113 # first handle display values
[25155]114 elsif($line =~ /<(combo|multiselect).*\s+text=(".*?").*\s+display_values="(.*?)"/)
[11529]115 {
[26180]116 my ($type,$n,$vals) = ($1,fix($2),$3);
[25155]117 my $sp = ($type eq "combo" ? ",":";");
[24764]118 $combo_n = $n;
119 $combo_idx = 0;
[17969]120 my $vctx = ($line =~ /values_context=(".*?")/) ? $1 : undef;
121 if($line =~ /text_context=(".*?")/)
122 {
[25155]123 print "/* item $item $type $n */ trc($1,$n);";
[17969]124 }
125 else
126 {
[25155]127 print "/* item $item $type $n */ tr($n);";
[17969]128 }
[25155]129 $vals =~ s/\\$sp/\x91/g;
130 @combo_values = split $sp,$vals;
[26180]131 for (my $i=0; $i<@combo_values; ++$i)
132 {
133 my $val = $combo_values[$i];
134 $val =~ s/\x91/$sp/g;
[26247]135 $combo_values[$i] = $val;
[26180]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\");");
[11529]139 }
140 print "\n";
141 }
[25155]142 elsif($line =~ /<(combo|multiselect).*\s+text=(".*?").*\s+values="(.*?)"/)
[11529]143 {
[26180]144 my ($type,$n,$vals) = ($1,fix($2),$3);
[25155]145 my $sp = ($type eq "combo" ? ",":";");
[24764]146 $combo_n = $n;
147 $combo_idx = 0;
[17969]148 my $vctx = ($line =~ /values_context=(".*?")/) ? $1 : undef;
149 if($line =~ /text_context=(".*?")/)
150 {
[25155]151 print "/* item $item $type $n */ trc($1,$n);";
[17969]152 }
153 else
154 {
[25155]155 print "/* item $item $type $n */ tr($n);";
[17969]156 }
[25155]157 @combo_values = split $sp,$vals;
[24764]158 foreach my $val (@combo_values)
[11529]159 {
160 next if $val =~ /^[0-9-]+$/; # search for non-numbers
[26180]161 $val = fix($val);
[25155]162 print "/* item $item $type $n display value */" . ($vctx ? " trc($vctx, \"$val\");" : " tr(\"$val\");");
[11529]163 }
164 print "\n";
165 }
[24903]166 elsif(!$comment && $line =~ /<short_description>(.*?)<\/short_description>/)
[24764]167 {
[26180]168 my $n = fix($1);
[24764]169 print "/* item $item combo $combo_n item \"$combo_values[$combo_idx]\" short description */ tr(\"$n\");\n";
170 $combo_idx++;
171 }
[11529]172 elsif($line =~ /<\/group>/)
173 {
[17969]174 $group = 0 if !($group =~ s/(.*\/).*?$//);
[11529]175 print "\n";
176 }
177 elsif($line =~ /<\/item>/)
178 {
179 $item = "";
180 print "\n";
181 }
[24764]182 elsif($line =~ /<\/combo/)
183 {
184 $combo_n = "";
185 $combo_idx = 0;
186 }
[13469]187 elsif(!$line)
188 {
189 print "\n";
190 }
[11529]191 elsif($line =~ /^\s*$/
[13140]192 || $line =~ /<separator *\/>/
193 || $line =~ /<space *\/>/
194 || $line =~ /<\/?optional>/
[11529]195 || $line =~ /<key/
[25155]196 || $line =~ /<presets/
197 || $line =~ /<\/presets/
[13469]198 || $line =~ /roles/
199 || $line =~ /href=/
[11529]200 || $line =~ /<!--/
[25155]201 || $line =~ /<\?xml/
[11529]202 || $line =~ /-->/
203 || $comment)
204 {
[13469]205 print "// $line\n";
[11529]206 }
207 else
208 {
209 print "/* unparsed line $line */\n";
[25155]210 $result = 20
[11529]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}
[13392]217
218print "}}\n";
[25155]219return $result if $result;
Note: See TracBrowser for help on using the repository browser.