blib/script/SplitCollection.pl untested

filter: Statementstmt 0.0% 0 / 98 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 2 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 124 SCAR 30.0
CC
4
Cov
0%
split_to_docRecs
20.0
BEGIN
2.0
BEGIN
2.0
CRAP 20.0
1 #!/usr/local/bin/perl
2  
3 use strict;
4  
5 use XML::LibXML;
6  
7 use Getopt::Long;
8 use Pod::Usage;
9  
10 my $verbose;
11 my $help;
12 my $man;
13  
14 my $line;
15 my $i;
16 my $rep;
17 my $prefix;
18 my $base;
19 my $doc_xml = "";
20 my @tab_docs_xml;
21 my $docset;
22  
23 my $collectionMax;
24 my @collection;
25  
26 my $charset = 'UTF-8';
27  
28 my $xmlhead="<?xml version=\"1.0\" encoding=\"$charset\"?>\n<documentCollection xmlns=\"http://alvis.info/enriched/\" version=\"1.1\">\n";
29 my $xmlfoot="</documentCollection>\n";
30  
31 if (scalar(@ARGV) ==0) {
32 $help = 1;
33 }
34  
35 Getopt::Long::Configure ("bundling");
36  
37 GetOptions('help|?' => \$help,
38 'man' => \$man,
39 'verbose|v' => \$verbose,
40 'dir|d=s' => \$rep,
41 'prefix|p:s' => \$prefix,
42 'base|b=s' => \$base,
43 'size|s=i' => \$collectionMax,
44 'file|f=s' => \@collection,
45 );
46  
47 pod2usage(1) if $help;
48 pod2usage(-exitstatus => 0, -verbose => 2) if $man;
49  
50  
51 foreach $docset (@collection) {
52 $doc_xml = "";
53 print STDERR "Reading $docset ... ";
54 open DOCSET, $docset;
55
56 binmode(DOCSET, ":utf8");
57 while($line=<DOCSET>) {
58 $doc_xml .= $line;
59 }
60 close DOCSET;
61 print STDERR "done\n";
62  
63 print STDERR "Splitting $docset ... ";
64 @tab_docs_xml = &split_to_docRecs($doc_xml);
65 print STDERR "done\n";
66  
67 warn "\t" . scalar(@tab_docs_xml) . " documents split\n";
68 warn "\t" . scalar(@tab_docs_xml) . " documents to print\n";
69  
70 warn "\tStart at " . ($base+1) . "\n";
71  
72 print STDERR "Printing $docset ... ";
73 my $j = 0;
74 for($i = 0; $i<scalar(@tab_docs_xml); $i++) {
75  
76 if ($i % $collectionMax == 0) {
77 open DOCREC, ">$rep/$prefix-" . ($base + $j + 1) . ".xml" or die "No such file\n";
78 binmode(DOCREC, ":utf8");
79 $j++;
80 print DOCREC $xmlhead;
81 }
82  
83 print DOCREC ${$tab_docs_xml[$i]}[1];
84 if (($i + 1) % $collectionMax == 0) {
85 print DOCREC $xmlfoot;
86 close DOCREC;
87 }
88 }
89 if (($i + 1) % $collectionMax != 0) {
90 print DOCREC $xmlfoot;
91 close DOCREC;
92 }
93 print STDERR "done\n";
94
95 print STDERR "\tLast document number: $i\n\n";
96 $base += $i;
97 }
98  
99 # print STDERR "Building the $rep/seq file ... ";
100 # open SEQ, ">$rep/seq";
101 # print SEQ "0 " . ($base - 1) . "\n";
102 # close SEQ;
103 # print STDERR "done\n";
104  
105  
106  
107 sub split_to_docRecs
108 {
109 my $xml=shift;
110  
111 my @recs=();
112
113 my $doc;
114 my $Parser=XML::LibXML->new();
115  
116 eval
117 {
118 $doc=$Parser->parse_string($xml);
119 };
120 if ($@)
121 {
122 warn "Parsing the doc failed: $@. Trying to get the IDs..\n";
123 eval
124 {
125 $xml=~s/<documentRecord\s(xmlns=[^\s]+)*\sid\s*=\s*\"([^\"]*?)\">/&unparseable_id($2)/esgo;
126 };
127 }
128 else
129 {
130 if ($doc)
131 {
132 my $root=$doc->documentElement();
133 for my $rec_node ($root->getChildrenByTagName('documentRecord'))
134 {
135 my $id=$rec_node->getAttribute("id");
136 if (defined($id))
137 {
138 $xml=$rec_node->toString();
139 push(@recs,[$id,$xml]);
140 }
141 else
142 {
143 my $rec_str=$rec_node->toString();
144 $rec_str=~s/\n/ /sgo;
145 warn "No id for record $rec_str\n";
146 }
147 }
148 }
149 else
150 {
151 my $doc_str=$xml;
152 $doc_str=~s/\n/ /sgo;
153 warn "Parsing the doc failed. Doc: $doc_str\n";
154 }
155 }
156  
157 return @recs;
158 }
159  
160 ########################################################################
161  
162 =head1 NAME
163  
164 .pl - ????
165  
166  
167 =head1 SYNOPSIS
168  
169 .....pl [option] --switch <filename>
170  
171 where option can be --help --man --verbose
172  
173 =head1 OPTIONS AND ARGUMENTS
174  
175 =over 4
176  
177 =item --switch <filename>
178  
179  
180 =item --help
181  
182 print help message for using grepTerms.pl
183  
184 =item --man
185  
186 print man page of grepTerms.pl
187  
188 =item --verbose
189  
190 Go into the verbose mode
191  
192 =back
193  
194 =head1 DESCRIPTION
195  
196 ...
197  
198  
199 =head1 SEE ALSO
200  
201  
202 =head1 AUTHOR
203  
204 Thierry Hamon, E<lt>thierry.hamon@limsi.frE<gt>
205  
206 =head1 COPYRIGHT AND LICENSE
207  
208 Copyright (C) 2012 Thierry Hamon
209  
210 This is free software; you can redistribute it and/or modify it under
211 the same terms as Perl itself, either Perl version 5.8.4 or, at your
212 option, any later version of Perl 5 you may have available.
213  
214 =cut
215