|
Posted by Olivier on 05/10/05 02:18
Hi coders!
first of all :
<?php
//abstract classed used for all XML parsers on this site
abstract class MERLINXMLTemplate {
private $tpl;//the actual xml resource
private $file;//path to file
private $xmldata;//file_get_contents
private $status = 0;//TODO define these symbols:
/*0 - OK 1 - FILE_NOT_FOUND, 2 - FILE_NOT_READABLE, 3 -
EXTERNAL_LOAD_HACK
4 - ILLEGAL_ENCODING, 5 - START_END_HANDLER_SETUP_ERROR, 6 -
DATA_HANDLER_SETUP_ERROR
*/
private $encoding = 'ISO-8859-1';
function MERLINXMLTemplate()
{
$this -> __construct();
register_shutdown_function(array($this,"__destruct"));
}
function __construct()
{
//constructor
$this->tpl = xml_parser_create($this->encoding);
echo self;
}
function __destruct()
{
//destructor
}
function free() {
xml_parser_free($this->tpl);
}
function load($file)
{
//REGEX below checks stops URL wrappers
if(!preg_match_all("/\b:\/\/.*\b/si",$file,$t))
{
if(file_exists($file))
{
if(is_readable($file))
{
//file is there, ready to get read
$this->xmldata = file_get_contents($file);
}
else
{
$this->status = 2;
//TODO error handling, file not readable (use exceptions?)
}
}
else
{
$this->status = 1;
//TODO error handling, file not found (use Exceptions?)
}
}
else
{
$this->status=3;
//TODO error handling, internal hacking attempt - url wrapper (use
exceptions?)
}
}
function getStatus() {
return $this->status;
}
function getEncoding(){
return $this->encoding;
}
function setEncoding($encoding)
{
switch($encoding)
{
case 'ISO-8859-1':
$this->encoding = 'ISO-8859-1';
break;
case 'UTF-8':
$this->encoding = 'UTF-8';
break;
case 'US-ASCII';
$this->encoding = 'US-ASCII';
break;
default:
//TODO don't change encoding, but report attack, change status
$this->status = 4;
}
}
//method used to parse it
function parse()
{
if(xml_set_element_handler($this->tpl,array($this,"shdl"),array($this,"ehdl")))
{
if(xml_set_character_data_handler($this->tpl,array($this,"dhdl")))
{
$t = xml_parse($this->tpl,$this->xmldata);
if($t == FALSE)
{
//TODO ERROR handling
echo 'error: '.xml_get_error_code($this->tpl).' =
'.xml_error_string(xml_get_error_code($this->tpl));
}
else
{
//all data parsed ok
}
}
else
{
//TODO error reporting, could not set data handler
$this->status = 6;
}
}
else
{
//TODO error reporting, could not set start and end handlers
$this->status = 5;
}
}
abstract private function shdl($tpl,$name,$attrs);
abstract private function ehdl($tpl,$name);
abstract private function dhdl($tpl,$data);
}
class MERLINXMLLayout extends MERLINXMLTemplate
{
function shdl($tpl,$name,$attrs)
{
//echo
}
function ehdl($tpl,$name)
{
//do stuff
}
function dhdl($tpl,$data)
{
//do stuff
}
}
$xml = new MERLINXMLLayout();
$xml -> load("example.xml");
$xml -> parse();
$xml -> free();
?>
the problem is everything below the setEncoding() method. php says:
Fatal error: Cannot call abstract method MERLINXMLTemplate::shdl() in /file/
on line x
but i want xml_set_element_handler() to call the methods defined in the
inherited class.
Anyone who has any idea of how this could be achieved?
Thanks
[Back to original message]
|