/usr/share/perl5/Data/Stag/BaseGenerator.pm is in libdata-stag-perl 0.14-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 | # $Id: BaseGenerator.pm,v 1.15 2004/12/21 02:26:25 cmungall Exp $
#
# Copyright (C) 2002 Chris Mungall <cjm@fruitfly.org>
#
# See also - http://stag.sourceforge.net
#
# This module is free software.
# You may distribute this module under the same terms as perl itself
package Data::Stag::BaseGenerator;
=head1 NAME
Data::Stag::BaseGenerator - base class for parsers and other event generators
=head1 SYNOPSIS
# writing the parser
package MyParser;
use base qw(Data::Stag::BaseGenerator);
sub parse_fh {
my ($self, $fh) = shift;
my $lnum = 0;
$self->start_event('data');
while (<$fh>) {
++$lnum;
$self->line_no($lnum);
# do stuff
$self->start_event('foo');
# ...
$self->event(blah=>5);
#
if (/incorrect_line/) {
$self->parse_err('line not in correct format');
}
# ...
$self->end_event('foo');
}
$self->pop_stack_to_depth(0);
}
1;
# using the parser
my $p = MyParser->new;
my $h = MyHandler->new; # see Data::Stag::BaseHandler
my $eh = Data::Stag->makehandler;
$p->handler($h);
$p->errhandler($eh);
$p->parse($file);
# result tree
print $h->stag->xml;
# write parse errs on standard err
printf \*STDERR $p->errhandler->stag->xml;
# using the parser from the command line
unix> stag-parse.pl -p MyParser -w xml -e err.xml > out.xml
# using the parser from the command line via intermediate handler
unix> stag-handle.pl -p MyParser -m MyHandler -w xml -e err.xml > out.xml
=cut
=head1 DESCRIPTION
This is the base class for all parsers and event generators
parsers/generators take some input (usually a filehandle, but a
generator could be a socket listener, for example) and fire stag
events
stag events are
=over
=item start_event NODENAME
=item evbody DATA
=item end_event NODENAME {optional}
=item event NODENAME DATA
=back
These events can be nested/hierarchical
If uncaught, these events are stacked into a stag tree, which can be
written as xml or one of the other stag formats
specialised handlers can be written to catch the events your parser
throws
For example, you may wish to write a pod parser that generates nested
events like this:
<pod>
<section>
<type>head1</type>
<name>NAME</name>
<text>Data::Stag - Structured Tags datastructures</text>
</section>
...
</pod>
(see the source for Data::Stag::PodParser for details)
You can write handlers that take the pod-xml and generate something -
for example HTML
parsers may encounter unexpected things along the way - they may throw
an exception, and fall over - or they may choose to fire an error
event. by default, error event streams are diverted to STDERR. You can
create your own error handlers
=head1 PUBLIC METHODS
=head3 new
Title: new
Args:
Return: L<Data::Stag::BaseGenerator>
Example:
CONSTRUCTOR
=head3 handler
Title: handler
Function: GET/SET ACCESSOR METHOD
Args: handler L<Data::Stag::BaseHandler> optional
Return: L<Data::Stag::BaseHandler>
Example: $p->handler(MyHandler->new);
each parser has a handler - all events generated are passed onto the
handler; the default handler simply sits there collecting events
=head3 errhandler
Title: errhandler
Function: GET/SET ACCESSOR METHOD
Args: handler L<Data::Stag::BaseHandler> optional
Return: L<Data::Stag::BaseHandler>
Example: $p->errhandler(Data::Stag->makehandler);
each parser has an error handler - if the parser encounters things it
does not expect, it can pass errors to the errorhandler
if no errorhandler is set, an XML event handler that writes to STDERR is used
=head3 cache_errors
Title: cache_errors
Args:
Return:
Example: $p->cache_errors
If this is called, all errors will be cached rather than written to STDERR
The error list can be accessed like this
$p->parse($fn);
@errs = $p->errhandler->stag->get_error;
=head2 parse
Example - $parser->parse($file1, $file2);
Returns -
Args - filenames str-LIST
parses a file
=head2 parse
Example - $parser->parse_fh($fh)
Returns -
Args - fh FILEHANDLE
parses an open filehandle
=cut
=head1 PROTECTED METHODS
These methods are only of interest if you are making your own
parser/generator class
=over
=item start_event NODENAME
=item evbody DATA
=item end_event NODENAME {optional}
=item event NODENAME DATA
=back
=head1 SEE ALSO
L<Data::Stag>
L<Data::Stag::BaseHandler>
=cut
use Exporter;
@ISA = qw(Exporter);
use Carp;
use FileHandle;
use Data::Stag::Util qw(rearrange);
use Data::Stag::null;
use strict qw(subs vars refs);
# Exceptions
sub throw {
my $self = shift;
confess("@_");
}
sub warn {
my $self = shift;
warn("@_");
}
#sub last_evcall_type {
# my $self = shift;
# $self->{_last_evcall_type} = shift if @_;
# return $self->{_last_evcall_type};
#}
sub stack {
my $self = shift;
$self->{_stack} = shift if @_;
$self->{_stack} = [] unless $self->{_stack};
return $self->{_stack};
}
sub stack_top {
my $self = shift;
$self->stack->[-1];
}
sub push_stack {
my $self = shift;
push(@{$self->stack}, @_);
}
sub pop_stack {
my $self = shift;
my $top = $self->stack_top;
$self->end_event($top);
$top;
}
sub pop_stack_to_depth {
my $self = shift;
my $depth = shift;
while ($depth < $self->stack_depth) {
$self->end_event;
}
}
sub pop_all {
my $self = shift;
$self->pop_stack_to_depth(0);
}
sub stack_depth {
my $self = shift;
scalar(@{$self->stack});
}
*error_list = \&messages;
sub message {
my $self = shift;
my $msg = shift;
unless (ref($msg)) {
$msg =
{msg=>$msg,
line=>$self->line,
line_no=>$self->line_no,
file=>$self->file};
}
push(@{$self->messages},
$msg);
}
sub new {
my ($class, $init_h) = @_;
my $self = {};
$self->{handler} = Data::Stag::null->new;
if ($init_h) {
map {$self->{$_} = $init_h->{$_}} keys %$init_h;
}
bless $self, $class;
$self->init if $self->can("init");
$self;
}
sub load_module {
my $self = shift;
my $classname = shift;
my $mod = $classname;
$mod =~ s/::/\//g;
if ($main::{"_<$mod.pm"}) {
}
else {
require "$mod.pm";
}
}
sub modulemap {
my $self = shift;
$self->{_modulemap} = shift if @_;
return $self->{_modulemap};
}
sub handler {
my $self = shift;
if (@_) {
my $h = shift;
if ($h && !ref($h)) {
my $base = "Data::Stag:";
my $mm = $self->modulemap;
if ($mm && $mm->{$h}) {
$h = $mm->{$h};
}
$h =~ s/^xml$/$base:XMLWriter/;
$h =~ s/^perl$/$base:PerlWriter/;
$h =~ s/^sxpr$/$base:SxprWriter/;
$h =~ s/^itext$/$base:ITextWriter/;
$h =~ s/^graph$/$base:GraphWriter/;
$self->load_module($h);
$h = $h->new;
if ($h->can("fh")) {
$h->fh(\*STDOUT);
}
}
$self->{_handler} = $h;
if (!$h->errhandler && $self->errhandler) {
$h->errhandler($self->errhandler);
}
}
# return $self->{_handler} || Data::Stag::null->new();
return $self->{_handler};
}
sub cache_errors {
my $self = shift;
return $self->errhandler(Data::Stag->makehandler);
}
sub errhandler {
my $self = shift;
if (@_) {
$self->{errhandler} = shift;
}
return $self->{errhandler};
}
sub err_event {
my $self = shift;
if (!$self->errhandler) {
$self->errhandler(Data::Stag->getformathandler('xml'));
$self->errhandler->fh(\*STDERR);
# my $estag = Data::Stag->new(@_);
# eval {
# confess;
# };
# $estag->set_stacktrace($@);
# print STDERR $estag->xml;
# exit 1;
}
if (!$self->errhandler->depth) {
$self->errhandler->start_event("error_eventset");
}
$self->errhandler->event(@_);
return;
}
sub err {
my $self = shift;
my $err = shift;
if (ref($err)) {
$self->throw("Bad error msg $err - must not by ref");
}
$self->err_event(message=>$err);
return;
}
sub parse_err {
my $self = shift;
my $err = shift || '';
if (ref($err)) {
$self->throw("Bad error msg $err - must not by ref");
}
my @tags = ([message=>$err],[file=>$self->file]);
my $line = $self->line;
push(@tags, [line=>$line]) if defined $line;
my $line_no = $self->line_no;
push(@tags, [line_no=>$line_no]) if $line_no;
my $pclass = ref($self);
push(@tags, [parse_class=>"$pclass"]);
$self->err_event(error=>[@tags]);
return;
}
sub line_no {
my $self = shift;
$self->{_line_no} = shift if @_;
return $self->{_line_no};
}
sub line {
my $self = shift;
$self->{_line} = shift if @_;
return $self->{_line};
}
sub file {
my $self = shift;
$self->{_file} = shift if @_;
return $self->{_file};
}
sub finish {
my $self = shift;
if ($self->errhandler && $self->errhandler->depth) {
$self->errhandler->end_event;
}
}
sub parse {
my $self = shift;
my ($file, $str, $fh) =
rearrange([qw(file str fh)], @_);
$self->file($file) if $file;
if ($str) {
$self->load_module("IO::String");
$fh = IO::String->new($str) || confess($str);
}
elsif ($file) {
if ($file eq '-') {
$fh = \*STDIN;
}
else {
$self->load_module("FileHandle");
$fh = FileHandle->new($file) || confess("cannot open file: $file");
}
}
else {
}
if (!$fh) {
confess("no filehandle");
}
$self->parse_fh($fh);
# problem with IO::String closing in perl5.6.1
unless ($str) {
#$fh->close || confess("cannot close file: $file"); // problem stdout
$fh->close;
}
return;
}
sub handler_err {
my $self = shift;
$self->err_event(error=>[[message=>'handler problem'],
[stack=>shift]]);
}
sub errlist {
my $self = shift;
$self->finish;
my $eh = $self->errhandler;
if ($eh && $eh->stag && $eh->stag->data) {
return ($eh->stag->get_error);
}
return ();
}
# MAIN EVENT HANDLING
# start/end/event/body
sub start_event {
my $self = shift;
push(@{$self->{_stack}}, $_[0]);
$self->{_handler}->start_event(@_);
return;
}
sub end_event {
my $self = shift;
my $ev = shift || $self->{_stack}->[-1];
$self->{_handler}->end_event($ev);
my $out = pop(@{$self->{_stack}});
return;
}
sub event {
my $self = shift;
$self->{_handler}->event(@_);
return;
}
sub evbody {
my $self = shift;
$self->{_handler}->evbody(@_);
# my $lc = $self->last_evcall_type;
# if ($lc && $lc eq 'evbody') {
# confess("attempting to event_body illegally (body already defined)");
# }
# eval {
# $self->handler->evbody(@_);
# };
# if ($@) {
# $self->handler_err($@);
# }
# $self->last_evcall_type('evbody');
return;
}
1;
|