line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Treex::PML::Schema::XMLNode; |
2
|
|
|
|
|
|
|
|
3
|
6
|
|
|
6
|
|
37
|
use strict; |
|
6
|
|
|
|
|
11
|
|
|
6
|
|
|
|
|
154
|
|
4
|
6
|
|
|
6
|
|
26
|
use warnings; |
|
6
|
|
|
|
|
10
|
|
|
6
|
|
|
|
|
143
|
|
5
|
|
|
|
|
|
|
|
6
|
6
|
|
|
6
|
|
26
|
use vars qw($VERSION); |
|
6
|
|
|
|
|
9
|
|
|
6
|
|
|
|
|
220
|
|
7
|
|
|
|
|
|
|
BEGIN { |
8
|
6
|
|
|
6
|
|
88
|
$VERSION='2.24'; # version template |
9
|
|
|
|
|
|
|
} |
10
|
6
|
|
|
6
|
|
27
|
no warnings 'uninitialized'; |
|
6
|
|
|
|
|
11
|
|
|
6
|
|
|
|
|
143
|
|
11
|
6
|
|
|
6
|
|
25
|
use Carp; |
|
6
|
|
|
|
|
10
|
|
|
6
|
|
|
|
|
296
|
|
12
|
6
|
|
|
6
|
|
32
|
use Scalar::Util qw(weaken isweak); |
|
6
|
|
|
|
|
12
|
|
|
6
|
|
|
|
|
267
|
|
13
|
|
|
|
|
|
|
|
14
|
6
|
|
|
6
|
|
30
|
use UNIVERSAL::DOES; |
|
6
|
|
|
|
|
10
|
|
|
6
|
|
|
|
|
9289
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub copy_decl { |
17
|
82
|
|
|
82
|
1
|
172
|
my ($self,$t)=@_; |
18
|
82
|
|
|
|
|
145
|
my $copy; |
19
|
82
|
100
|
|
|
|
207
|
if (ref $t->{-schema}) { |
20
|
63
|
|
|
|
|
259
|
$copy = Treex::PML::CloneValue($t,[$t->{-parent},$t->{-schema}], [$self,$self->{-schema}]); |
21
|
|
|
|
|
|
|
} else { |
22
|
19
|
|
|
|
|
72
|
$copy = Treex::PML::CloneValue($t,[$t->{-parent}], [$self]); |
23
|
|
|
|
|
|
|
} |
24
|
82
|
50
|
|
|
|
280
|
if (exists $self->{'-##'}) { |
25
|
82
|
|
|
|
|
166
|
$copy->{'-#'}=$self->{'-##'}++; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
# we must do this here, otherwise any operation |
28
|
|
|
|
|
|
|
# that rewrites this value will create an unaccessible crircular reference |
29
|
|
|
|
|
|
|
Treex::PML::Schema::_traverse_data( |
30
|
|
|
|
|
|
|
$copy => sub { |
31
|
370
|
|
|
370
|
|
569
|
my ($val,$is_hash) = @_; |
32
|
370
|
100
|
66
|
|
|
1852
|
weaken($val->{-parent}) if ref($val->{-parent}) and not isweak($val->{-parent}); |
33
|
370
|
100
|
66
|
|
|
1398
|
weaken($val->{-schema}) if ref($val->{-schema}) and not isweak($val->{-schema}); |
34
|
|
|
|
|
|
|
}, |
35
|
|
|
|
|
|
|
{ |
36
|
82
|
|
|
|
|
838
|
$self->{-schema}=>1, $self=> 1 # do not recurse into these |
37
|
|
|
|
|
|
|
}, |
38
|
|
|
|
|
|
|
1, # only hashes |
39
|
|
|
|
|
|
|
); |
40
|
82
|
|
|
|
|
751
|
return $copy; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub serialize_attributes { |
44
|
270
|
|
|
270
|
0
|
518
|
my ($self,$opts)=@_; |
45
|
270
|
|
50
|
|
|
630
|
my $attributes = $self->{-attributes}||[]; |
46
|
270
|
|
|
|
|
361
|
my @ret; |
47
|
270
|
|
|
|
|
509
|
for my $attr (@$attributes) { |
48
|
366
|
100
|
|
|
|
734
|
next if $attr=~/^xmlns/; |
49
|
355
|
|
|
|
|
748
|
my $value = $self->{$attr}; |
50
|
355
|
100
|
66
|
|
|
899
|
if (!defined($value) and $attr eq 'name') { # FIXME: THIS IS A HACK |
51
|
121
|
|
|
|
|
316
|
$value = $self->{'-'.$attr}; |
52
|
|
|
|
|
|
|
} |
53
|
355
|
50
|
|
|
|
621
|
if (defined $value) { |
54
|
355
|
|
|
|
|
890
|
push @ret, $attr, $value; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
} |
57
|
270
|
|
|
|
|
745
|
return \@ret; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
253
|
0
|
|
sub serialize_exclude_keys {} |
61
|
|
|
|
|
|
|
sub serialize_get_children { |
62
|
264
|
|
|
264
|
0
|
465
|
my ($self,$opts)=@_; |
63
|
264
|
|
|
|
|
420
|
my %exclude; |
64
|
|
|
|
|
|
|
@exclude{ |
65
|
264
|
50
|
|
|
|
355
|
@{$self->{-attributes}||[]}, |
|
264
|
|
|
|
|
797
|
|
66
|
|
|
|
|
|
|
$self->serialize_exclude_keys($opts) |
67
|
|
|
|
|
|
|
}=(); |
68
|
|
|
|
|
|
|
my @children = map { |
69
|
192
|
|
|
|
|
472
|
my $name = $_; |
70
|
192
|
|
|
|
|
329
|
my $val = $self->{$_}; |
71
|
121
|
|
|
|
|
913
|
(ref($val) eq 'HASH') ? ( map { [$name,$_] } grep { UNIVERSAL::DOES::does($_,'Treex::PML::Schema::XMLNode') } values(%{$val})) : |
|
121
|
|
|
|
|
1096
|
|
|
51
|
|
|
|
|
130
|
|
72
|
192
|
50
|
33
|
|
|
697
|
(ref($val) eq 'ARRAY') ? ( map { [$name,$_] } grep { UNIVERSAL::DOES::does($_,'Treex::PML::Schema::XMLNode') } @{$val}) : |
|
15
|
100
|
|
|
|
132
|
|
|
15
|
100
|
|
|
|
99
|
|
|
8
|
|
|
|
|
23
|
|
73
|
|
|
|
|
|
|
(UNIVERSAL::DOES::does($val,'Treex::PML::Schema::XMLNode') or !ref($val)) ? [$name,$val] : () |
74
|
264
|
|
100
|
|
|
1130
|
} grep {!/^[-@]/ and !exists($exclude{$_})} keys %$self; |
|
2400
|
|
|
|
|
6846
|
|
75
|
|
|
|
|
|
|
return ( |
76
|
269
|
|
|
|
|
680
|
(grep { !ref($_->[1]) } @children), |
77
|
264
|
|
|
|
|
2701
|
sort { $a->[1]{'-#'} <=> $b->[1]{'-#'} } grep { ref($_->[1]) } @children |
|
197
|
|
|
|
|
435
|
|
|
269
|
|
|
|
|
771
|
|
78
|
|
|
|
|
|
|
) |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
sub serialize_children { |
81
|
170
|
|
|
170
|
0
|
352
|
my ($self,$opts,$children)=@_; |
82
|
170
|
|
33
|
|
|
521
|
my $writer = $opts->{writer} || croak __PACKAGE__."->serialize: missing required option 'writer'!\n"; |
83
|
170
|
|
|
|
|
3028
|
my $ns = $opts->{DefaultNs}; |
84
|
170
|
|
50
|
|
|
345
|
$children ||= [$self->serialize_get_children($opts)]; |
85
|
170
|
|
|
|
|
363
|
for my $child (@$children) { |
86
|
317
|
|
|
|
|
12592
|
my ($key,$value) = @$child; |
87
|
317
|
100
|
|
|
|
780
|
if (UNIVERSAL::DOES::does($value,'Treex::PML::Schema::XMLNode')) { |
88
|
269
|
|
|
|
|
4268
|
$value->serialize($opts); |
89
|
|
|
|
|
|
|
} else { |
90
|
48
|
|
|
|
|
1201
|
my $tag = [$ns,$key]; |
91
|
48
|
50
|
|
|
|
183
|
$writer->startTag($tag) if defined $key; |
92
|
48
|
|
|
|
|
6115
|
$writer->characters($value); |
93
|
48
|
50
|
|
|
|
1411
|
$writer->endTag($tag) if defined $key; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
sub serialize { |
98
|
280
|
|
|
280
|
0
|
544
|
my ($self,$opts)=@_; |
99
|
280
|
|
33
|
|
|
773
|
my $writer = $opts->{writer} || croak __PACKAGE__."->serialize: missing required option 'writer'!\n"; |
100
|
280
|
|
|
|
|
4607
|
my $xml_name = $self->{-xml_name}; |
101
|
280
|
100
|
33
|
|
|
2142
|
if ($xml_name =~/^#/) { |
|
|
50
|
|
|
|
|
|
102
|
10
|
50
|
|
|
|
44
|
if ($xml_name =~/^#text/) { |
|
|
50
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
103
|
0
|
|
|
|
|
0
|
$writer->characters($self->{-value}); |
104
|
|
|
|
|
|
|
} elsif ($xml_name =~/^#comment/) { |
105
|
10
|
|
|
|
|
25
|
my $value = $self->{-value}; |
106
|
10
|
|
|
|
|
109
|
$value=~s/^ | $//g; # remove a leading and trailing space - XML::Writer addes them |
107
|
10
|
|
|
|
|
43
|
$writer->comment($value); |
108
|
|
|
|
|
|
|
} elsif ($xml_name =~/^#processing-instruction/) { |
109
|
0
|
|
|
|
|
0
|
$writer->pi($self->{-name}, $self->{-value}); |
110
|
|
|
|
|
|
|
} elsif ($xml_name =~/^#other/) { |
111
|
0
|
|
|
|
|
0
|
$writer->raw($self->{-xml}); |
112
|
|
|
|
|
|
|
} else { |
113
|
|
|
|
|
|
|
# ignoring |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
} elsif ($xml_name=~/^{(.*)}(.*)$/ or $xml_name=~/^()([^#].*)$/) { |
116
|
270
|
|
|
|
|
939
|
my ($ns,$name)=($1,$2); |
117
|
270
|
|
50
|
|
|
718
|
my $attrs = $self->serialize_attributes($opts) || []; |
118
|
270
|
|
50
|
|
|
841
|
my $prefix = $self->{-xml_prefix} || ''; |
119
|
270
|
|
33
|
|
|
1039
|
$ns ||= $opts->{DefaultNs}; |
120
|
270
|
50
|
|
|
|
552
|
if (($ns ne $opts->{DefaultNs})) { |
121
|
0
|
|
|
|
|
0
|
$writer->addPrefix($ns => $prefix); |
122
|
|
|
|
|
|
|
} |
123
|
270
|
|
|
|
|
836
|
$writer->addPrefix($ns => $prefix); |
124
|
|
|
|
|
|
|
{ |
125
|
270
|
|
|
|
|
3417
|
my @children = $self->serialize_get_children($opts); |
|
270
|
|
|
|
|
706
|
|
126
|
270
|
100
|
|
|
|
606
|
if (@children) { |
127
|
170
|
|
|
|
|
708
|
$writer->startTag([$ns,$name], @$attrs); |
128
|
170
|
|
|
|
|
31383
|
$self->serialize_children($opts,\@children); |
129
|
170
|
|
|
|
|
19210
|
$writer->endTag([$ns,$name]); |
130
|
|
|
|
|
|
|
} else { |
131
|
100
|
|
|
|
|
392
|
$writer->emptyTag([$ns,$name], @$attrs); |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
sub write { |
138
|
11
|
|
|
11
|
1
|
8945
|
my ($self,$opts)=@_; |
139
|
11
|
|
|
|
|
31
|
my $fh; |
140
|
|
|
|
|
|
|
my $have_backup; |
141
|
11
|
|
|
|
|
38
|
my $filename = $opts->{filename}; |
142
|
11
|
50
|
33
|
|
|
120
|
if (!defined($opts->{fh}) and |
|
|
|
33
|
|
|
|
|
143
|
|
|
|
|
|
|
!defined($opts->{string}) and |
144
|
|
|
|
|
|
|
defined($filename)) { |
145
|
11
|
50
|
|
|
|
43
|
unless ($opts->{no_backups}) { |
146
|
11
|
50
|
|
|
|
30
|
eval { Treex::PML::IO::rename_uri($filename,$filename."~"); $have_backup=1; } || carp($@); |
|
11
|
|
|
|
|
73
|
|
|
11
|
|
|
|
|
3398
|
|
147
|
|
|
|
|
|
|
} |
148
|
11
|
|
50
|
|
|
55
|
$fh = Treex::PML::IO::open_backend($filename,'w') |
149
|
|
|
|
|
|
|
|| die "Cannot open $filename for writing: $!"; |
150
|
11
|
|
|
|
|
55
|
binmode $fh; |
151
|
|
|
|
|
|
|
} |
152
|
11
|
|
|
|
|
31
|
eval { |
153
|
|
|
|
|
|
|
my $writer = XML::Writer->new( |
154
|
|
|
|
|
|
|
OUTPUT => ($opts->{fh} || $opts->{string} || $fh ), |
155
|
|
|
|
|
|
|
DATA_MODE => $opts->{no_indent} ? 0 : 1, |
156
|
11
|
50
|
33
|
|
|
335
|
DATA_INDENT => $opts->{no_indent} ? 0 : 1, |
|
|
50
|
|
|
|
|
|
157
|
|
|
|
|
|
|
NAMESPACES => 1, |
158
|
|
|
|
|
|
|
PREFIX_MAP => { |
159
|
|
|
|
|
|
|
(Treex::PML::Schema->PML_SCHEMA_NS) => '', |
160
|
|
|
|
|
|
|
}); |
161
|
11
|
|
|
|
|
4461
|
$self->serialize({ |
162
|
|
|
|
|
|
|
writer => $writer, |
163
|
|
|
|
|
|
|
DefaultNs => Treex::PML::Schema->PML_SCHEMA_NS, |
164
|
|
|
|
|
|
|
}); |
165
|
11
|
|
|
|
|
656
|
$writer->end(); |
166
|
|
|
|
|
|
|
}; |
167
|
11
|
50
|
|
|
|
2106
|
if ($@) { |
168
|
0
|
|
|
|
|
0
|
my $err=$@; |
169
|
0
|
0
|
|
|
|
0
|
$have_backup && eval { Treex::PML::IO::rename_uri($filename."~",$filename) }; |
|
0
|
|
|
|
|
0
|
|
170
|
0
|
0
|
|
|
|
0
|
$err.=$@ if $@; |
171
|
0
|
|
|
|
|
0
|
carp("Error while saving schema: $err\n"); |
172
|
|
|
|
|
|
|
} |
173
|
11
|
50
|
|
|
|
76
|
Treex::PML::IO::close_backend($fh) if $fh; |
174
|
|
|
|
|
|
|
} |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
sub DESTROY { |
177
|
2811
|
|
|
2811
|
|
97612
|
my ($self)=@_; |
178
|
2811
|
|
|
|
|
9597
|
%$self=(); # this should not be needed, but |
179
|
|
|
|
|
|
|
# without it, perl 5.10 leaks on weakened |
180
|
|
|
|
|
|
|
# structures, try: |
181
|
|
|
|
|
|
|
# Scalar::Util::weaken({}) while 1 |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
} |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
1; |
187
|
|
|
|
|
|
|
__END__ |