3 # converts the <rules>.xml file to the old format <rules>.lst file
7 # perl xml2lst.pl [lang] < filename.xml > filename.lst
17 $doc = new_document( 0, '');
20 ($reg) = node_by_name($doc, '/xkbConfigRegistry');
21 @models = node_by_name($reg, 'modelList/model/configItem');
22 @layouts = node_by_name($reg, 'layoutList/layout/configItem');
23 @options = node_by_name($reg, 'optionList/group/configItem');
27 ($name) = node_by_name($i, 'name');
28 @desc = node_by_name($i, 'description');
29 $descr = with_attribute(\@desc, 'xml:lang='.$lang);
30 if (! defined $descr) {
31 $descr = with_attribute(\@desc, 'xml:lang=');
33 printf(" %-15s %s\n", text_child($name), text_child($descr));
38 ($name) = node_by_name($i, 'name');
39 @desc = node_by_name($i, 'description');
40 $descr = with_attribute(\@desc, 'xml:lang='.$lang);
41 if (! defined $descr ) {
42 $descr = with_attribute(\@desc, 'xml:lang=');
44 printf(" %-15s %s\n", text_child($name), text_child($descr));
47 print "\n! variant\n";
49 ($lname) = node_by_name($l, 'name');
50 @variants = node_by_name($l, '../variantList/variant/configItem');
52 ($name) = node_by_name($v, 'name');
53 @desc = node_by_name($v, 'description');
54 $descr = with_attribute(\@desc, 'xml:lang='.$lang);
55 if (! defined $descr) {
56 $descr = with_attribute(\@desc, 'xml:lang=');
58 printf(" %-15s %s: %s\n",
59 text_child($name), text_child($lname), text_child($descr));
65 ($name) = node_by_name($g, 'name');
66 @desc = node_by_name($g, 'description');
67 $descr = with_attribute(\@desc, 'xml:lang='.$lang);
68 if (! defined $descr) {
69 $descr = with_attribute(\@desc, 'xml:lang=');
71 printf(" %-20s %s\n", text_child($name), text_child($descr));
73 @opts = node_by_name($g, '../option/configItem');
75 ($name) = node_by_name($o, 'name');
76 @desc = node_by_name($o, 'description');
77 $descr = with_attribute(\@desc, 'xml:lang='.$lang);
78 if (! defined $descr) {
79 $descr = with_attribute(\@desc, 'xml:lang=');
82 text_child($name), text_child($descr));
87 local ($nodelist, $attrexpr) = @_;
88 local ($attr, $value) = split (/=/, $attrexpr);
89 local ($node, $attrvalue);
90 if (defined $value && $value ne '') {
92 foreach $node (@{$nodelist}) {
93 $attrvalue = node_attribute($node, $attr);
94 if (defined $attrvalue && $attrvalue eq $value) {
99 foreach $node (@{$nodelist}) {
100 if (! defined node_attribute($node, $attr)) {
112 my (@node_stack, $parent);
118 @str = split /([<>])/;
119 shift @str if ($str[0] eq '' || $str[0] =~ /^[ \t]*$/);
121 while (scalar @str) {
126 add_text_node($parent, $text);
129 } elsif ($token eq '>') {
131 if ($tag =~ /^\/(.*)/) { # close tag
132 $parent = pop @node_stack;
133 } elsif ($tag =~ /^([^\/]*)\/$/) {
134 empty_tag($parent, $1);
136 if (defined ($node = open_tag($parent, $tag))) {
137 push @node_stack, $parent;
151 $text .= "\n" . $token;
162 $doc = new_node( 0, '', 'DOCUMENT');
163 $doc->{CHILDREN} = [];
168 local ($parent_node, $tag, $type) = @_;
171 $node{PARENT} = $parent_node;
174 if ($type eq 'COMMENT' || $type eq 'TEXT') {
180 local ($tname, $attr) = split(' ', $tag, 2);
181 $node{NAME} = $tname;
183 if (defined $attr && $attr ne '') {
185 local @attr_list = split ( /"/, $attr);
186 local ($name, $value);
187 while (scalar @attr_list) {
188 $name = shift @attr_list;
190 next if ($name eq '');
191 $value = shift @attr_list;
192 $attr_table{$name} =$value;
194 $node{ATTRIBUTES} = \%attr_table;
200 local ($parent_node, $node) = @_;
201 push @{$parent_node->{CHILDREN}}, $node;
203 local $tname = $node->{NAME};
204 if (defined $parent_node->{$tname}) {
205 push @{$parent_node->{$tname}}, $node
207 $parent_node->{$tname} = [ $node ];
212 local ($parent_node, $tag) = @_;
213 local $node = new_node($parent_node, $tag, 'EMPTY');
214 add_node($parent_node, $node);
218 local ($parent_node, $tag) = @_;
221 if ($tag =~ /^\?.*/ || $tag =~ /^\!.*/) {
222 $node = new_node($parent_node, $tag, 'COMMENT');
223 add_node($parent_node, $node);
226 $node = new_node($parent_node, $tag, 'NODE');
227 $node->{CHILDREN} = [];
228 add_node($parent_node, $node);
234 local ($parent_node, $text) = @_;
235 local $node = new_node($parent_node, $text, 'TEXT');
236 add_node($parent_node, $node);
240 local ($node, $name) = @_;
241 local ($tagname, $path) = split(/\//, $name, 2);
245 if ($tagname eq '') {
246 while ($node->{PARENT} != 0) {
247 $node = $node->{PARENT};
249 sublist_by_name($node, $path, \@nodelist);
251 sublist_by_name($node, $name, \@nodelist);
256 sub sublist_by_name {
257 local ($node, $name, $res) = @_;
258 local ($tagname, $path) = split(/\//, $name, 2);
260 if (! defined $path) {
261 push @{$res}, (@{$node->{$tagname}});
265 if ($tagname eq '..' && $node->{PARENT} != 0) {
266 $node = $node->{PARENT};
267 sublist_by_name($node, $path, $res);
270 for $n (@{$node->{$tagname}}) {
271 sublist_by_name($n, $path, $res);
278 if (defined $node->{ATTRIBUTES}) {
279 return $node->{ATTRIBUTES}{@_[1]};
286 local ($child) = node_by_name($node, 'TEXT');
287 return $child->{TEXT};