ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilSaxParser.php
Go to the documentation of this file.
1<?php
2
25abstract class ilSaxParser
26{
27 private const TYPE_FILE = 'file';
28 private const TYPE_STRING = 'string';
29
35 private string $input_type;
36
40 private string $xml_content;
41
42 protected ?ilLanguage $lng = null;
43
44 public string $xml_file;
45
46 public bool $throw_exception = false;
47
48
49 public function __construct(
50 ?string $path_to_file = '',
51 ?bool $throw_exception = false
52 ) {
53 global $DIC;
54
55 if ($path_to_file !== null && $path_to_file !== '') {
56 $this->xml_file = $path_to_file;
57 $this->input_type = self::TYPE_FILE;
58 } else {
59 $this->input_type = self::TYPE_STRING;
60 $this->xml_content = '';
61 }
62
63 $this->throw_exception = $throw_exception ?? false;
64 $this->lng = $DIC->isDependencyAvailable('language')
65 ? $DIC->language()
66 : null;
67 }
68
69 public function setXMLContent(string $a_xml_content): void
70 {
71 $this->xml_content = $a_xml_content;
72 $this->input_type = self::TYPE_STRING;
73 }
74
75 public function getXMLContent(): string
76 {
77 return $this->xml_content;
78 }
79
80 public function getInputType(): string
81 {
82 return $this->input_type;
83 }
84
89 public function startParsing(): void
90 {
91 $xml_parser = $this->createParser();
92 $this->setOptions($xml_parser);
93 $this->setHandlers($xml_parser);
94
95 switch ($this->getInputType()) {
96 case self::TYPE_FILE:
97 $fp = $this->openXMLFile();
98 $this->parse($xml_parser, $fp);
99 break;
100
102 $this->parse($xml_parser);
103 break;
104
105 default:
106 $this->handleError(
107 "No input type given. Set filename in constructor or choose setXMLContent()"
108 );
109 break;
110 }
111 $this->freeParser($xml_parser);
112 }
113
118 public function createParser()
119 {
120 $xml_parser = xml_parser_create("UTF-8");
121 if (!is_resource($xml_parser) && !is_object($xml_parser)) {
122 $this->handleError("Cannot create an XML parser handle");
123 }
124 return $xml_parser;
125 }
126
127
128 private function setOptions($a_xml_parser): void
129 {
130 xml_parser_set_option($a_xml_parser, XML_OPTION_CASE_FOLDING, false);
131 }
132
137 abstract public function setHandlers($a_xml_parser): void;
138
143 protected function openXMLFile()
144 {
145 if (!($fp = fopen($this->xml_file, 'r'))) {
146 $this->handleError("Cannot open xml file \"" . $this->xml_file . "\"");
147 }
148 return $fp;
149 }
150
156 public function parse($a_xml_parser, $a_fp = null): void
157 {
158 $parse_status = true;
159 switch ($this->getInputType()) {
160 case self::TYPE_FILE:
161 while ($data = fread($a_fp, 4096)) {
162 $parse_status = xml_parse($a_xml_parser, $data, feof($a_fp));
163 }
164 break;
165
167 $parse_status = xml_parse($a_xml_parser, $this->getXMLContent());
168 break;
169 }
170 $error_code = xml_get_error_code($a_xml_parser);
171 if (!$parse_status && ($error_code !== XML_ERROR_NONE)) {
172 $error = sprintf(
173 "XML Parse Error: %s at line %s, col %s (Code: %s)",
174 xml_error_string($error_code),
175 xml_get_current_line_number($a_xml_parser),
176 xml_get_current_column_number($a_xml_parser),
177 $error_code
178 );
179
180 $this->handleError($error);
181 }
182 }
183
187 protected function handleError(string $message): void
188 {
189 if ($this->throw_exception) {
191 }
192 }
193
198 private function freeParser($a_xml_parser): void
199 {
200 if (!xml_parser_free($a_xml_parser)) {
201 $this->handleError("Error freeing xml parser handle");
202 }
203 }
204
205
206 protected function setThrowException(bool $throw_exception): void
207 {
208 $this->throw_exception = $throw_exception;
209 }
210}
language handling
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
freeParser($a_xml_parser)
parse($a_xml_parser, $a_fp=null)
ilLanguage $lng
string $xml_content
XML-Content in case of content type 'string'.
setXMLContent(string $a_xml_content)
handleError(string $message)
setThrowException(bool $throw_exception)
startParsing()
stores xml data in array
setHandlers($a_xml_parser)
__construct(?string $path_to_file='', ?bool $throw_exception=false)
string $input_type
XML-Content type 'file' or 'string' If you choose file set the filename in constructor If you choose ...
setOptions($a_xml_parser)
global $DIC
Definition: shib_login.php:26
$message
Definition: xapiexit.php:31