Blame SOURCES/xml2lst.pl

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