File Coverage

blib/lib/Treex/PML/Schema/XMLNode.pm
Criterion Covered Total %
statement 115 124 92.7
branch 37 62 59.6
condition 22 46 47.8
subroutine 17 17 100.0
pod 2 7 28.5
total 193 256 75.3


line stmt bran cond sub pod time code
1             package Treex::PML::Schema::XMLNode;
2              
3 9     9   59 use strict;
  9         12  
  9         256  
4 9     9   31 use warnings;
  9         11  
  9         424  
5              
6 9     9   36 use vars qw($VERSION);
  9         27  
  9         457  
7             BEGIN {
8 9     9   173 $VERSION='2.29'; # version template
9             }
10 9     9   32 no warnings 'uninitialized';
  9         12  
  9         392  
11 9     9   35 use Carp;
  9         13  
  9         551  
12 9     9   37 use Scalar::Util qw(weaken isweak);
  9         23  
  9         388  
13              
14 9     9   33 use UNIVERSAL::DOES;
  9         12  
  9         13298  
15              
16             sub copy_decl {
17 82     82 1 150 my ($self,$t)=@_;
18 82         92 my $copy;
19 82 100       187 if (ref $t->{-schema}) {
20 63         300 $copy = Treex::PML::CloneValue($t,[$t->{-parent},$t->{-schema}], [$self,$self->{-schema}]);
21             } else {
22 19         85 $copy = Treex::PML::CloneValue($t,[$t->{-parent}], [$self]);
23             }
24 82 50       269 if (exists $self->{'-##'}) {
25 82         157 $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   585 my ($val,$is_hash) = @_;
32 370 100 66     1160 weaken($val->{-parent}) if ref($val->{-parent}) and not isweak($val->{-parent});
33 370 100 66     1144 weaken($val->{-schema}) if ref($val->{-schema}) and not isweak($val->{-schema});
34             },
35             {
36 82         851 $self->{-schema}=>1, $self=> 1 # do not recurse into these
37             },
38             1, # only hashes
39             );
40 82         820 return $copy;
41             }
42              
43             sub serialize_attributes {
44 270     270 0 412 my ($self,$opts)=@_;
45 270   50     643 my $attributes = $self->{-attributes}||[];
46 270         334 my @ret;
47 270         461 for my $attr (@$attributes) {
48 366 100       640 next if $attr=~/^xmlns/;
49 355         591 my $value = $self->{$attr};
50 355 100 66     834 if (!defined($value) and $attr eq 'name') { # FIXME: THIS IS A HACK
51 121         269 $value = $self->{'-'.$attr};
52             }
53 355 50       587 if (defined $value) {
54 355         820 push @ret, $attr, $value;
55             }
56             }
57 270         574 return \@ret;
58             }
59              
60       253 0   sub serialize_exclude_keys {}
61             sub serialize_get_children {
62 264     264 0 395 my ($self,$opts)=@_;
63 264         299 my %exclude;
64             @exclude{
65 264 50       330 @{$self->{-attributes}||[]},
  264         762  
66             $self->serialize_exclude_keys($opts)
67             }=();
68             my @children = map {
69 192         510 my $name = $_;
70 192         304 my $val = $self->{$_};
71 121         782 (ref($val) eq 'HASH') ? ( map { [$name,$_] } grep { UNIVERSAL::DOES::does($_,'Treex::PML::Schema::XMLNode') } values(%{$val})) :
  121         858  
  51         119  
72 192 50 33     674 (ref($val) eq 'ARRAY') ? ( map { [$name,$_] } grep { UNIVERSAL::DOES::does($_,'Treex::PML::Schema::XMLNode') } @{$val}) :
  15 100       111  
  15 100       71  
  8         15  
73             (UNIVERSAL::DOES::does($val,'Treex::PML::Schema::XMLNode') or !ref($val)) ? [$name,$val] : ()
74 264   100     1046 } grep {!/^[-@]/ and !exists($exclude{$_})} keys %$self;
  2400         5235  
75             return (
76 269         514 (grep { !ref($_->[1]) } @children),
77 264         2429 sort { $a->[1]{'-#'} <=> $b->[1]{'-#'} } grep { ref($_->[1]) } @children
  202         395  
  269         652  
78             )
79             }
80             sub serialize_children {
81 170     170 0 324 my ($self,$opts,$children)=@_;
82 170   33     524 my $writer = $opts->{writer} || croak __PACKAGE__."->serialize: missing required option 'writer'!\n";
83 170         2574 my $ns = $opts->{DefaultNs};
84 170   50     329 $children ||= [$self->serialize_get_children($opts)];
85 170         358 for my $child (@$children) {
86 317         10730 my ($key,$value) = @$child;
87 317 100       751 if (UNIVERSAL::DOES::does($value,'Treex::PML::Schema::XMLNode')) {
88 269         3906 $value->serialize($opts);
89             } else {
90 48         1033 my $tag = [$ns,$key];
91 48 50       150 $writer->startTag($tag) if defined $key;
92 48         5118 $writer->characters($value);
93 48 50       1127 $writer->endTag($tag) if defined $key;
94             }
95             }
96             }
97             sub serialize {
98 280     280 0 448 my ($self,$opts)=@_;
99 280   33     754 my $writer = $opts->{writer} || croak __PACKAGE__."->serialize: missing required option 'writer'!\n";
100 280         9515 my $xml_name = $self->{-xml_name};
101 280 100 33     2039 if ($xml_name =~/^#/) {
    50          
102 10 50       41 if ($xml_name =~/^#text/) {
    50          
    0          
    0          
103 0         0 $writer->characters($self->{-value});
104             } elsif ($xml_name =~/^#comment/) {
105 10         21 my $value = $self->{-value};
106 10         123 $value=~s/^ | $//g; # remove a leading and trailing space - XML::Writer addes them
107 10         77 $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         893 my ($ns,$name)=($1,$2);
117 270   50     747 my $attrs = $self->serialize_attributes($opts) || [];
118 270   50     750 my $prefix = $self->{-xml_prefix} || '';
119 270   33     1007 $ns ||= $opts->{DefaultNs};
120 270 50       560 if (($ns ne $opts->{DefaultNs})) {
121 0         0 $writer->addPrefix($ns => $prefix);
122             }
123 270         766 $writer->addPrefix($ns => $prefix);
124             {
125 270         2746 my @children = $self->serialize_get_children($opts);
  270         597  
126 270 100       510 if (@children) {
127 170         684 $writer->startTag([$ns,$name], @$attrs);
128 170         31237 $self->serialize_children($opts,\@children);
129 170         18274 $writer->endTag([$ns,$name]);
130             } else {
131 100         329 $writer->emptyTag([$ns,$name], @$attrs);
132             }
133             }
134             }
135             }
136              
137             sub write {
138 11     11 1 8504 my ($self,$opts)=@_;
139 11         24 my $fh;
140             my $have_backup;
141 11         34 my $filename = $opts->{filename};
142 11 50 33     150 if (!defined($opts->{fh}) and
      33        
143             !defined($opts->{string}) and
144             defined($filename)) {
145 11 50       41 unless ($opts->{no_backups}) {
146 11 50       20 eval { Treex::PML::IO::rename_uri($filename,$filename."~"); $have_backup=1; } || carp($@);
  11         66  
  11         3481  
147             }
148 11   50     49 $fh = Treex::PML::IO::open_backend($filename,'w')
149             || die "Cannot open $filename for writing: $!";
150 11         38 binmode $fh;
151             }
152 11         62 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     373 DATA_INDENT => $opts->{no_indent} ? 0 : 1,
    50          
157             NAMESPACES => 1,
158             PREFIX_MAP => {
159             (Treex::PML::Schema->PML_SCHEMA_NS) => '',
160             });
161 11         3751 $self->serialize({
162             writer => $writer,
163             DefaultNs => Treex::PML::Schema->PML_SCHEMA_NS,
164             });
165 11         680 $writer->end();
166             };
167 11 50       2420 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       79 Treex::PML::IO::close_backend($fh) if $fh;
174             }
175              
176             sub DESTROY {
177 2869     2869   87633 my ($self)=@_;
178 2869         9572 %$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__