PML/Backend/TrXML.pm

filter: Statementstmt 17.1% 30 / 175 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 36.6% 11 / 30 Podpod 0.0% 0 / 6 totaltotal 13.9% 41 / 293 CC 39 SCAR 43.2
CC
39
Cov
12%
write
210.0
start_element
156.0
test
20.0
CRAP 75.3
« PML/IO.pm ↑ Summary PML/Schema/Alt.pm »
1 ## This is a simple XML backend for TEI files -*-cperl-*-
2 ## author: Petr Pajas
3 # $Id: TrXML.pm 3025 2007-04-23 13:55:04Z pajas $ '
4 #############################################################
5  
6 package Treex::PML::Backend::TrXML;
7 1 ▶use Treex::PML;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use XML::LibXML::SAX;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Treex::PML::IO qw(close_backend);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 $VERSION='2.29'; # version template
16 }
17  
18 0 ▶sub test {
Subroutine test: not called CC=4 SCAR=30.0
Pod undocumented
19 0 my ($f)=@_;
20 0 ▶ if (ref($f)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $f) { } 0 0 0
21 0 ▶ return ($f->getline()=~/\s*\<\?xml / &&
Condition0%0/3
$f->getline =~ /\s*\<\?xml / && $f->getline =~ /\<!DOCTYPE trees[ >]|\<trees\s*>/i
!ll&&!rl&&r
000
MC/DC0%0/2
$f->getline =~ /\s*\<\?xml / && $f->getline =~ /\<!DOCTYPE trees[ >]|\<trees\s*>/i
$f->getline =~ /\s*\<\?xml / $f->getline =~ /\<!DOCTYPE trees[ >]|\<trees\s*>/i
Truth table0%0/3
A && B
A= $f->getline =~ /\s*\<\?xml /B= $f->getline =~ /\<!DOCTYPE trees[ >]|\<trees\s*>/i
ABresult
0X0
100
111
22 $f->getline()=~/\<!DOCTYPE trees[ >]|\<trees\s*>/i);
23 } else {
24 0 my $fh = Treex::PML::IO::open_backend($f,"r");
25 0 ▶ my $test = $fh && test($fh);
Condition0%0/3
$fh && test($fh)
!ll&&!rl&&r
000
MC/DC0%0/2
$fh && test($fh)
$fh test($fh)
Truth table0%0/3
A && B
A= $fhB= test($fh)
ABresult
0X0
100
111
26 0 Treex::PML::IO::close_backend($fh);
27 0 return $test;
28 }
29 }
30  
31 0 ▶sub open_backend {
Subroutine open_backend: not called CC=2 SCAR=17.9
Pod undocumented
32 0 my ($uri,$rw,$encoding)=@_;
33 # discard encoding and pass the rest to the Treex::PML::IO
34 0 ▶ Treex::PML::IO::open_backend($uri,$rw,($rw eq 'w' ? $encoding : undef));
Branch: 1 branch
BranchTrueFalseTotal
$rw eq 'w' ? : 0 0 0
35 }
36  
37  
38 0 ▶sub read {
Subroutine read: not called CC=2 SCAR=17.9
Pod undocumented
39 0 my ($input,$target_doc) = @_;
40 #my $handler = XML::SAX::Writer->new();
41
42 0 my $handler = Treex::PML::Backend::TrXML::SAXHandler->new(TargetDocument => $target_doc);
43 0 my $p = XML::LibXML::SAX->new(Handler => $handler);
44 0 ▶ if (ref($input)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $input) { } 0 0 0
45 0 $p->parse(Source => { ByteStream => $input });
46 } else {
47 0 $p->parse_uri($input);
48 }
49  
50 0 return 1;
51 }
52  
53 0 ▶sub xml_quote {
Subroutine xml_quote: not called CC=1 SCAR=6.9
Pod undocumented
54 0 local $_=$_[0];
55 0 s/&/&amp;/g;
56 0 s/'/&apos;/g;
57 0 s/"/&quot;/g;
58 0 s/>/&gt;/g;
59 0 s/</&lt;/g;
60 0 return $_;
61 }
62  
63 0 ▶sub xml_quote_pcdata {
Subroutine xml_quote_pcdata: not called CC=1 SCAR=6.9
Pod undocumented
64 0 local $_=$_[0];
65 0 s/&/&amp;/g;
66 0 s/>/&gt;/g;
67 0 s/</&lt;/g;
68 0 return $_;
69 }
70  
71 0 ▶sub write {
Subroutine write: not called CC=14 SCAR=53.5
Pod undocumented
72 0 my ($output, $src_doc) = @_;
73  
74 0 ▶ die "Require GLOB reference\n" unless ref($output);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $output 0 0 0
75  
76 # xml_decl
77 0 print $output "<?xml";
78 0 ▶ if ($src_doc->metaData('xmldecl_version') ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($src_doc->metaData('xmldecl_version') ne '') { } 0 0 0
79 0 print $output " version=\"".$src_doc->metaData('xmldecl_version')."\"";
80 } else {
81 0 print $output " version=\"1.0\"";
82 }
83 0 ▶ if ($src_doc->encoding() ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($src_doc->encoding ne "") 0 0 0
84 0 print $output " encoding=\"".$src_doc->encoding()."\"";
85 }
86 0 ▶ if ($src_doc->metaData('xmldecl_standalone') ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($src_doc->metaData("xmldecl_standalone") ne "") 0 0 0
87 0 print $output " standalone=\"".$src_doc->metaData('xmldecl_standalone')."\"";
88 }
89 0 print $output "?>\n";
90  
91 print $output ("<!DOCTYPE trees PUBLIC \"-//CKL.MFF.UK//DTD TrXML V1.0//EN\"".
92 " \"http://ufal.mff.cuni.cz/~pajas/tred.dtd\" [\n".
93 "<!ENTITY % trxml.attributes \"".
94 0 join("\n",map { " $_ CDATA #IMPLIED" }
95 0 grep { !/^(?:ORD|HIDE|ID)$/ } $src_doc->FS->attributes).
96 "\">\n]>\n");
97 0 print $output "<!-- Time-stamp: <".localtime()." Treex::PML::Backend::TrXML> -->\n";
98 0 print $output "<trees>\n";
99  
100 0 my @meta=grep { !/^xmldecl_/ } $src_doc->listMetaData();
101 0 ▶ if (@meta) {
Branch: 1 branch
BranchTrueFalseTotal
if (@meta) 0 0 0
102 0 print $output "<info>\n";
103 0 foreach (@meta) {
104 0 print $output " <meta name=\"$_\" content=\"".xml_quote($src_doc->metaData($_))."\"/>\n";
105 }
106 0 print $output "</info>\n";
107 }
108  
109 0 print $output "<types full=\"1\">\n";
110 0 foreach my $atr (grep { !/^(?:ORD|HIDE|ID)$/ } $src_doc->FS->attributes) {
111 0 print $output " <t n=\"$atr\"";
112 0 ▶ if ($src_doc->FS->isList($atr)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($src_doc->FS->isList($atr)) 0 0 0
113 0 print $output " v=\"",xml_quote(join("|",$src_doc->FS->listValues($atr))),"\"";
114 }
115 0 print $output "/>\n";
116 }
117 0 print $output "</types>\n";
118  
119 0 foreach my $tree ($src_doc->trees) {
120 0 my $node=$tree;
121 0 ▶ NODE: while ($node) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node) 0 0 0
122 0 print $output "<nd";
123 print $output
124 0 map { " $_=\"".xml_quote($node->{$_})."\"" }
125 0 grep { $node->{$_} ne "" }
126 0 grep { !/^(?:ORD|HIDE|ID)$/ } $src_doc->FS->attributes;
127 0 print $output ">\n";
128 0 ▶ if ($node->firstson) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->firstson) 0 0 0
129 0 $node=$node->firstson;
130 0 next;
131 }
132 0 ▶ while ($node) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node) 0 0 0
133 0 print $output "</nd>\n";
134 0 ▶ if ($node->rbrother) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->rbrother) 0 0 0
135 0 $node=$node->rbrother;
136 0 next NODE;
137 }
138 0 $node=$node->parent;
139 }
140 }
141 }
142 0 print $output "</trees>\n";
143 }
144  
145  
146 # SAX TrXML to Treex::PML::Document transducer
147 package Treex::PML::Backend::TrXML::SAXHandler;
148 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
149  
150 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
151 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
152 1 $VERSION='2.29'; # version template
153 }
154 1 ▶use Treex::PML;
Subroutine BEGIN: called CC=1 SCAR=0.0
155  
156 0 ▶sub decode {
Subroutine decode: not called CC=4 SCAR=30.0
157 0 my ($self, $str)=@_;
158 0 my $enc=$self->{TargetDocument}->encoding();
159 0 ▶ if ($]>=5.008 or $enc eq "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= 5.008 or $enc eq '') { } 0 0 0
Condition0%0/3
$] >= "5.008" or $enc eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
$] >= "5.008" or $enc eq ""
$] >= "5.008" $enc eq ""
Truth table0%0/3
A or B
A= $] >= "5.008"B= $enc eq ""
ABresult
1X1
011
000
160 0 return $str;
161 } else {
162 0 print "encoding: $enc, $str\n";
163 0 eval {
164 0 $str = XML::LibXML::decodeFromUTF8($enc,$str);
165 };
166 0 ▶ warn $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
167 0 return $str;
168 }
169 }
170  
171 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
172 0 my ($class, %args) = @_;
173 0 bless \%args, $class;
174 }
175  
176 0 ▶sub start_document {
Subroutine start_document: not called CC=3 SCAR=24.8
177 0 my ($self,$hash) = @_;
178 0 ▶ $self->{TargetDocument} ||= Treex::PML::Factory->createDocument();
Condition0%0/3
$self->{"TargetDocument"} ||= "Treex::PML::Factory"->createDocument
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"TargetDocument"} ||= "Treex::PML::Factory"->createDocument
$self->{"TargetDocument"} "Treex::PML::Factory"->createDocument
Truth table0%0/3
A ||= B
A= $self->{"TargetDocument"}B= "Treex::PML::Factory"->createDocument
ABresult
1X1
011
000
179 0 ▶ $self->{FSAttrs} ||= [];
Condition0%0/2
$self->{"FSAttrs"} ||= []
l!l
00
MC/DC0%0/1
$self->{"FSAttrs"} ||= []
$self->{"FSAttrs"}
Truth table0%0/2
A
A= $self->{"FSAttrs"}
Aresult
11
00
180 }
181  
182 0 ▶sub end_document {
Subroutine end_document: not called CC=1 SCAR=6.9
183 0 my ($self) = @_;
184 my $FS = Treex::PML::Factory->createFSFormat([
185 0 @{$self->{FSAttrs}},
186 '@N ORD', '@H HIDE', '@K ID'
187 ]);
188 0 $self->{TargetDocument}->changeFS($FS);
189 0 $self->{TargetDocument};
190 }
191  
192 0 ▶sub xml_decl {
Subroutine xml_decl: not called CC=1 SCAR=6.9
193 0 my ($self,$data) = @_;
194 0 my $doc = $self->{TargetDocument};
195 0 $doc->changeEncoding($data->{Encoding});# || 'iso-8859-2');
196 0 $doc->changeMetaData('xmldecl_version' => $data->{Version});
197 0 $doc->changeMetaData('xmldecl_standalone' => $data->{Standalone});
198 }
199  
200 0 ▶sub characters {
Subroutine characters: not called CC=1 SCAR=0.0
201 # nothing to do so far
202 }
203  
204 0 ▶sub start_element {
Subroutine start_element: not called CC=12 SCAR=50.5
205 0 my ($self, $hash) = @_;
206 0 my $elem = $hash->{Name};
207 0 my $attr = $hash->{Attributes};
208 0 my $target_doc = $self->{TargetDocument};
209 # my %attr = map { $_->{Name} => $_->{Value} } values %$attr;
210  
211 # $elem eq 'tree' && do { } # nothing to do
212 # $elem eq 'info' && do { } # nothing to do
213 0 ▶ if ($elem eq 'meta') {
Branch: 5 branches
BranchTrueFalseTotal
if ($elem eq 'meta') { } 0 0 0
elsif ($elem eq 'types') { } 0 0 0
elsif ($elem eq 't') { } 0 0 0
elsif ($elem eq 'nd') { } 0 0 0
elsif ($elem eq 'trees' or $elem eq 'info') { } 0 0 0
214  
215 $target_doc->changeMetaData($self->decode($attr->{'{}name'}->{Value}) =>
216 0 $self->decode($attr->{'{}content'}->{Value}));
217  
218 } elsif ($elem eq 'types') {
219  
220 # $target_doc->changeMetaData('TrXML types/@full' => $self->decode($attr->{'{}full'}->{Value}))
221 # if (exists($attr->{'{}full'}));
222  
223 } elsif ($elem eq 't') {
224  
225 0 my $atrname = $attr->{'{}n'}->{Value};
226 0 ▶ my $v = exists($attr->{'{}v'}) ? $self->decode($attr->{'{}v'}->{Value}) : "";
Branch: 1 branch
BranchTrueFalseTotal
exists $attr->{'{}v'} ? : 0 0 0
227  
228 0 push @{$self->{FSAttrs}}, '@P '.$atrname;
229 0 ▶ push @{$self->{FSAttrs}}, '@L '.$atrname.'|'.$v if ($v ne "");
Branch: 1 branch
BranchTrueFalseTotal
if $v ne "" 0 0 0
230 # d and m not implemented
231 } elsif ($elem eq 'nd') {
232  
233 0 my $parent = $self->{Node};
234 0 my $new;
235 0 ▶ if ($parent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent) { } 0 0 0
236 0 $self->{Node} = $new = Treex::PML::Factory->createNode();
237 } else {
238 0 undef $parent;
239 0 $self->{Tree} = $self->{TargetDocument}->new_tree($self->{TargetDocument}->lastTreeNo+1);
240 0 $self->{Node} = $new = $self->{Tree};
241 }
242 0 $new->{ORD}=$attr->{'{}n'}->{Value};
243 0 $new->{HIDE}='hide'x$attr->{'{}h'}->{Value};
244 0 $new->{ID}=$self->decode($attr->{'{}id'}->{Value});
245 0 foreach (grep { !/^{}(?:n|h|id)$/ } keys %$attr) {
246 0 $new->{$self->decode($attr->{$_}->{Name})} = $self->decode($attr->{$_}->{Value});
247 }
248 0 ▶ $new->paste_on($parent,'ORD') if ($parent);
Branch: 1 branch
BranchTrueFalseTotal
if $parent 0 0 0
249 ▶ } elsif ($elem eq 'trees' or $elem eq 'info') {
Condition0%0/3
$elem eq "trees" or $elem eq "info"
l!l&&r!l&&!r
000
MC/DC0%0/2
$elem eq "trees" or $elem eq "info"
$elem eq "trees" $elem eq "info"
Truth table0%0/3
A or B
A= $elem eq "trees"B= $elem eq "info"
ABresult
1X1
011
000
250 # do nothing
251 } else {
252 0 die "Treex::PML::Backend::TrXML: unknown element $elem\n";
253 }
254 0 $self->{attributes}=$attr;
255 }
256  
257 0 ▶sub end_element {
Subroutine end_element: not called CC=3 SCAR=24.8
258 0 my ($self,$hash) = @_;
259  
260 0 ▶ if ($hash->{Name} eq 'nd') {
Branch: 2 branches
BranchTrueFalseTotal
if ($hash->{'Name'} eq 'nd') { } 0 0 0
elsif ($hash->{'Name'} eq 'trees') { } 0 0 0
261 0 $self->{Node}=$self->{Node}->parent;
262 } elsif ($hash->{Name} eq 'trees') {
263 0 $self->{Node}=undef;
264 }
265 }
266  
267 0 ▶sub entity_reference {
Subroutine entity_reference: not called CC=1 SCAR=0.0
268 }
269  
270 0 ▶sub start_cdata { # not much use for this
Subroutine start_cdata: not called CC=1 SCAR=6.9
271 0 my $self = shift;
272 0 $self->{InCDATA} = 1;
273 }
274  
275 0 ▶sub end_cdata { # not much use for this
Subroutine end_cdata: not called CC=1 SCAR=6.9
276 0 my $self = shift;
277 0 $self->{InCDATA} = 0;
278 }
279  
280 0 ▶sub comment {
Subroutine comment: not called CC=2 SCAR=17.9
281 0 my $self = $_[0];
282 0 my $data = $_[1];
283 0 ▶ if ($self->{Node}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Node"}) 0 0 0
284 0 $self->{Node}->{xml_comment}.='<!--'.$data.'-->';
285 }
286 }
287  
288 0 ▶sub doctype_decl { # not use for this, so far
Subroutine doctype_decl: not called CC=2 SCAR=17.9
289 0 my ($self,$hash) = @_;
290 0 foreach (qw(Name SystemId PublicId Internal)) {
291 0 $self->{"DocType_$_"} = $hash->{$_};
292 }
293 }
294  
295 1 1;
296 __END__
« PML/IO.pm ↑ Summary PML/Schema/Alt.pm »