Code

add easier xml transformation functions
authorrettenbe <rettenbe@594d385d-05f5-0310-b6e9-bd551577e9d8>
Mon, 26 Oct 2009 09:02:46 +0000 (09:02 +0000)
committerrettenbe <rettenbe@594d385d-05f5-0310-b6e9-bd551577e9d8>
Mon, 26 Oct 2009 09:02:46 +0000 (09:02 +0000)
git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@14626 594d385d-05f5-0310-b6e9-bd551577e9d8

gosa-si/modules/GosaSupportDaemon.pm

index 6a28f8c730ec86655436e30746331836ea399b47..4c46c58e26fc43b649f456a11505c1d6e0e1f59d 100644 (file)
@@ -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)."</$tag>";
+               } elsif ( ref $content eq "ARRAY") {
+                       $s .= &_transformArrayToString($tag, $content);
+               } else {
+                       $s .= "<$tag>".$content."</$tag>";
+               }
+       }
+       return $s;
+}
+
+sub _transformArrayToString {
+       my ($tag, $contentArray) = @_;
+       my $s = "";
+       foreach my $content (@$contentArray) {
+               if (ref $content eq "HASH") {
+                       $s .= "<$tag>".&_transformHashToString($content)."</$tag>";
+               } else {
+                       $s .= "<$tag>$content</$tag>";
+               }
+       }
+       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 
+#                               <xml>
+#                                      <header>a</header>
+#                                      <source>c</source>
+#                                      <target>b</target>
+#                                      <hit>1</hit>
+#                                      <hit>2</hit>
+#                                      <hit>
+#                                              <hit31>ABC</hit31>
+#                                              <hit32>XYZ</hit32>
+#                                      </hit>
+#                                      <res0>
+#                                              <res1>
+#                                                      <res2>result</res2>
+#                                              </res1>
+#                                      </res0>
+#                              </xml>
+#
+#===============================================================================
+sub myXmlHashToString {
+       my ($hash) = @_;
+       return "<xml>".&_transformHashToString($hash)."</xml>";
+}
+
 
 #===  FUNCTION  ================================================================
 #         NAME:  create_xml_string