#!/usr/bin/env perl
#
#  xml-xml: read any well-formed xml document and prints it to stdout
#           in a easy-to-read form, resolving all external entities.
#
#  If the xml can be validated against a dtd or schema this will be done.
#  It also has the feature of using a standard order for all parameters
#  so that two equivalent xml documents will come out exactly alike even
#  if their form on input looks quite different.
#
#  Richard T. Jones
#  September 15, 2003
#  ------------------

use XML::Xerces;

sub Usage()
{
   print <<eoi;

Usage: xml-xml myfile.xml

reads any well-formed xml document and prints the contents to stdout
in a easy-to-read form, resolving all external entities.

eoi
}

if (@ARGV != 1) {
   Usage();
   exit 1;
}
$infile = $ARGV[0];
if (open(HDDM,"<$infile") == 0) {
   die "Unable to open input file $infile\n";
}

# check the result for correctness and pretty-print it

$parser = XML::Xerces::XercesDOMParser->new();
$parser->setValidationScheme (XML::Xerces::XercesDOMParser::Val_Auto);
$parser->setDoNamespaces (1);
$parser->setCreateEntityReferenceNodes(0);
$parser->setDoSchema (1);

$ERROR_HANDLER = XML::Xerces::PerlErrorHandler->new();
$parser->setErrorHandler($ERROR_HANDLER);
eval {$parser->parse ($infile)};
XML::Xerces::error($@) if ($@);

$doc = $parser->getDocument();

$impl = XML::Xerces::DOMImplementationRegistry::getDOMImplementation('LS');
$writer = $impl->createDOMWriter();
if ($writer->canSetFeature('format-pretty-print',1)) {
  $writer->setFeature('format-pretty-print',1);
}
$target = XML::Xerces::StdOutFormatTarget->new();
$writer->writeNode($target,$doc);

exit 0;