root/gsdl/trunk/perllib/manifest.pm @ 17110

Revision 16254, 3.8 KB (checked in by mdewsnip, 11 years ago)

Fixed up some crazy XML parsing code and removed unnecessary hard-wiring of the different file types.

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