File: convpreset.pl

package info (click to toggle)
josm 0.0.svn14760%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 187,192 kB
  • sloc: java: 317,260; xml: 197,001; perl: 10,125; jsp: 250; sh: 112; makefile: 94; python: 29
file content (253 lines) | stat: -rw-r--r-- 6,082 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#! /usr/bin/perl -w

# Written by Dirk Stöcker <openstreetmap@dstoecker.de>
# Public domain, no rights reserved.

use strict;

my $item = "";
my $chunk = "";
my $group;
my $combo_n;
my $combo_type;
my $result = 0;
my $comment = 0;
my $vctx;

# This is a simple conversion and in no way a complete XML parser
# but it works with a default Perl installation

# Print a header to write valid Java code.  No line break,
# so that the input and output line numbers will match.
print "class trans_preset { void tr(String s){} void f() {";

sub fix($)
{
  my ($val) = @_;
  $val =~ s/'/''/g;
  $val =~ s/&lt;/</g;
  $val =~ s/&gt;/>/g;
  $val =~ s/&amp;/&/g;
  return $val;
}

sub infoblock
{
  my $r = "";
  $r .= " item $item" if $item;
  $r .= " chunk $chunk" if $chunk;
  $r .= " group $group" if $group;
  $r .= " combo $combo_type $combo_n" if $combo_type;
  $r .= " $_[0]" if $_[0];
  return $r ? "/* $r */ " : "";
}

