<?php
class xml_form_up
{
var $parser = NULL;
var $result = NULL;
var $count = 0;
var $is_cdata = FALSE;
var $cdata_cache = NULL;
var $is_default = FALSE;
var $default_cache = NULL;
var $indenting = array('width' => 1,
'char' => ' ',
'dir' => STR_PAD_LEFT
);
var $tag_open = array('open' => '<',
'name' => '%s ',
'param' => '%s="%s" ',
'close_bs' => 1,
'close' => ">\n",
'pcre' => FALSE
);
var $tag_close = array('open' => '</',
'name' => '%s',
'close_bs' => 0,
'close' => ">\n",
'pcre' => FALSE
);
var $tag_cdata = array('before' => '',
'content' => '%s',
'trim' => TRUE,
'after' => "\n",
'pcre' => FALSE
);
var $tag_proc = array('open' => '<?',
'name' => '%s ',
'data' => '%s ',
'close_bs' => 1,
'close' => "?>\n",
'pcre' => FALSE
);
var $tag_unknown = array('before' => '',
'content' => '%s',
'after' => "\n",
'pcre' => array('/></', ">\n<")
);
function xml_form_up($create_parser=TRUE)
{
if( $create_parser == TRUE )
{
$this->create();
}
}
function create()
{
$this->parser = xml_parser_create();
xml_set_object($this->parser, $this);
xml_set_character_data_handler($this->parser, 'xml_character_data');
xml_set_default_handler($this->parser, 'xml_default');
xml_set_element_handler($this->parser, 'xml_start_element', 'xml_end_element');
xml_set_processing_instruction_handler($this->parser, 'xml_proc_instruction');
}
function set_option($option, $value)
{
xml_parser_set_option($this->parser, $option, $value);
}
function free()
{
xml_parser_free($this->parser);
$this->count = 0;
$this->result = NULL;
$this->is_cdata = FALSE;
$this->cdata_cache = NULL;
$this->is_default = FALSE;
$this->default_cache = FALSE;
}
function parse($data, $is_final=FALSE)
{
xml_parse($this->parser, $data, $is_final);
}
function xml_start_element($parser, $name, $attribs)
{
$this->xml_work();
$result = $this->tag_open['open'];
$result .= sprintf($this->tag_open['name'], $name);
foreach($attribs as $param => $value)
{
$result .= sprintf($this->tag_open['param'], $param, $value);
}
if( $this->tag_open['close_bs'] > 0 )
{
$len = strlen($result)-$this->tag_open['close_bs'];
$result = substr($result, 0, $len);
}
$result .= $this->tag_open['close'];
if( $this->tag_open['pcre'] !== FALSE && count($this->tag_open['pcre']) >= 2 )
{
$result = preg_replace($this->tag_open['pcre'][0], $this->tag_open['pcre'][1], $result);
}
$this->result .= str_pad($result, $this->count*$this->indenting['width']+strlen($result), $this->indenting['char'], $this->indenting['dir']);
$this->count++;
}
function xml_end_element($parser, $name)
{
$this->xml_work();
$this->count--;
$result = $this->tag_close['open'];
$result .= sprintf($this->tag_close['name'], $name);
if( $this->tag_close['close_bs'] > 0 )
{
$len = strlen($result)-$this->tag_close['close_bs'];
$result = substr($result, 0, $len);
}
$result .= $this->tag_close['close'];
if( $this->tag_close['pcre'] !== FALSE && count($this->tag_close['pcre']) >= 2 )
{
$result = preg_replace($this->tag_close['pcre'][0], $this->tag_close['pcre'][1], $result);
}
$this->result .= str_pad($result, $this->count*$this->indenting['width']+strlen($result), $this->indenting['char'], $this->indenting['dir']);
}
function xml_character_data($parser, $data)
{
$this->xml_default_work();
$this->is_cdata = TRUE;
$this->cdata_cache .= $data;
}
function xml_character_data_work()
{
if( $this->is_cdata == TRUE )
{
if( $this->tag_cdata['trim'] == TRUE )
{
$this->cdata_cache = trim($this->cdata_cache);
}
$result = $this->tag_cdata['before'].sprintf($this->tag_cdata['content'], $this->cdata_cache).$this->tag_cdata['after'];
if( $this->tag_cdata['pcre'] !== FALSE && count($this->tag_cdata['pcre']) >= 2 )
{
$result = preg_replace($this->tag_cdata['pcre'][0], $this->tag_cdata['pcre'][1], $result);
}
$this->result .= str_pad($result, $this->count*$this->indenting['width']+strlen($result), $this->indenting['char'], $this->indenting['dir']);
$this->is_cdata = FALSE;
$this->cdata_cache = NULL;
}
}
function xml_default($parser, $data)
{
$this->xml_character_data_work();
$this->is_default = TRUE;
$this->default_cache .= $data;
}
function xml_default_work()
{
if( $this->is_default == TRUE )
{
$result = $this->tag_unknown['before'].sprintf($this->tag_unknown['content'], $this->default_cache).$this->tag_unknown['after'];
if( $this->tag_unknown['pcre'] !== FALSE && count($this->tag_unknown['pcre']) >= 2 )
{
$result = preg_replace($this->tag_unknown['pcre'][0], $this->tag_unknown['pcre'][1], $result);
}
$this->result .= str_pad($result, $this->count*$this->indenting['width']+strlen($result), $this->indenting['char'], $this->indenting['dir']);
$this->is_default = FALSE;
$this->default_cache = NULL;
}
}
function xml_proc_instruction($parser, $target, $data)
{
$this->xml_work();
$result .= $this->tag_proc['open'];
$result .= sprintf($this->tag_proc['name'], $target);
$result .= sprintf($this->tag_proc['data'], $data);
if( $this->tag_proc['close_bs'] > 0 )
{
$len = strlen($result)-$this->tag_proc['close_bs'];
$result = substr($result, 0, $len);
}
$result .= $this->tag_proc['close'];
if( $this->tag_proc['pcre'] !== FALSE && count($this->tag_proc['pcre']) >= 2 )
{
$result = preg_replace($this->tag_proc['pcre'][0], $this->tag_proc['pcre'][1], $result);
}
$this->result .= str_pad($result, $this->count*$this->indenting['width']+strlen($result), $this->indenting['char'], $this->indenting['dir']);
}
function xml_work()
{
$this->xml_character_data_work();
$this->xml_default_work();
}
}
?>