[11994] | 1 | package manifest;
|
---|
| 2 |
|
---|
| 3 | use XMLParser;
|
---|
| 4 | use strict;
|
---|
| 5 | no strict 'refs'; # allow filehandles to be variables and viceversa
|
---|
| 6 |
|
---|
| 7 | our $self;
|
---|
| 8 |
|
---|
| 9 | sub new {
|
---|
| 10 | my ($class) = shift (@_);
|
---|
| 11 |
|
---|
| 12 | $self = {} ;
|
---|
| 13 |
|
---|
| 14 | $self->{'index'} = {};
|
---|
| 15 | $self->{'reindex'} = {};
|
---|
| 16 | $self->{'delete'} = {};
|
---|
| 17 |
|
---|
| 18 | return bless $self, $class;
|
---|
| 19 | }
|
---|
| 20 |
|
---|
| 21 | sub parse
|
---|
| 22 | {
|
---|
| 23 | my ($self) = shift (@_);
|
---|
| 24 | my ($filename) = @_;
|
---|
| 25 |
|
---|
| 26 | my $parser = new XML::Parser('Style' => 'Stream',
|
---|
| 27 | 'Handlers' => {'Char' => \&Char,
|
---|
| 28 | 'XMLDecl' => \&XMLDecl,
|
---|
| 29 | 'Entity' => \&Entity,
|
---|
| 30 | 'Doctype' => \&Doctype,
|
---|
| 31 | 'Default' => \&Default
|
---|
| 32 | });
|
---|
| 33 |
|
---|
| 34 | $parser->parsefile($filename);
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | sub StartDocument {$self->xml_start_document(@_);}
|
---|
| 38 | sub XMLDecl {$self->xml_xmldecl(@_);}
|
---|
| 39 | sub Entity {$self->xml_entity(@_);}
|
---|
| 40 | sub Doctype {$self->xml_doctype(@_);}
|
---|
| 41 | sub StartTag {$self->xml_start_tag(@_);}
|
---|
| 42 | sub EndTag {$self->xml_end_tag(@_);}
|
---|
| 43 | sub Text {$self->xml_text(@_);}
|
---|
| 44 | sub PI {$self->xml_pi(@_);}
|
---|
| 45 | sub EndDocument {$self->xml_end_document(@_);}
|
---|
| 46 | sub Default {$self->xml_default(@_);}
|
---|
| 47 |
|
---|
| 48 | # This Char function overrides the one in XML::Parser::Stream to overcome a
|
---|
| 49 | # problem where $expat->{Text} is treated as the return value, slowing
|
---|
| 50 | # things down significantly in some cases.
|
---|
| 51 | sub Char {
|
---|
| 52 | use bytes; # Necessary to prevent encoding issues with XML::Parser 2.31+
|
---|
| 53 | $_[0]->{'Text'} .= $_[1];
|
---|
| 54 | return undef;
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | # Called at the beginning of the XML document.
|
---|
| 58 | sub xml_start_document {
|
---|
| 59 | my $self = shift(@_);
|
---|
| 60 | my ($expat) = @_;
|
---|
| 61 |
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | # Called for XML declarations
|
---|
| 65 | sub xml_xmldecl {
|
---|
| 66 | my $self = shift(@_);
|
---|
| 67 | my ($expat, $version, $encoding, $standalone) = @_;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | # Called for XML entities
|
---|
| 71 | sub xml_entity {
|
---|
| 72 | my $self = shift(@_);
|
---|
| 73 | my ($expat, $name, $val, $sysid, $pubid, $ndata) = @_;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | # Called for DOCTYPE declarations - use die to bail out if this doctype
|
---|
| 77 | # is not meant for this plugin
|
---|
| 78 | sub xml_doctype {
|
---|
| 79 | my $self = shift(@_);
|
---|
| 80 | my ($expat, $name, $sysid, $pubid, $internal) = @_;
|
---|
| 81 | die "Manifest Cannot process XML document with DOCTYPE of $name";
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | # Called for every start tag. The $_ variable will contain a copy of the
|
---|
| 85 | # tag and the %_ variable will contain the element's attributes.
|
---|
[16254] | 86 | sub xml_start_tag
|
---|
| 87 | {
|
---|
[11994] | 88 | my $self = shift(@_);
|
---|
| 89 | my ($expat, $element) = @_;
|
---|
| 90 |
|
---|
[16254] | 91 | if ($element eq "Filename")
|
---|
| 92 | {
|
---|
| 93 | $self->{'filename'} = "";
|
---|
| 94 | }
|
---|
[17249] | 95 | elsif ($element eq "Manifest") {
|
---|
| 96 | }
|
---|
[16254] | 97 | else
|
---|
| 98 | {
|
---|
| 99 | if (defined($self->{'file-type'}))
|
---|
| 100 | {
|
---|
| 101 | print STDERR "Warning: Malformed XML manifest ($element nested inside " . $self->{'file-type'} . ")\n";
|
---|
[11994] | 102 | }
|
---|
| 103 |
|
---|
[16254] | 104 | $self->{'file-type'} = $element;
|
---|
[11994] | 105 | }
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | # Called for every end tag. The $_ variable will contain a copy of the tag.
|
---|
[16254] | 109 | sub xml_end_tag
|
---|
| 110 | {
|
---|
[11994] | 111 | my $self = shift(@_);
|
---|
| 112 | my ($expat, $element) = @_;
|
---|
| 113 |
|
---|
[16254] | 114 | if ($element eq "Filename")
|
---|
| 115 | {
|
---|
| 116 | $self->{lc($self->{'file-type'})}->{$self->{'filename'}} = 1;
|
---|
| 117 | $self->{'filename'} = undef;
|
---|
| 118 | }
|
---|
| 119 | else
|
---|
| 120 | {
|
---|
[11994] | 121 | $self->{'file-type'} = undef;
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | # Called just before start or end tags with accumulated non-markup text in
|
---|
| 126 | # the $_ variable.
|
---|
| 127 | sub xml_text {
|
---|
| 128 | my $self = shift(@_);
|
---|
| 129 | my ($expat) = @_;
|
---|
| 130 |
|
---|
| 131 | if (defined $self->{'filename'}) {
|
---|
| 132 | my $text = $_;
|
---|
| 133 | chomp($text);
|
---|
| 134 |
|
---|
| 135 | $text =~ s/^\s+//;
|
---|
| 136 | $text =~ s/\s+$//;
|
---|
| 137 |
|
---|
| 138 | $self->{'filename'} .= $text if ($text !~ m/^\s*$/);
|
---|
| 139 | }
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | # Called for processing instructions. The $_ variable will contain a copy
|
---|
| 143 | # of the pi.
|
---|
| 144 | sub xml_pi {
|
---|
| 145 | my $self = shift(@_);
|
---|
| 146 | my ($expat, $target, $data) = @_;
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | # Called at the end of the XML document.
|
---|
| 150 | sub xml_end_document {
|
---|
| 151 | my $self = shift(@_);
|
---|
| 152 | my ($expat) = @_;
|
---|
| 153 |
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | # Called for any characters not handled by the above functions.
|
---|
| 157 | sub xml_default {
|
---|
| 158 | my $self = shift(@_);
|
---|
| 159 | my ($expat, $text) = @_;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 |
|
---|
| 163 | 1;
|
---|