/usr/share/perl5/Grid/GPT/V1/XML.pm is in grid-packaging-tools 3.6.7-1.
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 | package Grid::GPT::V1::XML;
use strict;
use Carp;
require Exporter;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
# set the version for version checking
$VERSION = 0.01;
@ISA = qw(Exporter);
@EXPORT = qw();
%EXPORT_TAGS = ( ); # eg: TAG => [ qw!name1 name2! ],
# your exported package globals go here,
# as well as any optionally exported functions
@EXPORT_OK = qw();
use vars qw();
# non-exported package globals go here
use vars qw();
# initialize package globals, first exported ones
# then the others (which are still accessible as $Some::Module::stuff)
# all file-scoped lexicals must be created before
# the functions below that use them.
# file-private lexicals go here
# here's a file-private function as a closure,
# callable as &$priv_func.
# make all your functions, whether exported or not;
# remember to put something interesting in the {} stubs
sub new {
my $that = shift;
my $class = ref($that) || $that;
my $self = {};
bless $self, $class;
return $self;
}
sub read {
my ($self, $string) = @_;
my $filename;
my @lines;
if ($string =~ m{<.*?>}s or ref($string)) {
@lines= $string;
}else{
$filename=$string;
open (FILE, "$filename") or die "ERROR: cannot open $filename: $!\n";
@lines = <FILE>;
}
my $lineno = 0;
my $buffer = "";
my @tagstack;
my $leftover;
for my $l (@lines) {
$lineno++;
$l = "$leftover $l" if defined $leftover;
# print "line: $l";
my $lastmatch = 0;
while ($l =~ m!([^<]*)<([^>]+)>!g) {
my $contents = $1;
my $tagcontents = $2;
$lastmatch = pos($l);
if ( $tagcontents =~ m!\?xml\s+!) {
next;
}
if ( $tagcontents =~ m!\!\s*DOCTYPE\s+(\w+)\s+SYSTEM\s*\"([^\"]+)\"!) {
$self->doctype($1, $2);
next;
}
if ($tagcontents =~ m!^\s*/\s*(.+)$!) {
# process an endtag
my $tag = $1;
$tag =~ s!(\S+)\s*$!$1!;
if ($tag =~ m!\w\s+\w!) {
warn "ERROR: $lineno - Ending tag <$tag> cannot contain any spaces\n";
push @{$self->{'errors'}},
"ERROR: $lineno - Ending tag <$tag> cannot contain any spaces";
}
my $currenttag = pop @tagstack;
if ($tag ne $currenttag->{'name'}) {
my $found = 0;
my @tagunwind;
push @tagunwind, $currenttag;
for my $t (reverse @tagstack) {
# print "unwinding tag $t->{'name'}\n";
if ($tag eq $t->{'name'}) {
$found++;
last;
}
push @tagunwind, $t;
}
if (!$found) {
warn "ERROR: $lineno - Ending tag <$tag> does not have a corresponding start tag\n";
push @{$self->{'errors'}},
"ERROR: $lineno - Ending tag <$tag> does not have a corresponding start tag";
} else {
warn "ERROR: $lineno - Ending tag <$tag> is out of sequence\n";
push @{$self->{'errors'}},
"ERROR: $lineno - Ending tag <$tag> is out of sequence\n";
}
for my $t (@tagunwind) {
warn "ERROR: $t->{'lineno'} - Tag <$t->{'name'}> needs end tag before <$tag> at line $lineno\n";
push @{$self->{'errors'}},
"ERROR: $t->{'lineno'} - Tag <$t->{'name'}> needs end tag before <$tag> at line $lineno";
}
}
push @{$currenttag->{'contents'}}, $contents if defined $contents;
push @{$tagstack[-1]->{'contents'}}, $currenttag if @tagstack;
}
else {
# process a start tag
$tagcontents =~ m!^\s*(\w+)!;
my $tagsave = $tagcontents;
my $starttag = $1;
my $emptytag = 0;
$emptytag = 1 if $tagcontents =~ s!/\s*$!!;
if ($starttag eq "") {
warn "ERROR: $lineno - Starting tag <$tagcontents> is malformed\n";
push @{$self->{'errors'}},
"ERROR: $lineno - Starting tag <$tagcontents> is malformed";
}
my $tagobj = create_tag($starttag);
$tagobj->{'lineno'} = $lineno;
#The first tag encountered is the root tag
if (!defined ($self->{'roottag'})) {
$self->{'roottag'} = $tagobj;
}
$tagcontents =~ s!$starttag!!;
while ($tagcontents =~ s!(\w+)\s*=\s*\"([^\"]+)\"!!) {
$tagobj->{'attributes'}->{$1} = $2;
}
if ($tagcontents =~ m!\S!) {
warn "ERROR: $lineno - Tag <$tagsave> is malformed\n";
push @{$self->{'errors'}},
"ERROR: $lineno - Tag <$tagsave> is malformed";
}
if (@tagstack) {
push @{$tagstack[-1]->{'contents'}}, $contents if $contents ne "";
}
if ($emptytag) {
if (! @tagstack) {
warn "ERROR: $lineno - can't start a document with an empty tag <$tagcontents>\n";
push @{$self->{'errors'}},
"ERROR: $lineno - can't start a document with an empty tag <$tagcontents>";
}
push @{$tagstack[-1]->{'contents'}}, $tagobj;
} else {
push @tagstack, $tagobj;
}
}
}
$leftover = substr($l,$lastmatch);
}
for my $t (@tagstack) {
warn "ERROR: $t->{'lineno'} - Tag <$t->{'name'}> does not have an end tag\n";
push @{$self->{'errors'}},
"ERROR: $t->{'lineno'} - Tag <$t->{'name'}> does not have an end tag";
}
}
sub doctype
{
my ($self, $doctype, $system) = @_;
$self->{'doctype'} = $doctype;
$self->{'system'} = $system;
}
sub create_tag {
my ($name) = @_;
return { name => $name, attributes => {}, contents => []}
}
sub startTag {
my ($self, $name, %attrs) = @_;
my $tag = create_tag($name);
$tag->{'attributes'} = \%attrs;
if (defined $self->{'tagstack'}) {
push @{$self->{'tagstack'}->[-1]->{'contents'}}, $tag;
} else {
$self->{'tagstack'} = [];
$self->{'roottag'} = $tag;
}
push @{$self->{'tagstack'}},$tag;
}
sub emptyTag {
my ($self, $name, %attrs) = @_;
my $tag = create_tag($name);
$tag->{'attributes'} = \%attrs;
if (defined $self->{'tagstack'}) {
push @{$self->{'tagstack'}->[-1]->{'contents'}}, $tag;
}
}
sub dataElement {
my ($self, $name, $contents, %attrs) = @_;
my $tag = create_tag($name);
$tag->{'attributes'} = \%attrs;
push @{$tag->{'contents'}}, $contents;
if (defined $self->{'tagstack'}) {
push @{$self->{'tagstack'}->[-1]->{'contents'}}, $tag;
}
}
sub endTag {
my ($self, $name) = @_;
die "ERROR: no valid tags to end with $name\n" if ! defined $self->{'tagstack'};
my $tag = pop @{$self->{'tagstack'}};
die "ERROR: mismatched start tag $tag->{'name'} and end tag $name\n" if $tag->{'name'} ne $name;
}
sub characters {
my ($self, $contents) = @_;
die "ERROR: no valid tags for contents $contents\n" if ! defined $self->{'tagstack'};
push @{$self->{'tagstack'}->[-1]->{'contents'}}, $contents;
}
sub close_tag {
my ($self, $filename) = @_;
die "ERROR: no valid tags to close\n" if ! defined $self->{'tagstack'};
my $roottag = $self->{'tagstack'}->[0];
$self->{$roottag->{'name'}} = $roottag;
$self->{'roottag'} = $roottag->{'name'};
}
sub write {
my ($self, $filename) = @_;
local *FILE;
if (defined ($filename)) {
open (FILE, ">$filename") or die "ERROR could not write $filename\n";
$self->{'FILE'} = \*FILE;
} else {
$self->{'FILE'} = \*STDOUT;
}
my $root = $self->{'roottag'};
print FILE '<?xml version="1.0" encoding="UTF-8"?>
';
print FILE "<!DOCTYPE $self->{'doctype'} SYSTEM \"$self->{'system'}\">\n";
$self->write_tag($root);
print FILE "\n";
close(*FILE);
}
sub write_tag {
my ($self, $tag) = @_;
local *FILE = *{$self->{'FILE'}};
print FILE "<$tag->{'name'}";
for my $a (sort keys %{$tag->{attributes}}) {
print FILE " $a=\"$tag->{attributes}->{$a}\"";
}
if (@{$tag->{'contents'}} == 0) {
print FILE "/>";
return;
} else {
print FILE ">";
}
for my $c (@{$tag->{'contents'}}) {
if (ref($c) eq 'HASH') {
$self->write_tag($c);
} else {
print FILE "$c";
}
}
print FILE "</$tag->{'name'}>";
}
END { } # module clean-up code here (global destructor)
1;
__END__
# Below is the stub of documentation for your module. You better edit it!
=head1 NAME
Grid::GPT::V1::XML - Perl extension for parsing and writing XML files
=head1 SYNOPSIS
use Grid::GPT::V1::XML;
my $xml = new Grid::GPT::V1::XML;
$xml>read('src_metadata.xml');
$root = $xml->{'roottag'};
print "For tag $root->{'name'}\n";
for my $a ( sort keys %{$root->{'attributes'}}) {
print "Attribute $a is set to $root->{'attributes'}->{$a}\n";
}
=head1 DESCRIPTION
I<Grid::GPT::V1::XML> is used to read and write xml files along the lines
of XML::Simple and XML::Writer although without using the expat
library. Any simularity with the output methods of XML::Writer is
purely intentional.
=head1 Perl Representation of XML Tags.
I<Grid::GPT::V1::XML> Stores the pieces of the XML file in a hash
structure consisting of tag "objects". Each object consists of the
following structure:
=over 4
=item name
Name of the tag
=item attributes
A hash containing the attributes of a tag. Each attribute is keyed by
name and points to its value.
=item contents
A list that contents strings or other tag objects in order of occurance.
=back
=head1 Methods
=over 4
=item new
Create a new I<Grid::GPT::V1::XML> object.
=item read(filename)
Parse an xml file. Transform the file into its perl representation.
=item doctype(doctype, system)
Sets the doctype and system values for a document.
=item startTag(tagname[, attr1 => value1, attr2 => value2...])
Starts a new tag object. This object then becomes part of the
contents of the last tag started.
=item emptyTag(tagname[, attr1 => value1, attr2 => value2...])
Add an empty tag to the last tag started.
=item dataElement(tagname, contents[, attr1 => value1, attr2 => value2...])
Add a non-empty tag to the last tag started.
=item endTag(tagname)
End the last tag started. If tagname does not match the this tag then
the script dies.
=item characters(contents)
Add content to the last tag started.
=item close_tag
End the building of the XML document.
=item write(filename)
Transform the perl representation into XML and write it to the file.
Note that all tags that do not have content are trnsformed into empty
tags.
=back
=head1 Example
Given the following XML:
<foo_tag life="short" fun="much">
Are we there yet
<fum_tag death="iminent" temper="short"/>
</foo_tag>
The Perl representation looks like this:
VAR1 {
'name' => 'foo_tag',
'attributes' => {
'life' => 'short',
'fun' => 'much',
},
'contents' => [
'Are we there yet',
{
'name' => 'fum_tag',
'attributes' => {
'death' => 'iminent',
'temper' => 'short',
},
'contents' => [],
},
],
}
To create the XML do the following:
startTag("foo_tag", life => 'short', fun => 'much');
characters("Are we there yet");
emptyTag("fum_tag", death => "iminent", temper => "short");
=head1 AUTHOR
Eric Blau <eblau@ncsa.uiuc.edu> Michael Bletzinger <mbletzin@ncsa.uiuc,edu>
=head1 SEE ALSO
perl(1).
=cut
|