File: convpreset.pl

package info (click to toggle)
josm 0.0.svn3376-1%2Bsqueeze1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 53,192 kB
  • ctags: 15,964
  • sloc: java: 109,690; xml: 7,599; perl: 821; makefile: 51; sh: 21
file content (180 lines) | stat: -rw-r--r-- 4,087 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
#! /usr/bin/perl -w

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

use strict;

my $item = "";
my $group;
my $comment = 0;

# 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() {";

while(my $line = <>)
{
  chomp($line);
  if($line =~ /<item\s+name=(".*?")/)
  {
    my $val = $1;
    $item = $group ? "$group$val" : $val;
    $item =~ s/""/\//;
    if($line =~ /name_context=(".*?")/)
    {
      print "trc($1, $val); /* item $item */\n";
    }
    else
    {
      print "tr($val); /* item $item */\n";
    }
  }
  elsif($line =~ /<group.*\s+name=(".*?")/)
  {
    my $gr = $1;
    $group = $group ? "$group$gr" : $gr;
    $group =~ s/\"\"/\//;
    if($line =~ /name_context=(".*?")/)
    {
      print "trc($1,$gr); /* group $group */\n";
    }
    else
    {
      print "tr($gr); /* group $group */\n";
    }
  }
  elsif($line =~ /<label.*\s+text=" "/)
  {
    print "/* item $item empty label */\n";
  }
  elsif($line =~ /<label.*\s+text=(".*?")/)
  {
    my $text = $1;
    if($line =~ /text_context=(".*?")/)
    {
      print "trc($1,$text); /* item $item label $text */\n";
    }
    else
    {
      print "tr($text); /* item $item label $text */\n";
    }
  }
  elsif($line =~ /<text.*\s+text=(".*?")/)
  {
    my $n = $1;
    if($line =~ /text_context=(".*?")/)
    {
      print "trc($1,$n); /* item $item text $n */\n";
    }
    else
    {
      print "tr($n); /* item $item text $n */\n";
    }
  }
  elsif($line =~ /<check.*\s+text=(".*?")/)
  {
    my $n = $1;
    if($line =~ /text_context=(".*?")/)
    {
      print "trc($1,$n); /* item $item check $n */\n";
    }
    else
    {
      print "tr($n); /* item $item check $n */\n";
    }
  }
  elsif($line =~ /<role.*\s+text=(".*?")/)
  {
    my $n = $1;
    if($line =~ /text_context=(".*?")/)
    {
      print "trc($1,$n); /* item $item role $n */\n";
    }
    else
    {
      print "tr($n); /* item $item role $n */\n";
    }
  }
  # first handle display values
  elsif($line =~ /<combo.*\s+text=(".*?").*\s+display_values="(.*?)"/)
  {
    my ($n,$vals) = ($1,$2);
    my $vctx = ($line =~ /values_context=(".*?")/) ? $1 : undef;
    if($line =~ /text_context=(".*?")/)
    {
      print "trc($1,$n); /* item $item combo $n */";
    }
    else
    {
      print "tr($n); /* item $item combo $n */";
    }
    foreach my $val (split ",",$vals)
    {
      next if $val =~ /^[0-9-]+$/; # search for non-numbers
      print $vctx ? " trc($vctx, \"$val\");" : " tr(\"$val\");";
    }
    print "\n";
  }
  elsif($line =~ /<combo.*\s+text=(".*?").*\s+values="(.*?)"/)
  {
    my ($n,$vals) = ($1,$2);
    my $vctx = ($line =~ /values_context=(".*?")/) ? $1 : undef;
    if($line =~ /text_context=(".*?")/)
    {
      print "trc($1,$n); /* item $item combo $n */";
    }
    else
    {
      print "tr($n); /* item $item combo $n */";
    }
    foreach my $val (split ",",$vals)
    {
      next if $val =~ /^[0-9-]+$/; # search for non-numbers
      print $vctx ? " trc($vctx, \"$val\");" : " tr(\"$val\");";
    }
    print "\n";
  }
  elsif($line =~ /<\/group>/)
  {
    $group = 0 if !($group =~ s/(.*\/).*?$//);
    print "\n";
  }
  elsif($line =~ /<\/item>/)
  {
    $item = "";
    print "\n";
  }
  elsif(!$line)
  {
    print "\n";
  }
  elsif($line =~ /^\s*$/
     || $line =~ /<separator *\/>/
     || $line =~ /<space *\/>/
     || $line =~ /<\/?optional>/
     || $line =~ /<key/
     || $line =~ /annotations/
     || $line =~ /roles/
     || $line =~ /href=/
     || $line =~ /<!--/
     || $line =~ /-->/
     || $comment)
  {
    print "// $line\n";
  }
  else
  {
    print "/* unparsed line $line */\n";
#    print STDERR "Unparsed line $line\n";
  }

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

print "}}\n";