1 |
sashby |
1.1 |
#____________________________________________________________________
|
2 |
|
|
# File: SimpleXMLDoc.pm
|
3 |
|
|
#____________________________________________________________________
|
4 |
|
|
#
|
5 |
|
|
# Author: Shaun Ashby <Shaun.Ashby@cern.ch>
|
6 |
|
|
# Update: 2005-04-22 17:09:26+0200
|
7 |
|
|
# Revision: $Id$
|
8 |
|
|
#
|
9 |
|
|
# Copyright: 2005 (C) Shaun Ashby
|
10 |
|
|
#
|
11 |
|
|
#--------------------------------------------------------------------
|
12 |
|
|
package ActiveDoc::SimpleXMLDoc;
|
13 |
|
|
require 5.004;
|
14 |
|
|
use Exporter;
|
15 |
|
|
use XML::Parser::Expat;
|
16 |
|
|
|
17 |
|
|
@ISA=qw(Exporter);
|
18 |
|
|
@EXPORT_OK=qw( );
|
19 |
|
|
|
20 |
|
|
sub new()
|
21 |
|
|
###############################################################
|
22 |
|
|
# new #
|
23 |
|
|
###############################################################
|
24 |
|
|
# modified : Fri Apr 22 17:10:10 2005 / SFA #
|
25 |
|
|
# params : #
|
26 |
|
|
# : #
|
27 |
|
|
# function : #
|
28 |
|
|
# : #
|
29 |
|
|
###############################################################
|
30 |
|
|
{
|
31 |
|
|
my $proto=shift;
|
32 |
|
|
my $class=ref($proto) || $proto;
|
33 |
|
|
my $self={};
|
34 |
|
|
my ($start, $end, $char, $context)=@_;
|
35 |
|
|
bless $self,$class;
|
36 |
|
|
|
37 |
|
|
$self->{CONTEXT}=$context;
|
38 |
|
|
$self->{DEFAULT_HANDLERS}=[ $start, $end, $char ];
|
39 |
|
|
$self->{FUNCTIONDB}={};
|
40 |
|
|
$self->{ATTRIBUTEDB}={};
|
41 |
|
|
$self->{nested} = [];
|
42 |
|
|
|
43 |
|
|
$self->_initxmlparser();
|
44 |
|
|
|
45 |
|
|
return $self;
|
46 |
|
|
}
|
47 |
|
|
|
48 |
|
|
sub _initxmlparser()
|
49 |
|
|
{
|
50 |
|
|
my $self=shift;
|
51 |
|
|
$self->{xmlparser} = new XML::Parser::Expat;
|
52 |
|
|
}
|
53 |
|
|
|
54 |
|
|
sub setHandlers()
|
55 |
|
|
{
|
56 |
|
|
my $self=shift;
|
57 |
|
|
my ($start, $end, $char)=@_;
|
58 |
|
|
|
59 |
|
|
# Add the recognised XML tag handlers:
|
60 |
|
|
$self->{xmlparser}->setHandlers('Start' => $self->{DEFAULT_HANDLERS}->[0],
|
61 |
|
|
'End' => $self->{DEFAULT_HANDLERS}->[1],
|
62 |
|
|
'Char' => $self->{DEFAULT_HANDLERS}->[2]);
|
63 |
|
|
}
|
64 |
|
|
|
65 |
|
|
sub registerTag()
|
66 |
|
|
{
|
67 |
|
|
my $self=shift;
|
68 |
|
|
my ($context,$tagname,$tagfunction,$attributes,$nested)=@_;
|
69 |
|
|
$self->addattributestocontext($context,$tagname,$attributes);
|
70 |
|
|
$self->addtagtocontext($context,$tagname,$tagfunction);
|
71 |
|
|
|
72 |
|
|
if ($nested)
|
73 |
|
|
{
|
74 |
|
|
# Add the name of the tag which is nested:
|
75 |
|
|
$self->nested($tagname);
|
76 |
|
|
}
|
77 |
|
|
}
|
78 |
|
|
|
79 |
|
|
sub addtagtocontext()
|
80 |
|
|
{
|
81 |
|
|
my $self=shift;
|
82 |
|
|
my ($context,$tagname,$tagfunction)=@_;
|
83 |
|
|
$self->{FUNCTIONDB}->{$context}->{$tagname} = $tagfunction;
|
84 |
|
|
}
|
85 |
|
|
|
86 |
|
|
sub addattributestocontext()
|
87 |
|
|
{
|
88 |
|
|
my $self=shift;
|
89 |
|
|
my ($context,$tagname,$attributes)=@_;
|
90 |
|
|
return if ($attributes == 0);
|
91 |
|
|
$self->{ATTRIBUTEDB}->{$context}->{$tagname} = $attributes;
|
92 |
|
|
}
|
93 |
|
|
|
94 |
|
|
sub parsefile()
|
95 |
|
|
{
|
96 |
|
|
my $self=shift;
|
97 |
|
|
my ($filename)=@_;
|
98 |
|
|
$self->{xmlparser}->parsefile($filename);
|
99 |
|
|
# Clean up:
|
100 |
|
|
delete $self->{xmlparser};
|
101 |
|
|
delete $self->{FUNCTIONDB};
|
102 |
|
|
delete $self->{DEFAULT_HANDLERS};
|
103 |
|
|
}
|
104 |
|
|
|
105 |
|
|
sub gettagfunction()
|
106 |
|
|
{
|
107 |
|
|
my $self=shift;
|
108 |
|
|
my ($tagname)=@_;
|
109 |
|
|
# Return the function to be executed for the current tag name
|
110 |
|
|
# in the current context:
|
111 |
|
|
if (exists($self->{FUNCTIONDB}->{$self->{CONTEXT}}->{$tagname}))
|
112 |
|
|
{
|
113 |
|
|
return $self->{FUNCTIONDB}->{$self->{CONTEXT}}->{$tagname};
|
114 |
|
|
}
|
115 |
|
|
else
|
116 |
|
|
{
|
117 |
|
|
die "SCRAM Error: No function registered for tag name \"".$tagname."\" ","\n";
|
118 |
|
|
}
|
119 |
|
|
}
|
120 |
|
|
|
121 |
|
|
sub checkattributes()
|
122 |
|
|
{
|
123 |
|
|
my $self=shift;
|
124 |
|
|
my ($tagname,$attributes)=@_;
|
125 |
|
|
|
126 |
|
|
# If there's an entry in attributes DB for this element, check
|
127 |
|
|
# that each tag exists in %attributes:
|
128 |
|
|
if (exists($self->{ATTRIBUTEDB}->{$self->{CONTEXT}}->{$tagname}))
|
129 |
|
|
{
|
130 |
|
|
foreach my $param (@{$self->{ATTRIBUTEDB}->{$self->{CONTEXT}}->{$tagname}})
|
131 |
|
|
{
|
132 |
|
|
if ( ! exists($$attributes{$param}))
|
133 |
|
|
{
|
134 |
|
|
die "SCRAM Error: Incomplete Tag <$tagname> : $param required.","\n";
|
135 |
|
|
}
|
136 |
|
|
}
|
137 |
|
|
}
|
138 |
|
|
}
|
139 |
|
|
|
140 |
|
|
sub nested()
|
141 |
|
|
{
|
142 |
|
|
my $self=shift;
|
143 |
|
|
@_ ? push(@{$self->{nested}},@_)
|
144 |
|
|
: $self->{nested};
|
145 |
|
|
}
|
146 |
|
|
|
147 |
|
|
1;
|