summaryrefslogtreecommitdiffstats
path: root/urpm/xml_info.pm
blob: a03126f52611c5b6176cf1bd766a01f8bd6daff2 (plain)
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package urpm::xml_info;

use strict;
use XML::LibXML::Reader;

=head1 NAME

urpm::xml_info - XML data manipulation related routines for urpmi

=head1 SYNOPSIS

=head1 DESCRIPTION

=over

=cut 

# throw an exception on error
sub get_nodes {
    my ($xml_info, $xml_info_file, $fullnames) = @_;

    my $get_one_node = _get_one_node($xml_info);
    _get_xml_info_nodes($xml_info_file, $get_one_node, $fullnames);
}

# throw an exception on error
sub do_something_with_nodes {
    my ($xml_info, $xml_info_file, $do, $o_wanted_attributes) = @_;

    my $get_one_node = _get_one_node($xml_info, $o_wanted_attributes);
    _do_something_with_xml_info_nodes($xml_info_file, $get_one_node, $do);
}


sub open_lzma {
    my ($xml_info_file) = @_;

    $xml_info_file =~ s/'/'\\''/g;
    open(my $F, "xz -dc '$xml_info_file' |");
    $F;    
}

################################################################################
sub _open_xml_reader {
    my ($xml_info_file) = @_;

    my $reader = new XML::LibXML::Reader(IO => open_lzma($xml_info_file)) or die "cannot read $xml_info_file\n";

    $reader->read;
    $reader->name eq 'media_info' or die "global <media_info> tag not found\n";

    $reader->read; # first tag

    $reader;
}

sub _get_all_attributes {
    my ($reader) = @_;
    my %entry;

    $reader->moveToFirstAttribute;

    do { 
	$entry{$reader->name} = $reader->value;
    } while $reader->moveToNextAttribute == 1;
    
    \%entry;
}

sub _get_attributes {
    my ($reader, $o_wanted_attributes) = @_;

    if ($o_wanted_attributes) {
	my %entry = map { $_ => $reader->getAttribute($_) } @$o_wanted_attributes;
	\%entry;
    } else {
	_get_all_attributes($reader);
    }
}

sub _get_simple_value_node {
    my ($value_name, $o_wanted_attributes) = @_;

    sub {
	my ($reader) = @_;
	my $entry = _get_attributes($reader, $o_wanted_attributes);

	$reader->read; # get value
	$entry->{$value_name} = $reader->value;
	$entry->{$value_name} =~ s/^\n//;

	$reader->read; # close tag
	$reader->read; # open next tag

	$entry;
    };
}

sub _get_changelog_node {
    my ($reader, $fn) = @_;
	
    $reader->nextElement('log'); # get first <log>

    my @changelogs;
    my $time;
    while ($time = $reader->getAttribute('time')) {
	push @changelogs, my $e = { time => $time };

	$reader->nextElement('log_name'); $reader->read;
	$e->{name} = $reader->value;

	$reader->nextElement('log_text'); $reader->read;
	$e->{text} = $reader->value;
	
	$reader->read; # </log_text>
	$reader->read; # </log>
	$reader->read; # <log>
	$reader->read if $reader->readState != 0; # there may be SIGNIFICANT_WHITESPACE between </log_text> and </log>
    }

    { fn => $fn, changelogs => \@changelogs };
}

sub _get_one_node {
    my ($xml_info, $o_wanted_attributes) = @_;

    if ($xml_info eq 'changelog') {
	\&_get_changelog_node;
    } elsif ($xml_info eq 'info') {
	_get_simple_value_node('description', $o_wanted_attributes);
    } else {
	_get_simple_value_node('files', $o_wanted_attributes);
    }
}

sub _get_xml_info_nodes {
    my ($xml_info_file, $get_node, $fullnames) = @_;

    my $fullnames_re = '^(' . join('|', map { quotemeta $_ } @$fullnames) . ')$';

    my %todo = map { $_ => 1 } @$fullnames;
    my %nodes;
    _iterate_on_nodes($xml_info_file,
		      sub {
			  my ($reader, $fn) = @_;
			  if ($fn =~ /$fullnames_re/) {
			      $nodes{$fn} = $get_node->($reader);
			      delete $todo{$fn};
			      keys(%todo) == 0;
			  } else {
			      $reader->next;
			      0;
			  }
		      });

    %todo and die "could not find " . join(', ', keys %todo) . " in $xml_info_file\n";

    %nodes;
}

sub _do_something_with_xml_info_nodes {
    my ($xml_info_file, $get_node, $do) = @_;

    _iterate_on_nodes($xml_info_file,
		      sub {
			  my ($reader, $fn) = @_;
			  my $h = $get_node->($reader, $fn); # will read until closing tag
			  $do->($h);
			  0;
		      });
}

sub _iterate_on_nodes {
    my ($xml_info_file, $do) = @_;

    my $reader = _open_xml_reader($xml_info_file);

    my $fn;
    while ($fn = $reader->getAttribute('fn')) {
	$do->($reader, $fn) and return; # $do must go to next node otherwise it loops!
    }

    $reader->readState == 3 || $reader->name eq 'media_info' 
      or die qq(missing attribute "fn" in tag ") . $reader->name . qq("\n);
}

1;


=back

=head1 COPYRIGHT

Copyright (C) 2005 MandrakeSoft SA

Copyright (C) 2005-2010 Mandriva SA

=cut