• 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 
00037 class ilSaxParser
00038 {
00046         var $input_type = null;
00047 
00054         var $xml_content = '';
00055 
00061         var $ilias;
00062 
00068         var $lng;
00069 
00075         var $xml_file;
00076 
00082         var $throwException = false;
00088         function ilSaxParser($a_xml_file = '', $throwException = false)
00089         {
00090                 global $ilias, $lng;
00091 
00092                 if($a_xml_file)
00093                 {
00094                         $this->xml_file = $a_xml_file;
00095                         $this->input_type = 'file';
00096                 }
00097 
00098                 $this->throwException = $throwException;
00099                 $this->ilias = &$ilias;
00100                 $this->lng = &$lng;
00101         }
00102 
00103         function setXMLContent($a_xml_content)
00104         {
00105                 $this->xml_content = $a_xml_content;
00106                 $this->input_type = 'string';
00107         }
00108         
00109         function getXMLContent()
00110         {
00111                 return $this->xml_content;
00112         }
00113 
00114         function getInputType()
00115         {
00116                 return $this->input_type;
00117         }
00118 
00125         function startParsing()
00126         {
00127                 $xml_parser = $this->createParser();
00128                 $this->setOptions($xml_parser);
00129                 $this->setHandlers($xml_parser);
00130 
00131                 switch($this->getInputType())
00132                 {
00133                         case 'file':
00134                                 $fp = $this->openXMLFile();
00135                                 $this->parse($xml_parser,$fp);
00136                                 break;
00137 
00138                         case 'string':
00139                                 $this->parse($xml_parser);
00140                                 break;
00141 
00142                         default:
00143                                 $this->handleError("No input type given. Set filename in constructor or choose setXMLContent()",
00144                                                                                  $this->ilias->error_obj->FATAL);
00145                                 break;
00146                 }
00147                 $this->freeParser($xml_parser);
00148         }
00154         function createParser()
00155         {
00156                 $xml_parser = xml_parser_create("UTF-8");
00157 
00158                 if($xml_parser == false)
00159                 {
00160                         $this->handleError("Cannot create an XML parser handle",$this->ilias->error_obj->FATAL);
00161                 }
00162                 return $xml_parser;
00163         }
00169         function setOptions($a_xml_parser)
00170         {
00171                 xml_parser_set_option($a_xml_parser,XML_OPTION_CASE_FOLDING,false);
00172         }
00178         function setHandlers($a_xml_parser)
00179         {
00180                 echo 'ilSaxParser::setHandlers() must be overwritten';
00181         }
00187         function openXMLFile()
00188         {
00189                 if(!($fp = fopen($this->xml_file,'r')))
00190                 {
00191                         $this->handleError("Cannot open xml file",$this->ilias->error_obj->FATAL);
00192                 }
00193                 return $fp;
00194         }
00200         function parse($a_xml_parser,$a_fp = null)
00201         {
00202                 switch($this->getInputType())
00203                 {
00204                         case 'file':
00205 
00206                                 while($data = fread($a_fp,4096))
00207                                 {
00208                                         $parseOk = xml_parse($a_xml_parser,$data,feof($a_fp));
00209                                 }
00210                                 break;
00211                                 
00212                         case 'string':
00213                                 $parseOk = xml_parse($a_xml_parser,$this->getXMLContent());
00214                                 break;
00215                 }
00216                 if(!$parseOk
00217                    && (xml_get_error_code($a_xml_parser) != XML_ERROR_NONE))
00218                 {
00219                         $errorCode = xml_get_error_code($a_xml_parser);
00220                         $line = xml_get_current_line_number($a_xml_parser);
00221                         $col = xml_get_current_column_number($a_xml_parser);                    
00222                         $this->handleError("XML Parse Error: ".xml_error_string($errorCode)." at line ".$line.", col ". $col . " (Code: ".$errorCode.")" ,$this->ilias->error_obj->FATAL);
00223                 }
00224                 return true;
00225                                 
00226         }
00227         
00234         protected function handleError($message, $code) {
00235                 if ($this->throwException) {
00236                         require_once ('./classes/class.ilSaxParserException.php');                      
00237                         throw new ilSaxParserException ($message, $code);
00238                 } else {
00239                         $this->ilias->raiseError($message, $code);
00240                 }
00241                 return false;
00242         }
00248         function freeParser($a_xml_parser)
00249         {
00250                 if(!xml_parser_free($a_xml_parser))
00251                 {
00252                         $this->ilias->raiseError("Error freeing xml parser handle ",$this->ilias->error_obj->FATAL);
00253                 }
00254         }
00255         
00261         public function setThrowException ($throwException) 
00262         {
00263                 $this->throwException = $throwException;
00264         }
00265 }
00266 ?>

Generated on Fri Dec 13 2013 17:56:48 for ILIAS Release_3_9_x_branch .rev 46835 by  doxygen 1.7.1