#!/usr/bin/env perl

# This program was written by Robin Lee Powell, primarily on 16 Apr
# 2010.
# 
# It is hereby placed in the public domain, to the fullest extent
# permitted by low.

use strict;
use warnings;

use Data::Dumper;

$| = 1;

unless (@ARGV >= 1) {
  print STDERR qq(Usage:
  $0 xpath [filename]

  If no filename is given, supply XML on STDIN.
  );
  exit;
}

my $xpath = shift(@ARGV);

use XML::LibXML;

my $parser = XML::LibXML->new();

my $dom;

if( @ARGV )
{
  my $file = shift(@ARGV);
  $dom = $parser->load_html( location => $file,
    recover => 2, suppress_warnings => 1, suppress_errors => 1);

  if( ! $dom )
  {
    print "Whoops.  Parsing failed; retrying with errors turned on, and exiting, in case this will be of some help.  You might want to try running your HTML through tidy ( http://www.w3.org/People/Raggett/tidy/ )\n";
    $dom = $parser->load_html( location => $file );
    exit 1;
  }
} else {
  $dom = $parser->load_html( IO => \*STDIN,
    recover => 2, suppress_warnings => 1, suppress_errors => 1);

  if( ! $dom )
  {
    print "Whoops.  Parsing failed; can't retry with STDIN.  Please put the data in a file and run against the file name.\n";
    exit 1;
  }
}

#$Data::Dumper::Maxdepth=5;
#print "dom: ".Dumper(\$dom->documentElement()->toString())."\n";
#exit;

my $xpc = XML::LibXML::XPathContext->new( $dom->documentElement() );
my @results = $xpc->findnodes($xpath);

foreach my $result (@results) {
  print "\n\n--------------------------------------------------------------------------\n\n";
  print $result->toString(2);
}

print "\n\n--------------------------------------------------------------------------\n\n";
