Debian Codegraph @

bin/xmlsort

raw
1 #!/usr/bin/perl -w
2 ##############################################################################
3 # $Id: xmlsort,v 1.1.1.1 2002/06/14 20:40:12 grantm Exp $
4 #
5 # Title: xmlsort
6 #
7 # Author: Grant McLean <[email protected]>
8 #
9 # Script for sorting 'records' in XML files. Use -h option for help.
10 #
11
12 use strict;
13
14 use Getopt::Long;
15 use Pod::Usage;
16
17 use IO::File;
18 use XML::SAX::ParserFactory;
19 use XML::SAX::Writer;
20 use XML::Filter::Sort;
21
22
23 ##############################################################################
24 # Handle command line parameters
25 #
26
27 my %opt = ();
28
29 GetOptions(\%opt, 'r=s', 'k=s', 'i', 's', 't=s', 'm=s', 'h') ||
30 pod2usage(0);
31
32 pod2usage({-verbose => 2, -exitval => 0}) if($opt{h});
33
34 pod2usage(0) unless($opt{r});
35
36 my $filename = shift || '-';
37
38
39 ##############################################################################
40 # Build up the list of options for constructing the sort filter object.
41 #
42
43 my %sort_opts = ( Record => $opt{r} );
44
45 $sort_opts{Keys} = $opt{k} if($opt{k});
46 $sort_opts{IgnoreCase} = 1 if($opt{i});
47 $sort_opts{NormaliseKeySpace} = 1 if($opt{s});
48 $sort_opts{TempDir} = $opt{t} if($opt{t});
49 $sort_opts{MaxMem} = $opt{m} if($opt{m});
50
51
52 ##############################################################################
53 # Create a filter pipeline and 'run' it
54 #
55
56 my $writer = XML::SAX::Writer->new( Output => \*STDOUT );
57 my $sorter = XML::Filter::Sort->new( %sort_opts, Handler => $writer );
58 my $parser = XML::SAX::ParserFactory->parser(Handler => $sorter);
59
60 my $fd = IO::File->new("<$filename") || die "$!";
61
62 $parser->parse_file($fd);
63
64 print "\n";
65
66 exit;
67
68 __END__
69
70 =head1 NAME
71
72 xmlsort - sorts 'records' in XML files
73
74 =head1 SYNOPSIS
75
76 xmlsort -r=<recordname> [ <other options> ] [ <filename> ]
77
78 Options:
79 -r <name> name of the elements to be sorted
80 -k <keys> child nodes to be used as sort keys
81 -i ignore case when sorting
82 -s normalise whitespace when comparing sort keys
83 -t <dir> buffer records to named directory rather than in memory
84 -m <bytes> set memory chunk size for disk buffering
85 -h help - display the full documentation
86
87 Example:
88 xmlsort -r 'person' -k 'lastname;firstname' -i -s in.xml >out.xml
89
90 =head1 DESCRIPTION
91
92 This script takes an XML document either on STDIN or from a named file and
93 writes a sorted version of the file to STDOUT. The C<-r> option should be used
94 to identify 'records' in the document - the bits you want sorted. Elements
95 before and after the records will be unaffected by the sort.
96
97 =head1 OPTIONS
98
99 Here is a brief summary of the command line options (and the XML::Filter::Sort
100 options which they correspond to). For more details see L<XML::Filter::Sort>.
101
102 =over 4
103
104 =item -r <recordname> (Record)
105
106 The name of the elements to be sorted. This can be a simple element name like
107 C<'person'> or a pathname like C<'employees/person'> (only person elements
108 contained directly within an employees element).
109
110 =item -k <keys> (Keys)
111
112 Semicolon separated list of elements (or attributes) within a record which
113 should be used as sort keys. Each key can optionally be followed by 'alpha' or
114 'num' to indicate alphanumeric of numeric sorting and 'asc' or 'desc' for
115 ascending or descending order (eg: -k 'lastname;firstname;age,n,d').
116
117 =item -i (IgnoreCase)
118
119 This option makes sort comparisons case insensitive.
120
121 =item -s (NormaliseKeySpace)
122
123 By default all whitespace in the sort key elements is considered significant.
124 Specifying -s will case leading and trailing whitespace to be stripped and
125 internal whitespace runs to be collapsed to a single space.
126
127 =item -t <directory> (TempDir)
128
129 When sorting large documents, it may be prudent to use disk buffering rather
130 than memory buffering. This option allows you to specify where temporary
131 files should be written.
132
133 =item -m <bytes> (MaxMem)
134
135 If you use the -t option to enable disk buffering, records will be collected
136 in memory in 'chunks' of up to about 10 megabytes before being sorted and
137 spooled to temporary files. This option allows you to specify a larger
138 chunk size. A suffix of K or M indicates kilobytes or megabytes respectively.
139
140 =back
141
142 =head1 SEE ALSO
143
144 This script uses the following modules:
145
146 XML::SAX::ParserFactory
147 XML::Filter::Sort
148 XML::SAX::Writer
149
150 =head1 AUTHOR
151
152 Grant McLean <[email protected]>
153
154 =head1 COPYRIGHT
155
156 Copyright (c) 2002 Grant McLean. All rights reserved. This program is
157 free software; you can redistribute it and/or modify it under the same terms
158 as Perl itself.
159
160 =cut
161
162
163