phpgroupware-cvs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Phpgroupware-cvs] api/class.xmltool.php, 1.1.1.1.2.10


From: nomail
Subject: [Phpgroupware-cvs] api/class.xmltool.php, 1.1.1.1.2.10
Date: Mon, 7 Jun 2004 23:45:56 +0200

Update of /api
Modified Files:
        Branch: proposal-branch
          class.xmltool.php

date: 2004/06/07 21:45:56;  author: dcech;  state: Exp;  lines: +102 -76

Log Message:
more efficient recursion model
general cleanup
fixed attribute bug in export_var function
=====================================================================
Index: api/class.xmltool.php
diff -u api/class.xmltool.php:1.1.1.1.2.9 api/class.xmltool.php:1.1.1.1.2.10
--- api/class.xmltool.php:1.1.1.1.2.9   Fri Apr 16 20:59:49 2004
+++ api/class.xmltool.php       Mon Jun  7 21:45:56 2004
@@ -26,7 +26,7 @@
 
        function var2xml($data, $name = 'appcontent', $is_root = true)
        {
-               $doc = new api_xmltool('root','','');
+               $doc = new xmldoc();
                if(!$is_root)
                {
                        $doc->show_xml_headers = false;
@@ -43,7 +43,7 @@
 //             $args->set('xmldata', REQUIRED, 'any');
 //             $args = $args->get(func_get_args());
 
-               $doc = new api_xmltool('root','','');
+               $doc = new xmldoc();
 //             $doc->import_xml($args['xmldata']);
                $doc->import_xml($xmldata);
                return $doc->export_var();
@@ -72,7 +72,7 @@
                {
                        $args = new safe_args();
                        $args->set('node_type', 'root', 'any');
-                       $args->set('name', 'anonymous', 'any');
+                       $args->set('name', '', 'any');
                        $args->set('indentstring', '  ', 'any');
                        $args = $args->get(func_get_args());
 
@@ -116,35 +116,34 @@
                        switch ($this->node_type)
                        {
                                case 'root':
-                                       if (is_object($node_object))
+                                       if (!is_object($node_object))
                                        {
-                                               $this->data = $node_object;
+                                               $this->import_var($name, 
$node_object);
                                                break;
                                        }
-                                       $this->data = $this->import_var($name, 
$node_object);
+
+                                       $this->data = $node_object;
                                        break;
                                case 'node':
+                                       if (!is_object($node_object))
+                                       {
+                                               $this->import_var($name, 
$node_object);
+                                               break;
+                                       }
+
                                        if (!is_array($this->data))
                                        {
                                                $this->data      = array();
                                                $this->data_type = 'node';
                                        }
 
-                                       if (is_object($node_object))
+                                       if ($name == '')
                                        {
-                                               if ($name == '')
-                                               {
-                                                       $this->data[] = 
$node_object;
-                                               }
-                                               else
-                                               {
-                                                       $this->data[$name] = 
$node_object;
-                                               }
+                                               $this->data[] = $node_object;
                                        }
                                        else
                                        {
-//                                             $this->data[$name] = 
$this->import_var($name, $node_object);
-                                               $this->data[$name] = 
$this->import_var($name, $node_object);
+                                               $this->data[$name] = 
$node_object;
                                        }
                                        break;
                                default:
@@ -155,7 +154,7 @@
 
                function get_node ($name = '')  // what is that function doing: 
NOTHING !!!
                {
-                       switch  ($this->data_type)
+                       switch ($this->data_type)
                        {
                                case 'root':
                                        break;
@@ -164,7 +163,8 @@
                                case 'object':
                                        break;
                        }
-               
+
+                       return false;
                }
 
                function set_value ($string)
@@ -208,23 +208,22 @@
                function import_var 
($name,$value,$is_root=false,$export_xml=false)
                {
                        $node = new 
api_xmltool('node',$name,$this->indentstring);
-                       $node = $this->import_var_sub($name, $value, $node);
+                       $node->import_var_sub($name,$value);
+                       $this->add_node($node,$name);
 
                        if($is_root)
                        {
-                               $this->add_node($node);
                                if($export_xml)
                                {
-                                       $xml = $this->export_xml();
-                                       return $xml;
+                                       return $this->export_xml();
                                }
                                return true;
                        }
-                       $this->add_node($node);
                        //return $node;
+                       return true;
                }
 
-               function import_var_sub($name, $value, $orignode)
+               function import_var_sub($name, $value)
                {
                        switch (gettype($value))
                        {
@@ -232,36 +231,22 @@
                                case 'integer':
                                case 'double':
                                case 'NULL':
-                                       $orignode->set_value($value);
+                                       $this->set_value($value);
                                        break;
                                case 'boolean':
-                                       if($value == true)
-                                       {
-                                               $orignode->set_value('1');
-                                       }
-                                       else
-                                       {
-                                               $orignode->set_value('0');
-                                       }
+                                       $this->set_value($value ? '1' : '0');
                                        break;
                                case 'array':
                                        $new_index = array_key_exists(0,$value);
                                        reset($value);
                                        foreach($value as $key=>$val)
                                        {
-                                               if($new_index)
-                                               {
-                                                       $keyname = 
$name.'_item';
-                                               }
-                                               else
-                                               {
-                                                       $keyname = $key;
-                                               }
-                                               $orignode->import_var($keyname, 
$val);
+                                               $keyname = $new_index ? 
$name.'_item' : $key;
+                                               $this->import_var($keyname, 
$val);
                                        }
                                        break;
                                case 'object':
-                                       
$orignode->set_value('PHP_SERIALIZED_OBJECT&:'.serialize($value));
+                                       
$this->set_value('PHP_SERIALIZED_OBJECT&:'.serialize($value));
                                        break;
                                case 'resource':
                                        trigger_error('Cannot package PHP 
resource pointers into XML',E_USER_ERROR);
@@ -270,7 +255,7 @@
                                        trigger_error('Invalid or unknown data 
type',E_USER_ERROR);
                                        break;
                        }
-                       return $orignode;
+                       return true;
                }
                
                function export_var()
@@ -279,45 +264,53 @@
                        {
                                return $this->data->export_var();
                        }
+                       
+                       $return_array = array();
 
-                       if ($this->data_type != 'node')
+                       if ($this->data_type == 'node')
                        {
-                               $found_at = 
strstr($this->data,'PHP_SERIALIZED_OBJECT&:');
-                               if ($found_at !== false)
+                               $new_index = false;
+                               $found_keys = array();
+
+                               foreach($this->data as $i => $item)
                                {
-                                       return unserialize(str_replace 
('PHP_SERIALIZED_OBJECT&:', '', $this->data));
+                                       if (!isset($found_keys[$item->name]))
+                                       {
+                                               $found_keys[$item->name] = true;
+                                       }
+                                       else
+                                       {
+                                               $new_index = true;
+                                       }
                                }
-                               return $this->data;
-                       }
 
-                       $new_index    = false;
-                       $return_array = array();
-
-                       foreach($this->data as $i => $item)
-                       {
-                               if (!isset($found_keys[$item->name]))
+                               if ($new_index)
                                {
-                                       $found_keys[$item->name] = true;
+                                       foreach ($this->data as $i => $item)
+                                       {
+                                               $return_array[$item->name][] = 
$this->data[$i]->export_var();
+                                       }
                                }
                                else
                                {
-                                       $new_index = true;
+                                       foreach ($this->data as $i => $item)
+                                       {
+                                               $return_array[$item->name] = 
$this->data[$i]->export_var();
+                                       }
                                }
                        }
-
-                       if ($new_index)
+                       else
                        {
-                               foreach ($this->data as $i => $item)
+                               $found_at = 
strstr($this->data,'PHP_SERIALIZED_OBJECT&:');
+                               if ($found_at !== false)
                                {
-                                       $return_array[$item->name][] = 
$this->data[$i]->export_var();
+                                       return unserialize(str_replace 
('PHP_SERIALIZED_OBJECT&:', '', $this->data));
                                }
-                       }
-                       else
-                       {
-                               foreach ($this->data as $i => $item)
+                               if (empty($this->attributes))
                                {
-                                       $return_array[$item->name] = 
$this->data[$i]->export_var();
+                                       return $this->data;
                                }
+                               $return_array['##DATA##'] = $this->data;
                        }
 
                        // Handle attributes
@@ -359,7 +352,40 @@
                        return $retval;
                }
 
-               function import_xml_children($data, &$i, $parent_node)
+               function export_obj()
+               {
+                       if($this->node_type == 'root')
+                       {
+                               return $this->data->export_obj();
+                       }
+
+                       $retval = new stdClass;
+                       $retval->tag = $this->name;
+                       $retval->attributes = $this->attributes;
+                       if($this->data_type != 'node')
+                       {
+                               $found_at = 
strstr($this->data,'PHP_SERIALIZED_OBJECT&:');
+                               if($found_at !== false)
+                               {
+                                       $retval->value = 
unserialize(str_replace ('PHP_SERIALIZED_OBJECT&:', '', $this->data));
+                               }
+                               else
+                               {
+                                       $retval->value = $this->data;
+                               }
+                       }
+                       else
+                       {
+                               foreach ($this->data as $i => $item)
+                               {
+                                       $retval->children[] = 
$this->data[$i]->export_obj();
+                               }
+                       }
+
+                       return $retval;
+               }
+
+               function import_xml_children($data, &$i)
                {
                        $n = count($data);
                        while (++$i < $n)
@@ -377,7 +403,7 @@
                                                        }
                                                }
                                                
$node->set_value($data[$i]['value']);
-                                               $parent_node->add_node($node);
+                                               $this->add_node($node);
                                                break;
                                        case 'open':
                                                $node = new 
api_xmltool('node',$data[$i]['tag'],$this->indentstring);
@@ -390,11 +416,11 @@
                                                        }
                                                }
 
-                                               $node = 
$this->import_xml_children($data, $i, $node);
-                                               $parent_node->add_node($node);
+                                               
$node->import_xml_children($data, $i);
+                                               $this->add_node($node);
                                                break;
                                        case 'close':
-                                               return $parent_node;
+                                               return true;
                                }
                        }
                }
@@ -424,7 +450,7 @@
                                        $node->set_value($vals[0]['value']);
                                        break;
                                case 'open':
-                                       $node = 
$this->import_xml_children($vals, $i = 0, $node);
+                                       $node->import_xml_children($vals, $i = 
0);
                                        break;
                                case 'closed':
                                        echo 'Calling exit for some odd reason';




reply via email to

[Prev in Thread] Current Thread [Next in Thread]