Blame SOURCES/xml2lst.pl

ea84b2
#!/usr/bin/perl
ea84b2
ea84b2
# converts the <rules>.xml file to the old format <rules>.lst file
ea84b2
#
ea84b2
# Usage:
ea84b2
#
ea84b2
# perl xml2lst.pl < filename.xml > filename.lst
ea84b2
#
ea84b2
# author Ivan Pascal
ea84b2
# modified by Vitezslav Crhonek
ea84b2
ea84b2
$doc = new_document( 0, '');
ea84b2
parse('', $doc);
ea84b2
ea84b2
($reg)   = node_by_name($doc, '/xkbConfigRegistry');
ea84b2
@models  = node_by_name($reg, 'modelList/model/configItem');
ea84b2
@layouts = node_by_name($reg, 'layoutList/layout/configItem');
ea84b2
@options = node_by_name($reg, 'optionList/group/configItem');
ea84b2
ea84b2
for $i (@layouts) {
ea84b2
   ($name) = node_by_name($i, 'name');
ea84b2
   @variants = node_by_name($i, '../variantList/variant/configItem');
ea84b2
   for $v (@variants) {
ea84b2
      ($variant) = node_by_name($v, 'name');
ea84b2
      printf("%s %s\n", text_child($name), text_child($variant));
ea84b2
   }
ea84b2
}
ea84b2
ea84b2
sub with_attribute {
ea84b2
    local ($nodelist, $attrexpr) = @_;
ea84b2
    local ($attr, $value) = split (/=/, $attrexpr);
ea84b2
    local ($node, $attrvalue);
ea84b2
    if (defined $value && $value ne '') {
ea84b2
        $value =~ s/"//g;
ea84b2
        foreach $node (@{$nodelist}) {
ea84b2
           $attrvalue = node_attribute($node, $attr); 
ea84b2
           if (defined $attrvalue && $attrvalue eq $value) {
ea84b2
               return $node;
ea84b2
           }
ea84b2
        }
ea84b2
    } else {
ea84b2
        foreach $node (@{$nodelist}) {
ea84b2
           if (! defined node_attribute($node, $attr)) {
ea84b2
               return $node;
ea84b2
           }
ea84b2
        }
ea84b2
    }
ea84b2
    undef;
ea84b2
}
ea84b2
ea84b2
# Subroutines
ea84b2
ea84b2
sub parse {
ea84b2
   local $intag = 0;
ea84b2
   my (@node_stack, $parent);
ea84b2
   $parent = @_[1];
ea84b2
   local ($tag, $text);
ea84b2
ea84b2
   while (<>) {
ea84b2
      chomp;
ea84b2
      @str = split /([<>])/;
ea84b2
      shift @str if ($str[0] eq '' || $str[0] =~ /^[ \t]*$/);
ea84b2
ea84b2
      while (scalar @str) {
ea84b2
         $token = shift @str;
ea84b2
         if ($token eq '<') {
ea84b2
            $intag = 1;
ea84b2
            if (defined $text) {
ea84b2
               add_text_node($parent, $text);
ea84b2
               undef $text;
ea84b2
            }
ea84b2
         } elsif ($token eq '>') {
ea84b2
            $intag = 0;
ea84b2
            if ($tag =~ /^\/(.*)/) { # close tag
ea84b2
               $parent = pop @node_stack;
ea84b2
            } elsif ($tag =~ /^([^\/]*)\/$/) {
ea84b2
               empty_tag($parent, $1);
ea84b2
            } else {
ea84b2
               if (defined ($node = open_tag($parent, $tag))) {
ea84b2
                  push @node_stack, $parent;
ea84b2
                  $parent = $node;
ea84b2
               }
ea84b2
            }
ea84b2
            undef $tag;
ea84b2
         } else {
ea84b2
            if ($intag == 1) {
ea84b2
               if (defined $tag) {
ea84b2
                  $tag .= ' '. $token;
ea84b2
               } else {
ea84b2
                  $tag = $token;
ea84b2
               }
ea84b2
            } else {
ea84b2
               if (defined $text) {
ea84b2
                  $text .= "\n" . $token;
ea84b2
               } else {
ea84b2
                  $text = $token;
ea84b2
               }
ea84b2
            }
ea84b2
         }
ea84b2
      }
ea84b2
   }
ea84b2
}
ea84b2
ea84b2
sub new_document {
ea84b2
   $doc = new_node( 0, '', 'DOCUMENT');
ea84b2
   $doc->{CHILDREN} = [];
ea84b2
   return $doc;
ea84b2
}
ea84b2
ea84b2
sub new_node {
ea84b2
  local ($parent_node, $tag, $type) = @_;
ea84b2
ea84b2
  my %node;
ea84b2
  $node{PARENT} = $parent_node;
ea84b2
  $node{TYPE} = $type;
ea84b2
ea84b2
  if ($type eq 'COMMENT' || $type eq 'TEXT') {
ea84b2
     $node{TEXT} = $tag;
ea84b2
     $node{NAME} = $type;
ea84b2
     return \%node;
ea84b2
  }
ea84b2
ea84b2
  local ($tname, $attr) = split(' ', $tag, 2);
ea84b2
  $node{NAME} = $tname;
ea84b2
ea84b2
  if (defined $attr && $attr ne '') {
ea84b2
     my %attr_table;
ea84b2
     local @attr_list = split ( /"/, $attr);
ea84b2
     local ($name, $value);
ea84b2
     while (scalar @attr_list) {
ea84b2
        $name = shift @attr_list;
ea84b2
        $name =~ s/[ =]//g;
ea84b2
        next if ($name eq '');
ea84b2
        $value =  shift @attr_list;
ea84b2
        $attr_table{$name} =$value;
ea84b2
     }
ea84b2
     $node{ATTRIBUTES} = \%attr_table;
ea84b2
  }
ea84b2
  return \%node;
ea84b2
}
ea84b2
ea84b2
sub add_node {
ea84b2
  local ($parent_node, $node) = @_;
ea84b2
  push @{$parent_node->{CHILDREN}}, $node;
ea84b2
ea84b2
  local $tname = $node->{NAME};
ea84b2
  if (defined $parent_node->{$tname}) {
ea84b2
      push @{$parent_node->{$tname}}, $node
ea84b2
  } else {
ea84b2
      $parent_node->{$tname} = [ $node ];
ea84b2
  }
ea84b2
}
ea84b2
ea84b2
sub empty_tag {
ea84b2
   local ($parent_node, $tag) = @_;
ea84b2
   local $node = new_node($parent_node, $tag, 'EMPTY');
ea84b2
   add_node($parent_node, $node);
ea84b2
}
ea84b2
ea84b2
sub open_tag {
ea84b2
   local ($parent_node, $tag) = @_;
ea84b2
   local $node;
ea84b2
ea84b2
   if ($tag =~ /^\?.*/ || $tag =~ /^\!.*/) {
ea84b2
      $node = new_node($parent_node, $tag, 'COMMENT');
ea84b2
      add_node($parent_node, $node);
ea84b2
      undef; return;
ea84b2
   } else {
ea84b2
      $node = new_node($parent_node, $tag, 'NODE');
ea84b2
      $node->{CHILDREN} = [];
ea84b2
      add_node($parent_node, $node);
ea84b2
      return $node;
ea84b2
   }
ea84b2
}
ea84b2
ea84b2
sub add_text_node {
ea84b2
   local ($parent_node, $text) = @_;
ea84b2
   local $node = new_node($parent_node, $text, 'TEXT');
ea84b2
   add_node($parent_node, $node);
ea84b2
}
ea84b2
ea84b2
sub node_by_name {
ea84b2
   local ($node, $name) = @_;
ea84b2
   local ($tagname, $path) = split(/\//, $name, 2);
ea84b2
ea84b2
   my @nodelist;
ea84b2
ea84b2
   if ($tagname eq '') {
ea84b2
      while ($node->{PARENT} != 0) {
ea84b2
         $node = $node->{PARENT};
ea84b2
      }
ea84b2
      sublist_by_name($node, $path, \@nodelist);
ea84b2
   } else {
ea84b2
      sublist_by_name($node, $name, \@nodelist);
ea84b2
   }
ea84b2
   return @nodelist;
ea84b2
}
ea84b2
ea84b2
sub sublist_by_name {
ea84b2
   local ($node, $name, $res) = @_;
ea84b2
   local ($tagname, $path) = split(/\//, $name, 2);
ea84b2
ea84b2
   if (! defined $path) {
ea84b2
       push @{$res}, (@{$node->{$tagname}});
ea84b2
       return;
ea84b2
   }
ea84b2
ea84b2
   if ($tagname eq '..' && $node->{PARENT} != 0) {
ea84b2
      $node = $node->{PARENT};
ea84b2
      sublist_by_name($node, $path, $res);
ea84b2
   } else {
ea84b2
      local $n;
ea84b2
      for $n (@{$node->{$tagname}}) {
ea84b2
         sublist_by_name($n, $path, $res);
ea84b2
      }
ea84b2
   }
ea84b2
}
ea84b2
ea84b2
sub node_attribute {
ea84b2
    local $node = @_[0];
ea84b2
    if (defined $node->{ATTRIBUTES}) {
ea84b2
       return $node->{ATTRIBUTES}{@_[1]};
ea84b2
    }
ea84b2
    undef;
ea84b2
}
ea84b2
ea84b2
sub text_child {
ea84b2
    local ($node) = @_;
ea84b2
    local ($child) = node_by_name($node, 'TEXT');
ea84b2
    return $child->{TEXT};
ea84b2
}