????
| Current Path : /usr/lib/sonarpush/SonarPush/ |
| Current File : //usr/lib/sonarpush/SonarPush/Providers.pm |
package SonarPush::Providers;
use strict;
=head1 NAME
SonarPush::Providers
=head1 DESCRIPTION
a providers class.
=cut
sub new {
my ($class, $parent) = @_;
my $self = { sonar => $parent };
bless $self, $class;
return $self;
}
sub sonar {
my ($self) = @_;
return $self->{sonar};
}
=pod
The encodeXML method can be used to convert a suitable perl data structure
into compatible xml.
print encodeXML(
{
name => "test10",
attributes => {
provider => "testProvider",
type => "numeric",
},
value => [
{
name => "test11",
attributes => {
provider => "testProvider",
type => "numeric",
},
value => {
name => "test125",
value => "testValue",
attributes => {
provider => "testProvider",
type => "numeric",
},
},
}
],
}
);
<test10 provider="testProvider" type="numeric">
<test11 provider="testProvider" type="numeric">
<test125 provider="testProvider" type="numeric">testValue</test125>
</test11>
</test10>
=cut
sub encodeXML {
my ($structure) = @_;
my $xml = "";
return $xml unless $structure;
my $elementName = escapeXMLName($structure->{name});
$xml .= "<$elementName " . join(' ', map {"$_=\"$structure->{attributes}{$_}\""} sort keys %{ $structure->{attributes} }) . ">";
my $valueType = ref($structure->{value});
if ($valueType eq 'HASH') {
$xml .= "\n";
$xml .= encodeXML($structure->{value});
}
elsif ($valueType eq 'ARRAY') {
if (@{ $structure->{value} }) {
$xml .= "\n";
$xml .= join('', map { encodeXML($_) } @{ $structure->{value} });
}
}
else {
$xml .= $structure->{value};
}
$xml .= "</$elementName>\n";
}
sub escapeXMLName {
my ($name) = @_;
# Element names must start with a letter or underscore.
# Element names cannot start with the letters xml (or XML, or Xml, etc)
# Element names can contain letters, digits, hyphens, underscores, and periods.
# Element names cannot contain spaces.
my $startsWithInvalid = $name =~ /^[^a-z_]/i;
my $startsWithXML = $name =~ /^xml/i;
my $hasInvalidChars = $name =~ /[^a-z\d\-_.]/i;
my $hasSpaces = $name =~ /\s/;
if ($startsWithInvalid || $startsWithXML) {
$name = '_' . $name;
}
if ($hasInvalidChars) {
$name =~ s/([^a-z\d\-_.])/sprintf('_%X_', ord($1))/ige;
}
if($hasSpaces) {
$name =~ s/([\s])/_)/g;
}
return $name;
}
1;