tools/xml_split/xml_split

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« tools/xml_pp/xml_pp ↑ Summary check_optional_modules »
1 #!/usr/bin/perl -w
2 # $Id: /xmltwig/trunk/tools/xml_split/xml_split 17 2007-06-04T11:57:10.366292Z mrodrigu $
3 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 22 ▶use XML::Twig;
Subroutine BEGIN: called CC=1 SCAR=2.6
6 use FindBin qw( $RealBin $RealScript);
7 use Getopt::Std;
8  
9 import xml_split::state::parser;
10 import xml_split::state::twig;
11  
12 undef $Getopt::Std::STANDARD_HELP_VERSION;
13 $Getopt::Std::STANDARD_HELP_VERSION=1; # to stop processing after --help or --version
14  
15 use vars qw( $VERSION $USAGE);
16  
17 $VERSION= "0.06";
18 $USAGE= "xml_split [-l <level> [-s <size> | -g <nb_grouped>] | -c <cond>] [-b <base>] [-n <nb>] [-e <ext>] [-p <plugin>] [-I <plugin_dir>] [-i] [-d] [-v] [-h] [-m] [-V] <files>\n";
19  
20 { # main block
21  
22 my $opt={};
23 getopts('l:c:b:g:n:e:p:is:dvhmV', $opt);
24  
25 # defaults
26 $opt->{n} ||= 2; # number of digits used for creating parts
27 $opt->{I} ||= ($ENV{HOME} || '') . "/.xml_split";
28  
29 if( $opt->{h}) { die $USAGE, "\n"; }
30 if( $opt->{m}) { exec "pod2text $RealBin/$RealScript"; }
31 if( $opt->{V}) { print "xml_split version $VERSION\n"; exit; }
32  
33 my %factor=( ' ' => 1, K => 1000, M => 1_000_000, G => 1_000_000_000);
34 if( $opt->{s}) { if( $opt->{c}) { die "cannot use -c and -s at the same time\n"; }
35  
36 if( $opt->{s}=~ m{^\s*(\d+)\s*(G[bo]?|M[bo]?|K[bo]?\s*)?$}i)
37 { my( $size, $unit)= ($1, uc substr( $2 || ' ', 0, 1));
38 $opt->{s}= $size * $factor{$unit};
39 }
40 else
41 { die "invalid size (should be in Kb, Mb or Gb): '$opt->{s}'\n"; }
42 }
43  
44 if( $opt->{g}) { die "cannot use -g and -s at the same time\n" if( $opt->{s});
45 die "cannot use -g and -c at the same time\n" if( $opt->{c});
46 $opt->{l} ||= 1;
47 }
48 elsif( $opt->{c}) { die "cannot use -l and -c at the same time\n" if( $opt->{l}); }
49 else { $opt->{l} ||= 1; $opt->{c}= "level( $opt->{l})"; }
50  
51  
52 my $options= { cond => $opt->{c},
53 base => $opt->{b}, nb_digits => $opt->{n}, ext => $opt->{e},
54 plugin => $opt->{p},
55 no_pi => $opt->{d},
56 verbose => $opt->{v},
57 xinclude => $opt->{i} ? 1 : 0,
58 };
59  
60  
61 my $state;
62 if( my $plugin= $opt->{p})
63 { if( $plugin!~ m{^[\w:.-]+$}) { die "wrong plugin name '$plugin' (only word characters are allowed in plugin names)\n"; }
64 push @INC, $opt->{I};
65 eval { require $plugin };
66 if( $@) { die "cannot find plugin '$plugin': $!"; }
67 import $plugin;
68 $state= $plugin->new( $options);
69 }
70
71  
72 if( $opt->{s})
73 { $state||= xml_split::state::parser->new( $options);
74 $state->{level} = $opt->{l};
75 $state->{size} = $opt->{s};
76 $state->{current_size}=0;
77 $state->{handlers}= { Start => \&parser_start_tag_size, End => \&parser_end_tag_size , Default => \&parser_default_size};
78 warn "using XML::Parser\n" if( $opt->{v});
79 split_with_parser( $state, @ARGV);
80 }
81 elsif( $opt->{g})
82 { $state||= xml_split::state::parser->new( $options);
83 $state->{level}= $opt->{l};
84 $state->{group}= $opt->{g};
85 $state->{handlers}= { Start => \&parser_start_tag_grouped, End => \&parser_end_tag_grouped , Default => \&parser_default_grouped};
86 warn "using XML::Parser\n" if( $opt->{v});
87 split_with_parser( $state, @ARGV);
88 }
89 elsif( $opt->{l})
90 { $state||= xml_split::state::parser->new( $options);
91 $state->{level}= $opt->{l};
92 $state->{handlers}= { Start => \&parser_start_tag_level, End => \&parser_end_tag_level , Default => \&parser_default_level};
93 warn "using XML::Parser\n" if( $opt->{v});
94 split_with_parser( $state, @ARGV);
95 }
96 else
97 { $state||= xml_split::state::twig->new( $options);
98 split_with_twig( $state, @ARGV);
99 }
100  
101 exit;
102 }
103  
104 sub split_with_twig
105 { my( $state, @files)= @_;
106 if( !@files)
107 { $state->{base} ||= 'out';
108 $state->{ext} ||= '.xml';
109 my $twig_options= twig_options( $state);
110 my $t= XML::Twig->new( %$twig_options, $state);
111 $state->{twig}= $t;
112 $t->parse( \*STDIN);
113 end_file( $t, $state);
114 }
115 else
116 { foreach my $file (@files)
117 {
118 unless( $state->{base}) { $state->{seq_nb}=0; }
119 my( $base, $ext)= ($file=~ m{^(.*?)(\.\w+)?$});
120 $state->{base} ||= $base;
121 $state->{ext} ||= $ext || '.xml';
122 my $twig_options= twig_options( $state);
123 my $t= XML::Twig->new( %$twig_options);
124 $state->{twig}= $t;
125 $t->parsefile( $file);
126 end_file( $t, $state);
127 }
128 }
129 }
130  
131 sub split_with_parser
132 { my( $state, @files)= @_;
133 if( !@files)
134 { $state->{base} ||= 'out';
135 $state->{ext} ||= '.xml';
136 my $parser_options= parser_options( $state);
137 my $p= XML::Parser->new( %$parser_options);
138 $state->{parser}= $p;
139 $p->parse( \*STDIN);
140 }
141 else
142 { foreach my $file (@files)
143 {
144 unless( $state->{base}) { $state->{seq_nb}=0; }
145 my( $base, $ext)= ($file=~ m{^(.*?)(\.\w+)?$});
146 $state->{base} ||= $base;
147 $state->{ext} ||= $ext || '.xml';
148 my $parser_options= parser_options( $state);
149 my $p= XML::Parser->new( %$parser_options);
150 $state->{parser}= $p;
151 $p->parsefile( $file);
152 }
153 }
154 }
155
156 sub parser_options
157 { my( $state)= @_;
158 # prepare output to the main document
159 unless( $state->{no_pi})
160 { my $file_name= $state->main_file_name(); # main file name
161 warn "generating main file $file_name\n" if( $state->{verbose});
162 open( my $out, '>', $file_name) or die "cannot create main file '$file_name': $!";
163 $state->{main_fh}= $out;
164 $state->{current_fh}= $out;
165 }
166 my $handlers= { Start => sub { $state->{handlers}->{Start}->( $state, shift( @_)); },
167 End => sub { $state->{handlers}->{End}->( $state, shift( @_)); },
168 Default => sub { $state->{handlers}->{Default}->( $state, shift( @_)); },
169 XMLDecl => sub { parser_declaration( $state, @_); },
170 };
171
172 return { Handlers => $handlers };
173 }
174  
175 ###################################################################################
176 # #
177 # handlers for the -l option #
178 # #
179 ###################################################################################
180  
181 sub parser_start_tag_level
182 { my( $state, $p)= @_;
183  
184 if( $p->depth == $state->{level})
185 { $state->{seq_nb}++;
186 my $file_name= $state->file_name;
187 # prepare chunk file
188 warn "generating $file_name\n" if( $state->{verbose});
189 open( my $out, '>', $file_name) or die "cannot create output file '$file_name': $!";
190 $state->{current_fh}= $out;
191 if( $state->{xml_declaration}) { print {$state->{current_fh}} $state->{xml_declaration}, "\n"; }
192 # output pi
193 unless( $state->{no_pi})
194 { print {$state->{main_fh}} $state->include( $file_name) ; }
195 }
196 print {$state->{current_fh}} $p->original_string if( $state->{current_fh});
197 }
198
199 sub parser_end_tag_level
200 { my( $state, $p)= @_;
201 print {$state->{current_fh}} $p->original_string if( $state->{current_fh});
202 if( $p->depth == $state->{level})
203 { unless( $state->{current_fh} == $state->{main_fh})
204 { close $state->{current_fh};
205 $state->{current_fh}= $state->{main_fh};
206 }
207 }
208 }
209  
210 sub parser_default_level
211 { my( $state, $p)= @_;
212 print {$state->{current_fh}} $p->original_string if( $state->{current_fh});
213 }
214  
215  
216 ###################################################################################
217 # #
218 # handlers for the -s option #
219 # #
220 ###################################################################################
221  
222 sub parser_start_tag_size
223 { my( $state, $p)= @_;
224 if( $p->depth == $state->{level} && !$state->{current_size})
225 {
226 $state->{seq_nb}++;
227 my $file_name= $state->file_name;
228 # prepare chunk file
229 warn "generating $file_name\n" if( $state->{verbose});
230 open( my $out, '>', $file_name) or die "cannot create output file '$file_name': $!";
231 $state->{current_fh}= $out;
232 print {$state->{current_fh}} qq{$state->{xml_declaration}\n} if $state->{xml_declaration};
233 print {$state->{current_fh}} qq{<xml_split:root xmlns:xml_split="http://xmltwig.com/xml_split">\n};
234 # output pi
235 unless( $state->{no_pi})
236 { print {$state->{main_fh}} $state->include( $file_name) ; }
237 $state->{store_size}=1;
238 }
239 my $original_string= $p->original_string;
240 $state->{current_size} += length( $original_string) if( $state->{store_size});
241 print {$state->{current_fh}} $original_string if( $state->{current_fh});
242 }
243
244 sub parser_end_tag_size
245 { my( $state, $p)= @_;
246 my $original_string= $p->original_string;
247 $state->{current_size} += length( $original_string) if( $state->{store_size});
248 if( $p->depth == $state->{level} && $state->{current_size} > $state->{size})
249 { print {$state->{current_fh}} $original_string if( $state->{current_fh});
250 end_file_with_size( $state);
251 }
252 else
253 { if($p->depth < $state->{level}) { end_file_with_size( $state); }
254 print {$state->{current_fh}} $p->original_string if( $state->{current_fh});
255 }
256 }
257  
258 sub end_file_with_size
259 { my( $state)= @_;
260 unless( $state->{current_fh} == $state->{main_fh})
261 { print {$state->{current_fh}} qq{\n</xml_split:root>\n};
262 close $state->{current_fh};
263 $state->{current_size}=0;
264 $state->{store_size}=0;
265 $state->{current_fh}= $state->{main_fh};
266 }
267 }
268  
269 sub parser_default_size
270 { my( $state, $p)= @_;
271 my $string= $p->original_string;
272 if( $state->{store_size})
273 { $state->{current_size} += length( $string);
274 if( $p->depth < $state->{level}) { end_file_with_size( $state); }
275 }
276 print {$state->{current_fh}} $string if( $state->{current_fh});
277 }
278  
279 ###################################################################################
280 # #
281 # handlers for the -g option #
282 # #
283 ###################################################################################
284  
285 sub parser_start_tag_grouped
286 { my( $state, $p)= @_;
287 if( $p->depth == $state->{level})
288 { if( !$state->{current_nb})
289 { $state->{seq_nb}++;
290 my $file_name= $state->file_name;
291 # prepare chunk file
292 warn "generating $file_name\n" if( $state->{verbose});
293 open( my $out, '>', $file_name) or die "cannot create output file '$file_name': $!";
294 $state->{current_fh}= $out;
295 print {$state->{current_fh}} join( "\n", grep { $_ } ( $state->{xml_declaration},
296 qq{<xml_split:root xmlns:xml_split="http://xmltwig.com/xml_split">\n }
297 )
298 );
299 # output pi
300 unless( $state->{no_pi})
301 { print {$state->{main_fh}} $state->include( $file_name) ; }
302 }
303 }
304 print {$state->{current_fh}} $p->original_string if( $state->{current_fh});
305 }
306
307 sub parser_end_tag_grouped
308 { my( $state, $p)= @_;
309 if( $p->depth == $state->{level})
310 { print {$state->{current_fh}} $p->original_string if( $state->{current_fh});
311 $state->{current_nb}++;
312 if( $state->{current_nb} == $state->{group}) { end_file_grouped( $state); }
313 }
314 else
315 { if($p->depth < $state->{level}) { end_file_grouped( $state, { no_nl => 1 }); }
316 print {$state->{current_fh}} $p->original_string if( $state->{current_fh});
317 }
318 }
319  
320 sub end_file_grouped
321 { my( $state, $options)= @_;
322 print {$state->{current_fh}} qq{\n} unless( $options->{no_nl});
323 unless( $state->{current_fh} == $state->{main_fh})
324 { print {$state->{current_fh}} qq{</xml_split:root>\n};
325 close $state->{current_fh};
326 $state->{current_nb}=0;
327 $state->{current_fh}= $state->{main_fh};
328 }
329 }
330  
331 sub parser_default_grouped
332 { my( $state, $p)= @_;
333 print {$state->{current_fh}} $p->original_string if( $state->{current_fh});
334 }
335  
336 sub char_parser
337 { my( $state, $p)=( shift, shift);
338 print {$state->{current_fh}} $_[0] if( $state->{current_fh});
339 }
340  
341 sub parser_declaration
342 { my( $state, $p, $version, $encoding, $standalone)= @_;
343 $state->{xml_declaration}= $p->recognized_string || '';
344 print {$state->{main_fh}} $state->{xml_declaration};
345 # avoid calling original_string if not needed
346 #if( !$state->{xml_declaration} || $state->{xml_declaration}=~ m{encoding\s*=\s*["']utf-?8["']}i)
347 # { $state->{utf8_encoded}=1;
348 # $p->setHandlers( Char => \&char_parser);
349 # }
350 }
351  
352  
353 sub twig_options
354 { my( $state)= @_;
355  
356 # base options, ensures maximum fidelity to the original document
357 my $twig_options= { keep_encoding => 1, keep_spaces => 1 };
358  
359 # prepare output to the main document
360 unless( $state->{no_pi})
361 { my $file_name= $state->main_file_name(); # main file name
362 warn "generating main file $file_name\n" if( $state->{verbose});
363 open( my $out, '>', $file_name) or die "cannot create main file '$file_name': $!";
364 $state->{out}= $out;
365 $twig_options->{twig_print_outside_roots}= $out;
366 $twig_options->{start_tag_handlers}= { $state->{cond} => sub { $_->set_att( '#in_fragment' => 1); } };
367 }
368
369 $twig_options->{twig_roots}= { $state->{cond} => sub { dump_elt( @_, $state); } };
370 return $twig_options;
371 }
372  
373 sub dump_elt
374 { my( $t, $elt, $state)= @_;
375 $state->{seq_nb}++;
376 $state->{elt}= $elt;
377  
378 my $file_name= $state->file_name;
379 warn "generating $file_name\n" if( $state->{verbose});
380  
381 my $fragment= XML::Twig->new();
382 $fragment->{twig_xmldecl} = $t->{twig_xmldecl};
383 $fragment->{twig_doctype} = $t->{twig_doctype};
384 $fragment->{twig_dtd} = $t->{twig_dtd};
385
386 if( !$state->{no_pis})
387 { # if we are still within a fragment, just replace the element by the PI
388 # otherwise print it to the main document
389 my $include= $state->include( $file_name);
390  
391 $elt->del_att( '#in_fragment');
392
393 if( $elt->inherited_att( '#in_fragment'))
394 { $elt->parent( '*[@#in_fragment="1"]')->set_att( '#has_subdocs' => 1);
395 $include->replace( $elt);
396 }
397 else
398 { $elt->cut;
399 $include->print( $state->{out});
400 }
401 }
402 else
403 { $elt->cut; }
404
405 $fragment->set_root( $elt);
406 open( my $out, '>', $file_name) or die "cannot create output file '$file_name': $!";
407 #if( $state->{xml_declaration}) { warn "c1"; print {$out} $state->{xml_declaration}, "\n"; }
408 #if( $fragment->{xml_decl}) { warn "c2"; print {$out} $fragment->xml_decl, "\n"; }
409 $fragment->set_keep_encoding( 1);
410 $fragment->print( $out);
411 close $out;
412 }
413
414 sub end_file
415 { my( $t, $state)= @_;
416 unless( $state->{no_pi})
417 { close $state->{out}; }
418 }
419  
420
421 # for Getop::Std
422 sub HELP_MESSAGE { return $USAGE; }
423 sub VERSION_MESSAGE { return $VERSION; }
424  
425 package xml_split::state;
426  
427 sub new
428 { my( $ref, $options)= @_;
429 my $state= bless $options, $ref;
430 $state->{seq_nb}=0;
431 return $state;
432 }
433  
434 sub file_name
435 { my( $state)= @_;
436 my $nb= sprintf( "%0$state->{nb_digits}d", $state->{seq_nb});
437 my $file_name= "$state->{base}-$nb$state->{ext}";
438 $file_name =~ s{\\}{/}g;
439 return $file_name;
440 }
441  
442 sub main_file_name
443 { my( $state)= @_;
444 my $nb= sprintf( "%0$state->{nb_digits}d", 0);
445 my $file_name= "$state->{base}-$nb$state->{ext}";
446 return $file_name;
447 }
448 1;
449  
450 ###################################################################################
451 # #
452 # state when using XML::Parser #
453 # #
454 ###################################################################################
455  
456 package xml_split::state::parser;
457 import xml_split::state;
458 use base 'xml_split::state';
459  
460 sub include
461 { my( $state, $file_name)= @_;
462 if( $state->{xinclude})
463 { return qq{<xi:include href="$file_name" />}; }
464 else
465 { return qq{<?merge subdocs = 0 :$file_name?>}; }
466 }
467 1;
468  
469 ###################################################################################
470 # #
471 # state when using XML::Twig #
472 # #
473 ###################################################################################
474  
475 package xml_split::state::twig;
476 import xml_split::state;
477 use base 'xml_split::state';
478  
479 sub include
480 { my( $state, $file_name)= @_;
481 my $include;
482 my $subdocs= $state->{elt}->att( '#has_subdocs') || 0;
483 if( $state->{xinclude})
484 { $include= XML::Twig::Elt->new( 'xi:include', { href => $file_name });
485 if( $subdocs) { $include->set_att( subdocs => 1); }
486 }
487 else
488 {
489 $include= XML::Twig::Elt->new( '#PI')
490 ->set_pi( merge => " subdocs = $subdocs :$file_name");
491 }
492 return $include;
493 }
494  
495 1;
496  
497 package main;
498  
499 __END__
« tools/xml_pp/xml_pp ↑ Summary check_optional_modules »