my $linenr = 0;
while(my $line = <>)
{
  ++$linenr;
  chomp($line);
  print "tr(\"---DUMMY-MARKER---\"); ";
  if($line =~ /<item\s+name=(".*?")/ || $line =~ /<item.* name=(".*?")/)
  {
    my $val = fix($1);
    $item = $group ? "$group$val" : $val;
    $item =~ s/""/\//;
    if($line =~ /name_context=(".*?")/)
    {
      print infoblock() . "trc($1, $val);\n";
    }
    else
    {
      print infoblock() . "tr($val);\n";
    }
  }
  elsif($line =~ /<chunk\s+id=(".*?")/)
  {
    $chunk = fix($1);
  }
  elsif($line =~ /<group.*\s+name=(".*?")/)
  {
    my $gr = fix($1);
    $group = $group ? "$group$gr" : $gr;
    $group =~ s/\"\"/\//;
    if($line =~ /name_context=(".*?")/)
    {
      print infoblock() . "trc($1,$gr);\n";
    }
    else
    {
      print infoblock() . "tr($gr);\n";
    }
  }
  elsif($line =~ /<label.*\s+text=" "/)
  {
    print infoblock("empty label") . "\n";
  }
  elsif($line =~ /<label.*\s+text=(".*?")/)
  {
    my $text = fix($1);
    if($line =~ /text_context=(".*?")/)
    {
      print infoblock("label $text") ."trc($1,$text);\n";
    }
    else
    {
      print infoblock("label $text") . "tr($text);\n";
    }
  }
  elsif($line =~ /<text.*\s+text=(".*?")/)
  {
    my $n = fix($1);
    if($line =~ /text_context=(".*?")/)
    {
      print infoblock("text $n") . "trc($1,$n);\n";
    }
    else
    {
      print infoblock("text $n") . "tr($n);\n";
    }
  }
  elsif($line =~ /<check.*\s+text=(".*?")/)
  {
    my $n = fix($1);
    if($line =~ /text_context=(".*?")/)
    {
      print infoblock("check $n") . "trc($1,$n);\n";
    }
    else
    {
      print infoblock("check $n") . "tr($n);\n";
    }
  }
  elsif($line =~ /<role.*\s+text=(".*?")/)
  {
    my $n = fix($1);
    if($line =~ /text_context=(".*?")/)
    {
      print infoblock("role $n") . "trc($1,$n);\n";
    }
    else
    {
      print infoblock("role $n") . "tr($n);\n";
    }
  }
  elsif($line =~ /<optional.*\s+text=(".*?")/)
  {
    my $n = fix($1);
    if($line =~ /text_context=(".*?")/)
    {
      print infoblock("optional $n") . "trc($1,$n);\n";
    }
    else
    {
      print infoblock("optional $n") . "tr($n);\n";
    }
  }
  elsif($line =~ /<(combo|multiselect).*\s+text=(".*?")/)
  {
    my ($type,$n) = ($1,fix($2));
    $combo_n = $n;
    $combo_type = $type;
    $vctx = ($line =~ /values_context=(".*?")/) ? $1 : undef;
    # text
    my $tctx = ($line =~ /text_context=(".*?")/) ? $1 : undef;
    print infoblock("$type $n") . ($tctx ? " trc($tctx, $n);" : " tr($n);");
    # display_values / values
    my $sp = ($line =~ /delimiter="(.*?)"/) ? $1 : ($type eq "combo" ? ",":";");
    my $vals = ($line =~ / display_values="(.*?)"/) ? $1 : ($line =~ /values="(.*?)"/) ? $1 : undef;
    $vals = undef if ($line =~ /values_no_i18n="true"/);
    if($vals)
    {
      my @combo_values = split "\Q$sp\E" ,$vals;
      foreach my $val (@combo_values)
      {
        next if $val =~ /^[0-9-]+$/; # search for non-numbers
        $val = fix($val);
        print infoblock("$type $n display value") . ($vctx ? " trc($vctx, \"$val\");" : " tr(\"$val\");");
      }
    }
    print "\n";
  }
  elsif(!$comment && $line =~ /<list_entry/)
  {
    my $vctxi = ($line =~ /value_context=(".*?")/) ? $1 : $vctx;
    my $value = ($line =~ /value=(".*?")/) ? $1 : undef;
    if($line =~ /[^.]display_value=(".*?")/)
    {
      my $val = fix($1);
      print infoblock("entry $value display value") . ($vctxi ? " trc($vctxi, $val);" : " tr($val);");
    }
    else
    {
      my $val = fix($value);
      print infoblock("entry $value display value") . ($vctxi ? " trc($vctxi, $val);" : " tr($val);");
    }
    if($line =~ /short_description=(".*?")/)
    {
      my $val = fix($1);
      print infoblock("entry $value short description") . "tr($val);";
    }
    print "\n";
  }
  elsif($line =~ /<\/group>/)
  {
    $group = 0 if !($group =~ s/(.*\/).*?$//);
    print "\n";
  }
  elsif($line =~ /<\/item>/)
  {
    $item = "";
    print "\n";
  }
  elsif($line =~ /<\/chunk>/)
  {
    $chunk = "";
  }
  elsif($line =~ /<\/(combo|multiselect)/)
  {
    $combo_n = "";
    print "\n";
  }
  elsif(!$line)
  {
    print "\n";
  }
  elsif($line =~ /^\s*$/
     || $line =~ /<separator *\/>/
     || $line =~ /<space *\/>/
     || $line =~ /<\/?optional>/
     || $line =~ /<key/
     || $line =~ /<presets/
     || $line =~ /<checkgroup/
     || $line =~ /<\/checkgroup/
     || $line =~ /<\/presets/
     || $line =~ /roles/
     || $line =~ /<link wiki=/
     || $line =~ /href=/
     || $line =~ /<!--/
     || $line =~ /<\?xml/
     || $line =~ /-->/
     || $line =~ /<\/?chunk/
     || $line =~ /<reference/
     || $line =~ /<preset_link/
     || $line =~ /<item_separator\/>/
     || $comment)
  {
    $line =~ s/[ \t]+((?:short)?description) *= *"([^"]+)/*\/ \/* $1 *\/ tr("$2"); \/*/g;
    print "/* $line */\n";
  }
  else
  {
    print "/* unparsed line $line */\n";
    print STDERR "/* unparsed line $linenr $line */\n";
    $result = 20
  }

  # note, these two must be in this order ore oneliners aren't handled
  $comment = 1 if($line =~ /<!--/);
  $comment = 0 if($line =~ /-->/);
}

print "}}\n";
exit($result) if $result;