root/trunk/midgard/tools/yamp/sort_by_guid.pl

Revision 6955, 2.9 kB (checked in by martin, 6 years ago)

Updated comments and credits.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1 #!/usr/bin/perl -w
2
3 # $Header$
4
5 #-----------------------------------------------------------------------------------------------------------
6 #       File: sort_by_guid.pl
7 #
8 #               Parser for xml files exported by repligard.  Sorts the data by GUID number.
9 #
10 #       Originally by Tim Higgs (tim@cwa.co.nz).
11 #       Also Torben (torben@nehmer.net) and Martin Langhoff (martin@nzl.com.ar)
12 #
13 #-----------------------------------------------------------------------------------------------------------
14
15 use strict;
16 use utf8;
17 use XML::Parser::Expat;
18
19 # Get the file name from the command line
20 my $file = $ARGV[0] || die( &usage );
21
22 my %hosts;
23
24 # ID of the current record
25 my $id = "";
26
27 # Content for the current record
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 # Set up the handlers for the parser
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"; # if keys( %hosts );
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 # Only replace the file if we happened to have done something
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         # we did nothing, leave the original alone, delete the tmp
69         unlink  "$file.sort" || die "could not delete $file.sort : $!";
70 }
71
72 #-----------------------------------------------------------------------------------------------------------
73 #       Sub routines
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                 # Get it's ID
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
Note: See TracBrowser for help on using the browser.