00001 <?php
00002
00003 class aliroXMLException extends Exception {
00004
00005 public function __construct ($message) {
00006 parent::__construct($message, 0);
00007 }
00008
00009 }
00010
00011 class aliroXML {
00012 protected $xmlobject = null;
00013 protected $maintag = '';
00014 protected $valid = true;
00015
00016 public function __get ($property) {
00017 if (is_null($this->xmlobject) OR is_null($result = $this->xmlobject->$property)) return null;
00018 return $result;
00019 }
00020
00021 public function baseAttribute ($attribute) {
00022 return (string) $this->xmlobject[$attribute];
00023 }
00024
00025 public function loadFile ($xmlfile, $attribs=0) {
00026 if (!file_exists($xmlfile)) throw new aliroXMLException(sprintf(T_('Requested XML file %s does not exist'), $xmlfile));
00027 if (!is_readable($xmlfile)) throw new aliroXMLException(sprintf(T_('Requested XML file %s is not readable'), $xmlfile));
00028 $string = file_get_contents($xmlfile);
00029 return $this->loadString($string, $attribs);
00030 }
00031
00032 public function loadString ($xmlstring, $attribs=0) {
00033 $ampencode = '/(&(?!(#[0-9]{1,5};))(?!([0-9a-zA-Z]{1,10};)))/';
00034 $xmlstring = preg_replace($ampencode, '&', $xmlstring);
00035 $tag = preg_match('/(<(?!\?)(?!\!)[^> ]*)/', $xmlstring, $matches);
00036 if ($tag) $this->maintag = substr($matches[0],1);
00037 else throw new aliroXMLException(T_('XML Handler cannot find main tag'));
00038 $filename = ('install' == $this->maintag) ? 'josinstall' : $this->maintag;
00039 $filename = _ALIRO_ABSOLUTE_PATH.'/xml/'.$filename.'.dtd';
00040 if (!file_exists($filename)) throw new aliroXMLException(T_('XML Handler - no matching DTD'));
00041 $href = 'file://'.$filename;
00042 $xmlstring = '<?xml version="1.0" encoding="utf-8"?>'
00043 ."<!DOCTYPE $this->maintag SYSTEM \"$href\">"
00044 .strstr($xmlstring, $matches[0]);
00045 set_error_handler(array($this, 'xmlerror'));
00046 $this->xmlobject = simplexml_load_string($xmlstring, null, LIBXML_DTDVALID);
00047 restore_error_handler();
00048 return $this->valid;
00049 }
00050
00051 public function xmlerror ($errno, $errmsg) {
00052 $this->valid = false;
00053 $split = explode('parser error :', $errmsg);
00054 if (isset($split[1])) $errordetail = T_(' parser error: ').$split[1];
00055 else $errordetail = T_(' non-parser XML error').$errmsg;
00056 throw new aliroXMLException(T_('An XML processing error occurred in class aliroXML'.$errordetail));
00057 }
00058
00059 public function getXML ($properties) {
00060 $ps = explode('->', $properties);
00061 $obj = $this->xmlobject;
00062 foreach ($ps as $p) {
00063 if (is_null($obj)) return null;
00064 if ('[' == $p[0] AND ']' == substr($p,-1)) return (string)$obj[substr($p,1,-1)];
00065 if (is_null($obj = $obj->$p)) return null;
00066 }
00067 return $obj;
00068 }
00069
00070 public static function attribArray ($xmlobject) {
00071 $result = array();
00072 foreach ($xmlobject->attributes() as $key=>$value) $result[$key] = (string) $value;
00073 return $result;
00074 }
00075
00076 public static function elementArray ($xmlobject) {
00077 $result = array();
00078 foreach ($xmlobject as $element) $result[] = (string) $element;
00079 return $result;
00080 }
00081
00082 }