From: rettenbe Date: Mon, 26 Oct 2009 09:02:46 +0000 (+0000) Subject: add easier xml transformation functions X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=8dcd6394cc066861c89f28ab065b267833226b2d;p=gosa.git add easier xml transformation functions git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@14626 594d385d-05f5-0310-b6e9-bd551577e9d8 --- diff --git a/gosa-si/modules/GosaSupportDaemon.pm b/gosa-si/modules/GosaSupportDaemon.pm index 6a28f8c73..4c46c58e2 100644 --- a/gosa-si/modules/GosaSupportDaemon.pm +++ b/gosa-si/modules/GosaSupportDaemon.pm @@ -5,6 +5,8 @@ use Exporter; my @functions = ( "create_passwd", "create_xml_hash", + "createXmlHash", + "myXmlHashToString", "get_content_from_xml_hash", "add_content2xml_hash", "create_xml_string", @@ -49,7 +51,6 @@ use Data::Dumper; use Net::DNS; use DateTime; - my $op_hash = { 'eq' => '=', 'ne' => '!=', @@ -113,6 +114,89 @@ sub create_xml_hash { return $hash } +sub createXmlHash { + my ($header, $source, $target) = @_; + return { header=>$header, source=>$source, target=>$target}; +} + +sub _transformHashToString { + my ($hash) = @_; + my $s = ""; + + while (my ($tag, $content) = each(%$hash)) { + + if (ref $content eq "HASH") { + $s .= "<$tag>".&_transformHashToString($content).""; + } elsif ( ref $content eq "ARRAY") { + $s .= &_transformArrayToString($tag, $content); + } else { + $s .= "<$tag>".$content.""; + } + } + return $s; +} + +sub _transformArrayToString { + my ($tag, $contentArray) = @_; + my $s = ""; + foreach my $content (@$contentArray) { + if (ref $content eq "HASH") { + $s .= "<$tag>".&_transformHashToString($content).""; + } else { + $s .= "<$tag>$content"; + } + } + return $s; +} + + +#=== FUNCTION ================================================================ +# NAME: myXmlHashToString +# PARAMETERS: xml_hash - hash - hash from function createXmlHash +# RETURNS: xml_string - string - xml string representation of the hash +# DESCRIPTION: Transforms the given hash to a xml wellformed string. I.e.: +# { +# 'header' => 'a' +# 'source' => 'c', +# 'target' => 'b', +# 'hit' => [ '1', +# '2', +# { +# 'hit31' => 'ABC', +# 'hit32' => 'XYZ' +# } +# ], +# 'res0' => { +# 'res1' => { +# 'res2' => 'result' +# } +# }, +# }; +# +# will be transformed to +# +#
a
+# c +# b +# 1 +# 2 +# +# ABC +# XYZ +# +# +# +# result +# +# +#
+# +#=============================================================================== +sub myXmlHashToString { + my ($hash) = @_; + return "".&_transformHashToString($hash).""; +} + #=== FUNCTION ================================================================ # NAME: create_xml_string