bin/alvisXMLsplit

filter: Statementstmt 100% 15 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 100% 20 / 20 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« bin/alvisXMLmerge ↑ Summary bin/alvisXSL »
1 #!/usr/bin/perl
2
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
6
7 1 ▶use Alvis::Utils qw(open_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
8
9 ###################### CONFIGURATION #####################
10
11 my $report = 0; # switch off STDERR status
12 my $CollectionHeader="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" .
13 "<documentCollection xmlns=\"http://alvis.info/enriched/\" version=\"1.1\">\n";
14
15 # NOTE: also documentRecord and documentCollection are hardwired
16 # into matches and such below
17
18 ###################### END CONFIGURATION #####################
19
20 # static used by get_next_rec()
21 my $recleft = "";
22
23 # input parameters from command line args
24 my $bz = 0;
25 my $bz_in = 0;
26 my $offset=1;
27 my $F=shift @ARGV;
28 my $gotarg = 1;
29 while ( $F && ($gotarg==1) ) {
30 if ( $F eq "--bzip2" ) {
31 $bz = 1;
32 } elsif ($F eq '--bzip2-in') {
33 $bz_in = 1;
34 } elsif ($F eq '--start') {
35 $offset = int(shift @ARGV);
36 if ( $offset <1 ) {
37 pod2usage(1);
38 }
39 } else {
40 $gotarg = 0;
41 }
42 if ( $gotarg ) {
43 $F=shift @ARGV;
44 }
45 }
46
47 pod2usage(1) if @ARGV != 2;
48
49 my $Size=shift @ARGV;
50 my $ODir=shift @ARGV;
51 $report = 0; # switch off STDERR status
52
53 1 ▶use encoding 'utf8';
Subroutine BEGIN: called CC=1 SCAR=0.0
54 use open ':utf8';
55
56 # have to make sure documentRecord elements one per line
57 if ( $bz || $bz_in ) {
58 open(W,"bzcat $F | perl -p -e \"s/<documentRecord/\n<documentRecord/g;\" |")
59 || die("Unable to open \"$F\"");
60 } else {
61 *W = open_file($F);
62 #open(W,"perl -p -e \"s/<documentRecord/\n<documentRecord/g;\" $F |")
63 # || die("Unable to open \"$F\"");
64 }
65
66 system("mkdir -p $ODir");
67
68 my $N = 1;
69 my $Collection = $CollectionHeader;
70 while (my $record=&get_next_rec(*W))
71 {
72 $Collection.=$record;
73 if ($N%$Size==0)
74 {
75 $Collection.="</documentCollection>\n";
76 my $FN = int($N/$Size) + $offset - 1;
77 my $out_f="$ODir/$FN.xml";
78 open(OUT,">:utf8",$out_f) || die("Unable to open $out_f");
79 print OUT $Collection;
80 close(OUT);
81 if ( $bz ) {
82 system("bzip2 $out_f");
83 }
84 $Collection=$CollectionHeader;
85 }
86 if ( $report ) { print STDERR "$N\r"; }
87 $N++;
88 }
89 if ( $report ) { print STDERR "\n"; }
90
91 if (($N-1)%$Size)
92 {
93 $Collection.="</documentCollection>\n";
94 my $FN = int($N/$Size) + $offset;
95 my $out_f="$ODir/$FN.xml";
96 open(OUT,">:utf8",$out_f) || die("Unable to open $out_f");
97 print OUT $Collection;
98 close(OUT);
99 if ( $bz ) {
100 system("bzip2 $out_f");
101 }
102 }
103
104
105 sub get_next_rec
106 {
107 my $fh=shift;
108 my $rec = "";
109
110 if ( $recleft =~/<documentRecord/ ) {
111 $rec = $recleft;
112 } else {
113 my $l;
114 while ($rec eq "" && ($l=<$fh>)) {
115 # print "IN: $l";
116 if ($l=~/<documentRecord/) {
117 $rec=$l;
118 }
119 }
120 }
121 if ( $rec =~/<documentRecord[^>]+\/>/ ) {
122 $recleft = $rec;
123 $recleft =~ s/.*<documentRecord[^>]+\/>//;
124 $rec =~ s/(<documentRecord[^>]+\/>).*/$1/;
125 return $rec . "\n";
126 }
127 # print STDERR "Start 1 with $rec\n";
128 $recleft = "";
129 while (my $l=<$fh>) {
130 # print "IN: $l";
131 if ($l=~/<\/documentRecord/)
132 {
133 $recleft = $l;
134 $recleft =~ s/.*<\/documentRecord>//;
135 $l =~ s/<\/documentRecord>.*/<\/documentRecord>/;
136 $rec.=$l;
137 # print STDERR "Got 1, left $recleft\n";
138 if ( $rec !~ /\n$/ ) {
139 $rec .= "\n";
140 }
141 return $rec;
142 } else {
143 $rec.=$l;
144 }
145 }
146 }
147
148 __END__
« bin/alvisXMLmerge ↑ Summary bin/alvisXSL »