ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
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 }
112
117 public function createParser()
118 {
119 $xml_parser = xml_parser_create("UTF-8");
120 if (!is_resource($xml_parser) && !is_object($xml_parser)) {
121 $this->handleError("Cannot create an XML parser handle");
122 }
123 return $xml_parser;
124 }
125
126
127 private function setOptions($a_xml_parser): void
128 {
129 xml_parser_set_option($a_xml_parser, XML_OPTION_CASE_FOLDING, false);
130 }
131
136 abstract public function setHandlers($a_xml_parser): void;
137
142 protected function openXMLFile()
143 {
144 if (!($fp = fopen($this->xml_file, 'r'))) {
145 $this->handleError("Cannot open xml file \"" . $this->xml_file . "\"");
146 }
147 return $fp;
148 }
149
155 public function parse($a_xml_parser, $a_fp = null): void
156 {
157 $parse_status = true;
158 switch ($this->getInputType()) {
159 case self::TYPE_FILE:
160 while ($data = fread($a_fp, 4096)) {
161 $parse_status = xml_parse($a_xml_parser, $data, feof($a_fp));
162 }
163 break;
164
166 $parse_status = xml_parse($a_xml_parser, $this->getXMLContent());
167 break;
168 }
169 $error_code = xml_get_error_code($a_xml_parser);
170 if (!$parse_status && ($error_code !== XML_ERROR_NONE)) {
171 $error = sprintf(
172 "XML Parse Error: %s at line %s, col %s (Code: %s)",
173 xml_error_string($error_code),
174 xml_get_current_line_number($a_xml_parser),
175 xml_get_current_column_number($a_xml_parser),
176 $error_code
177 );
178
179 $this->handleError($error);
180 }
181 }
182
186 protected function handleError(string $message): void
187 {
188 if ($this->throw_exception) {
189 throw new ilSaxParserException($message);
190 }
191 }
192
193 protected function setThrowException(bool $throw_exception): void
194 {
195 $this->throw_exception = $throw_exception;
196 }
197}
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...
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