File: convstyle.pl

package info (click to toggle)
josm 0.0.svn7643%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 67,480 kB
  • ctags: 25,960
  • sloc: java: 177,482; xml: 8,896; perl: 1,570; sh: 102; makefile: 81
file content (71 lines) | stat: -rw-r--r-- 1,832 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
#! /usr/bin/perl -w

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

use strict;

my $rule_cond; # cumulated conditions from current rule

# 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_style { void tr(String s){} void f() {";

while(my $line = <>)
{
  chomp($line);
  print "tr(\"\"); ";
  if($line =~ /<rules\s+name=(".*?")/)
  {
    print "/* mappaint style named $1 */ tr($1);\n";
  }
  elsif($line =~ /<rule\s*>/)
  {
    $rule_cond = "";
    print "/* $line */\n";
  }
  elsif($line =~ /<condition.*\s+k="([^"]*)"/)
  {
    my $cond_k = $1; # according to schema, k is always present
    my $cond_v = ($line =~ /\s+v="([^"]*)"/) ? $1 : "";
    my $cond_b = ($line =~ /\s+b="([^"]*)"/) ? $1 : "";
    print STDERR "$0: Found both v=\"$cond_v\" and b=\"$cond_b\" for k=\"$cond_k\" at line $.\n" if ($cond_v && $cond_b);
    my $cond = ($cond_v || $cond_b) ? "$cond_k=$cond_v$cond_b" : "$cond_k"; # v and b shouldn't appear both
    if ($rule_cond)
    {
      $rule_cond .= ", " . $cond;
    }
    else
    {
      $rule_cond = $cond;
    }
    print "/* $line */\n";
  }
  elsif($line =~ /colour="([^"]+)#/)
  {
    if ($line =~ /\s+colour="([^"]+)#/)
    {
      my $c = $1;
      my $co = $1;
      $c =~ s/[^a-z0-9]+/./g;
      print "/* color $co (applied for \"$rule_cond\") */ tr(\"$c\");";
    }
    if ($line =~ /\s+dashedcolour="([^"]+)#/)
    {
      my $c = $1;
      my $co = $1;
      $c =~ s/[^a-z0-9]+/./g;
      print "/* color $co (applied for \"$rule_cond\") */ tr(\"$c\");";
    }
    print "\n";
  }
  else
  {
    print "/* $line */\n";
  }
}

print "}}\n";