CAB/Format/XmlVz.pm untested

filter: Statementstmt 0.0% 0 / 123 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 191 CC 10 SCAR 19.9
CC
10
Cov
0%
documentNode
20.0
parseDocument
12.0
putDocument
12.0
CRAP 7.3
1 ## -*- Mode: CPerl -*-
2 ##
3 ## File: DTA::CAB::Format::XmlVz.pm
4 ## Author: Bryan Jurish <moocow@cpan.org>
5 ## Description: Datum parser|formatter: XML (Vz)
6  
7 package DTA::CAB::Format::XmlVz;
8 use DTA::CAB::Format::XmlCommon;
9 use DTA::CAB::Datum ':all';
10 use XML::LibXML;
11 use IO::File;
12 use Carp;
13 use strict;
14  
15 ##==============================================================================
16 ## Globals
17 ##==============================================================================
18  
19 our @ISA = qw(DTA::CAB::Format::XmlCommon);
20  
21 BEGIN {
22 DTA::CAB::Format->registerFormat(name=>__PACKAGE__, filenameRegex=>qr/\.(?i:xml\-vz|(?:vz[\-\._]xml))$/);
23 }
24  
25 ##==============================================================================
26 ## Constructors etc.
27 ##==============================================================================
28  
29 ## $fmt = CLASS_OR_OBJ->new(%args)
30 ## + object structure: HASH ref
31 ## {
32 ## ##-- input
33 ## xdoc => $xdoc, ##-- XML::LibXML::Document
34 ## xprs => $xprs, ##-- XML::LibXML parser
35 ##
36 ## ##-- output
37 ## #encoding => $inputEncoding, ##-- default: UTF-8; applies to output only!
38 ## level => $level, ##-- output formatting level (default=0)
39 ## }
40 sub new {
41 my $that = shift;
42 my $fmt = $that->SUPER::new(
43 ##-- input
44 xprs => undef,
45 xdoc => undef,
46  
47 ##-- output
48 #encoding => 'UTF-8',
49 level => 1,
50  
51 ##-- user args
52 @_
53 );
54  
55 if (!$fmt->{xprs}) {
56 $fmt->{xprs} = XML::LibXML->new;
57 $fmt->{xprs}->keep_blanks(0);
58 }
59 return $fmt;
60 }
61  
62 ##==============================================================================
63 ## Methods: Persistence
64 ## + see Format::XmlCommon
65 ##==============================================================================
66  
67 ##=============================================================================
68 ## Methods: Input
69 ##==============================================================================
70  
71  
72 ##--------------------------------------------------------------
73 ## Methods: Input: Input selection
74 ## + see Format::XmlCommon
75  
76 ##--------------------------------------------------------------
77 ## Methods: Input: Generic API
78  
79 ## $doc = $fmt->parseDocument()
80 ## + parses buffered XML::LibXML::Document
81 sub parseDocument {
82 my $fmt = shift;
83 if (!defined($fmt->{xdoc})) {
84 $fmt->logconfess("parseDocument(): no source document {xdoc} defined!");
85 return undef;
86 }
87 my $root = $fmt->{xdoc}->documentElement;
88 my $sents = [];
89 my $doc = bless({body=>$sents},'DTA::CAB::Document');
90  
91 ##-- common variables
92 my ($cnod,$c, $snod,$s,$stoks, $wnod,$w);
93  
94 ##-- doc attributes: xmlbase
95 $doc->{$_->name} = $_->value foreach ($root->attributes);
96  
97 ##-- classes
98 foreach $cnod (@{ $root->findnodes(".//cat|.//vat") }) {
99 $c = {};
100 $c->{$_->name} = $_->value foreach ($cnod->attributes);
101 push(@{$doc->{cats}},$c);
102 }
103  
104 ##-- loop: sentences
105 foreach $snod (@{ $root->findnodes(".//s") }) {
106 push(@$sents, $s=bless({tokens=>($stoks=[])},'DTA::CAB::Sentence'));
107 $s->{$_->name} = $_->value foreach ($snod->attributes);
108  
109 ##-- loop: sentence/tokens
110 foreach $wnod (@{ $snod->findnodes("./w") }) {
111 push(@$stoks, $w=bless({},'DTA::CAB::Token'));
112 $w->{$_->name} = $_->value foreach ($wnod->attributes);
113 $w->{text} = $w->{plain} if (!defined($w->{text})); ##-- hack
114 }
115 }
116  
117 ##-- return document
118 return $doc;
119 }
120  
121  
122 ##=============================================================================
123 ## Methods: Output
124 ##==============================================================================
125  
126 ##--------------------------------------------------------------
127 ## Methods: Output: Local: Nodes
128  
129 ## $xmlnod = $fmt->tokenNode($tok)
130 ## + returns formatted token $tok as an XML node
131 sub tokenNode {
132 my ($fmt,$tok) = @_;
133 $tok = toToken($tok);
134 my $wnod = XML::LibXML::Element->new('w');
135 $wnod->setAttribute($_,$tok->{$_}) foreach (grep {defined($tok->{$_}) && !ref($tok->{$_})} sort(keys(%$tok)));
136 return $wnod;
137 }
138  
139 ## $xmlnod = $fmt->sentenceNode($sent)
140 sub sentenceNode {
141 my ($fmt,$sent) = @_;
142 $sent = toSentence($sent);
143 my $snod = XML::LibXML::Element->new('s');
144 $snod->setAttribute($_,$sent->{$_}) foreach (grep {defined($sent->{$_}) && !ref($sent->{$_})} sort(keys(%$sent)));
145 $snod->addChild($fmt->tokenNode($_)) foreach (@{$sent->{tokens}});
146 return $snod;
147 }
148  
149 ## $xmlnod = $fmt->documentNode($doc)
150 sub documentNode {
151 my ($fmt,$doc) = @_;
152 $doc = toDocument($doc);
153 my $docnod = XML::LibXML::Element->new('doc');
154 $docnod->setAttribute($_,$doc->{$_}) foreach (grep {defined($doc->{$_}) && !ref($doc->{$_})} sort(keys(%$doc)));
155 if ($doc->{cats}) {
156 my $croot = $docnod->addNewChild(undef,'classification');
157 my ($c,$cnod);
158 foreach $c (@{$doc->{cats}}) {
159 $cnod = $croot->addNewChild(undef,'cat');
160 $cnod->setAttribute($_, (defined($c->{$_}) ? $c->{$_} : '')) foreach (sort(keys(%$c)));
161 }
162 }
163 if ($doc->{body}) {
164 my $cooked = $docnod->addNewChild(undef,'cooked');
165 $cooked->addChild($fmt->sentenceNode($_)) foreach (@{$doc->{body}});
166 }
167 return $docnod;
168 }
169  
170 ##--------------------------------------------------------------
171 ## Methods: Output: Local: Utils
172  
173 ## $xmldoc = $fmt->xmlDocument()
174 ## + create or return output buffer $fmt->{xdoc}
175 ## + inherited from XmlCommon
176  
177 ## $rootnode = $fmt->xmlRootNode($nodname)
178 ## + returns root node
179 ## + inherited from XmlCommon
180  
181 ## $bodynode = $fmt->xmlBodyNode()
182 ## + really just a wrapper for $fmt->xmlRootNode($fmt->{documentElement})
183 sub xmlBodyNode {
184 my $fmt = shift;
185 return $fmt->xmlRootNode('doc');
186 }
187  
188 ## $sentnod = $fmt->xmlSentenceNode()
189 sub xmlSentenceNode {
190 my $fmt = shift;
191 my $body = $fmt->xmlBodyNode();
192 my ($snod) = $body->findnodes(".//s\[last()]");
193 return $snod if (defined($snod));
194 return $body->addNewChild(undef,'s');
195 }
196  
197  
198 ##--------------------------------------------------------------
199 ## Methods: Output: API
200  
201 ## $fmt = $fmt->putToken($tok)
202 sub putToken {
203 my ($fmt,$tok) = @_;
204 $fmt->xmlSentenceNode->addChild($fmt->tokenNode($tok));
205 return $fmt;
206 }
207  
208 ## $fmt = $fmt->putSentence($sent)
209 sub putSentence {
210 my ($fmt,$sent) = @_;
211 $fmt->xmlBodyNode->addChild($fmt->sentenceNode($sent));
212 return $fmt;
213 }
214  
215 ## $fmt = $fmt->putDocument($doc)
216 sub putDocument {
217 my ($fmt,$doc) = @_;
218 my $docnod = $fmt->documentNode($doc);
219 my ($xdoc,$root);
220 if (!defined($xdoc=$fmt->{xdoc}) || !defined($root=$fmt->{xdoc}->documentElement)) {
221 $xdoc = $fmt->{xdoc} = $fmt->xmlDocument() if (!$fmt->{xdoc});
222 $xdoc->setDocumentElement($docnod);
223 } else {
224 ##-- append-mode for real or converted input
225 $root->appendChild($docnod);
226 }
227  
228 return $fmt;
229 }
230  
231 ##========================================================================
232 ## package DTA::CAB::Format::VzXml : alias for 'XmlVz'
233 package DTA::CAB::Format::VzXml;
234 use strict;
235 use base qw(DTA::CAB::Format::XmlVz);
236  
237  
238 1; ##-- be happy
239  
240 __END__