#!/usr/bin/perl -w # # $Id: xml-test.pl,v 2.2 2003/01/13 05:28:43 jmates Exp $ # # Copyright (c) 2001, Jeremy Mates. This script is free software; # you can redistribute it and/or modify it under the same terms as # Perl itself. # # Run perldoc(1) on this file for additional documentation. # ###################################################################### # # REQUIREMENTS require 5; use strict; ###################################################################### # # MODULES use Carp; # better error reporting use Getopt::Std; # command line option processing use XML::Checker::Parser; # validating XML parser ###################################################################### # # VARIABLES my $VERSION; ($VERSION = '$Revision: 2.2 $ ') =~ s/[^0-9.]//g; my (%opts, $handled); ###################################################################### # # MAIN # parse command-line options getopts('h?', \%opts); help() if exists $opts{'h'} or exists $opts{'?'}; # read from STDIN if no args left chomp(@ARGV = ) unless @ARGV; my $file = shift or help(); my $parser = XML::Checker::Parser->new(); eval { local $XML::Checker::FAIL = \&failure_handle; $parser->parsefile($file); }; if ($@) { # Either XML::Parser (expat) threw an exception or my_fail() died. # clean up error message and bail out if not handled... unless ($handled) { $@ =~ s/^\s+//; die "Error: $@"; } } exit; ###################################################################### # # SUBROUTINES sub failure_handle { my $code = shift; die XML::Checker::error_string($code, @_) if $code < 200; XML::Checker::print_error($code, @_); } # a generic help blarb sub help { print <<"HELP"; Usage: $0 [opts] file.xml XML document validity checker. Options for version $VERSION: -h/-? Display this message Run perldoc(1) on this script for additional documentation. HELP exit 1; } ###################################################################### # # DOCUMENTATION =head1 NAME xml-test.pl - XML document validity checker =head1 SYNOPSIS To check whether foo.xml is valid: $ xml-test.pl foo.xml =head1 DESCRIPTION This script simply attempts to parse the passed XML filename using the XML::Checker::Parser module. =head2 Normal Usage $ xml-test.pl [options] file.xml If no files are mentioned on the command line, this script will attempt to read filenames from STDIN. See L<"OPTIONS"> for details on the command line switches supported. =head1 OPTIONS This script currently supports the following command line switches: =over 4 =item B<-h>, B<-?> Prints a brief usage note about the script. =back =head1 BUGS =head2 Reporting Bugs Newer versions of this script may be available from: http://sial.org/code/perl/ If the bug is in the latest version, send a report to the author. Patches that fix problems or add new features are welcome. =head2 Known Issues No known bugs. =head1 SEE ALSO perl(1) =head1 AUTHOR Jeremy Mates, http://sial.org/contact/ =head1 COPYRIGHT Copyright (c) 2001, Jeremy Mates. This script is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =head1 VERSION $Id: xml-test.pl,v 2.2 2003/01/13 05:28:43 jmates Exp $ =cut