Go to the documentation of this file.00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00036 class ilSaxParser
00037 {
00045 var $input_type = null;
00046
00053 var $xml_content = '';
00054
00060 var $ilias;
00061
00067 var $lng;
00068
00074 var $xml_file;
00075
00081 function ilSaxParser($a_xml_file = '')
00082 {
00083 global $ilias, $lng;
00084
00085 if($a_xml_file)
00086 {
00087 $this->xml_file = $a_xml_file;
00088 $this->input_type = 'file';
00089 }
00090
00091 $this->ilias = &$ilias;
00092 $this->lng = &$lng;
00093 }
00094
00095 function setXMLContent($a_xml_content)
00096 {
00097 $this->xml_content = $a_xml_content;
00098 $this->input_type = 'string';
00099 }
00100
00101 function getXMLContent()
00102 {
00103 return $this->xml_content;
00104 }
00105
00106 function getInputType()
00107 {
00108 return $this->input_type;
00109 }
00110
00116 function startParsing()
00117 {
00118 $xml_parser = $this->createParser();
00119 $this->setOptions($xml_parser);
00120 $this->setHandlers($xml_parser);
00121
00122 switch($this->getInputType())
00123 {
00124 case 'file':
00125 $fp = $this->openXMLFile();
00126 $this->parse($xml_parser,$fp);
00127 break;
00128
00129 case 'string':
00130 $this->parse($xml_parser);
00131 break;
00132
00133 default:
00134 $this->ilias->raiseError("No input type given. Set filename in constructor or choose setXMLContent()",
00135 $this->ilias->error_obj->FATAL);
00136 break;
00137 }
00138 $this->freeParser($xml_parser);
00139 }
00145 function createParser()
00146 {
00147 $xml_parser = xml_parser_create("UTF-8");
00148
00149 if($xml_parser == false)
00150 {
00151 $this->ilias->raiseError("Cannot create an XML parser handle",$this->ilias->error_obj->FATAL);
00152 }
00153 return $xml_parser;
00154 }
00160 function setOptions($a_xml_parser)
00161 {
00162 xml_parser_set_option($a_xml_parser,XML_OPTION_CASE_FOLDING,false);
00163 }
00169 function setHandlers($a_xml_parser)
00170 {
00171 echo 'ilSaxParser::setHandlers() must be overwritten';
00172 }
00178 function openXMLFile()
00179 {
00180 if(!($fp = fopen($this->xml_file,'r')))
00181 {
00182 $this->ilias->raiseError("Cannot open xml file",$this->ilias->error_obj->FATAL);
00183 }
00184 return $fp;
00185 }
00191 function parse($a_xml_parser,$a_fp = null)
00192 {
00193 switch($this->getInputType())
00194 {
00195 case 'file':
00196
00197 while($data = fread($a_fp,4096))
00198 {
00199 $parseOk = xml_parse($a_xml_parser,$data,feof($a_fp));
00200 }
00201 break;
00202
00203 case 'string':
00204 $parseOk = xml_parse($a_xml_parser,$this->getXMLContent());
00205 break;
00206 }
00207 if(!$parseOk
00208 && (xml_get_error_code($a_xml_parser) != XML_ERROR_NONE))
00209 {
00210 $this->ilias->raiseError("XML Parse Error: ".xml_get_error_code($a_xml_parser),$this->ilias->error_obj->FATAL);
00211 }
00212 return true;
00213
00214 }
00220 function freeParser($a_xml_parser)
00221 {
00222 if(!xml_parser_free($a_xml_parser))
00223 {
00224 $this->ilias->raiseError("Error freeing xml parser handle ",$this->ilias->error_obj->FATAL);
00225 }
00226 }
00227 }
00228 ?>