ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 {
24  public $xmlStr;
25 
31  public $version;
32 
38  public $outEnc;
39 
45  public $inEnc;
46 
52  public $dtdDef = "";
53 
59  public $stSheet = "";
60 
66  public $genCmt = "Generated by ILIAS XmlWriter";
67 
75  public function __construct($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  public function _ilXmlWriter()
91  {
92  // terminate xml string
93  unset($this->xmlStr);
94  }
95 
101  public function xmlSetDtdDef($dtdDef)
102  {
103  $this->dtdDef = $dtdDef;
104  }
105 
111  public function xmlSetStSheet($stSheet)
112  {
113  $this->stSheet = $stSheet;
114  }
115 
121  public function xmlSetGenCmt($genCmt)
122  {
123  $this->genCmt = $genCmt;
124  }
125 
132  public static function _xmlEscapeData($data)
133  {
134  $position = 0;
135  $length = strlen($data);
136  $escapedData = "";
137 
138  for (; $position < $length;) {
139  $character = substr($data, $position, 1);
140  $code = Ord($character);
141 
142  switch ($code) {
143  case 34:
144  $character = "&quot;";
145  break;
146 
147  case 38:
148  $character = "&amp;";
149  break;
150 
151  case 39:
152  $character = "&apos;";
153  break;
154 
155  case 60:
156  $character = "&lt;";
157  break;
158 
159  case 62:
160  $character = "&gt;";
161  break;
162 
163  default:
164  if ($code < 32) {
165  $character = ("&#" . strval($code) . ";");
166  }
167  break;
168  }
169 
170  $escapedData .= $character;
171  $position++;
172  }
173  return $escapedData;
174  }
175 
182  public function xmlEncodeData($data)
183  {
184  if ($this->inEnc == $this->outEnc) {
185  $encodedData = $data;
186  } else {
187  switch (strtolower($this->outEnc)) {
188  case "utf-8":
189  if (strtolower($this->inEnc) == "iso-8859-1") {
190  $encodedData = utf8_encode($data);
191  } else {
192  die("<b>Error</b>: Cannot encode iso-8859-1 data in " . $this->outEnc .
193  " in <b>" . __FILE__ . "</b> on line <b>" . __LINE__ . "</b><br />");
194  }
195  break;
196 
197  case "iso-8859-1":
198  if (strtolower($this->inEnc) == "utf-8") {
199  $encodedData = utf8_decode($data);
200  } else {
201  die("<b>Error</b>: Cannot encode utf-8 data in " . $this->outEnc .
202  " in <b>" . __FILE__ . "</b> on line <b>" . __LINE__ . "</b><br />");
203  }
204  break;
205 
206  default:
207  die("<b>Error</b>: Cannot encode " . $this->inEnc . " data in " . $this->outEnc .
208  " in <b>" . __FILE__ . "</b> on line <b>" . __LINE__ . "</b><br />");
209  }
210  }
211  return $encodedData;
212  }
213 
220  public function xmlFormatData($data)
221  {
222  // regular expression for tags
223  $formatedXml = preg_replace_callback("|<[^>]*>[^<]*|", array($this, "xmlFormatElement"), $data);
224 
225  return $formatedXml;
226  }
227 
234  public function xmlFormatElement($array)
235  {
236  $found = trim($array[0]);
237 
238  static $indent;
239 
240  // linebreak (default)
241  $nl = "\n";
242 
243  $tab = str_repeat(" ", $indent * 2);
244 
245  // closing tag
246  if (substr($found, 0, 2) == "</") {
247  if ($indent) {
248  $indent--;
249  }
250  $tab = str_repeat(" ", $indent * 2);
251  } elseif (substr($found, -2, 1) == "/" or // opening and closing, comment, ...
252  strpos($found, "/>") or
253  substr($found, 0, 2) == "<!") {
254  // do not change indent
255  } elseif (substr($found, 0, 2) == "<?") {
256  // do not change indent
257  // no linebreak
258  $nl = "";
259  } else { // opening tag
260  $indent++;
261  }
262 
263  // content
264  if (substr($found, -1) != ">") {
265  $found = str_replace(">", ">\n" . str_repeat(" ", ($indent + 0) * 2), $found);
266  }
267 
268  return $nl . $tab . $found;
269  }
270 
275  public function xmlHeader()
276  {
277  // version and encoding
278  $this->xmlStr .= "<?xml version=\"" . $this->version . "\" encoding=\"" . $this->outEnc . "\"?>";
279 
280  // dtd definition
281  if ($this->dtdDef <> "") {
282  $this->xmlStr .= $this->dtdDef;
283  }
284 
285  // stSheet
286  if ($this->stSheet <> "") {
287  $this->xmlStr .= $this->stSheet;
288  }
289 
290  // generated comment
291  if ($this->genCmt <> "") {
292  $this->xmlComment($this->genCmt);
293  }
294 
295  return $xmlStr;
296  }
297 
307  public function xmlStartTag($tag, $attrs = null, $empty = false, $encode = true, $escape = true)
308  {
309  // write first part of the starttag
310  $this->xmlStr .= "<" . $tag;
311 
312  // check for existing attributes
313  if (is_array($attrs)) {
314  // write attributes
315  foreach ($attrs as $name => $value) {
316  // encode
317  if ($encode) {
318  $value = $this->xmlEncodeData($value);
319  }
320 
321  // escape
322  if ($escape) {
323  $value = ilXmlWriter::_xmlEscapeData($value);
324  }
325 
326  $this->xmlStr .= " " . $name . "=\"" . $value . "\"";
327  }
328  }
329 
330  // write last part of the starttag
331  if ($empty) {
332  $this->xmlStr .= "/>";
333  } else {
334  $this->xmlStr .= ">";
335  }
336  }
337 
343  public function xmlEndTag($tag)
344  {
345  $this->xmlStr .= "</" . $tag . ">";
346  }
347 
353  public function xmlComment($comment)
354  {
355  $this->xmlStr .= "<!--" . $comment . "-->";
356  }
357 
365  public function xmlData($data, $encode = true, $escape = true)
366  {
367  // encode
368  if ($encode) {
369  $data = $this->xmlEncodeData($data);
370  }
371 
372  // escape
373  if ($escape) {
375  }
376 
377  $this->xmlStr .= $data;
378  }
379 
389  public function xmlElement($tag, $attrs = null, $data = null, $encode = true, $escape = true)
390  {
391  // check for existing data (element's content)
392  if (is_string($data) or
393  is_integer($data) or
394  is_float($data)) {
395  // write starttag
396  $this->xmlStartTag($tag, $attrs, false, $encode, $escape);
397 
398  // write text
399  $this->xmlData($data, $encode, $escape);
400 
401  // write endtag
402  $this->xmlEndTag($tag);
403  } else { // no data
404  // write starttag (= empty tag)
405  $this->xmlStartTag($tag, $attrs, true, $encode, $escape);
406  }
407  }
408 
415  public function xmlDumpFile($file, $format = true)
416  {
417  // open file
418  if (!($fp = @fopen($file, "w+"))) {
419  die("<b>Error</b>: Could not open \"" . $file . "\" for writing" .
420  " in <b>" . __FILE__ . "</b> on line <b>" . __LINE__ . "</b><br />");
421  }
422 
423  // set file permissions
424  chmod($file, 0770);
425 
426  // format xml data
427  if ($format) {
428  $xmlStr = $this->xmlFormatData($this->xmlStr);
429  } else {
431  }
432 
433  // write xml data into the file
434  fwrite($fp, $xmlStr);
435 
436  // close file
437  fclose($fp);
438  }
439 
446  public function xmlDumpMem($format = true)
447  {
448  // format xml data
449  if ($format) {
450  $xmlStr = $this->xmlFormatData($this->xmlStr);
451  } else {
453  }
454 
455  return $xmlStr;
456  }
457 
461  public function appendXML($a_str)
462  {
463  $this->xmlStr .= $a_str;
464  }
465 
470  public function xmlClear()
471  {
472  // reset xml string
473  $this->xmlStr = "";
474  }
475 }
xmlStartTag($tag, $attrs=null, $empty=false, $encode=true, $escape=true)
Writes a starttag.
xmlSetGenCmt($genCmt)
Sets generated comment.
$format
Definition: metadata.php:141
xmlSetDtdDef($dtdDef)
Sets dtd definition.
$code
Definition: example_050.php:99
xmlDumpMem($format=true)
Returns xml document from memory.
XML writer class.
static _xmlEscapeData($data)
Escapes reserved characters.
xmlEncodeData($data)
Encodes text from input encoding into output encoding.
xmlData($data, $encode=true, $escape=true)
Writes data.
xmlEndTag($tag)
Writes an endtag.
__construct($version="1.0", $outEnc="utf-8", $inEnc="utf-8")
constructor
xmlFormatElement($array)
Callback function for xmlFormatData; do not invoke directly.
xmlHeader()
Writes xml header public.
$comment
Definition: buildRTE.php:83
xmlSetStSheet($stSheet)
Sets stylesheet.
xmlFormatData($data)
Indents text for better reading.
xmlElement($tag, $attrs=null, $data=null, $encode=true, $escape=true)
Writes a basic element (no children, just textual content)
appendXML($a_str)
append xml string to document
xmlClear()
clears xmlStr public
if(function_exists('posix_getuid') &&posix_getuid()===0) if(!array_key_exists('t', $options)) $tag
Definition: cron.php:35
xmlDumpFile($file, $format=true)
Dumps xml document from memory into a file.
xmlComment($comment)
Writes a comment.
$data
Definition: bench.php:6
_ilXmlWriter()
destructor public