ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilXmlWriter.php
Go to the documentation of this file.
1<?php
33{
34 private string $xmlStr;
35
36 private string $version;
37
38 private string $outEnc;
39
40 private string $inEnc;
41
42 private string $dtdDef = "";
43
44 private string $stSheet = "";
45
46 private string $genCmt = "Generated by ILIAS XmlWriter";
47
48 public function __construct(
49 string $version = "1.0",
50 string $outEnc = "utf-8",
51 string $inEnc = "utf-8"
52 ) {
53 // initialize xml string
54 $this->xmlStr = "";
55
56 // set properties
57 $this->version = $version;
58 $this->outEnc = $outEnc;
59 $this->inEnc = $inEnc;
60 }
61
65 public function xmlSetDtdDef(string $dtdDef): void
66 {
67 $this->dtdDef = $dtdDef;
68 }
69
73 private function xmlSetStSheet(string $stSheet): void
74 {
75 $this->stSheet = $stSheet;
76 }
77
81 public function xmlSetGenCmt(string $genCmt): void
82 {
83 $this->genCmt = $genCmt;
84 }
85
89 private function xmlEscapeData(string $data): string
90 {
91 $position = 0;
92 $length = strlen($data);
93 $escapedData = "";
94
95 for (; $position < $length;) {
96 $character = substr($data, $position, 1);
97 $code = Ord($character);
98
99 switch ($code) {
100 case 34:
101 $character = "&quot;";
102 break;
103
104 case 38:
105 $character = "&amp;";
106 break;
107
108 case 39:
109 $character = "&apos;";
110 break;
111
112 case 60:
113 $character = "&lt;";
114 break;
115
116 case 62:
117 $character = "&gt;";
118 break;
119
120 default:
121 if ($code < 32) {
122 $character = ("&#" . $code . ";");
123 }
124 break;
125 }
126
127 $escapedData .= $character;
128 $position++;
129 }
130 return $escapedData;
131 }
132
136 private function xmlEncodeData(string $data): string
137 {
138 if ($this->inEnc == $this->outEnc) {
139 $encodedData = $data;
140 } else {
141 throw new ilException(
142 'Differing in and out-encodings are not supported.'
143 );
144 }
145 return $encodedData;
146 }
147
151 public function xmlFormatData(string $data): ?string
152 {
153 // regular expression for tags
154 $formatedXml = preg_replace_callback(
155 "|<[^>]*>[^<]*|",
156 [$this, "xmlFormatElement"],
157 $data
158 );
159
160 return $formatedXml;
161 }
162
166 private function xmlFormatElement(array $array): string
167 {
168 $found = trim($array[0]);
169
170 static $indent;
171
172 // linebreak (default)
173 $nl = "\n";
174
175 $tab = str_repeat(" ", $indent * 2);
176
177 // closing tag
178 if (substr($found, 0, 2) == "</") {
179 if ($indent) {
180 $indent--;
181 }
182 $tab = str_repeat(" ", $indent * 2);
183 } elseif (substr(
184 $found,
185 -2,
186 1
187 ) == "/" or // opening and closing, comment, ...
188 strpos($found, "/>") or
189 substr($found, 0, 2) == "<!") {
190 // do not change indent
191 } elseif (substr($found, 0, 2) == "<?") {
192 // do not change indent
193 // no linebreak
194 $nl = "";
195 } else { // opening tag
196 $indent++;
197 }
198
199 // content
200 if (substr($found, -1) != ">") {
201 $found = str_replace(
202 ">",
203 ">\n" . str_repeat(" ", ($indent + 0) * 2),
204 $found
205 );
206 }
207
208 return $nl . $tab . $found;
209 }
210
214 public function xmlHeader(): void
215 {
216 // version and encoding
217 $this->xmlStr .= "<?xml version=\"" . $this->version . "\" encoding=\"" . $this->outEnc . "\"?>";
218
219 // dtd definition
220 if ($this->dtdDef <> "") {
221 $this->xmlStr .= $this->dtdDef;
222 }
223
224 // stSheet
225 if ($this->stSheet <> "") {
226 $this->xmlStr .= $this->stSheet;
227 }
228
229 // generated comment
230 if ($this->genCmt <> "") {
231 $this->xmlComment($this->genCmt);
232 }
233 }
234
238 public function xmlStartTag(
239 string $tag,
240 ?array $attrs = null,
241 bool $empty = false,
242 bool $encode = true,
243 bool $escape = true
244 ): void {
245 // write first part of the starttag
246 $this->xmlStr .= "<" . $tag;
247
248 // check for existing attributes
249 if (is_array($attrs)) {
250 // write attributes
251 foreach ($attrs as $name => $value) {
252 if ($value === null) {
253 $value = '';
254 }
255
256 // encode
257 if ($encode) {
258 $value = $this->xmlEncodeData($value);
259 }
260
261 // escape
262 if ($escape) {
263 $value = $this->xmlEscapeData($value);
264 }
265
266 $this->xmlStr .= " " . $name . "=\"" . $value . "\"";
267 }
268 }
269
270 // write last part of the starttag
271 if ($empty) {
272 $this->xmlStr .= "/>";
273 } else {
274 $this->xmlStr .= ">";
275 }
276 }
277
281 public function xmlEndTag(string $tag): void
282 {
283 $this->xmlStr .= "</" . $tag . ">";
284 }
285
289 private function xmlComment(string $comment): void
290 {
291 $this->xmlStr .= "<!--" . $comment . "-->";
292 }
293
297 public function xmlData(
298 string $data,
299 bool $encode = true,
300 bool $escape = true
301 ): void {
302 // encode
303 if ($encode) {
304 $data = $this->xmlEncodeData($data);
305 }
306
307 // escape
308 if ($escape) {
309 $data = $this->xmlEscapeData($data);
310 }
311
312 $this->xmlStr .= $data;
313 }
314
318 public function xmlElement(
319 string $tag,
320 $attrs = null,
321 $data = null,
322 $encode = true,
323 $escape = true
324 ): void {
325 // check for existing data (element's content)
326 if (is_string($data) or
327 is_integer($data) or
328 is_float($data)) {
329 // write starttag
330 $this->xmlStartTag($tag, $attrs, false, $encode, $escape);
331
332 // write text
333 $this->xmlData($data, $encode, $escape);
334
335 // write endtag
336 $this->xmlEndTag($tag);
337 } else { // no data
338 // write starttag (= empty tag)
339 $this->xmlStartTag($tag, $attrs, true, $encode, $escape);
340 }
341 }
342
346 public function xmlDumpFile(string $file, bool $format = true): void
347 {
348 // open file
349 if (!($fp = fopen($file, "w+"))) {
350 throw new RuntimeException(
351 "<b>Error</b>: Could not open \"" . $file . "\" for writing" .
352 " in <b>" . __FILE__ . "</b> on line <b>" . __LINE__ . "</b><br />"
353 );
354 }
355
356 // set file permissions
357 chmod($file, 0770);
358
359 // format xml data
360 if ($format) {
361 $xmlStr = $this->xmlFormatData($this->xmlStr);
362 } else {
363 $xmlStr = $this->xmlStr;
364 }
365
366 // write xml data into the file
367 fwrite($fp, $xmlStr);
368
369 // close file
370 fclose($fp);
371 }
372
376 public function xmlDumpMem(bool $format = true): string
377 {
378 // format xml data
379 if ($format) {
380 $xmlStr = $this->xmlFormatData($this->xmlStr);
381 } else {
382 $xmlStr = $this->xmlStr;
383 }
384
385 return $xmlStr;
386 }
387
391 public function appendXML(string $a_str): void
392 {
393 $this->xmlStr .= $a_str;
394 }
395
399 public function xmlClear(): void
400 {
401 // reset xml string
402 $this->xmlStr = "";
403 }
404}
$comment
Definition: buildRTE.php:72
Base class for ILIAS Exception handling.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
xmlSetGenCmt(string $genCmt)
Sets generated comment.
xmlElement(string $tag, $attrs=null, $data=null, $encode=true, $escape=true)
Writes a basic element (no children, just textual content)
__construct(string $version="1.0", string $outEnc="utf-8", string $inEnc="utf-8")
xmlClear()
clears xmlStr
xmlHeader()
Writes xml header.
xmlEndTag(string $tag)
Writes an endtag.
xmlSetDtdDef(string $dtdDef)
Sets dtd definition.
xmlDumpMem(bool $format=true)
Returns xml document from memory.
xmlData(string $data, bool $encode=true, bool $escape=true)
Writes data.
xmlEscapeData(string $data)
Escapes reserved characters.
xmlEncodeData(string $data)
Encodes text from input encoding into output encoding.
xmlSetStSheet(string $stSheet)
Sets stylesheet.
xmlComment(string $comment)
Writes a comment.
xmlFormatElement(array $array)
Callback function for xmlFormatData; do not invoke directly.
appendXML(string $a_str)
append xml string to document
xmlFormatData(string $data)
Indents text for better reading.
xmlStartTag(string $tag, ?array $attrs=null, bool $empty=false, bool $encode=true, bool $escape=true)
Writes a starttag.
xmlDumpFile(string $file, bool $format=true)
Dumps xml document from memory into a file.
if(!file_exists('../ilias.ini.php'))