#!/usr/bin/perl -w use strict; use Getopt::Std; use Pod::Usage; use IO::File; use XML::SAX::Expat; use XML::SAX::Writer; use XML::Handler::Dtd2Html; my %opts; getopts('bCDdfHhMvl:p:s:t:o:x:Z', \%opts) or pod2usage(-verbose => 0); if ($opts{h}) { pod2usage(-verbose => 0); } if ($opts{v}) { print "$0\n"; print "XML::Handler::Dtd2Html Version $XML::Handler::Dtd2Html::VERSION\n"; exit(0); } my $file = $ARGV[0]; die "No input file\n" unless (defined $file); warn "Don't use directly a DTD file (see the embedded pod or the README).\n" if ($file =~ /\.dtd$/i); my $io = new IO::File($file,"r"); die "Can't open $file ($!)\n" unless (defined $io); if (exists $opts{d}) { if (exists $opts{o}) { my $outfile = $opts{o}; open STDOUT, "> $outfile" or die "can't open $outfile ($!).\n"; } my $handler = new XML::SAX::Writer(Writer => 'DtdWriter', Output => \*STDOUT); my $parser = new XML::SAX::Expat(Handler => $handler); $parser->set_feature("http://xml.org/sax/features/external-general-entities", 1); $parser->parse(Source => {ByteStream => $io}); } else { my $handler = new XML::Handler::Dtd2Html(); my $parser = new XML::SAX::Expat(Handler => $handler); $parser->set_feature("http://xml.org/sax/features/external-general-entities", 1); my $doc = $parser->parse(Source => {ByteStream => $io}); my $outfile; if (exists $opts{o}) { $outfile = $opts{o}; } else { my $root = $doc->{root_name}; $root =~ s/[:\.\-]/_/g; $outfile = "dtd_" . $root; } my @examples = (); @examples = split /\s+/, $opts{x} if (exists $opts{x}); if ($opts{b}) { bless($doc, "XML::Handler::Dtd2Html::DocumentBook"); } elsif ($opts{f}) { bless($doc, "XML::Handler::Dtd2Html::DocumentFrame"); } $doc->GenerateHTML( outfile => $outfile, title => $opts{t}, css => $opts{s}, examples => \@examples, flag_comment => !exists($opts{C}), flag_date => !exists($opts{D}), flag_href => exists($opts{H}), flag_multi => exists($opts{M}), flag_zombi => exists($opts{Z}), language => $opts{l}, path_tmpl => $opts{p} ); } package DtdWriter; use base qw(XML::SAX::Writer::XML); sub start_element {} sub end_element {} sub characters {} sub processing_instruction {} sub ignorable_whitespace {} sub comment {} sub start_cdata {} sub end_cdata {} sub start_entity {} sub end_entity {} sub xml_decl {} sub start_dtd { my $self = shift; $self->{BufferDTD} = ''; } sub end_dtd { my $self = shift; my $dtd = $self->{BufferDTD}; $dtd = $self->{Encoder}->convert($dtd); $self->{Consumer}->output($dtd); $self->{BufferDTD} = ''; } __END__ =head1 NAME dtd2html - Generate a HTML documentation from a DTD =head1 SYNOPSIS dtd2html [B<-b> | B<-f> | B<-d>] [B<-C> | B<-M>] [B<-HZ>] [B<-o> I] [B<-s> I