1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
#!/usr/local/bin/perl -w
# POD docs at end
use strict;
use Carp;
use Data::Stag qw(:all);
use Getopt::Long;
my $parser = "";
my $handler = "";
my $writer = "";
my $errhandler = "";
my $errf;
my $outf;
my $mapf;
my $tosql;
my $toxml;
my $toperl;
my $debug;
my $help;
my $color;
my $root_node;
GetOptions(
"help|h"=>\$help,
"root|r=s"=>\$root_node,
"parser|format|p=s" => \$parser,
"handler|writer|w=s" => \$writer,
"errhandler=s" => \$errhandler,
"errf|e=s" => \$errf,
"out|o=s" => \$outf,
"debug"=>\$debug,
"colour|color"=>\$color,
);
if ($help) {
system("perldoc $0");
exit 0;
}
$errhandler = Data::Stag->getformathandler($errhandler || 'xml');
$handler = Data::Stag->getformathandler($writer);
if ($handler->can("fh")) {
if ($outf) {
$handler->file($outf);
}
else {
$handler->fh(\*STDOUT);
}
}
if ($color) {
$handler->use_color(1);
}
if ($errf) {
$errhandler->file($errf);
}
else {
$errhandler->fh(\*STDERR);
}
my @files = @ARGV;
if (@files > 1) {
$root_node = 'set' unless $root_node;
}
$handler->start_event($root_node) if $root_node;
foreach my $fn (@files) {
my @pargs = (-file=>$fn, -format=>$parser, -handler=>$handler, -errhandler=>$errhandler);
if ($fn eq '-') {
if (!$parser) {
$parser = 'xml';
}
@pargs = (-format=>$parser, -handler=>$handler,
-fh=>\*STDIN, -errhandler=>$errhandler);
}
my $tree =
Data::Stag->parse(@pargs);
if ($toxml) {
print $tree->xml;
}
if ($toperl) {
print tree2perldump($tree);
}
}
if ($errf) {
$errhandler->finish;
}
$handler->end_event($root_node) if $root_node;
$handler->fh->close if $outf;
exit 0;
__END__
=head1 NAME
stag-parse - parses a file and fires events (e.g. sxpr to xml)
=head1 SYNOPSIS
# convert XML to IText
stag-parse -p xml -w itext file1.xml file2.xml
# use a custom parser/generator and a custom writer/generator
stag-parse -p MyMod::MyParser -w MyMod::MyWriter file.txt
=head1 DESCRIPTION
script wrapper for the Data::Stag modules
feeds in files into a parser object that generates nestarray events,
and feeds the events into a handler/writer class
=head1 ARGUMENTS
=over
=item -p|parser FORMAT
FORMAT is one of xml, sxpr or itext, or the name of a perl module
this is the class that parsers the input file(s) and generates stag
events
xml assumed as default
=item -w|writer FORMAT
FORMAT is one of xml, sxpr or itext, or the name of a perl module
this is the class that catches the events thrown by the parser; it can
be any class, but the class is typically a writer
xml assumed as default
=item -o|out FILE
the writer will use this file (defaults to STDOUT)
=item -e|errf FILE
file to store parse error handler output
=item -errhandler FORMAT/MODULE
FORMAT is one of xml, sxpr or itext, or the name of a perl module
all parse error events go to this module
=item -r|root NODE_NAME
if this is specified, NODE_NAME becomes the root of the stag tree, and
anything that was previously the root is placed below this.
this happens automatically if more than one file is parsed (because
there can only be one tree root)
=item -color
Works only if the output handler is able to provide ASCII-colors
(currently supported for itext and xml)
=back
=head1 SEE ALSO
L<Data::Stag>
This script is a wrapper for the method
Data::Stag->parse()
=cut
|