blib/lib/Lingua/Ogmios/DocumentCollection.pm untested

filter: Statementstmt 0.0% 0 / 130 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 6 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 24 Podpod 0.0% 0 / 20 totaltotal 0.0% 0 / 192 SCAR 40.3
CC
7
Cov
0%
new
12.0
_parseDocumentCollectionFromFile
12.0
_parseDocumentCollectionFromData
12.0
CRAP 56.0
1 package Lingua::Ogmios::DocumentCollection;
2  
3 use strict;
4 use warnings;
5  
6 use Lingua::Ogmios::DocumentRecord;
7 use XML::LibXML;
8  
9 sub new {
10 my ($class, $file, $type, $lingAnalysisLoad) = @_;
11  
12 my $collection = {
13 'documents' => {},
14 'count' => 0,
15 'attributes' => [],
16 };
17  
18 bless $collection, $class;
19  
20 # Parsing the file and loading documents
21 if (($type eq "file") && (defined $file)) {
22 $collection->_parseDocumentCollectionFromFile($file, $lingAnalysisLoad);
23 }
24  
25 if (($type eq "data") && (defined $file)) {
26 $collection->_parseDocumentCollectionFromData($file, $lingAnalysisLoad);
27 }
28
29 return($collection);
30 }
31  
32  
33  
34 sub setAttributes {
35 my ($self, $attributes) = @_;
36 my $attr;
37  
38 foreach $attr (@$attributes) {
39 push @{$self->{'attributes'}}, {'nodeName' => $attr->nodeName,
40 'value' => $attr->value,
41 };
42 }
43 }
44  
45 sub getAttributes {
46 my ($self) = @_;
47  
48 return($self->{'attributes'});
49 }
50  
51 sub _parseDocumentCollectionFromFile {
52 my ($self, $file, $lingAnalysisLoad) = @_;
53
54 # Parsing the file and loading documents
55
56 my $Parser=XML::LibXML->new();
57 my $document;
58 my $document_record;
59 my $parsedDocument;
60 my $id;
61
62 eval {
63 $document=$Parser->parse_file($file);
64 };
65 if ($@){
66 warn "Parsing the doc failed: $@. Trying to get the IDs..\n";
67 } else {
68 if ($document) {
69 $self->_parseDocumentCollection($document, $lingAnalysisLoad);
70 } else {
71 warn "Parsing the doc failed. Doc: " . ($self->getCount + 1) . "(in $file)\n";
72 }
73 }
74 }
75  
76 sub _parseDocumentCollectionFromData {
77 my ($self, $file, $lingAnalysisLoad) = @_;
78
79 # Parsing the file and loading documents
80
81 my $Parser=XML::LibXML->new();
82 my $document;
83 my $document_record;
84 my $parsedDocument;
85 my $id;
86
87 eval {
88 $document=$Parser->parse_string($file);
89 };
90 if ($@){
91 warn "Parsing the doc failed: $@. Trying to get the IDs..\n";
92 } else {
93 if ($document) {
94 $self->_parseDocumentCollection($document, $lingAnalysisLoad);
95 } else {
96 warn "Parsing the doc failed. Doc: " . ($self->getCount + 1) . "(in $file)\n";
97 }
98 }
99 }
100  
101 sub _parseDocumentCollection {
102 my ($self, $document, $platformConfig) = @_;
103
104 # Parsing the file and loading documents
105  
106 # my $Parser=XML::LibXML->new();
107 # my $document;
108 my $document_record;
109 my $parsedDocument;
110 # my $id;
111  
112 # eval {
113 # $document=$Parser->parse_file($file);
114 # };
115 # if ($@){
116 # warn "Parsing the doc failed: $@. Trying to get the IDs..\n";
117 # }
118 # else {
119 # if ($document) {
120 my $root=$document->documentElement();
121
122 my @attr = $root->attributes;
123 $self->setAttributes(\@attr);
124
125 foreach $document_record ($root->getChildrenByTagName('documentRecord')) {
126 $parsedDocument = Lingua::Ogmios::DocumentRecord->new($document_record, $platformConfig);
127 $self->addDocument($parsedDocument);
128 }
129 # }
130 }
131  
132 sub addDocument {
133 my ($self, $parsedDocument) = @_;
134  
135 $self->{'documents'}->{$parsedDocument->getId}->{"document"} = $parsedDocument;
136 $self->{'documents'}->{$parsedDocument->getId}->{"order"} = $self->{'count'};
137 $self->{'count'}++;
138 }
139  
140 sub setCount {
141 my ($self, $value)= @_;
142  
143 $self->{'count'} = $value;
144 }
145  
146  
147 sub resetCount {
148 my ($self)= @_;
149  
150 $self->setCount(0);
151  
152 }
153  
154 sub getCount {
155 my ($self)= @_;
156  
157 return($self->{'count'});
158 }
159  
160  
161 sub incrCount {
162 my ($self)= @_;
163  
164 $self->setCount($self->getCount + 1);
165 }
166  
167 sub decrCount {
168 my ($self)= @_;
169  
170 $self->setCount($self->getCount - 1);
171 }
172  
173 sub getDocuments {
174 my ($self) = @_;
175  
176 my %documents;
177 my $documentId;
178  
179 foreach $documentId (keys %{$self->{'documents'}}) {
180 $documents{$documentId} = $self->{'documents'}->{$documentId}->{"document"};
181 }
182  
183 return(\%documents);
184 }
185  
186 sub getSortedDocuments {
187 my ($self) = @_;
188  
189 my @documents;
190 my $documentId;
191  
192 foreach $documentId (sort { $self->{'documents'}->{$a}->{"order"} <=> $self->{'documents'}->{$b}->{"order"}} keys %{$self->{'documents'}}) {
193 push @documents, $self->{'documents'}->{$documentId}->{"document"};
194 }
195  
196 return(\@documents);
197 }
198  
199 sub getDocument {
200 my ($self, $id) = @_;
201  
202 return($self->getDocuments->{$id});
203 }
204  
205 sub existsDocument {
206 my ($self, $id) = @_;
207  
208 return(exists($self->getDocuments->{$id}));
209 }
210  
211 sub tokenisation {
212 my ($self) = @_;
213  
214 my $document;
215 my $record_log = 1;
216  
217 foreach $document (values %{$self->getDocuments}) {
218 $record_log = $document->tokenisation;
219 # if ($document->getAnnotations->getTokenLevel->getSize == 0) {
220 # $record_log = 0;
221 # }
222 $document->computeSectionFromToken($record_log);
223 # $record_log = 1;
224 }
225 }
226  
227 sub XMLout {
228 my ($self) = @_;
229  
230 my $document;
231  
232 my $str;
233 my $attr;
234  
235 $str = '<documentCollection';
236 foreach $attr (@{$self->getAttributes}) {
237 $str .= " " . $attr->{'nodeName'} . '="' . $attr->{'value'} . '"';
238 }
239 $str .= ">\n";
240  
241  
242 # foreach $document (values %{$self->getDocuments}) {
243 foreach $document (@{$self->getSortedDocuments}) {
244 $str .= $document->XMLout;
245 }
246 $str .= "</documentCollection>";
247  
248 return($str);
249 }
250  
251 sub getDocumentList {
252 my ($self) = @_;
253  
254 return(keys %{$self->getDocuments});
255 }
256  
257 sub printDocumentList {
258 my ($self) = @_;
259 my $doc_id;
260  
261 warn "\n\nDocument Id List \n";
262 foreach $doc_id ($self->getDocumentList) {
263 warn "\t$doc_id\n";
264 }
265 warn "\n";
266 }
267  
268  
269 1;
270  
271 __END__