ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
class.ilXmlWriter.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
4
18{
25
32
39
45 var $inEnc;
46
52 var $dtdDef = "";
53
59 var $stSheet = "";
60
66 var $genCmt = "Generated by ILIAS XmlWriter";
67
75 function ilXmlWriter ($version = "1.0", $outEnc = "utf-8", $inEnc = "utf-8")
76 {
77 // initialize xml string
78 $this->xmlStr = "";
79
80 // set properties
81 $this->version = $version;
82 $this->outEnc = $outEnc;
83 $this->inEnc = $inEnc;
84 }
85
90 function _ilXmlWriter ()
91 {
92 // terminate xml string
93 unset($this->xmlStr);
94 }
95
102 {
103 $this->dtdDef = $dtdDef;
104 }
105
112 {
113 $this->stSheet = $stSheet;
114 }
115
122 {
123 $this->genCmt = $genCmt;
124 }
125
133 {
134 $position = 0;
135 $length = strlen($data);
136 $escapedData = "";
137
138 for(; $position < $length;)
139 {
140 $character = substr($data, $position, 1);
141 $code = Ord($character);
142
143 switch($code)
144 {
145 case 34:
146 $character = "&quot;";
147 break;
148
149 case 38:
150 $character = "&amp;";
151 break;
152
153 case 39:
154 $character = "&apos;";
155 break;
156
157 case 60:
158 $character = "&lt;";
159 break;
160
161 case 62:
162 $character = "&gt;";
163 break;
164
165 default:
166 if ($code < 32)
167 {
168 $character = ("&#".strval($code).";");
169 }
170 break;
171 }
172
173 $escapedData .= $character;
174 $position ++;
175 }
176 return $escapedData;
177 }
178
186 {
187 if ($this->inEnc == $this->outEnc)
188 {
189 $encodedData = $data;
190 }
191 else
192 {
193 switch(strtolower($this->outEnc))
194 {
195 case "utf-8":
196 if(strtolower($this->inEnc) == "iso-8859-1")
197 {
198 $encodedData = utf8_encode($data);
199 }
200 else
201 {
202 die ("<b>Error</b>: Cannot encode iso-8859-1 data in ".$this->outEnc.
203 " in <b>".__FILE__."</b> on line <b>".__LINE__."</b><br />");
204 }
205 break;
206
207 case "iso-8859-1":
208 if(strtolower($this->inEnc) == "utf-8")
209 {
210 $encodedData = utf8_decode($data);
211 }
212 else
213 {
214 die ("<b>Error</b>: Cannot encode utf-8 data in ".$this->outEnc.
215 " in <b>".__FILE__."</b> on line <b>".__LINE__."</b><br />");
216 }
217 break;
218
219 default:
220 die ("<b>Error</b>: Cannot encode ".$this->inEnc." data in ".$this->outEnc.
221 " in <b>".__FILE__."</b> on line <b>".__LINE__."</b><br />");
222 }
223 }
224 return $encodedData;
225 }
226
234 {
235 // regular expression for tags
236 $formatedXml = preg_replace_callback("|<[^>]*>[^<]*|", array($this, "xmlFormatElement"), $data);
237
238 return $formatedXml;
239 }
240
247 function xmlFormatElement($array)
248 {
249 $found = trim($array[0]);
250
251 static $indent;
252
253 // linebreak (default)
254 $nl = "\n";
255
256 $tab = str_repeat(" ", $indent * 2);
257
258 // closing tag
259 if (substr($found, 0, 2) == "</")
260 {
261 if($indent)
262 {
263 $indent --;
264 }
265 $tab = str_repeat(" ", $indent * 2);
266 }
267 elseif (substr($found, -2, 1) == "/" or // opening and closing, comment, ...
268 strpos($found, "/>") or
269 substr($found, 0, 2) == "<!")
270 {
271 // do not change indent
272 }
273 elseif (substr($found, 0, 2) == "<?")
274 {
275 // do not change indent
276 // no linebreak
277 $nl = "";
278 }
279 else // opening tag
280 {
281 $indent ++;
282 }
283
284 // content
285 if (substr($found, -1) != ">")
286 {
287 $found = str_replace(">", ">\n".str_repeat(" ", ($indent + 0) * 2), $found);
288 }
289
290 return $nl.$tab.$found;
291 }
292
297 function xmlHeader()
298 {
299 // version and encoding
300 $this->xmlStr .= "<?xml version=\"".$this->version."\" encoding=\"".$this->outEnc."\"?>";
301
302 // dtd definition
303 if ($this->dtdDef <> "")
304 {
305 $this->xmlStr .= $this->dtdDef;
306 }
307
308 // stSheet
309 if ($this->stSheet <> "")
310 {
311 $this->xmlStr .= $this->stSheet;
312 }
313
314 // generated comment
315 if ($this->genCmt <> "")
316 {
317 $this->xmlComment($this->genCmt);
318 }
319
320 return $xmlStr;
321 }
322
332 function xmlStartTag ($tag, $attrs = NULL, $empty = FALSE, $encode = TRUE, $escape = TRUE)
333 {
334 // write first part of the starttag
335 $this->xmlStr .= "<".$tag;
336
337 // check for existing attributes
338 if (is_array($attrs))
339 {
340 // write attributes
341 foreach ($attrs as $name => $value)
342 {
343 // encode
344 if ($encode)
345 {
346 $value = $this->xmlEncodeData($value);
347 }
348
349 // escape
350 if ($escape)
351 {
352 $value = ilXmlWriter::_xmlEscapeData($value);
353 }
354
355 $this->xmlStr .= " ".$name."=\"".$value."\"";
356 }
357 }
358
359 // write last part of the starttag
360 if ($empty)
361 {
362 $this->xmlStr .= "/>";
363 }
364 else
365 {
366 $this->xmlStr .= ">";
367 }
368 }
369
375 function xmlEndTag ($tag)
376 {
377 $this->xmlStr .= "</".$tag.">";
378 }
379
386 {
387 $this->xmlStr .= "<!--".$comment."-->";
388 }
389
397 function xmlData ($data, $encode = TRUE, $escape = TRUE)
398 {
399 // encode
400 if ($encode)
401 {
402 $data = $this->xmlEncodeData($data);
403 }
404
405 // escape
406 if ($escape)
407 {
409 }
410
411 $this->xmlStr .= $data;
412 }
413
423 function xmlElement ($tag, $attrs = NULL, $data = Null, $encode = TRUE, $escape = TRUE)
424 {
425 // check for existing data (element's content)
426 if (is_string($data) or
427 is_integer($data) or
428 is_float($data))
429 {
430 // write starttag
431 $this->xmlStartTag($tag, $attrs, FALSE, $encode, $escape);
432
433 // write text
434 $this->xmlData($data, $encode, $escape);
435
436 // write endtag
437 $this->xmlEndTag($tag);
438 }
439 else // no data
440 {
441 // write starttag (= empty tag)
442 $this->xmlStartTag($tag, $attrs, TRUE, $encode, $escape);
443 }
444 }
445
452 function xmlDumpFile($file, $format = TRUE)
453 {
454 // open file
455 if (!($fp = @fopen($file,"w+")))
456 {
457 die ("<b>Error</b>: Could not open \"".$file."\" for writing".
458 " in <b>".__FILE__."</b> on line <b>".__LINE__."</b><br />");
459 }
460
461 // set file permissions
462 chmod($file, 0770);
463
464 // format xml data
465 if ($format)
466 {
467 $xmlStr = $this->xmlFormatData($this->xmlStr);
468 }
469 else
470 {
472 }
473
474 // write xml data into the file
475 fwrite($fp, $xmlStr);
476
477 // close file
478 fclose($fp);
479 }
480
487 function xmlDumpMem($format = TRUE)
488 {
489 // format xml data
490 if ($format)
491 {
492 $xmlStr = $this->xmlFormatData($this->xmlStr);
493 }
494 else
495 {
497 }
498
499 return $xmlStr;
500 }
501
505 function appendXML($a_str)
506 {
507 $this->xmlStr .= $a_str;
508 }
509
514 function xmlClear ()
515 {
516 // reset xml string
517 $this->xmlStr = "";
518 }
519
520}
521
522?>
print $file
$comment
Definition: buildRTE.php:83
XML writer class.
xmlEndTag($tag)
Writes an endtag.
xmlData($data, $encode=TRUE, $escape=TRUE)
Writes data.
xmlFormatData($data)
Indents text for better reading.
ilXmlWriter($version="1.0", $outEnc="utf-8", $inEnc="utf-8")
constructor
xmlDumpMem($format=TRUE)
Returns xml document from memory.
xmlSetGenCmt($genCmt)
Sets generated comment.
xmlDumpFile($file, $format=TRUE)
Dumps xml document from memory into a file.
xmlStartTag($tag, $attrs=NULL, $empty=FALSE, $encode=TRUE, $escape=TRUE)
Writes a starttag.
xmlClear()
clears xmlStr @access public
xmlHeader()
Writes xml header @access public.
xmlEncodeData($data)
Encodes text from input encoding into output encoding.
appendXML($a_str)
append xml string to document
xmlSetStSheet($stSheet)
Sets stylesheet.
_ilXmlWriter()
destructor @access public
xmlComment($comment)
Writes a comment.
xmlSetDtdDef($dtdDef)
Sets dtd definition.
_xmlEscapeData($data)
Escapes reserved characters.
xmlFormatElement($array)
Callback function for xmlFormatData; do not invoke directly.
xmlElement($tag, $attrs=NULL, $data=Null, $encode=TRUE, $escape=TRUE)
Writes a basic element (no children, just textual content)
$data
$code
Definition: example_050.php:99