#!/usr/bin/perl -s
use XML::DT;
use XML::DTDParser "ParseDTDFile";

our ($dtd, $html);

my $filename = shift;

$dtd = 1 if ($filename =~ m!\.dtd$!);

if ($dtd) {
  my $file = ParseDTDFile($filename);

  print <<'PERL';
#!/usr/bin/perl -w
use XML::DT;

my $filename = shift;
my %handler = (
PERL


  for (keys %{$file}) {
    print "\t'$_' => sub {\n";
    print "\t\t#remember ", 
      join(", ", map { "\$v{$_}" } keys %{$file->{$_}{attributes}}), "\n" if exists($file->{$_}{attributes});
    print "\t},\n";
  }


  print <<'PERL';
);

print dt($filename, %handler);
PERL


### OLD WAY
#   $/ = ">";
#   while(<>) {
#     s/\n/ /g;
#     s/
/ /g;
#     s/^\s*//;
#     if (m/<!ELEMENT\s+([A-Za-z_-]+)/) {
#       $elem{$1} = [];
#     } elsif (m/<!ATTLIST\s+([A-Za-z_-]+)\s+(.*)\s*>/) {
#       my $elem = $1;
#       my $fields = $2;
#       while($fields =~ m!([A-Za-z_-]+)+.*?#(REQUIRED|IMPLIED)\s*!g) {
# 	push @{$elem{$elem}}, $1;
#       }
#     } else {
#       #print STDERR "Ignoring $_\n";
#       #---
#     }
#   }

#   print <<'EOH';
# #!/usr/bin/perl -w
# use XML::DT;


# my %handler = (
# EOH

#   for (keys %elem) {
#     print "\t'$_' => sub {},";
#     print " #remember ",join(", ",map {"'$_'"} @{$elem{$_}}) if @{$elem{$_}};
#     print "\n";
#   }

#   print <<'EOH';
# );
# print dt($filename, %handler);
# EOH

} else {
  if ($html) {
    mkdtskel ("-html", $filename, @ARGV);
  } else {
    mkdtskel ($filename, @ARGV);
  }
}

__END__

=head1 NAME

mkdtskel - Perl code skeleton generator to process XML files with XML::DT

=head1 SYNOPSIS

  mkdtskel <xmlfile>

  mkdtskel -dtd <dtdfile>

  mkdtskel -html <htmlfile>

=head1 DESCRIPTION

Use this command to prepare a skeleton file with basic code needed to
process your XML file with XML::DT; The command checks the element
names and for each one, the attributes. This information is described
on the generated file to remember the programmer.

=head1 SEE ALSO

XML::DT(1), mkdtdskel(1) and perl(1)

=cut
