| 1 | #!/usr/local/bin/perl -w | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use XML::Validate qw(); | ||
| 5 | use Getopt::Long qw(); | ||
| 6 | require Log::Trace; | ||
| 7 | |||
| 8 | use vars qw($VERSION); | ||
| 9 | ($VERSION) = ('$Revision: 1.10 $' =~ /([\d\.]+)/ ); | ||
| 10 | |||
| 11 | my $assert_invalid = 0; | ||
| 12 | my $help = 0; | ||
| 13 | my $tracing = 0; | ||
| 14 | my $deep_tracing = 0; | ||
| 15 | my $backend = 'BestAvailable'; | ||
| 16 | Getopt::Long::GetOptions( | ||
| 17 | 't' => \$tracing, | ||
| 18 | 'T' => \$deep_tracing, | ||
| 19 | 'assert-invalid' => \$assert_invalid, | ||
| 20 | 'backend:s' => \$backend, | ||
| 21 | 'help' => \$help | ||
| 22 | ); | ||
| 23 | |||
| 24 | import Log::Trace 'print' if $tracing; | ||
| 25 | import Log::Trace 'print' => { Deep => 1 } if $deep_tracing; | ||
| 26 | |||
| 27 | my @files; | ||
| 28 | while (my $mask = shift) { | ||
| 29 | push @files, glob($mask); | ||
| 30 | } | ||
| 31 | if ($help || @files < 1) { | ||
| 32 | require Pod::Usage; | ||
| 33 | Pod::Usage::pod2usage(-verbose => 2); | ||
| 34 | } | ||
| 35 | |||
| 36 | my $validator = new XML::Validate(Type => $backend); | ||
| 37 | |||
| 38 | my %errors; | ||
| 39 | |||
| 40 | for my $file (@files) { | ||
| 41 | open(FH,$file) || die "Unable to open file handle FH for file '$file': $!\n"; | ||
| 42 | local $/ = undef; | ||
| 43 | my $xml = <FH>; | ||
| 44 | close(FH) || warn "Unable to close file handle FH for file '$file': $!\n"; | ||
| 45 | |||
| 46 | if (my $tree = $validator->validate($xml)) { | ||
| 47 | $errors{$file} = ''; | ||
| 48 | } else { | ||
| 49 | if ($validator->last_error()->{message}) { | ||
| 50 | $errors{$file} = sprintf("%s at %d:%d",@{$validator->last_error()}{'message','line','column'}); | ||
| 51 | } else{ | ||
| 52 | $errors{$file} = 'Unknown error'; | ||
| 53 | } | ||
| 54 | if (length($errors{$file}) > 0) { | ||
| 55 | $errors{$file} =~ s/(\n|\r|\cM)/ /gi; | ||
| 56 | } | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | print "1..".@files."\n"; | ||
| 61 | for my $file (@files) { | ||
| 62 | if ($errors{$file}) { | ||
| 63 | print(($assert_invalid ? '' : 'not '). | ||
| 64 | "ok - $file - $errors{$file}\n"); | ||
| 65 | } else { | ||
| 66 | print(($assert_invalid ? 'not ' : ''). | ||
| 67 | "ok - $file\n"); | ||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | |||
| 72 | =pod | ||
| 73 | |||
| 74 | =head1 NAME | ||
| 75 | |||
| 76 | validxml - Command-line driver for XML::Validate. | ||
| 77 | |||
| 78 | =head1 SYNOPSIS | ||
| 79 | |||
| 80 | validxml *.xml | ||
| 81 | validxml --assert-invalid *.xml | ||
| 82 | |||
| 83 | =head1 DESCRIPTION | ||
| 84 | |||
| 85 | Command-line driver for XML::Validate using the 'BestAvailable' processing | ||
| 86 | type. | ||
| 87 | |||
| 88 | =head2 Commandline Options | ||
| 89 | |||
| 90 | =over 4 | ||
| 91 | |||
| 92 | =item --assert-invalid | ||
| 93 | |||
| 94 | Swap the ok/not ok so invalid things are OK - still output the validation | ||
| 95 | error) - this is useful for schema "unit tests". | ||
| 96 | |||
| 97 | =item --backend [validator type] | ||
| 98 | |||
| 99 | Specify an C<XML::Validate> backend to use (e.g LibXML, Xerces). Defaults to | ||
| 100 | I<BestAvailable>. | ||
| 101 | |||
| 102 | =back | ||
| 103 | |||
| 104 | =head2 Output | ||
| 105 | |||
| 106 | Output is Test::Harness compatible. | ||
| 107 | |||
| 108 | 1..scalar @files | ||
| 109 | ok - filename/not ok - filename - validation error | ||
| 110 | |||
| 111 | =head1 VERSION | ||
| 112 | |||
| 113 | $Revision: 1.10 $ | ||
| 114 | |||
| 115 | $Id: validxml.pl,v 1.10 2006/04/07 09:43:10 johnl Exp $ | ||
| 116 | |||
| 117 | =head1 AUTHOR | ||
| 118 | |||
| 119 | Nicola Worthington | ||
| 120 | |||
| 121 | $Author: johnl $ | ||
| 122 | |||
| 123 | =head1 COPYRIGHT | ||
| 124 | |||
| 125 | (c) BBC 2005. This program is free software; you can redistribute it and/or modify it under the GNU GPL. | ||
| 126 | See the file COPYING in this distribution, or http://www.gnu.org/licenses/gpl.txt | ||
| 127 | |||
| 128 | =cut | ||
| 129 | |||
| 130 | __END__ |