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

Last change on this file since 29933 was 29933, checked in by stoecker, 11 years ago

update parser

File size: 5.2 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_type;
12my $result = 0;
13my $comment = 0;
14my $vctx;
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
30my $linenr = 0;
31while(my $line = <>)
32{
33 ++$linenr;
34 chomp($line);
35 if($line =~ /<item\s+name=(".*?")/)
36 {
37 my $val = fix($1);
38 $item = $group ? "$group$val" : $val;
39 $item =~ s/""/\//;
40 if($line =~ /name_context=(".*?")/)
41 {
42 print "/* item $item */ trc($1, $val);\n";
43 }
44 else
45 {
46 print "/* item $item */ tr($val);\n";
47 }
48 }
49 elsif($line =~ /<group.*\s+name=(".*?")/)
50 {
51 my $gr = fix($1);
52 $group = $group ? "$group$gr" : $gr;
53 $group =~ s/\"\"/\//;
54 if($line =~ /name_context=(".*?")/)
55 {
56 print "/* group $group */ trc($1,$gr);\n";
57 }
58 else
59 {
60 print "/* group $group */ tr($gr);\n";
61 }
62 }
63 elsif($line =~ /<label.*\s+text=" "/)
64 {
65 print "/* item $item empty label */\n";
66 }
67 elsif($line =~ /<label.*\s+text=(".*?")/)
68 {
69 my $text = fix($1);
70 if($line =~ /text_context=(".*?")/)
71 {
72 print "/* item $item label $text */ trc($1,$text);\n";
73 }
74 else
75 {
76 print "/* item $item label $text */ tr($text);\n";
77 }
78 }
79 elsif($line =~ /<text.*\s+text=(".*?")/)
80 {
81 my $n = fix($1);
82 if($line =~ /text_context=(".*?")/)
83 {
84 print "/* item $item text $n */ trc($1,$n);\n";
85 }
86 else
87 {
88 print "/* item $item text $n */ tr($n);\n";
89 }
90 }
91 elsif($line =~ /<check.*\s+text=(".*?")/)
92 {
93 my $n = fix($1);
94 if($line =~ /text_context=(".*?")/)
95 {
96 print "/* item $item check $n */ trc($1,$n);\n";
97 }
98 else
99 {
100 print "/* item $item check $n */ tr($n);\n";
101 }
102 }
103 elsif($line =~ /<role.*\s+text=(".*?")/)
104 {
105 my $n = fix($1);
106 if($line =~ /text_context=(".*?")/)
107 {
108 print "/* item $item role $n */ trc($1,$n);\n";
109 }
110 else
111 {
112 print "/* item $item role $n */ tr($n);\n";
113 }
114 }
115 elsif($line =~ /<optional.*\s+text=(".*?")/)
116 {
117 my $n = fix($1);
118 if($line =~ /text_context=(".*?")/)
119 {
120 print "/* item $item optional $n */ trc($1,$n);\n";
121 }
122 else
123 {
124 print "/* item $item optional $n */ tr($n);\n";
125 }
126 }
127 elsif($line =~ /<(combo|multiselect).*\s+text=(".*?")/)
128 {
129 my ($type,$n) = ($1,fix($2));
130 $combo_n = $n;
131 $combo_type = $type;
132 $vctx = ($line =~ /values_context=(".*?")/) ? $1 : undef;
133 # text
134 my $tctx = ($line =~ /text_context=(".*?")/) ? $1 : undef;
135 print "/* item $item $type $n */" . ($tctx ? " trc($tctx, $n);" : " tr($n);");
136 # display_values / values
137 my $sp = ($type eq "combo" ? ",":";");
138 my $vals = ($line =~ /display_values="(.*?)"/) ? $1 : ($line =~ /values="(.*?)"/) ? $1 : undef;
139 if($vals)
140 {
141 my @combo_values = split $sp,$vals;
142 foreach my $val (@combo_values)
143 {
144 next if $val =~ /^[0-9-]+$/; # search for non-numbers
145 $val = fix($val);
146 print "/* item $item $type $n display value */" . ($vctx ? " trc($vctx, \"$val\");" : " tr(\"$val\");");
147 }
148 }
149 print "\n";
150 }
151 elsif(!$comment && $line =~ /<list_entry/)
152 {
153 my $vctxi = ($line =~ /value_context=(".*?")/) ? $1 : $vctx;
154 my $value = ($line =~ /value=(".*?")/) ? $1 : undef;
155 if($line =~ /display_value=(".*?")/)
156 {
157 my $val = fix($1);
158 print "/* item $item $combo_type $combo_n entry $value display value */" . ($vctxi ? " trc($vctxi, $val);" : " tr($val);");
159 }
160 else
161 {
162 my $val = fix($value);
163 print "/* item $item $combo_type $combo_n entry $value display value */" . ($vctxi ? " trc($vctxi, $val);" : " tr($val);");
164 }
165 if($line =~ /short_description=(".*?")/)
166 {
167 my $val = fix($1);
168 print "/* item $item $combo_type $combo_n entry $value short description */ tr($val);";
169 }
170 print "\n";
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 }
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 =~ /<checkgroup/
197 || $line =~ /<\/checkgroup/
198 || $line =~ /<\/presets/
199 || $line =~ /roles/
200 || $line =~ /href=/
201 || $line =~ /<!--/
202 || $line =~ /<\?xml/
203 || $line =~ /-->/
204 || $comment)
205 {
206 print "// $line\n";
207 }
208 else
209 {
210 print "/* unparsed line $line */\n";
211 print STDERR "/* unparsed line $linenr $line */\n";
212 $result = 20
213 }
214
215 # note, these two must be in this order ore oneliners aren't handled
216 $comment = 1 if($line =~ /<!--/);
217 $comment = 0 if($line =~ /-->/);
218}
219
220print "}}\n";
221exit($result) if $result;
Note: See TracBrowser for help on using the repository browser.