using class.xmltool.inc.php from the api instead of Seek3r's old xmltools.php.

class.xmltool.inc.php is copied here til Seek3r aplied a patch in the api.
This commit is contained in:
Ralf Becker 2002-09-24 08:42:51 +00:00
parent 6d6050190e
commit 7e2f07bde1
3 changed files with 598 additions and 482 deletions

View File

@ -0,0 +1,589 @@
<?php
function var2xml($name, $data)
{
$doc = new xmltool();
return $doc->import_var($name,$data,True,True);
}
class xmltool
{
/* for root nodes */
var $xmlversion = '1.0';
var $doctype = Array();
/* shared */
var $node_type = '';
var $name = '';
var $data_type;
var $data;
/* for nodes */
var $attributes = Array();
var $comments = Array();
var $indentstring = " ";
/* start the class as either a root or a node */
function xmltool ($node_type = 'root', $name='')
{
$this->node_type = $node_type;
if ($this->node_type == 'node')
{
if($name != '')
{
$this->name = $name;
}
else
{
echo 'You must name node type objects<br>';
exit;
}
}
}
function set_version ($version = '1.0')
{
$this->xmlversion = $version;
return True;
}
function set_doctype ($name, $uri = '')
{
if ($this->node_type == 'root')
{
$this->doctype[$name] = $uri;
return True;
}
else
{
return False;
}
}
function add_node ($node_object, $name = '')
{
switch ($this->node_type)
{
case 'root':
if (is_object($node_object))
{
$this->data = $node_object;
}
else
{
$this->data = $this->import_var($name, $node_object);
}
break;
case 'node':
if(!is_array($this->data))
{
$this->data = Array();
$this->data_type = 'node';
}
if (is_object($node_object))
{
if ($name != '')
{
$this->data[$name] = $node_object;
}
else
{
$this->data[] = $node_object;
}
}
else
{
$this->data[$name] = $this->import_var($name, $node_object);
}
return True;
break;
}
}
function get_node ($name = '') // what is that function doing: NOTHING !!!
{
switch ($this->data_type)
{
case 'root':
break;
case 'node':
break;
case 'object':
break;
}
}
function set_value ($string)
{
$this->data = $string;
$this->data_type = 'value';
return True;
}
function get_value ()
{
if($this->data_type == 'value')
{
return $this->data;
}
else
{
return False;
}
}
function set_attribute ($name, $value = '')
{
$this->attributes[$name] = $value;
return True;
}
function get_attribute ($name)
{
return $this->attributes[$name];
}
function get_attributes ()
{
return $this->attributes;
}
function add_comment ($comment)
{
$this->comments[] = $comment;
return True;
}
function import_var($name, $value,$is_root=False,$export_xml=False)
{
$node = new xmltool('node',$name);
switch (gettype($value))
{
case 'string':
case 'integer':
case 'double':
case 'NULL':
$node->set_value($value);
break;
case 'boolean':
if($value == True)
{
$node->set_value('1');
}
else
{
$node->set_value('0');
}
break;
case 'array':
$new_index = False;
while (list ($idxkey, $idxval) = each ($value))
{
if(is_array($idxval))
{
while (list ($k, $i) = each ($idxval))
{
if (is_int($k))
{
$new_index = True;
}
}
}
}
reset($value);
while (list ($key, $val) = each ($value))
{
if($new_index)
{
$keyname = $name;
$nextkey = $key;
}
else
{
$keyname = $key;
$nextkey = $key;
}
switch (gettype($val))
{
case 'string':
case 'integer':
case 'double':
case 'NULL':
$subnode = new xmltool('node', $nextkey);
$subnode->set_value($val);
$node->add_node($subnode);
break;
case 'boolean':
$subnode = new xmltool('node', $nextkey);
if($val == True)
{
$subnode->set_value('1');
}
else
{
$subnode->set_value('0');
}
$node->add_node($subnode);
break;
case 'array':
if($new_index)
{
while (list ($subkey, $subval) = each ($val))
{
$node->add_node($this->import_var($nextkey, $subval));
}
}
else
{
$subnode = $this->import_var($nextkey, $val);
$node->add_node($subnode);
}
break;
case 'object':
$subnode = new xmltool('node', $nextkey);
$subnode->set_value('PHP_SERIALIZED_OBJECT&:'.serialize($val));
$node->add_node($subnode);
break;
case 'resource':
echo 'Halt: Cannot package PHP resource pointers into XML<br>';
exit;
default:
echo 'Halt: Invalid or unknown data type<br>';
exit;
}
}
break;
case 'object':
$node->set_value('PHP_SERIALIZED_OBJECT&:'.serialize($value));
break;
case 'resource':
echo 'Halt: Cannot package PHP resource pointers into XML<br>';
exit;
default:
echo 'Halt: Invalid or unknown data type<br>';
exit;
}
if($is_root)
{
$this->add_node($node);
if($export_xml)
{
$xml = $this->export_xml();
return $xml;
}
else
{
return True;
}
}
else
{
$this->add_node($node);
return $node;
}
}
function export_var()
{
if($this->node_type == 'root')
{
return $this->data->export_var();
}
if($this->data_type != 'node')
{
$found_at = strstr($this->data,'PHP_SERIALIZED_OBJECT&:');
if($found_at != False)
{
return unserialize(str_replace ('PHP_SERIALIZED_OBJECT&:', '', $this->data));
}
return $this->data;
}
else
{
$new_index = False;
reset($this->data);
while(list($key,$val) = each($this->data))
{
if(!isset($found_keys[$val->name]))
{
$found_keys[$val->name] = True;
}
else
{
$new_index = True;
}
}
if($new_index)
{
reset($this->data);
while(list($key,$val) = each($this->data))
{
$return_array[$val->name][] = $val->export_var();
}
}
else
{
reset($this->data);
while(list($key,$val) = each($this->data))
{
$return_array[$val->name] = $val->export_var();
}
}
return $return_array;
}
}
function export_struct()
{
if($this->node_type == 'root')
{
return $this->data->export_struct();
}
$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;
}
return $retval;
}
else
{
reset($this->data);
while(list($key,$val) = each($this->data))
{
$retval['children'][] = $val->export_struct();
}
return $retval;
}
}
function import_xml_children($data, &$i, $parent_node)
{
while (++$i < count($data))
{
switch ($data[$i]['type'])
{
case 'cdata':
case 'complete':
$node = new xmltool('node',$data[$i]['tag']);
if(is_array($data[$i]['attributes']) && count($data[$i]['attributes']) > 0)
{
while(list($k,$v)=each($data[$i]['attributes']))
{
$node->set_attribute($k,$v);
}
}
$node->set_value($data[$i]['value']);
$parent_node->add_node($node);
break;
case 'open':
$node = new xmltool('node',$data[$i]['tag']);
if(is_array($data[$i]['attributes']) && count($data[$i]['attributes']) > 0)
{
while(list($k,$v)=each($data[$i]['attributes']))
{
$node->set_attribute($k,$v);
}
}
$node = $this->import_xml_children($data, $i, $node);
$parent_node->add_node($node);
break;
case 'close':
return $parent_node;
}
}
}
function import_xml($xmldata)
{
$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($parser, $xmldata, $vals, $index);
xml_parser_free($parser);
unset($index);
$node = new xmltool('node',$vals[0]['tag']);
if(isset($vals[0]['attributes']))
{
while(list($key,$value) = each($vals[0]['attributes']))
{
$node->set_attribute($key, $value);
}
}
switch ($vals[0]['type'])
{
case 'complete':
$node->set_value($vals[0]['value']);
break;
case 'cdata':
$node->set_value($vals[0]['value']);
break;
case 'open':
$node = $this->import_xml_children($vals, $i = 0, $node);
break;
case 'closed':
exit;
}
$this->add_node($node);
}
function export_xml($indent = 1)
{
if ($this->node_type == 'root')
{
$result = '<?xml version="'.$this->xmlversion.'"?>'."\n";
if(count($this->doctype) == 1)
{
list($doctype_name,$doctype_uri) = each($this->doctype);
$result .= '<!DOCTYPE '.$doctype_name.' SYSTEM "'.$doctype_uri.'">'."\n";
}
if(count($this->comments) > 0 )
{
//reset($this->comments);
while(list($key,$val) = each ($this->comments))
{
$result .= "<!-- $val -->\n";
}
}
if(is_object($this->data))
{
$indent = 0;
$result .= $this->data->export_xml($indent);
}
return $result;
}
else /* For node objects */
{
for ($i = 0; $i < $indent; $i++)
{
$indentstring .= $this->indentstring;
}
$result = $indentstring.'<'.$this->name;
if(count($this->attributes) > 0 )
{
reset($this->attributes);
while(list($key,$val) = each ($this->attributes))
{
$result .= ' '.$key.'="'.htmlspecialchars($val).'"';
}
}
$endtag_indent = $indentstring;
if (empty($this->data_type))
{
$result .= '/>'."\n";
}
else
{
$result .= '>';
switch ($this->data_type)
{
case 'value':
if(is_array($this->data))
{
$type_error = True;
break;
}
if(preg_match("(&|<)", $this->data)) // this is unnecessary with htmlspecialchars($this->data)
{
$result .= '<![CDATA['.$this->data.']]>';
$endtag_indent = '';
}
elseif(strlen($this->data) > 30)
{
$result .= "\n".$indentstring.$this->indentstring.htmlspecialchars($this->data)."\n";
$endtag_indent = $indentstring;
}
else
{
$result .= htmlspecialchars($this->data);
$endtag_indent = '';
}
break;
case 'node':
$result .= "\n";
if(!is_array($this->data))
{
$type_error = True;
break;
}
$subindent = $indent+1;
reset($this->data);
while(list($key,$val) = each ($this->data))
{
if(is_object($val))
{
$result .= $val->export_xml($subindent);
}
}
break;
default:
if($this->data != '')
{
echo 'Invalid or unset data type ('.$this->data_type.'). This should not be possible if using the class as intended<br>';
}
}
if ($type_error)
{
echo 'Invalid data type. Tagged as '.$this->data_type.' but data is '.gettype($this->data).'<br>';
}
$result .= $endtag_indent.'</'.$this->name.'>';
if($indent != 0)
{
$result .= "\n";
}
}
if(count($this->comments) > 0 )
{
reset($this->comments);
while(list($key,$val) = each ($this->comments))
{
$result .= $endtag_indent."<!-- $val -->\n";
}
}
return $result;
}
}
}
class xmlnode extends xmltool
{
function xmlnode($name)
{
$this->xmltool('node',$name);
}
}
class xmldoc extends xmltool
{
function xmldoc($version = '1.0')
{
$this->xmltool('root');
$this->set_version($version);
}
function add_root($root_node)
{
return $this->add_node($root_node);
}
}
?>

