line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package PPM::XML::ValidatingElement; |
2
|
2
|
|
|
2
|
|
742
|
use PPM::XML::Element; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
77
|
|
3
|
2
|
|
|
2
|
|
16
|
use vars qw( $VERSION @ISA ); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
1602
|
|
4
|
|
|
|
|
|
|
$VERSION = '0.01_01'; |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
############################################################################### |
7
|
|
|
|
|
|
|
# |
8
|
|
|
|
|
|
|
# PPM::XML::ValidatingElement |
9
|
|
|
|
|
|
|
# |
10
|
|
|
|
|
|
|
# Base class for validating elements. Allows for applying DTD type |
11
|
|
|
|
|
|
|
# restrictions to elements parsed using the XML::Parser module. |
12
|
|
|
|
|
|
|
# |
13
|
|
|
|
|
|
|
############################################################################### |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
############################################################################### |
16
|
|
|
|
|
|
|
# Define the validating element class. |
17
|
|
|
|
|
|
|
############################################################################### |
18
|
|
|
|
|
|
|
@ISA = qw( PPM::XML::Element ); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
############################################################################### |
21
|
|
|
|
|
|
|
# Recursively validate myself and all child elements with all four types of |
22
|
|
|
|
|
|
|
# validation. Returns non-zero on success and zero on any errors. |
23
|
|
|
|
|
|
|
############################################################################### |
24
|
|
|
|
|
|
|
sub rvalidate |
25
|
|
|
|
|
|
|
{ |
26
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
27
|
0
|
|
|
|
|
|
my $func = shift; |
28
|
0
|
|
|
|
|
|
my $success = 1; |
29
|
|
|
|
|
|
|
|
30
|
0
|
|
|
|
|
|
$success &= $self->validate_possible_attrs( $func ); |
31
|
0
|
|
|
|
|
|
$success &= $self->validate_required_attrs( $func ); |
32
|
0
|
|
|
|
|
|
$success &= $self->validate_possible_kids( $func ); |
33
|
0
|
|
|
|
|
|
$success &= $self->validate_required_kids( $func ); |
34
|
|
|
|
|
|
|
|
35
|
0
|
|
|
|
|
|
foreach (@{$self->{Kids}}) |
|
0
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
{ |
37
|
0
|
0
|
|
|
|
|
if ((ref $_) !~ /::Characters$/o) |
38
|
0
|
|
|
|
|
|
{ $success &= $_->rvalidate( $func ); } |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
0
|
|
|
|
|
|
return $success; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
############################################################################### |
45
|
|
|
|
|
|
|
# Validate the element with all four types of validation. Returns non-zero on |
46
|
|
|
|
|
|
|
# success any zero if any errors occurred. |
47
|
|
|
|
|
|
|
############################################################################### |
48
|
|
|
|
|
|
|
sub validate |
49
|
|
|
|
|
|
|
{ |
50
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
51
|
0
|
|
|
|
|
|
my $func = shift; |
52
|
0
|
|
|
|
|
|
my $success = 1; |
53
|
|
|
|
|
|
|
|
54
|
0
|
|
|
|
|
|
$success &= $self->validate_possible_attrs( $func ); |
55
|
0
|
|
|
|
|
|
$success &= $self->validate_required_attrs( $func ); |
56
|
0
|
|
|
|
|
|
$success &= $self->validate_possible_kids( $func ); |
57
|
0
|
|
|
|
|
|
$success &= $self->validate_required_kids( $func ); |
58
|
|
|
|
|
|
|
|
59
|
0
|
|
|
|
|
|
return $success; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
############################################################################### |
63
|
|
|
|
|
|
|
# Validate possible attributes. Returns non-zero on sucess, and zero if any |
64
|
|
|
|
|
|
|
# errors occurred. |
65
|
|
|
|
|
|
|
############################################################################### |
66
|
|
|
|
|
|
|
sub validate_possible_attrs |
67
|
|
|
|
|
|
|
{ |
68
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
69
|
0
|
|
|
|
|
|
my $func = shift; |
70
|
0
|
|
|
|
|
|
my $attr; |
71
|
0
|
|
|
|
|
|
my $type = ref $self; |
72
|
0
|
|
|
|
|
|
my $success = 1; |
73
|
|
|
|
|
|
|
|
74
|
0
|
|
|
|
|
|
my $elem = $type; |
75
|
0
|
|
|
|
|
|
$elem =~ s/.*:://; |
76
|
|
|
|
|
|
|
|
77
|
0
|
|
|
|
|
|
my @allattrs; |
78
|
0
|
|
|
|
|
|
push( @allattrs, @{"$type\::oattrs"}, @{"$type\::rattrs"} ); |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
# Check our list of attributes against the list of possible attributes we |
81
|
|
|
|
|
|
|
# can have. |
82
|
0
|
|
|
|
|
|
foreach $attr (keys %{$self}) |
|
0
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
{ |
84
|
0
|
0
|
0
|
|
|
|
if ( ($attr ne 'Kids') and ($attr ne 'Text') ) |
85
|
|
|
|
|
|
|
{ |
86
|
0
|
0
|
|
|
|
|
if (!grep( /^$attr$/, @allattrs )) |
87
|
|
|
|
|
|
|
{ |
88
|
0
|
|
|
|
|
|
&$func( "Element '$elem' doesn't allow the '$attr' attribute." ); |
89
|
0
|
|
|
|
|
|
$success = 0; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
0
|
|
|
|
|
|
return $success; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
############################################################################### |
98
|
|
|
|
|
|
|
# Validate required attributes. Returns non-zero on success and zero if any |
99
|
|
|
|
|
|
|
# errors occurred. |
100
|
|
|
|
|
|
|
############################################################################### |
101
|
|
|
|
|
|
|
sub validate_required_attrs |
102
|
|
|
|
|
|
|
{ |
103
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
104
|
0
|
|
|
|
|
|
my $func = shift; |
105
|
0
|
|
|
|
|
|
my $attr; |
106
|
0
|
|
|
|
|
|
my $type = ref $self; |
107
|
0
|
|
|
|
|
|
my $success = 1; |
108
|
|
|
|
|
|
|
|
109
|
0
|
|
|
|
|
|
my $elem = $type; |
110
|
0
|
|
|
|
|
|
$elem =~ s/.*:://; |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
# Check the list of required attributes against the list of attributes |
113
|
|
|
|
|
|
|
# which were parsed. |
114
|
0
|
|
|
|
|
|
foreach $attr (@{"$type\::rattrs"}) |
|
0
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
{ |
116
|
0
|
0
|
|
|
|
|
if (!grep( /^$attr$/, (keys %{$self}) )) |
|
0
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
{ |
118
|
0
|
|
|
|
|
|
&$func( "Element '$elem' must have a '$attr' attribute." ); |
119
|
0
|
|
|
|
|
|
$success = 0; |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
0
|
|
|
|
|
|
return $success; |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
############################################################################### |
127
|
|
|
|
|
|
|
# Validate possible child elements. Returns non-zero on success and zero if |
128
|
|
|
|
|
|
|
# any errors occurred. |
129
|
|
|
|
|
|
|
############################################################################### |
130
|
|
|
|
|
|
|
sub validate_possible_kids |
131
|
|
|
|
|
|
|
{ |
132
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
133
|
0
|
|
|
|
|
|
my $func = shift; |
134
|
0
|
|
|
|
|
|
my $kid; |
135
|
0
|
|
|
|
|
|
my $type = ref $self; |
136
|
0
|
|
|
|
|
|
my $success = 1; |
137
|
|
|
|
|
|
|
|
138
|
0
|
|
|
|
|
|
my $elem = $type; |
139
|
0
|
|
|
|
|
|
$elem =~ s/.*:://; |
140
|
|
|
|
|
|
|
|
141
|
0
|
|
|
|
|
|
my $base = $type; |
142
|
0
|
|
|
|
|
|
$base =~ s/::[^:]*?$//; |
143
|
|
|
|
|
|
|
|
144
|
0
|
|
|
|
|
|
my @allkids; |
145
|
0
|
|
|
|
|
|
push( @allkids, @{"$type\::okids"}, @{"$type\::rkids"} ); |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
|
147
|
0
|
|
|
|
|
|
foreach $kid (@{ $self->{Kids} }) |
|
0
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
{ |
149
|
0
|
|
|
|
|
|
my $kid_type = ref $kid; |
150
|
0
|
|
|
|
|
|
$kid_type =~ s/.*:://; |
151
|
0
|
0
|
|
|
|
|
next if ($kid_type eq 'Characters'); # Don't validate character data |
152
|
|
|
|
|
|
|
|
153
|
0
|
0
|
|
|
|
|
if (!grep( /^$kid_type$/, @allkids )) |
154
|
|
|
|
|
|
|
{ |
155
|
0
|
|
|
|
|
|
&$func( "Element '$elem' cannot contain a child element '$kid_type'" ); |
156
|
0
|
|
|
|
|
|
$success = 0; |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
|
160
|
0
|
|
|
|
|
|
return $success; |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
############################################################################### |
164
|
|
|
|
|
|
|
# Validate required child elements. Returns non-zero on success and zero if |
165
|
|
|
|
|
|
|
# any errors occurred. |
166
|
|
|
|
|
|
|
############################################################################### |
167
|
|
|
|
|
|
|
sub validate_required_kids |
168
|
|
|
|
|
|
|
{ |
169
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
170
|
0
|
|
|
|
|
|
my $func = shift; |
171
|
0
|
|
|
|
|
|
my $kid; |
172
|
0
|
|
|
|
|
|
my $type = ref $self; |
173
|
0
|
|
|
|
|
|
my $success = 1; |
174
|
|
|
|
|
|
|
|
175
|
0
|
|
|
|
|
|
my $elem = $type; |
176
|
0
|
|
|
|
|
|
$elem =~ s/.*:://; |
177
|
|
|
|
|
|
|
|
178
|
0
|
|
|
|
|
|
my $base = $type; |
179
|
0
|
|
|
|
|
|
$base =~ s/::[^:]*?$//; |
180
|
|
|
|
|
|
|
|
181
|
0
|
|
|
|
|
|
foreach $kid (@{"$type\::rkids"}) |
|
0
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
{ |
183
|
0
|
|
|
|
|
|
my @kidlist = map( ref, @{$self->{Kids}} ); |
|
0
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
|
185
|
0
|
0
|
|
|
|
|
if (!grep( /^$base\::$kid$/, @kidlist )) |
186
|
|
|
|
|
|
|
{ |
187
|
0
|
|
|
|
|
|
&$func( "Element '$elem' must contain a '$kid' element." ); |
188
|
0
|
|
|
|
|
|
$success = 0; |
189
|
|
|
|
|
|
|
} |
190
|
|
|
|
|
|
|
} |
191
|
|
|
|
|
|
|
|
192
|
0
|
|
|
|
|
|
return $success; |
193
|
|
|
|
|
|
|
} |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
__END__ |