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

Last change on this file since 24903 was 24903, checked in by stoecker, 14 years ago

minor fix in comment handling

File size: 4.7 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;
[11529]13my $comment = 0;
14
15# This is a simple conversion and in no way a complete XML parser
16# but it works with a default Perl installation
17
[13392]18# Print a header to write valid Java code. No line break,
19# so that the input and output line numbers will match.
20print "class trans_preset { void tr(String s){} void f() {";
21
[11529]22while(my $line = <>)
23{
24 chomp($line);
25 if($line =~ /<item\s+name=(".*?")/)
26 {
[17969]27 my $val = $1;
28 $item = $group ? "$group$val" : $val;
[11529]29 $item =~ s/""/\//;
[17969]30 if($line =~ /name_context=(".*?")/)
31 {
[23091]32 print "/* item $item */ trc($1, $val);\n";
[17969]33 }
34 else
35 {
[23091]36 print "/* item $item */ tr($val);\n";
[17969]37 }
[11529]38 }
[17969]39 elsif($line =~ /<group.*\s+name=(".*?")/)
[11529]40 {
[17973]41 my $gr = $1;
42 $group = $group ? "$group$gr" : $gr;
[17969]43 $group =~ s/\"\"/\//;
44 if($line =~ /name_context=(".*?")/)
45 {
[23091]46 print "/* group $group */ trc($1,$gr);\n";
[17969]47 }
48 else
49 {
[23091]50 print "/* group $group */ tr($gr);\n";
[17969]51 }
[11529]52 }
[17969]53 elsif($line =~ /<label.*\s+text=" "/)
[11529]54 {
55 print "/* item $item empty label */\n";
56 }
[17969]57 elsif($line =~ /<label.*\s+text=(".*?")/)
[11529]58 {
[17969]59 my $text = $1;
60 if($line =~ /text_context=(".*?")/)
61 {
[23091]62 print "/* item $item label $text */ trc($1,$text);\n";
[17969]63 }
64 else
65 {
[23091]66 print "/* item $item label $text */ tr($text);\n";
[17969]67 }
[11529]68 }
[17969]69 elsif($line =~ /<text.*\s+text=(".*?")/)
[11529]70 {
71 my $n = $1;
[17969]72 if($line =~ /text_context=(".*?")/)
73 {
[23091]74 print "/* item $item text $n */ trc($1,$n);\n";
[17969]75 }
76 else
77 {
[23091]78 print "/* item $item text $n */ tr($n);\n";
[17969]79 }
[11529]80 }
[17969]81 elsif($line =~ /<check.*\s+text=(".*?")/)
[11529]82 {
83 my $n = $1;
[17969]84 if($line =~ /text_context=(".*?")/)
85 {
[23091]86 print "/* item $item check $n */ trc($1,$n);\n";
[17969]87 }
88 else
89 {
[23091]90 print "/* item $item check $n */ tr($n);\n";
[17969]91 }
[11529]92 }
[17969]93 elsif($line =~ /<role.*\s+text=(".*?")/)
[13469]94 {
95 my $n = $1;
[17969]96 if($line =~ /text_context=(".*?")/)
97 {
[23091]98 print "/* item $item role $n */ trc($1,$n);\n";
[17969]99 }
100 else
101 {
[23091]102 print "/* item $item role $n */ tr($n);\n";
[17969]103 }
[13469]104 }
[11529]105 # first handle display values
[17969]106 elsif($line =~ /<combo.*\s+text=(".*?").*\s+display_values="(.*?)"/)
[11529]107 {
108 my ($n,$vals) = ($1,$2);
[24764]109 $combo_n = $n;
110 $combo_idx = 0;
[17969]111 my $vctx = ($line =~ /values_context=(".*?")/) ? $1 : undef;
112 if($line =~ /text_context=(".*?")/)
113 {
[23091]114 print "/* item $item combo $n */ trc($1,$n);";
[17969]115 }
116 else
117 {
[23091]118 print "/* item $item combo $n */ tr($n);";
[17969]119 }
[24764]120 $vals =~ s/\\,/\x91/g;
121 @combo_values = split ",",$vals;
122 for (my $i=0; $i<@combo_values; ++$i) {
123 $combo_values[$i] =~ s/\x91/,/g;
124 next if $combo_values[$i] =~ /^[0-9-]+$/; # search for non-numbers
[24903]125 print "/* item $item combo $n display value */" . ($vctx ? " trc($vctx, \"$combo_values[$i]\");" : " tr(\"$combo_values[$i]\");");
[11529]126 }
127 print "\n";
128 }
[17969]129 elsif($line =~ /<combo.*\s+text=(".*?").*\s+values="(.*?)"/)
[11529]130 {
131 my ($n,$vals) = ($1,$2);
[24764]132 $combo_n = $n;
133 $combo_idx = 0;
[17969]134 my $vctx = ($line =~ /values_context=(".*?")/) ? $1 : undef;
135 if($line =~ /text_context=(".*?")/)
136 {
[23091]137 print "/* item $item combo $n */ trc($1,$n);";
[17969]138 }
139 else
140 {
[23091]141 print "/* item $item combo $n */ tr($n);";
[17969]142 }
[24764]143 @combo_values = split ",",$vals;
144 foreach my $val (@combo_values)
[11529]145 {
146 next if $val =~ /^[0-9-]+$/; # search for non-numbers
[24903]147 print "/* item $item combo $n display value */" . ($vctx ? " trc($vctx, \"$val\");" : " tr(\"$val\");");
[11529]148 }
149 print "\n";
150 }
[24903]151 elsif(!$comment && $line =~ /<short_description>(.*?)<\/short_description>/)
[24764]152 {
153 my $n = $1;
154 print "/* item $item combo $combo_n item \"$combo_values[$combo_idx]\" short description */ tr(\"$n\");\n";
155 $combo_idx++;
156 }
[11529]157 elsif($line =~ /<\/group>/)
158 {
[17969]159 $group = 0 if !($group =~ s/(.*\/).*?$//);
[11529]160 print "\n";
161 }
162 elsif($line =~ /<\/item>/)
163 {
164 $item = "";
165 print "\n";
166 }
[24764]167 elsif($line =~ /<\/combo/)
168 {
169 $combo_n = "";
170 $combo_idx = 0;
171 }
[13469]172 elsif(!$line)
173 {
174 print "\n";
175 }
[11529]176 elsif($line =~ /^\s*$/
[13140]177 || $line =~ /<separator *\/>/
178 || $line =~ /<space *\/>/
179 || $line =~ /<\/?optional>/
[11529]180 || $line =~ /<key/
181 || $line =~ /annotations/
[13469]182 || $line =~ /roles/
183 || $line =~ /href=/
[11529]184 || $line =~ /<!--/
185 || $line =~ /-->/
186 || $comment)
187 {
[13469]188 print "// $line\n";
[11529]189 }
190 else
191 {
192 print "/* unparsed line $line */\n";
[13140]193# print STDERR "Unparsed line $line\n";
[11529]194 }
195
196 # note, these two must be in this order ore oneliners aren't handled
197 $comment = 1 if($line =~ /<!--/);
198 $comment = 0 if($line =~ /-->/);
199}
[13392]200
201print "}}\n";
Note: See TracBrowser for help on using the repository browser.