• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

classes/class.ilSaxParser.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003         +-----------------------------------------------------------------------------+
00004         | ILIAS open source                                                           |
00005         +-----------------------------------------------------------------------------+
00006         | Copyright (c) 1998-2001 ILIAS open source, University of Cologne            |
00007         |                                                                             |
00008         | This program is free software; you can redistribute it and/or               |
00009         | modify it under the terms of the GNU General Public License                 |
00010         | as published by the Free Software Foundation; either version 2              |
00011         | of the License, or (at your option) any later version.                      |
00012         |                                                                             |
00013         | This program is distributed in the hope that it will be useful,             |
00014         | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
00015         | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
00016         | GNU General Public License for more details.                                |
00017         |                                                                             |
00018         | You should have received a copy of the GNU General Public License           |
00019         | along with this program; if not, write to the Free Software                 |
00020         | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
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 ?>

Generated on Fri Dec 13 2013 13:52:08 for ILIAS Release_3_7_x_branch .rev 46817 by  doxygen 1.7.1