ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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 var $input_type = null;
27
34 var $xml_content = '';
35
41 var $ilias;
42
48 var $lng;
49
56
62 var $throwException = false;
68 function ilSaxParser($a_xml_file = '', $throwException = false)
69 {
70 global $ilias, $lng;
71
72 if($a_xml_file)
73 {
74 $this->xml_file = $a_xml_file;
75 $this->input_type = 'file';
76 }
77
78 $this->throwException = $throwException;
79 $this->ilias = &$ilias;
80 $this->lng = &$lng;
81 }
82
83 function setXMLContent($a_xml_content)
84 {
85 $this->xml_content = $a_xml_content;
86 $this->input_type = 'string';
87 }
88
89 function getXMLContent()
90 {
91 return $this->xml_content;
92 }
93
94 function getInputType()
95 {
96 return $this->input_type;
97 }
98
105 function startParsing()
106 {
107 $xml_parser = $this->createParser();
108 $this->setOptions($xml_parser);
109 $this->setHandlers($xml_parser);
110
111 switch($this->getInputType())
112 {
113 case 'file':
114 $fp = $this->openXMLFile();
115 $this->parse($xml_parser,$fp);
116 break;
117
118 case 'string':
119 $this->parse($xml_parser);
120 break;
121
122 default:
123 $this->handleError("No input type given. Set filename in constructor or choose setXMLContent()",
124 $this->ilias->error_obj->FATAL);
125 break;
126 }
127 $this->freeParser($xml_parser);
128 }
135 function createParser()
136 {
137 $xml_parser = xml_parser_create("UTF-8");
138
139 if($xml_parser == false)
140 {
141 $this->handleError("Cannot create an XML parser handle",$this->ilias->error_obj->FATAL);
142 }
143 return $xml_parser;
144 }
150 function setOptions($a_xml_parser)
151 {
152 xml_parser_set_option($a_xml_parser,XML_OPTION_CASE_FOLDING,false);
153 }
159 function setHandlers($a_xml_parser)
160 {
161 echo 'ilSaxParser::setHandlers() must be overwritten';
162 }
169 function openXMLFile()
170 {
171 if(!($fp = fopen($this->xml_file,'r')))
172 {
173 $this->handleError("Cannot open xml file \"".$this->xml_file."\"",$this->ilias->error_obj->FATAL);
174 }
175 return $fp;
176 }
183 function parse($a_xml_parser,$a_fp = null)
184 {
185 switch($this->getInputType())
186 {
187 case 'file':
188
189 while($data = fread($a_fp,4096))
190 {
191 $parseOk = xml_parse($a_xml_parser,$data,feof($a_fp));
192 }
193 break;
194
195 case 'string':
196 $parseOk = xml_parse($a_xml_parser,$this->getXMLContent());
197 break;
198 }
199 if(!$parseOk
200 && (xml_get_error_code($a_xml_parser) != XML_ERROR_NONE))
201 {
202 $errorCode = xml_get_error_code($a_xml_parser);
203 $line = xml_get_current_line_number($a_xml_parser);
204 $col = xml_get_current_column_number($a_xml_parser);
205 $this->handleError("XML Parse Error: ".xml_error_string($errorCode)." at line ".$line.", col ". $col . " (Code: ".$errorCode.")" ,$this->ilias->error_obj->FATAL);
206 }
207 return true;
208
209 }
210
218 protected function handleError($message, $code) {
219 if ($this->throwException) {
220 require_once ('./Services/Xml/exceptions/class.ilSaxParserException.php');
221 throw new ilSaxParserException ($message, $code);
222 } else {
223 if (is_object($this->ilias))
224 {
225 $this->ilias->raiseError($message, $code);
226 }
227 else
228 {
229 die($message);
230 }
231 }
232 return false;
233 }
239 function freeParser($a_xml_parser)
240 {
241 if(!xml_parser_free($a_xml_parser))
242 {
243 $this->ilias->raiseError("Error freeing xml parser handle ",$this->ilias->error_obj->FATAL);
244 }
245 }
246
252 public function setThrowException ($throwException)
253 {
254 $this->throwException = $throwException;
255 }
256}
257?>
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
ilSaxParser($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
$data
$code
Definition: example_050.php:99
redirection script todo: (a better solution should control the processing via a xml file)