Created
March 8, 2015 16:43
-
-
Save nanis/ce3e7f8c9b00be393d54 to your computer and use it in GitHub Desktop.
Fix malformed XML using Perl's XML::Parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
=for purpose | |
See http://www.nu42.com/2015/03/fix-malformed-xml-with-perl-xml-parser.html | |
for motivation. This script is the simplest possible implementation of an | |
idea. Not tested. Likely to contain bugs ;-) | |
=cut | |
=for license | |
You may use this code under the terms of The BSD 3-Clause License | |
see http://opensource.org/licenses/BSD-3-Clause | |
Copyright (c) 2015, A. Sinan Unur <nanis@cpan.org> | |
All rights reserved. | |
Redistribution and use in source and binary forms, with or without | |
modification, are permitted provided that the following conditions | |
are met: | |
1. Redistributions of source code must retain the above copyright | |
notice, this list of conditions and the following disclaimer. | |
2. Redistributions in binary form must reproduce the above copyright | |
notice, this list of conditions and the following disclaimer in the | |
documentation and/or other materials provided with the distribution. | |
3. Neither the name of the copyright holder nor the names of its | |
contributors may be used to endorse or promote products derived from | |
this software without specific prior written permission. | |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | |
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | |
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
POSSIBILITY OF SUCH DAMAGE. | |
=cut | |
use utf8; | |
use 5.020; | |
use strict; | |
use warnings; | |
use open qw(:std :utf8); | |
use Carp qw( croak ); | |
use Getopt::Long qw(GetOptionsFromArray); | |
use Path::Class; | |
use XML::Parser; | |
run(\@ARGV); | |
sub run { | |
my $argv = shift; | |
my $opt = parse_argv($argv); | |
my $xml = $opt->{input}->slurp(iomode => '<:raw'); | |
my $tries; | |
TRY: | |
for ($tries = 0; $tries < $opt->{limit}; $tries += 1) { | |
my $pos = try_parse( \$xml ); | |
last TRY if not defined ($pos); | |
$xml = join('<', | |
substr($xml, 0, $pos), | |
substr($xml, $pos + 1) | |
); | |
} | |
if ($tries < $opt->{limit}) { | |
warn "'$opt->{input}' well-formed after $tries corrections\n"; | |
} | |
else { | |
croak "'$opt->{input}' still malformed after $tries corrections\n"; | |
} | |
file($opt->{output})->spew(iomode => '>:raw', $xml); | |
return; | |
} | |
sub try_parse { | |
my $xml = shift; | |
my $parser = XML::Parser->new; | |
return if eval { | |
$parser->parse( $$xml ); | |
1; | |
}; | |
my $err = $@; | |
if ($err =~ m{invalid \s+ token .+? byte \s+ ([0-9]+) }x ) { | |
my $pos = $1; | |
if ( substr($$xml, $pos, 1) eq '<' ) { | |
return $pos; | |
} | |
} | |
croak "Unexpected XML::Parser exception: '$err'"; | |
} | |
sub parse_argv { | |
my $argv = shift; | |
my %opt = ( | |
limit => 1_000, | |
); | |
GetOptionsFromArray( | |
$argv, | |
\%opt, | |
'input=s', | |
'limit=i', | |
'output=s', | |
) or croak "Failed to parse command line options from '@$argv'\n"; | |
for my $arg (qw(input output)) { | |
next if defined $opt{ $arg }; | |
croak "Missing '$arg' command line argument"; | |
} | |
$opt{input} = file($opt{input})->resolve; # croaks on failure | |
return \%opt; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment