ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilSaxParser.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
24 
38 {
46  var $input_type = null;
47 
54  var $xml_content = '';
55 
61  var $ilias;
62 
68  var $lng;
69 
75  var $xml_file;
76 
82  var $throwException = false;
88  function ilSaxParser($a_xml_file = '', $throwException = false)
89  {
90  global $ilias, $lng;
91 
92  if($a_xml_file)
93  {
94  $this->xml_file = $a_xml_file;
95  $this->input_type = 'file';
96  }
97 
98  $this->throwException = $throwException;
99  $this->ilias = &$ilias;
100  $this->lng = &$lng;
101  }
102 
103  function setXMLContent($a_xml_content)
104  {
105  $this->xml_content = $a_xml_content;
106  $this->input_type = 'string';
107  }
108 
109  function getXMLContent()
110  {
111  return $this->xml_content;
112  }
113 
114  function getInputType()
115  {
116  return $this->input_type;
117  }
118 
125  function startParsing()
126  {
127  $xml_parser = $this->createParser();
128  $this->setOptions($xml_parser);
129  $this->setHandlers($xml_parser);
130 
131  switch($this->getInputType())
132  {
133  case 'file':
134  $fp = $this->openXMLFile();
135  $this->parse($xml_parser,$fp);
136  break;
137 
138  case 'string':
139  $this->parse($xml_parser);
140  break;
141 
142  default:
143  $this->handleError("No input type given. Set filename in constructor or choose setXMLContent()",
144  $this->ilias->error_obj->FATAL);
145  break;
146  }
147  $this->freeParser($xml_parser);
148  }
154  function createParser()
155  {
156  $xml_parser = xml_parser_create("UTF-8");
157 
158  if($xml_parser == false)
159  {
160  $this->handleError("Cannot create an XML parser handle",$this->ilias->error_obj->FATAL);
161  }
162  return $xml_parser;
163  }
169  function setOptions($a_xml_parser)
170  {
171  xml_parser_set_option($a_xml_parser,XML_OPTION_CASE_FOLDING,false);
172  }
178  function setHandlers($a_xml_parser)
179  {
180  echo 'ilSaxParser::setHandlers() must be overwritten';
181  }
187  function openXMLFile()
188  {
189  if(!($fp = fopen($this->xml_file,'r')))
190  {
191  $this->handleError("Cannot open xml file \"".$this->xml_file."\"",$this->ilias->error_obj->FATAL);
192  }
193  return $fp;
194  }
200  function parse($a_xml_parser,$a_fp = null)
201  {
202  switch($this->getInputType())
203  {
204  case 'file':
205 
206  while($data = fread($a_fp,4096))
207  {
208  $parseOk = xml_parse($a_xml_parser,$data,feof($a_fp));
209  }
210  break;
211 
212  case 'string':
213  $parseOk = xml_parse($a_xml_parser,$this->getXMLContent());
214  break;
215  }
216  if(!$parseOk
217  && (xml_get_error_code($a_xml_parser) != XML_ERROR_NONE))
218  {
219  $errorCode = xml_get_error_code($a_xml_parser);
220  $line = xml_get_current_line_number($a_xml_parser);
221  $col = xml_get_current_column_number($a_xml_parser);
222  $this->handleError("XML Parse Error: ".xml_error_string($errorCode)." at line ".$line.", col ". $col . " (Code: ".$errorCode.")" ,$this->ilias->error_obj->FATAL);
223  }
224  return true;
225 
226  }
227 
234  protected function handleError($message, $code) {
235  if ($this->throwException) {
236  require_once ('./classes/class.ilSaxParserException.php');
237  throw new ilSaxParserException ($message, $code);
238  } else {
239  if (is_object($this->ilias))
240  {
241  $this->ilias->raiseError($message, $code);
242  }
243  else
244  {
245  die($message);
246  }
247  }
248  return false;
249  }
255  function freeParser($a_xml_parser)
256  {
257  if(!xml_parser_free($a_xml_parser))
258  {
259  $this->ilias->raiseError("Error freeing xml parser handle ",$this->ilias->error_obj->FATAL);
260  }
261  }
262 
269  {
270  $this->throwException = $throwException;
271  }
272 }
273 ?>