ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilSaxParser.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4
18{
26 public $input_type = null;
27
34 public $xml_content = '';
35
41 public $ilias;
42
48 public $lng;
49
55 public $xml_file;
56
62 public $throwException = false;
68 public function __construct($a_xml_file = '', $throwException = false)
69 {
70 global $ilias, $lng;
71
72 if ($a_xml_file) {
73 $this->xml_file = $a_xml_file;
74 $this->input_type = 'file';
75 }
76
77 $this->throwException = $throwException;
78 $this->ilias = &$ilias;
79 $this->lng = &$lng;
80 }
81
82 public function setXMLContent($a_xml_content)
83 {
84 $this->xml_content = $a_xml_content;
85 $this->input_type = 'string';
86 }
87
88 public function getXMLContent()
89 {
90 return $this->xml_content;
91 }
92
93 public function getInputType()
94 {
95 return $this->input_type;
96 }
97
104 public function startParsing()
105 {
106 $xml_parser = $this->createParser();
107 $this->setOptions($xml_parser);
108 $this->setHandlers($xml_parser);
109
110 switch ($this->getInputType()) {
111 case 'file':
112 $fp = $this->openXMLFile();
113 $this->parse($xml_parser, $fp);
114 break;
115
116 case 'string':
117 $this->parse($xml_parser);
118 break;
119
120 default:
121 $this->handleError(
122 "No input type given. Set filename in constructor or choose setXMLContent()",
123 $this->ilias->error_obj->FATAL
124 );
125 break;
126 }
127 $this->freeParser($xml_parser);
128 }
135 public function createParser()
136 {
137 $xml_parser = xml_parser_create("UTF-8");
138
139 if ($xml_parser == false) {
140 $this->handleError("Cannot create an XML parser handle", $this->ilias->error_obj->FATAL);
141 }
142 return $xml_parser;
143 }
149 public function setOptions($a_xml_parser)
150 {
151 xml_parser_set_option($a_xml_parser, XML_OPTION_CASE_FOLDING, false);
152 }
158 public function setHandlers($a_xml_parser)
159 {
160 echo 'ilSaxParser::setHandlers() must be overwritten';
161 }
168 public function openXMLFile()
169 {
170 if (!($fp = fopen($this->xml_file, 'r'))) {
171 $this->handleError("Cannot open xml file \"" . $this->xml_file . "\"", $this->ilias->error_obj->FATAL);
172 }
173 return $fp;
174 }
181 public function parse($a_xml_parser, $a_fp = null)
182 {
183 switch ($this->getInputType()) {
184 case 'file':
185
186 while ($data = fread($a_fp, 4096)) {
187 $parseOk = xml_parse($a_xml_parser, $data, feof($a_fp));
188 }
189 break;
190
191 case 'string':
192 $parseOk = xml_parse($a_xml_parser, $this->getXMLContent());
193 break;
194 }
195 if (!$parseOk
196 && (xml_get_error_code($a_xml_parser) != XML_ERROR_NONE)) {
197 $errorCode = xml_get_error_code($a_xml_parser);
198 $line = xml_get_current_line_number($a_xml_parser);
199 $col = xml_get_current_column_number($a_xml_parser);
200 $this->handleError("XML Parse Error: " . xml_error_string($errorCode) . " at line " . $line . ", col " . $col . " (Code: " . $errorCode . ")", $this->ilias->error_obj->FATAL);
201 }
202 return true;
203 }
204
212 protected function handleError($message, $code)
213 {
214 if ($this->throwException) {
215 require_once('./Services/Xml/exceptions/class.ilSaxParserException.php');
217 } else {
218 if (is_object($this->ilias)) {
219 $this->ilias->raiseError($message, $code);
220 } else {
221 die($message);
222 }
223 }
224 return false;
225 }
231 public function freeParser($a_xml_parser)
232 {
233 if (!xml_parser_free($a_xml_parser)) {
234 $this->ilias->raiseError("Error freeing xml parser handle ", $this->ilias->error_obj->FATAL);
235 }
236 }
237
244 {
245 $this->throwException = $throwException;
246 }
247}
An exception for terminatinating execution or to throw for unit testing.
SaxParserException thrown by ilSaxParser if property throwException is set.
Base class for sax-based expat parsing extended classes need to overwrite the method setHandlers and ...
openXMLFile()
open xml file
setThrowException($throwException)
set error handling
freeParser($a_xml_parser)
free xml parser handle
parse($a_xml_parser, $a_fp=null)
parse xml file
setXMLContent($a_xml_content)
startParsing()
stores xml data in array
__construct($a_xml_file='', $throwException=false)
Constructor setup ILIAS global object @access public.
setHandlers($a_xml_parser)
set event handler should be overwritten by inherited class @access private
handleError($message, $code)
use given error handler to handle error message or internal ilias error message handle
createParser()
create parser
setOptions($a_xml_parser)
set parser options
$code
Definition: example_050.php:99
catch(Exception $e) $message
redirection script todo: (a better solution should control the processing via a xml file)
$errorCode