/usr/share/arc/XmlPrinter.pm is in nordugrid-arc-arex 4.0.0-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 | package XmlPrinter;
sub new {
my ($this, $handle) = @_;
my $class = ref($this) || $this;
binmode $handle, ':encoding(utf8)';
#print $handle '<?xml version="1.0" encoding="UTF-8"?>'."\n";
my $self = {fh => $handle, indent => ''};
return bless $self, $class;
}
sub escape {
my ($chars) = @_;
$chars =~ s/&/&/g;
$chars =~ s/>/>/g;
$chars =~ s/</</g;
return $chars;
}
sub begin {
my ($self, $name, $data, @attributes) = @_;
my $fh = $self->{fh};
if (not @attributes) {
print $fh $self->{indent}."<$name>\n";
} else {
die "$name: Not a HASH reference" unless ref $data eq 'HASH';
print $fh $self->{indent}."<$name";
for my $attr (@attributes) {
my $val = $data->{$attr};
print $fh " $attr=\"$val\"" if defined $val;
}
print $fh ">\n";
}
$self->{indent} .= ' ';
}
sub end {
my ($self, $name) = @_;
chop $self->{indent};
chop $self->{indent};
my $fh = $self->{fh};
print $fh $self->{indent}."</$name>\n";
}
sub property {
my ($self, $prop, $val) = @_;
my $indent = $self->{indent};
my $fh = $self->{fh};
return unless defined $val;
if (not ref $val) {
print $fh "$indent<$prop>".escape($val)."</$prop>\n";
} elsif (ref $val eq 'ARRAY') {
print $fh "$indent<$prop>".escape($_)."</$prop>\n" for @$val;
} else {
die "$prop: Not an ARRAY reference";
}
}
sub properties {
my ($self, $data, @props) = @_;
my $indent = $self->{indent};
my $fh = $self->{fh};
for my $prop (@props) {
my $val = $data->{$prop};
next unless defined $val;
if (not ref $val) {
print $fh "$indent<$prop>".escape($val)."</$prop>\n";
} elsif (ref $val eq 'ARRAY') {
print $fh "$indent<$prop>".escape($_)."</$prop>\n" for @$val;
} else {
die "$prop: Not an ARRAY reference";
}
}
}
#### TEST ##### TEST ##### TEST ##### TEST ##### TEST ##### TEST ##### TEST ####
sub test {
my $printer = XmlPrinter->new(*STDOUT);
my $data = { xmlns => "blah/blah", date => "today" };
$printer->header();
$printer->begin("Persons", $data, qw( date ));
$data = { id => "1", name => "James", nick => "Jimmy" };
$printer->begin("Person", $data, "id");
$printer->properties($data, qw( name nick ));
$printer->end("Person");
$printer->end("Persons");
}
#test;
1;
|