script/dtd2db.pl untested

filter: Statementstmt 0.0% 0 / 65 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 125 CC 1 SCAR 6.9
CC
1
Cov
0%
start_element
2.0
end_element
2.0
characters
2.0
CRAP 2.0
1 #!/usr/local/bin/perl -w
2
3 use strict;
4
5 use Getopt::Std;
6 use Pod::Usage;
7 use IO::File;
8 use XML::SAX::Expat;
9 use XML::SAX::Writer;
10 use XML::Handler::Dtd2DocBook;
11
12 my %opts;
13 getopts('CDdHhMvl:p:t:o:x:Z', \%opts)
14 or pod2usage(-verbose => 0);
15
16 if ($opts{h}) {
17 pod2usage(-verbose => 0);
18 }
19
20 if ($opts{v}) {
21 print "$0\n";
22 print "XML::Handler::Dtd2DocBook Version $XML::Handler::Dtd2DocBook::VERSION\n";
23 exit(0);
24 }
25
26 my $file = $ARGV[0];
27 die "No input file\n"
28 unless (defined $file);
29 warn "Don't use directly a DTD file (see the embedded pod or the README).\n"
30 if ($file =~ /\.dtd$/i);
31 my $io = new IO::File($file,"r");
32 die "Can't open $file ($!)\n"
33 unless (defined $io);
34
35 if (exists $opts{d}) {
36 if (exists $opts{o}) {
37 my $outfile = $opts{o};
38 open STDOUT, "> $outfile"
39 or die "can't open $outfile ($!).\n";
40 }
41 my $handler = new XML::SAX::Writer(Writer => 'DtdWriter', Output => \*STDOUT);
42 my $parser = new XML::SAX::Expat(Handler => $handler);
43 $parser->set_feature("http://xml.org/sax/features/external-general-entities", 1);
44 $parser->parse(Source => {ByteStream => $io});
45 } else {
46 my $handler = new XML::Handler::Dtd2DocBook();
47 my $parser = new XML::SAX::Expat(Handler => $handler);
48 $parser->set_feature("http://xml.org/sax/features/external-general-entities", 1);
49 my $doc = $parser->parse(Source => {ByteStream => $io});
50
51 my $outfile;
52 if (exists $opts{o}) {
53 $outfile = $opts{o};
54 } else {
55 my $root = $doc->{root_name};
56 $root =~ s/[:\.\-]/_/g;
57 $outfile = "dtd_" . $root;
58 }
59
60 my @examples = ();
61 @examples = split /\s+/, $opts{x} if (exists $opts{x});
62
63 $doc->GenerateDocBook(
64 outfile => $outfile,
65 title => $opts{t},
66 examples => \@examples,
67 flag_comment => !exists($opts{C}),
68 flag_date => !exists($opts{D}),
69 flag_href => exists($opts{H}),
70 flag_multi => exists($opts{M}),
71 flag_zombi => exists($opts{Z}),
72 language => $opts{l},
73 path_tmpl => $opts{p}
74 );
75 }
76
77 package DtdWriter;
78
79 use base qw(XML::SAX::Writer::XML);
80
81 sub start_element {}
82 sub end_element {}
83 sub characters {}
84 sub processing_instruction {}
85 sub ignorable_whitespace {}
86 sub comment {}
87 sub start_cdata {}
88 sub end_cdata {}
89 sub start_entity {}
90 sub end_entity {}
91 sub xml_decl {}
92
93 sub start_dtd {
94 my $self = shift;
95
96 $self->{BufferDTD} = '';
97 }
98
99 sub end_dtd {
100 my $self = shift;
101
102 my $dtd = $self->{BufferDTD};
103 $dtd = $self->{Encoder}->convert($dtd);
104 $self->{Consumer}->output($dtd);
105 $self->{BufferDTD} = '';
106 }
107
108 __END__