line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package XML::Handler::ExtOn::Element; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
#$Id: Element.pm 357 2008-10-17 11:48:11Z zag $ |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=pod |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 NAME |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
XML::Handler::ExtOn::Element - Class for Element object. |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 SYNOPSYS |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
use XML::Handler::ExtOn; |
14
|
|
|
|
|
|
|
my $buf; |
15
|
|
|
|
|
|
|
my $wrt = XML::SAX::Writer->new( Output => \$buf ); |
16
|
|
|
|
|
|
|
my $ex_parser = new XML::Handler::ExtOn:: Handler => $wrt; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
... |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
#create Element |
21
|
|
|
|
|
|
|
my $elem = $ex_parser->mk_element("Root"); |
22
|
|
|
|
|
|
|
$elem->add_content( $elem->mk_element("tag1")); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
... |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
#delete tag from XML |
27
|
|
|
|
|
|
|
$elem->delete_element; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
... |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
#delete tag from XML and skip content |
32
|
|
|
|
|
|
|
$elem->delete_element->skip_content; |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
... |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
#set default namespace( scoped in element ) |
37
|
|
|
|
|
|
|
$elem->add_namespace(''=>"http://example.com/defaultns"); |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
... |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
#get attribites by prefix |
42
|
|
|
|
|
|
|
my $hash_ref = $elem->attrs_by_prefix('myprefix'); |
43
|
|
|
|
|
|
|
$hash_ref->{attr1} = 1; |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
$ex_parser->start_element($elem) |
46
|
|
|
|
|
|
|
$ex_parser->end_element; |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=head1 METHODS |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=cut |
51
|
|
|
|
|
|
|
|
52
|
6
|
|
|
6
|
|
2983
|
use strict; |
|
6
|
|
|
|
|
23
|
|
|
6
|
|
|
|
|
254
|
|
53
|
6
|
|
|
6
|
|
32
|
use warnings; |
|
6
|
|
|
|
|
10
|
|
|
6
|
|
|
|
|
280
|
|
54
|
|
|
|
|
|
|
|
55
|
6
|
|
|
6
|
|
34
|
use Carp; |
|
6
|
|
|
|
|
7
|
|
|
6
|
|
|
|
|
663
|
|
56
|
6
|
|
|
6
|
|
224
|
use Data::Dumper; |
|
6
|
|
|
|
|
12
|
|
|
6
|
|
|
|
|
351
|
|
57
|
6
|
|
|
6
|
|
2368
|
use XML::Handler::ExtOn::TieAttrs; |
|
6
|
|
|
|
|
14
|
|
|
6
|
|
|
|
|
249
|
|
58
|
6
|
|
|
6
|
|
3805
|
use XML::Handler::ExtOn::Attributes; |
|
6
|
|
|
|
|
18
|
|
|
6
|
|
|
|
|
188
|
|
59
|
6
|
|
|
6
|
|
60
|
use XML::Handler::ExtOn::Element; |
|
6
|
|
|
|
|
15
|
|
|
6
|
|
|
|
|
250
|
|
60
|
|
|
|
|
|
|
for my $key (qw/ _context attributes _skip_content _delete_element _stack /) { |
61
|
6
|
|
|
6
|
|
34
|
no strict 'refs'; |
|
6
|
|
|
|
|
11
|
|
|
6
|
|
|
|
|
7226
|
|
62
|
|
|
|
|
|
|
*{ __PACKAGE__ . "::$key" } = sub { |
63
|
20
|
|
|
20
|
|
32
|
my $self = shift; |
64
|
20
|
100
|
|
|
|
64
|
$self->{$key} = $_[0] if @_; |
65
|
20
|
|
|
|
|
95
|
return $self->{$key}; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
# new name=>< element name>, context=>< context >[, sax2=>[] ] |
70
|
|
|
|
|
|
|
# |
71
|
|
|
|
|
|
|
#Create Element object |
72
|
|
|
|
|
|
|
# |
73
|
|
|
|
|
|
|
# my $element = new XML::Handler::ExtOn::Element:: |
74
|
|
|
|
|
|
|
# name => "p", |
75
|
|
|
|
|
|
|
# context => $context, |
76
|
|
|
|
|
|
|
# [sax2 => $t1_elemnt ]; |
77
|
|
|
|
|
|
|
# |
78
|
|
|
|
|
|
|
# |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub new { |
81
|
3
|
|
|
3
|
0
|
30
|
my ( $class, %attr ) = @_; |
82
|
3
|
|
|
|
|
10
|
my $self = bless {}, $class; |
83
|
3
|
50
|
|
|
|
12
|
$self->_context( $attr{context} ) or die "not exists context parametr"; |
84
|
3
|
|
|
|
|
7
|
my $name = $attr{name}; |
85
|
3
|
50
|
|
|
|
10
|
$self->attributes( |
86
|
|
|
|
|
|
|
new XML::Handler::ExtOn::Attributes:: |
87
|
|
|
|
|
|
|
context => $self->_context, |
88
|
|
|
|
|
|
|
sax2 => exists $attr{sax2} ? $attr{sax2}->{Attributes} : {} |
89
|
|
|
|
|
|
|
); |
90
|
|
|
|
|
|
|
|
91
|
3
|
50
|
|
|
|
11
|
if ( my $sax2 = $attr{sax2} ) { |
92
|
3
|
|
66
|
|
|
18
|
$name ||= $sax2->{Name}; |
93
|
3
|
|
100
|
|
|
20
|
my $prefix = $sax2->{Prefix} || ''; |
94
|
3
|
|
|
|
|
12
|
$self->set_prefix( ); |
95
|
3
|
|
|
|
|
11
|
$self->set_ns_uri( $self->ns->get_uri( $prefix ) ); |
96
|
|
|
|
|
|
|
} |
97
|
3
|
|
|
|
|
15
|
$self->_stack([]); |
98
|
3
|
|
|
|
|
10
|
$self->_set_name($name); |
99
|
3
|
|
|
|
|
10
|
return $self; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub _set_name { |
103
|
3
|
|
|
3
|
|
5
|
my $self = shift; |
104
|
3
|
|
50
|
|
|
22
|
$self->{__name} = shift || return $self->{__name}; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=head2 add_content [, ...] |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
Add commands to contents stack.Return C<$self> |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
$elem->add_content( |
112
|
|
|
|
|
|
|
$self->mk_from_xml(""), |
113
|
|
|
|
|
|
|
$self->mk_cdata("TEST CDATA"), |
114
|
|
|
|
|
|
|
) |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=cut |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
sub add_content { |
119
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
120
|
0
|
|
|
|
|
0
|
push @{$self->_stack()}, @_; |
|
0
|
|
|
|
|
0
|
|
121
|
0
|
|
|
|
|
0
|
return $self |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=head2 mk_element |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
Create element object in namespace of element. |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=cut |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
sub mk_element { |
132
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
133
|
0
|
|
|
|
|
0
|
my $name = shift; |
134
|
0
|
|
|
|
|
0
|
my %args = @_; |
135
|
0
|
|
0
|
|
|
0
|
$args{context} ||= $self->ns->sub_context(); |
136
|
0
|
|
|
|
|
0
|
my $elem = new XML::Handler::ExtOn::Element:: |
137
|
|
|
|
|
|
|
name => $name, |
138
|
|
|
|
|
|
|
%args; |
139
|
0
|
|
|
|
|
0
|
return $elem; |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
sub set_prefix { |
143
|
3
|
|
|
3
|
0
|
6
|
my $self = shift; |
144
|
3
|
|
|
|
|
5
|
my $prefix = shift; |
145
|
3
|
50
|
|
|
|
14
|
if ( defined $prefix ) { |
146
|
0
|
|
|
|
|
0
|
$self->{__prefix} = $prefix; |
147
|
0
|
|
|
|
|
0
|
$self->set_ns_uri( $self->ns->get_uri($prefix) ); |
148
|
|
|
|
|
|
|
} |
149
|
3
|
|
|
|
|
6
|
$self->{__prefix}; |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
sub ns { |
153
|
3
|
|
|
3
|
0
|
9
|
return $_[0]->_context; |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=head2 add_namespace => , [ => , ... ] |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
Add Namespace mapping. return C<$self> |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
If C eq '', this namespace will then apply to all elements |
161
|
|
|
|
|
|
|
that have no prefix. |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
$elem->add_namespace( |
164
|
|
|
|
|
|
|
"myns" => 'http://example.com/myns', |
165
|
|
|
|
|
|
|
"myns_test", 'http://example.com/myns_test', |
166
|
|
|
|
|
|
|
''=>'http://example.com/new_default_namespace' |
167
|
|
|
|
|
|
|
); |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=cut |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
sub add_namespace { |
172
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
173
|
0
|
|
|
|
|
0
|
my ( $prefix, $ns_uri ) = @_; |
174
|
0
|
|
|
|
|
0
|
my $default1_uri = $self->ns->get_uri(''); |
175
|
0
|
|
|
|
|
0
|
$self->ns->declare_prefix(@_); |
176
|
0
|
|
|
|
|
0
|
my $default2_uri = $self->ns->get_uri(''); |
177
|
0
|
0
|
|
|
|
0
|
unless ( $default1_uri ne $default2_uri ) { |
178
|
0
|
0
|
|
|
|
0
|
$self->set_prefix('') unless $self->set_prefix; |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
$self |
181
|
0
|
|
|
|
|
0
|
} |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
sub set_ns_uri { |
184
|
3
|
|
|
3
|
0
|
117
|
my $self = shift; |
185
|
3
|
50
|
|
|
|
13
|
$self->{__ns_iri} = shift if @_; |
186
|
3
|
|
|
|
|
7
|
$self->{__ns_iri}; |
187
|
|
|
|
|
|
|
} |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
sub default_ns_uri { |
190
|
0
|
|
|
0
|
0
|
0
|
return $_[0]->ns->get_uri('') |
191
|
|
|
|
|
|
|
} |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=head2 default_uri |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
Return default I for Element scope. |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=cut |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
sub default_uri { |
200
|
0
|
|
|
0
|
1
|
0
|
$_[0]->ns->get_uri(''); |
201
|
|
|
|
|
|
|
} |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
sub name { |
204
|
0
|
|
|
0
|
1
|
0
|
return $_[0]->_set_name(); |
205
|
|
|
|
|
|
|
} |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=head2 local_name |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
Return localname of elemnt ( without prefix ) |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
=cut |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
sub local_name { |
214
|
0
|
|
|
0
|
1
|
0
|
return $_[0]->_set_name(); |
215
|
|
|
|
|
|
|
} |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
# to_sax2 |
218
|
|
|
|
|
|
|
# |
219
|
|
|
|
|
|
|
# Export elemnt as SAX2 struct |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
sub to_sax2 { |
222
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
223
|
0
|
0
|
|
|
|
0
|
my $res = { |
|
|
0
|
|
|
|
|
|
224
|
|
|
|
|
|
|
Prefix => $self->set_prefix, |
225
|
|
|
|
|
|
|
LocalName => $self->local_name, |
226
|
|
|
|
|
|
|
Attributes => $self->attributes->to_sax2, |
227
|
|
|
|
|
|
|
Name => $self->set_prefix |
228
|
|
|
|
|
|
|
? $self->set_prefix() . ":" . $self->local_name |
229
|
|
|
|
|
|
|
: $self->local_name, |
230
|
|
|
|
|
|
|
NamespaceURI => $self->set_prefix ? $self->set_ns_uri() : '', |
231
|
|
|
|
|
|
|
}; |
232
|
0
|
|
|
|
|
0
|
return $res; |
233
|
|
|
|
|
|
|
} |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
=head2 attrs_by_prefix |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
Return reference to hash of attributes for I. |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
=cut |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
sub attrs_by_prefix { |
242
|
1
|
|
|
1
|
1
|
5
|
my $self = shift; |
243
|
1
|
|
|
|
|
42
|
return $self->attributes->by_prefix(@_); |
244
|
|
|
|
|
|
|
} |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
=head2 attrs_by_prefix |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
Return reference to hash of attributes for I. |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
=cut |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
sub attrs_by_ns_uri { |
253
|
1
|
|
|
1
|
0
|
5
|
my $self = shift; |
254
|
1
|
|
|
|
|
4
|
return $self->attributes->by_ns_uri(@_); |
255
|
|
|
|
|
|
|
} |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
=head2 attrs_by_name |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
Return reference to hash of attributes by name. |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
=cut |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
sub attrs_by_name { |
264
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
265
|
0
|
|
|
|
|
|
return $self->attributes->by_name(@_); |
266
|
|
|
|
|
|
|
} |
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
=head2 skip_content |
269
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
Skip entry of element. Return $self |
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
=cut |
273
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
sub skip_content { |
275
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
276
|
0
|
0
|
|
|
|
|
return 1 if $self->is_skip_content; |
277
|
0
|
|
|
|
|
|
$self->is_skip_content(1); |
278
|
0
|
|
|
|
|
|
$self; |
279
|
|
|
|
|
|
|
} |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
=head2 is_skip_content |
282
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
Return 1 - if element marked to skip content |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
=cut |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
sub is_skip_content { |
288
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
289
|
0
|
0
|
|
|
|
|
$self->_skip_content(@_) || 0 |
290
|
|
|
|
|
|
|
} |
291
|
|
|
|
|
|
|
|
292
|
|
|
|
|
|
|
=head2 delete_element, delete |
293
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
Delete start and close element from stream. return C<$self> |
295
|
|
|
|
|
|
|
|
296
|
|
|
|
|
|
|
=cut |
297
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
sub delete { |
299
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
300
|
0
|
|
|
|
|
|
return $self->delete_element; |
301
|
|
|
|
|
|
|
} |
302
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
sub delete_element { |
304
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
305
|
0
|
0
|
|
|
|
|
return 1 if $self->is_delete_element; |
306
|
0
|
|
|
|
|
|
$self->is_delete_element(1); |
307
|
0
|
|
|
|
|
|
$self; |
308
|
|
|
|
|
|
|
} |
309
|
|
|
|
|
|
|
|
310
|
|
|
|
|
|
|
=head2 is_delete_element |
311
|
|
|
|
|
|
|
|
312
|
|
|
|
|
|
|
Return 1 - if element marked to delete |
313
|
|
|
|
|
|
|
|
314
|
|
|
|
|
|
|
=cut |
315
|
|
|
|
|
|
|
|
316
|
|
|
|
|
|
|
sub is_delete_element { |
317
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
318
|
0
|
0
|
|
|
|
|
$self->_delete_element(@_) || 0 |
319
|
|
|
|
|
|
|
} |
320
|
|
|
|
|
|
|
|
321
|
|
|
|
|
|
|
1; |
322
|
|
|
|
|
|
|
__END__ |