/usr/share/perl5/XML/Doctype.pm is in libxml-autowriter-perl 0.40-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 | package XML::Doctype ;
=head1 NAME
XML::Doctype - A DTD object class
=head1 SYNOPSIS
# To parse an external DTD at compile time, useful when
# using XML::ValidWriter
use XML::Doctype NAME => 'FooML', SYSTEM_ID => 'FooML.dtd' ;
use XML::Doctype NAME => 'FooML', DTD_TEXT => $dtd ;
# Parsing at run-time
$doctype = XML::Doctype->new( 'FooML', SYSTEM_ID => 'FooML.dtd' ) ;
# or
$doctype = XML::Doctype->new() ;
$doctype->parse( 'FooML', 'FooML.dtd' ) ;
# Saving the parsed object
open( PM, ">FooML/DTD/v1_000.pm" ) or die $! ;
print PM $doctype->as_pm( 'FooML::DTD::v1_000' ) ;
# Using a saved parsed DTD
use FooML::DTD::v1_000 ;
$doctype = FooML::DTD::v1_000->new() ;
=head1 DESCRIPTION
This module parses DTDs and allows them to be saved as .pm files and
reloaded. The ability to save and reload is intended to aid in packaging
parsed DTDs with XML tools so that XML::Parser need not be installed.
=head1 STATUS
This module is alpha code. It's developed enough to support XML::ValidWriter,
but need a lot of work. Some big things that are lacking are:
=over
=item *
methods or objects to build / traverse the DTD
=item *
XML::Doctype::ELEMENT
=item *
XML::Doctype::ATTLIST
=item *
XML::Doctype::ENITITY
=back
=cut
use strict ;
use vars qw( $VERSION %_default_dtds ) ;
use fields (
'ELTS', # A hash of declared & undeclared elements, keyed by name
'NAME', # The root node (the name from the DOCTYPE decl).
'SYSID',
'PUBID',
) ;
use Carp ;
use XML::Doctype::ElementDecl ;
use XML::Doctype::AttDef ;
$VERSION = 0.11 ;
=head1 METHODS
=over
=item new
$doctype = XML::Doctype->new() ;
$doctype = XML::Doctype->new( 'FooML', DTD_TEXT => $doctype_text ) ;
$doctype = XML::Doctype->new( 'FooML', SYSTEM_ID => 'FooML.dtd' ) ;
=cut
sub new {
my XML::Doctype $self = fields::new( shift );
return $self unless @_ ;
my $name = shift ;
if ( @_ == 1 ) {
$self->parse_dtd_file( $name, shift ) ;
}
else {
while ( @_ ) {
for ( shift ) {
if ( /^SYSTEM(?:_ID)?$/ ) {
$self->parse_dtd_file( $name, shift ) ;
}
elsif ( $_ eq 'DTD_TEXT' ) {
$self->parse_dtd( $name, shift ) ;
}
else {
croak "Unrecognized parameter '$_'" ;
}
}
}
}
## Do this here so subclass author won't be suprised when eventually
## calling save_as_pm.
my $class = ref $self;
no strict 'refs' ;
croak "\$$class\::VERSION not defined"
unless defined ${"$class\::VERSION"} ;
return $self ;
}
=item name
$name = $doctype->name() ;
Sets/gets the name.
=cut
sub name {
my XML::Doctype $self = shift ;
$self->{NAME} = shift if @_ ;
return $self->{NAME}
}
##
## Called to translate the XML::Parser::ContentModel passed by XML::Parser
## in to a tree of XML::Doctype::ChildDecl instances.
sub _import_ContentModel {
}
sub _do_parse {
my XML::Doctype $self = shift ;
my ( $fake_doc ) = @_ ;
my $elts = $self->{ELTS} = {} ;
## Should maybe use libwww to fetch URLs, but will do files for now
## We require this lazily to save load time and allow it to be
## not present if it's not needed.
require XML::Parser ;
my $p = XML::Parser->new(
ParseParamEnt => 1,
Handlers => {
Doctype => sub {
my $expat = shift ;
my ( $name, $sysid, $pubid, $internal ) = @_ ;
$self->{NAME} = $name ;
$self->{SYSID} = $sysid ;
$self->{PUBID} = $pubid ;
},
Element => sub {
my $expat = shift ;
my ( $name, $model ) = @_ ;
croak "ELEMENT '$name' already defined"
if exists $elts->{$name} && $elts->{$name}->is_declared ;
my $elt = XML::Doctype::ElementDecl->new( $name, $model ) ;
$elt->is_declared( 1 ) ;
$elts->{$name} = $elt ;
for ( $elt->child_names ) {
$elts->{$_} = XML::Doctype::ElementDecl->new( $_ )
unless $elts->{$_} ;
}
},
Attlist => sub {
my $expat = shift ;
my ( $elt_name, $att_name, $type, $default, $fixed ) = @_ ;
$elts->{$elt_name} = XML::Doctype::ElementDecl->new()
unless exists $elts->{$elt_name} ;
$default =~ s/^'(.*)'$/$1/ || $default =~ s/^"(.*)"$/$1/ ;
$elts->{$elt_name}->add_attdef(
XML::Doctype::AttDef->new(
$att_name,
$type,
$fixed ? ( '#FIXED', $default ) : ( $default, undef ),
)
) ;
},
},
) ;
$p->parse( $fake_doc ) ;
croak "Doctype",
defined $self->{SYSID} ? " SYSTEM_ID $self->{SYSID}" : (),
" did not declare root node <$self->{NAME}>"
unless exists $self->{ELTS}->{$self->{NAME}} ;
# require Data::Dumper ; print Data::Dumper::Dumper( $elts ) ;
## TODO: Check that all elements referred-to by name in the element tree
## rooted at $self->{NAME} are actually declared.
}
=item parse_dtd
$doctype->parse_dtd( $name, $doctype_text ) ;
$doctype->parse_dtd( $name, $doctype_text, 'internal' ) ;
Parses the text of a DTD from a scalar. $name is used to indicate the
name of the DOCTYPE, and thus the root node.
The DTD is considered to be external unless the third parameter is
TRUE.
=cut
sub parse_dtd {
my XML::Doctype $self = shift ;
my ( $name, $text, $internal ) = @_ ;
$self->_do_parse( <<TOHERE ) ;
<?xml version="1.0" encoding="US-ASCII" standalone="yes"?>
<!DOCTYPE $name [
$text
]>
<$name></$name>
TOHERE
}
=item parse_dtd_file
$doctype->parse_dtd_file( $name, $system_id [, $public_id] ) ;
$doctype->parse_dtd_file( $name, $system_id [, $public_id], 'internal' ) ;
Parses a DTD from a file. Eventually will support full URL syntax.
$public_id is ignored for now, and $system_id is used to locate
the DTD.
This routine requires XML::Parser. XML::Parser is not loaded at any
other time and is not needed to use the resulting DTD object.
The DTD is considered to be external unless the fourth parameter is
TRUE.
$doctype->parse_dtd_file( $name, $system_id, $p_id, 'internal' ) ;
$doctype->parse_dtd_file( $name, $system_id, undef, 'internal' ) ;
=cut
sub parse_dtd_file {
my XML::Doctype $self = shift ;
my ( $name, $system_id, undef, $internal ) = @_ ;
$self->_do_parse( <<TOHERE ) ;
<?xml version="1.0" encoding="US-ASCII" standalone="no"?>
<!DOCTYPE $name SYSTEM "$system_id" >
<$name></$name>
TOHERE
}
=item system_id
$system_id = $doctype->system_id() ;
Sets/gets the system ID.
=cut
sub system_id {
my XML::Doctype $self = shift ;
$self->{SYSID} = shift if @_ ;
return $self->{SYSID}
}
=item public_id
$public_id = $doctype->public_id() ;
Sets/gets the public_id.
=cut
sub public_id {
my XML::Doctype $self = shift ;
$self->{PUBID} = shift if @_ ;
return $self->{PUBID}
}
=item element_decl
$elt_decl = $doctype->element_decl( $name ) ;
Returns the XML::Doctype:Element object associated with $name. These can
be defined by <!ELEMENT> tags or undefined, which can happen if they
were just referred-to by <!ELEMENT> or <!ATTLIST> tags.
=cut
sub element_decl {
my XML::Doctype $self = shift ;
my ( $name ) = @_ ;
return $self->{ELTS}->{$name} if exists $self->{ELTS}->{$name} ;
return ;
}
=item element_names
Returns an unsorted list of element names. This list includes names that
are declared and undeclared (but referred to in element declarations or
attribute definitions).
=cut
sub element_names {
my XML::Doctype $self = shift ;
my $h = {} ;
for ( keys %{$self->{ELTS}} ) {
$h->{$_} = 1 ;
$h->{$_} = 1 for $self->{ELTS}->{$_}->child_names() ;
}
return keys %$h ;
}
=item as_pm
open( PM, "FooML/DTD/v1_001.pm" ) or die $! ;
print PM $doctype->as_pm( 'FooML::DTD::v1_001' ) or die $! ;
close PM or die $! ;
Then, later:
use FooML::DTD::v1_001 ; # Do *not* use () as a parameter list!
Returns string containing the DTD as an independant module, allowing the
DTD to be parsed in the development environment and shipped as Perl code,
so that the target environment need not have XML::Parser installed.
This is useful for XML creation-only tools and as an
efficiency tuning measure if you will be rereading the same set of DTDs over
and over again.
=cut
## TODO: Save as pure, unblessed data structure that XML::Doctype can
## convert to internal format, to increase inter-version compatibility.
sub as_pm {
my XML::Doctype $self = shift ;
my ( $package ) = @_ ;
my $date = localtime ;
my $class = ref $self ;
my $version ;
if ( $class ne __PACKAGE__ ) {
no strict 'refs' ;
croak "\$$class\::VERSION not defined"
unless defined ${"$class\::VERSION"} ;
$version = "$class, v" . ${"$class\::VERSION"} . ", (" ;
}
$version .= __PACKAGE__ . ", v$VERSION" ;
$version .= ')'
if $class ne __PACKAGE__ ;
require Data::Dumper ;
my $d = Data::Dumper->new( [$self], ['$doctype'], ) ;
# $d->Freezer( '_freeze' ) unless $d->can( 'Dumpperl' ) ;
$d->Purity(1); ## We really do want to dump executable code.
$d->Indent(1); ## Used fixed indent depth. I find this more readable.
return
join( '', <<ENDPREAMBLE, $d->can( 'Dumpperl' ) ? $d->Dumpperl : $d->Dump, "\n 1 ;\n" );
package $package ;
##
## THIS FILE CREATED AUTOMATICALLY: YOU MAY LOSE ANY EDITS IF YOU MOFIFY IT.
##
## When: $date
## By: $version
##
require XML::Doctype ;
sub import {
my \$pkg = shift ;
my \$callpkg = caller ;
\$XML::Doctype::_default_dtds{\$callpkg} = \$doctype ;
}
ENDPREAMBLE
}
sub _freeze {
my $self = shift ;
$_->_freeze for values %{$self->{ELTS}} ;
return $self ;
}
=item import
=item use
use XML::Doctype NAME => 'FooML', SYSTEM_ID => 'dtds/FooML.dtd' ;
import() constructs a default DTD object for the calling package
so that XML::ValidWriter's functional interface can use it.
If XML::Doctype is subclassed, the subclasses' constructor is called with
all parameters.
=cut
sub import {
my $class = shift ;
my $callpkg = caller ;
my @others ;
my @dtd_args ;
while ( @_ ) {
for ( shift ) {
if ( $_ eq 'NAME' ) {
push @dtd_args, shift ;
}
elsif ( /^[A-Z][A-Z_0-9]*$/ ) {
push @dtd_args, $_, shift ;
}
else {
push @others, $_ ;
}
}
}
$_default_dtds{$callpkg} = $class->new( @dtd_args )
if @dtd_args ;
croak join( ', ', @others ), " not exported by $class" if @others ;
}
=back
=head1 SUBCLASSING
This object uses the fields pragma, so you should use base and fields for
any subclasses.
=head1 AUTHOR
Barrie Slaymaker <barries@slaysys.com>
=head1 COPYRIGHT
This module is Copyright 2000, 2005 Barrie Slaymaker. All rights reserved.
This module is licensed under your choice of the Artistic, BSD or
General Public License.
=cut
1 ;
|