| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
use strict; |
|---|
| 16 |
use utf8; |
|---|
| 17 |
use XML::Parser::Expat; |
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
my $file = $ARGV[0] || die( &usage ); |
|---|
| 21 |
|
|---|
| 22 |
my %hosts; |
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
my $id = ""; |
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
my $content = ""; |
|---|
| 29 |
|
|---|
| 30 |
my $start = ""; |
|---|
| 31 |
my $end = ""; |
|---|
| 32 |
|
|---|
| 33 |
my $current_element_name = ""; |
|---|
| 34 |
|
|---|
| 35 |
my @element_names = ( "HOST", "PAGE", "PERSON" ); |
|---|
| 36 |
|
|---|
| 37 |
my $parser = new XML::Parser::Expat; |
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
$parser->setHandlers( 'Start' => \&start_handler, |
|---|
| 41 |
'End' => \&end_handler, |
|---|
| 42 |
'Default' => \&default_handler); |
|---|
| 43 |
|
|---|
| 44 |
open(FILE, $file) || die "Couldn't open file $!\n"; |
|---|
| 45 |
|
|---|
| 46 |
$parser->parse(*FILE); |
|---|
| 47 |
|
|---|
| 48 |
close(FILE); |
|---|
| 49 |
|
|---|
| 50 |
open (OUTFILE, ">$file.sorted") || die "Couldn't open $file.sorted $!\n"; |
|---|
| 51 |
|
|---|
| 52 |
print OUTFILE "$start\n"; |
|---|
| 53 |
foreach my $key ( sort( keys( %hosts ) ) ){ |
|---|
| 54 |
print OUTFILE $hosts{$key}."\n"; |
|---|
| 55 |
} |
|---|
| 56 |
|
|---|
| 57 |
print OUTFILE "\n$end"; |
|---|
| 58 |
print OUTFILE "\n"; |
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
close OUTFILE; |
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
if (keys %hosts ){ |
|---|
| 65 |
unlink $file || die "could not delete $file : $!"; |
|---|
| 66 |
rename ("$file.sorted", "$file") or die "could not rename $file.sorted : $!"; |
|---|
| 67 |
} else { |
|---|
| 68 |
|
|---|
| 69 |
unlink "$file.sort" || die "could not delete $file.sort : $!"; |
|---|
| 70 |
} |
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
sub start_handler { |
|---|
| 77 |
my ($p, $element, %attributes) = @_; |
|---|
| 78 |
|
|---|
| 79 |
if( uc( $element ) eq "DATABASE" ){ |
|---|
| 80 |
$start = $content.$p->recognized_string(); |
|---|
| 81 |
} |
|---|
| 82 |
elsif( defined( $attributes{"id"} ) ){ |
|---|
| 83 |
|
|---|
| 84 |
$id = $attributes{"id"}; |
|---|
| 85 |
$content = $p->recognized_string(); |
|---|
| 86 |
$current_element_name = $element; |
|---|
| 87 |
} |
|---|
| 88 |
else{ |
|---|
| 89 |
$content .= $p->recognized_string(); |
|---|
| 90 |
} |
|---|
| 91 |
} |
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
sub end_handler { |
|---|
| 95 |
my ($p, $element) = @_; |
|---|
| 96 |
|
|---|
| 97 |
if( uc( $element ) eq "DATABASE" ){ |
|---|
| 98 |
$end = $content.$p->recognized_string(); |
|---|
| 99 |
} |
|---|
| 100 |
elsif( uc( $element ) eq uc( $current_element_name ) ){ |
|---|
| 101 |
$hosts{$id} = $content.$p->recognized_string(); |
|---|
| 102 |
$content = ""; |
|---|
| 103 |
$id = ""; |
|---|
| 104 |
$current_element_name = ""; |
|---|
| 105 |
} |
|---|
| 106 |
else{ |
|---|
| 107 |
$content .= $p->recognized_string(); |
|---|
| 108 |
} |
|---|
| 109 |
} |
|---|
| 110 |
|
|---|
| 111 |
sub default_handler { |
|---|
| 112 |
my ($p, $string) = @_; |
|---|
| 113 |
$content .= $string; |
|---|
| 114 |
} |
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
sub usage { |
|---|
| 118 |
print<<EOP; |
|---|
| 119 |
sort_by_guid.pl <filename> |
|---|
| 120 |
EOP |
|---|
| 121 |
} |
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
|
|---|