View File

@ -12,8 +12,6 @@
/* $Id$ */ /* $Id$ */
include(PHPGW_SERVER_ROOT . '/etemplate/inc/xmltools.php');
class xul_io class xul_io
{ {
@ -23,6 +21,8 @@
function xul_io() function xul_io()
{ {
$this->xmltool = CreateObject('etemplate.xmltool');
$this->attr2xul = array( // how to translate attr, common to all widgets $this->attr2xul = array( // how to translate attr, common to all widgets
'name' => 'id', 'name' => 'id',
'help' => 'statustext', 'help' => 'statustext',
@ -252,7 +252,7 @@
if ($etempl->style != '') if ($etempl->style != '')
{ {
$styles = new xmlnode('styles'); $styles = new xmlnode('styles');
$styles->set_text($etempl->style); $styles->set_value($etempl->style);
$xul_grid->add_node($styles); $xul_grid->add_node($styles);
} }
$root->add_node($xul_grid); $root->add_node($xul_grid);
@ -273,7 +273,7 @@
$this->etempl2grid($etempl,&$xul_overlay); $this->etempl2grid($etempl,&$xul_overlay);
$doc->add_root($xul_overlay); $doc->add_root($xul_overlay);
$xml = $doc->dump_mem(); $xml = $doc->export_xml();
//if ($this->debug) //if ($this->debug)
{ {
@ -352,6 +352,7 @@
{ {
$cname = ($etempl->template == '' ? 'default' : $etempl->template).'/'.$etempl->name. $cname = ($etempl->template == '' ? 'default' : $etempl->template).'/'.$etempl->name.
($etempl->lang == '' ? '' : '.'.$etempl->lang); ($etempl->lang == '' ? '' : '.'.$etempl->lang);
$imported[] = $etempl->name;
$GLOBALS['phpgw_info']['etemplate']['cache'][$cname] = $etempl->as_array(1); $GLOBALS['phpgw_info']['etemplate']['cache'][$cname] = $etempl->as_array(1);
} }
$grid_started = True; $grid_started = True;
@ -500,7 +501,9 @@
{ {
_debug_array($etempl->data); _debug_array($etempl->data);
} }
return ''; $imported[] = $etempl->name;
return $imported;
} }
} }

View File

@ -1,476 +0,0 @@
<?php
/**************************************************************************\
* phpGroupWare - xmltools *
* http://www.phpgroupware.org *
* Written by Seek3r *
* -------------------------------------------- *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU General Public License as published by the *
* Free Software Foundation; either version 2 of the License, or (at your *
* option) any later version. *
\**************************************************************************/
/* $Id$ */
function xml_get_children($vals, &$i)
{
$children = array();
if (isset($vals[$i]['value']))
{
$children[] = $vals[$i]['value'];
}
while (++$i < count($vals))
{
switch ($vals[$i]['type'])
{
case 'cdata':
$children[] = $vals[$i]['value'];
break;
case 'complete':
$children[] = array(
'tag' => $vals[$i]['tag'],
'attributes' => isset($vals[$i]['attributes']) ? $vals[$i]['attributes'] : null,
'value' => $vals[$i]['value'],
);
break;
case 'open':
$children[] = array(
'tag' => $vals[$i]['tag'],
'attributes' => isset($vals[$i]['attributes']) ? $vals[$i]['attributes'] : null,
'children' => xml_get_children($vals, $i),
);
break;
case 'close':
return $children;
}
}
}
function xml_get_tree($data)
{
$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($parser, $data, $vals, $index);
xml_parser_free($parser);
return array(
'tag' => $vals[0]['tag'],
'attributes' => isset($vals[0]['attributes']) ? $vals[0]['attributes'] : null,
'children' => xml_get_children($vals, $i = 0),
);
}
function xml2array ($xmldata,$is_start = True)
{
if($is_start)
{
$xmldata = xml_get_tree($xmldata);
}
if(!is_array($xmldata['children']))
{
$found_at = strstr($xmldata['value'],'PHP_SERIALIZED_OBJECT:');
if($found_at != False)
{
$xmldata['value'] = str_replace ('PHP_SERIALIZED_OBJECT:', '', $xmldata['value']);
$xmldata['value'] = unserialize ($xmldata['value']);
}
if($is_start)
{
$xml_array[$xmldata['tag']] = $xmldata['value'];
}
else
{
return $xmldata['value'];
}
}
else
{
$new_index = False;
reset($xmldata['children']);
while(list($key,$val) = each($xmldata['children']))
{
if(!isset($found_keys[$val['tag']]))
{
$found_keys[$val['tag']] = True;
}
else
{
$new_index = True;
}
}
if($new_index)
{
reset($xmldata['children']);
while(list($key,$val) = each($xmldata['children']))
{
$xml_array[$val['tag']][] = xml2array($val,False);
}
}
else
{
reset($xmldata['children']);
while(list($key,$val) = each($xmldata['children']))
{
$xml_array[$val['tag']] = xml2array($val,False);
}
}
}
return $xml_array;
}
function var2xml ($name, $value,$is_root=True)
{
$node = new xmlnode($name);
switch (gettype($value))
{
case 'string':
case 'integer':
case 'double':
case 'NULL':
$node->set_text($value);
break;
case 'boolean':
if($value == True)
{
$node->set_text('1');
}
else
{
$node->set_text('0');
}
break;
case 'array':
$new_index = False;
while (list ($idxkey, $idxval) = each ($value))
{
if(is_array($idxval))
{
while (list ($k, $i) = each ($idxval))
{
if (is_int($k))
{
$new_index = True;
}
}
}
}
reset($value);
while (list ($key, $val) = each ($value))
{
if($new_index)
{
$keyname = $name;
$nextkey = $key;
}
else
{
$keyname = $key;
$nextkey = $key;
}
switch (gettype($val))
{
case 'string':
case 'integer':
case 'double':
case 'NULL':
$subnode = new xmlnode($nextkey);
$subnode->set_text($val);
$node->add_node($subnode);
break;
case 'boolean':
$subnode = new xmlnode($nextkey);
if($val == True)
{
$subnode->set_text('1');
}
else
{
$subnode->set_text('0');
}
$node->add_node($subnode);
break;
case 'array':
if($new_index)
{
while (list ($subkey, $subval) = each ($val))
{
$node->add_node(var2xml ($nextkey, $subval, False));
}
}
else
{
$subnode = var2xml ($nextkey, $val, False);
$node->add_node($subnode);
}
break;
case 'object':
$subnode = new xmlnode($nextkey);
$subnode->set_cdata('PHP_SERIALIZED_OBJECT:'.serialize($val));
$node->add_node($subnode);
break;
case 'resource':
echo 'Halt: Cannot package PHP resource pointers into XML<br>';
exit;
default:
echo 'Halt: Invalid or unknown data type<br>';
exit;
}
}
break;
case 'object':
$node->set_cdata('PHP_SERIALIZED_OBJECT:'.serialize($value));
break;
case 'resource':
echo 'Halt: Cannot package PHP resource pointers into XML<br>';
exit;
default:
echo 'Halt: Invalid or unknown data type<br>';
exit;
}
if($is_root)
{
$xmldoc = new xmldoc();
$xmldoc->add_root($node);
return $xmldoc;
}
else
{
return $node;
}
}
class xmldoc
{
var $xmlversion = '';
var $root_node;
var $doctype = Array();
var $comments = Array();
function xmldoc ($version = '1.0')
{
$this->xmlversion = $version;
}
function add_root ($node_object)
{
if(is_object($node_object))
{
$this->root_node = $node_object;
}
else
{
echo 'Not a valid xmlnode object<br>';
exit;
}
}
function set_doctype ($name, $uri = '')
{
$this->doctype[$name] = $uri;
}
function add_comment ($comment)
{
$this->comments[] = $comment;
}
function dump_mem()
{
$result = '<?xml version="'.$this->xmlversion.'"?>'."\n";
if(count($this->doctype) == 1)
{
list($doctype_name,$doctype_uri) = each($this->doctype);
$result .= '<!DOCTYPE '.$doctype_name.' SYSTEM "'.$doctype_uri.'">'."\n";
}
if(count($this->comments) > 0 )
{
reset($this->comments);
while(list($key,$val) = each ($this->comments))
{
$result .= "<!-- $val -->\n";
}
}
if(is_object($this->root_node))
{
$indent = 0;
$result .= $this->root_node->dump_mem($indent);
}
return $result;
}
}
class xmlnode
{
var $name = '';
var $data;
var $data_type;
var $attributes = Array();
var $comments = Array();
var $indentstring = " ";
function xmlnode ($name)
{
$this->name = $name;
}
function add_node ($node_object , $name = '')
{
if (is_object($node_object))
{
if(!is_array($this->data))
{
$this->data = Array();
$this->data_type = 'node';
}
if ($name != '')
{
$this->data[$name] = $node_object;
}
else
{
$this->data[] = $node_object;
}
}
else
{
if(!is_array($this->data))
{
$this->data = Array();
$this->data_type = 'node';
}
$this->data[$name] = var2xml ($name, $node_object, False);
//echo 'Not a valid xmlnode object<br>';
//exit;
}
}
function set_text ($string)
{
$this->data = $string;
$this->data_type = 'text';
}
function set_cdata ($string)
{
$this->data = $string;
$this->data_type = 'cdata';
}
function set_attribute ($name, $value = '')
{
$this->attributes[$name] = $value;
}
function add_comment ($comment)
{
$this->comments[] = $comment;
}
function dump_mem($indent = 1)
{
for ($i = 0; $i < $indent; $i++)
{
$indentstring .= $this->indentstring;
}
$result = $indentstring.'<'.$this->name;
if(count($this->attributes) > 0 )
{
reset($this->attributes);
while(list($key,$val) = each ($this->attributes))
{
$result .= ' '.$key.'="'.$val.'"';
}
}
$endtag_indent = $indentstring;
if (empty($this->data_type))
{
$result .= '/>'."\n";
}
else
{
$result .= '>';
switch ($this->data_type)
{
case 'text':
if(is_array($this->data))
{
$type_error = True;
break;
}
if(strlen($this->data) > 30)
{
$result .= "\n".$indentstring.$this->indentstring.$this->data."\n";
$endtag_indent = $indentstring;
}
else
{
$result .= $this->data;
$endtag_indent = '';
}
break;
case 'cdata':
if(is_array($this->data))
{
$type_error = True;
break;
}
$result .= '<![CDATA['.$this->data.']]>';
$endtag_indent = '';
break;
case 'node':
$result .= "\n";
if(!is_array($this->data))
{
$type_error = True;
break;
}
$subindent = $indent+1;
while(list($key,$val) = each ($this->data))
{
if(is_object($val))
{
$result .= $val->dump_mem($subindent);
}
}
break;
default:
if($this->data != '')
{
echo 'Invalid or unset data type ('.$this->data_type.'). This should not be possible if using the class as intended<br>';
}
}
if ($type_error)
{
echo 'Invalid data type. Tagged as '.$this->data_type.' but data is '.gettype($this->data).'<br>';
}
$result .= $endtag_indent.'</'.$this->name.'>';
if($indent != 0)
{
$result .= "\n";
}
}
if(count($this->comments) > 0 )
{
reset($this->comments);
while(list($key,$val) = each ($this->comments))
{
$result .= $endtag_indent."<!-- $val -->\n";
}
}
return $result;
}
}