line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Simple::SAX::Serializer::Element; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
15
|
use warnings; |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
82
|
|
4
|
3
|
|
|
3
|
|
17
|
use strict; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
92
|
|
5
|
3
|
|
|
3
|
|
16
|
use vars qw($VERSION); |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
109
|
|
6
|
3
|
|
|
3
|
|
26
|
use Carp 'confess'; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
191
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
$VERSION = 0.03; |
9
|
|
|
|
|
|
|
|
10
|
3
|
|
|
3
|
|
17
|
use Abstract::Meta::Class ':all'; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
2750
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 NAME |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
Simple::SAX::Serializer::Element - XML node element. |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=head1 SYNOPSIS |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
my $xml = Simple::SAX::Serializer->new; |
19
|
|
|
|
|
|
|
$xml->handler('dataset', sub { |
20
|
|
|
|
|
|
|
my ($self, $element, $parent) = @_; |
21
|
|
|
|
|
|
|
my $attributes = $element->attributes; |
22
|
|
|
|
|
|
|
my $children_result = $element->children_result; |
23
|
|
|
|
|
|
|
{properties => $attributes, dataset => $children_result} |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
); |
26
|
|
|
|
|
|
|
$xml->handler('*', sub { |
27
|
|
|
|
|
|
|
my ($self, $element, $parent) = @_; |
28
|
|
|
|
|
|
|
my $attributes = $element->attributes; |
29
|
|
|
|
|
|
|
my $children_result = $parent->children_array_result; |
30
|
|
|
|
|
|
|
my $result = $parent->children_result; |
31
|
|
|
|
|
|
|
push @$children_result, $element->name => {%$attributes}; |
32
|
|
|
|
|
|
|
}); |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
=head1 DESCRIPTION |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
Represents xml node element. |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
=head2 EXPORT |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
None. |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=head2 ATTRIBUTES |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=over |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=item node |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
Stores reference to the xml node. |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=cut |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
has '$.node'; |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=back |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=head2 METHODS |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=over |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=item attributes |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
Return attributes as hash ref. |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=cut |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub attributes { |
68
|
64
|
|
|
64
|
1
|
161
|
my ($self) = @_; |
69
|
64
|
|
|
|
|
156
|
my $node = $self->node; |
70
|
64
|
|
|
|
|
526
|
$node->[1]; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=item name |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=cut |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub name { |
79
|
20
|
|
|
20
|
1
|
39
|
my ($self) = @_; |
80
|
20
|
|
|
|
|
45
|
my $node = $self->node; |
81
|
20
|
|
|
|
|
253
|
$node->[0]; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=item children_result |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
Returns children results. |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=cut |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub children_result { |
91
|
47
|
|
|
47
|
1
|
107
|
my ($self, $value) = @_; |
92
|
47
|
|
|
|
|
111
|
my $node = $self->node; |
93
|
47
|
50
|
|
|
|
343
|
$node->[-2] = $value if $value; |
94
|
47
|
|
|
|
|
173
|
$node->[-2]; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=item children_array_result |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
Returns children result as array ref |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=cut |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub children_array_result { |
105
|
20
|
|
|
20
|
1
|
51
|
my ($self, $value) = @_; |
106
|
20
|
|
|
|
|
46
|
my $node = $self->node; |
107
|
20
|
100
|
|
|
|
154
|
$node->[-2] = [] unless $node->[-2] ; |
108
|
20
|
50
|
|
|
|
45
|
$node->[-2] = $value if $value; |
109
|
20
|
|
|
|
|
89
|
$node->[-2]; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=item children_hash_result |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
Returns children result as hash ref |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=cut |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
sub children_hash_result { |
120
|
26
|
|
|
26
|
1
|
53
|
my ($self, $value) = @_; |
121
|
26
|
|
|
|
|
61
|
my $node = $self->node; |
122
|
26
|
100
|
|
|
|
221
|
$node->[-2] = {} unless $node->[-2] ; |
123
|
26
|
50
|
|
|
|
51
|
$node->[-2] = $value if $value; |
124
|
26
|
|
|
|
|
58
|
$node->[-2]; |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=item set_children_result |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
Sets children result |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=cut |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
sub set_children_result { |
135
|
0
|
|
|
0
|
1
|
0
|
my ($self, $value) = @_; |
136
|
0
|
|
|
|
|
0
|
my $node = $self->node; |
137
|
0
|
|
|
|
|
0
|
$node->[-2] = $value; |
138
|
0
|
|
|
|
|
0
|
$self; |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=item value |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
Return element's value. Takes optionally normalise spaces flag. |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=cut |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
sub value { |
149
|
9
|
|
|
9
|
1
|
14
|
my ($self, $normailise_spaces) = @_; |
150
|
9
|
|
|
|
|
23
|
my $node = $self->node; |
151
|
9
|
|
|
|
|
63
|
my $result = $node->[-1]; |
152
|
9
|
50
|
33
|
|
|
73
|
$result =~ s/^\s+|\s+$//sg if defined($result) && $normailise_spaces; |
153
|
9
|
|
|
|
|
40
|
$result; |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=item validate_attributes |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
Validates element attributes takes, required attributes parameter as array ref, |
160
|
|
|
|
|
|
|
optional attributes parameter as hash ref |
161
|
|
|
|
|
|
|
$element->validate_attributes(['name'], {type => 'text'}); |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=cut |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
sub validate_attributes { |
166
|
13
|
|
|
13
|
1
|
43
|
my ($self, $required, $optional) = @_; |
167
|
13
|
|
50
|
|
|
34
|
$required ||= []; |
168
|
13
|
|
100
|
|
|
37
|
$optional ||= {}; |
169
|
13
|
|
|
|
|
26
|
my %attributes = map { $_ => 1 } @$required; |
|
15
|
|
|
|
|
44
|
|
170
|
13
|
|
|
|
|
33
|
my $attributes = $self->attributes; |
171
|
13
|
|
|
|
|
30
|
for (@$required) { |
172
|
15
|
50
|
|
|
|
51
|
confess "attribute $_ is required" |
173
|
|
|
|
|
|
|
unless exists $attributes->{$_}; |
174
|
|
|
|
|
|
|
} |
175
|
|
|
|
|
|
|
|
176
|
13
|
|
|
|
|
40
|
for my $k (keys %$optional) { |
177
|
8
|
50
|
|
|
|
36
|
$attributes->{$k} = $optional->{$k} |
178
|
|
|
|
|
|
|
unless exists $attributes->{$k}; |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
|
181
|
13
|
|
|
|
|
33
|
for my $k(keys %$attributes) { |
182
|
25
|
100
|
|
|
|
66
|
next if $k =~ /^_/; |
183
|
23
|
50
|
66
|
|
|
125
|
confess "unknown attributes $k on tag " . $self->name |
184
|
|
|
|
|
|
|
if (! exists($attributes{$k}) && ! exists($optional->{$k})); |
185
|
|
|
|
|
|
|
} |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
} |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
1; |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
__END__ |