1#!/usr/bin/perl -w
2##
3
4=pod
5
6=head1 NAME
7
8xmlfilter.pl - A command line client program using Net::DRI towards the
9.NO EPP registry.
10
11=head1 DESCRIPTION
12
13This program is a small filter utility which acts an xml_pp (pretty-print)
14function for the xml-dump output coming from Net::DRI Transport.pm log
15function.
16
17The format in input is a long line like this:
18
19 "2008-02-20 10:22:19.092865 C<=S [SOCKET_INET-92047-1203499339055994]\
20  <?xml version="1.0" encoding="UTF-8" standalone="no" ?><epp xmlns=" .\
21  .....</epp>"
22
23This filter separates the date/time part and the xml-part and prints
24a pretty-print of the xml.
25
26if the -s (skip) option is set, it suppresses the greeting, login and
27logout sequences.
28
29=head1 COPYRIGHT
30
31Copyright (c) 2008 UNINETT Norid AS, E<lt>http://www.norid.noE<gt>,
32Trond Haugen E<lt>info@norid.noE<gt>
33All rights reserved.
34
35This program is free software; you can redistribute it and/or modify
36it under the terms of the GNU General Public License as published by
37the Free Software Foundation; either version 2 of the License, or
38(at your option) any later version.
39
40See the LICENSE file that comes with this distribution for more details.
41
42=head1 AUTHOR
43
44Trond Haugen, E<lt>info@norid.noE<gt>
45
46=cut
47
48use strict;
49use vars qw($opt_s);
50use Getopt::Std;
51
52# There are \0 chars in input, set binmode on stdin
53binmode STDIN;
54binmode STDOUT;
55use XML::Twig;
56
57&getopts('s');
58
59my $tw = new XML::Twig(
60    pretty_print    => 'indented',
61    output_encoding => 'UTF-8',
62);
63my $fh = \*STDOUT;
64
65my $skipnext;
66
67while (<>) {
68
69    #chomp;
70    my $tp = $_;
71
72    # .NO: changed the log output to a pretty-printed one
73
74    if ( $tp =~ m|(^20\d\d.+C.+)(\<\?xml .+$)|msx ) {
75        my $t = $1;
76        my $s = $2;
77
78        #print "t: $t\n";
79        #print "s: $s\n\n\n";
80
81        if ($opt_s) {
82            if ($skipnext) {
83                $skipnext = undef;
84                next;
85            }
86
87            # skip dump of greeting, login, logout
88            if ( $s =~ m/<greeting>/gmx ) {
89                next;
90            }
91            if ( $s =~ m|<command><login>|gmx ) {
92                ++$skipnext;
93                next;
94            }
95            if ( $s =~ m|<command><logout/>|gmx ) {
96                ++$skipnext;
97                next;
98            }
99
100        }
101        $tw->parse($s);
102
103        $t = "";    # if ($opt_s);   # No timestamps either if skip is on
104
105        my $tpp = $t . $tw->sprint;
106        print {$fh} $tpp . "\n---\n\n";
107    }
108    else {
109        print {$fh} $tp;
110    }
111}
112