ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilXmlWriter.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  +-----------------------------------------------------------------------------+
5  | ILIAS open source |
6  +-----------------------------------------------------------------------------+
7  | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
8  | |
9  | This program is free software; you can redistribute it and/or |
10  | modify it under the terms of the GNU General Public License |
11  | as published by the Free Software Foundation; either version 2 |
12  | of the License, or (at your option) any later version. |
13  | |
14  | This program is distributed in the hope that it will be useful, |
15  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
16  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17  | GNU General Public License for more details. |
18  | |
19  | You should have received a copy of the GNU General Public License |
20  | along with this program; if not, write to the Free Software |
21  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
22  +-----------------------------------------------------------------------------+
23 */
24 
38 {
44  var $xmlStr;
45 
51  var $version;
52 
58  var $outEnc;
59 
65  var $inEnc;
66 
72  var $dtdDef = "";
73 
79  var $stSheet = "";
80 
86  var $genCmt = "Generated by ILIAS XmlWriter";
87 
95  function ilXmlWriter ($version = "1.0", $outEnc = "utf-8", $inEnc = "utf-8")
96  {
97  // initialize xml string
98  $this->xmlStr = "";
99 
100  // set properties
101  $this->version = $version;
102  $this->outEnc = $outEnc;
103  $this->inEnc = $inEnc;
104  }
105 
110  function _ilXmlWriter ()
111  {
112  // terminate xml string
113  unset($this->xmlStr);
114  }
115 
122  {
123  $this->dtdDef = $dtdDef;
124  }
125 
132  {
133  $this->stSheet = $stSheet;
134  }
135 
142  {
143  $this->genCmt = $genCmt;
144  }
145 
153  {
154  $position = 0;
155  $length = strlen($data);
156  $escapedData = "";
157 
158  for(; $position < $length;)
159  {
160  $character = substr($data, $position, 1);
161  $code = Ord($character);
162 
163  switch($code)
164  {
165  case 34:
166  $character = "&quot;";
167  break;
168 
169  case 38:
170  $character = "&amp;";
171  break;
172 
173  case 39:
174  $character = "&apos;";
175  break;
176 
177  case 60:
178  $character = "&lt;";
179  break;
180 
181  case 62:
182  $character = "&gt;";
183  break;
184 
185  default:
186  if ($code < 32)
187  {
188  $character = ("&#".strval($code).";");
189  }
190  break;
191  }
192 
193  $escapedData .= $character;
194  $position ++;
195  }
196  return $escapedData;
197  }
198 
206  {
207  if ($this->inEnc == $this->outEnc)
208  {
209  $encodedData = $data;
210  }
211  else
212  {
213  switch(strtolower($this->outEnc))
214  {
215  case "utf-8":
216  if(strtolower($this->inEnc) == "iso-8859-1")
217  {
218  $encodedData = utf8_encode($data);
219  }
220  else
221  {
222  die ("<b>Error</b>: Cannot encode iso-8859-1 data in ".$this->outEnc.
223  " in <b>".__FILE__."</b> on line <b>".__LINE__."</b><br />");
224  }
225  break;
226 
227  case "iso-8859-1":
228  if(strtolower($this->inEnc) == "utf-8")
229  {
230  $encodedData = utf8_decode($data);
231  }
232  else
233  {
234  die ("<b>Error</b>: Cannot encode utf-8 data in ".$this->outEnc.
235  " in <b>".__FILE__."</b> on line <b>".__LINE__."</b><br />");
236  }
237  break;
238 
239  default:
240  die ("<b>Error</b>: Cannot encode ".$this->inEnc." data in ".$this->outEnc.
241  " in <b>".__FILE__."</b> on line <b>".__LINE__."</b><br />");
242  }
243  }
244  return $encodedData;
245  }
246 
254  {
255  // regular expression for tags
256  $formatedXml = preg_replace_callback("|<[^>]*>[^<]*|", array($this, "xmlFormatElement"), $data);
257 
258  return $formatedXml;
259  }
260 
267  function xmlFormatElement($array)
268  {
269  $found = trim($array[0]);
270 
271  static $indent;
272 
273  // linebreak (default)
274  $nl = "\n";
275 
276  $tab = str_repeat(" ", $indent * 2);
277 
278  // closing tag
279  if (substr($found, 0, 2) == "</")
280  {
281  if($indent)
282  {
283  $indent --;
284  }
285  $tab = str_repeat(" ", $indent * 2);
286  }
287  elseif (substr($found, -2, 1) == "/" or // opening and closing, comment, ...
288  strpos($found, "/>") or
289  substr($found, 0, 2) == "<!")
290  {
291  // do not change indent
292  }
293  elseif (substr($found, 0, 2) == "<?")
294  {
295  // do not change indent
296  // no linebreak
297  $nl = "";
298  }
299  else // opening tag
300  {
301  $indent ++;
302  }
303 
304  // content
305  if (substr($found, -1) != ">")
306  {
307  $found = str_replace(">", ">\n".str_repeat(" ", ($indent + 0) * 2), $found);
308  }
309 
310  return $nl.$tab.$found;
311  }
312 
317  function xmlHeader()
318  {
319  // version and encoding
320  $this->xmlStr .= "<?xml version=\"".$this->version."\" encoding=\"".$this->outEnc."\"?>";
321 
322  // dtd definition
323  if ($this->dtdDef <> "")
324  {
325  $this->xmlStr .= $this->dtdDef;
326  }
327 
328  // stSheet
329  if ($this->stSheet <> "")
330  {
331  $this->xmlStr .= $this->stSheet;
332  }
333 
334  // generated comment
335  if ($this->genCmt <> "")
336  {
337  $this->xmlComment($this->genCmt);
338  }
339 
340  return $xmlStr;
341  }
342 
352  function xmlStartTag ($tag, $attrs = NULL, $empty = FALSE, $encode = TRUE, $escape = TRUE)
353  {
354  // write first part of the starttag
355  $this->xmlStr .= "<".$tag;
356 
357  // check for existing attributes
358  if (is_array($attrs))
359  {
360  // write attributes
361  foreach ($attrs as $name => $value)
362  {
363  // encode
364  if ($encode)
365  {
366  $value = $this->xmlEncodeData($value);
367  }
368 
369  // escape
370  if ($escape)
371  {
372  $value = ilXmlWriter::_xmlEscapeData($value);
373  }
374 
375  $this->xmlStr .= " ".$name."=\"".$value."\"";
376  }
377  }
378 
379  // write last part of the starttag
380  if ($empty)
381  {
382  $this->xmlStr .= "/>";
383  }
384  else
385  {
386  $this->xmlStr .= ">";
387  }
388  }
389 
395  function xmlEndTag ($tag)
396  {
397  $this->xmlStr .= "</".$tag.">";
398  }
399 
405  function xmlComment ($comment)
406  {
407  $this->xmlStr .= "<!--".$comment."-->";
408  }
409 
417  function xmlData ($data, $encode = TRUE, $escape = TRUE)
418  {
419  // encode
420  if ($encode)
421  {
422  $data = $this->xmlEncodeData($data);
423  }
424 
425  // escape
426  if ($escape)
427  {
429  }
430 
431  $this->xmlStr .= $data;
432  }
433 
443  function xmlElement ($tag, $attrs = NULL, $data = Null, $encode = TRUE, $escape = TRUE)
444  {
445  // check for existing data (element's content)
446  if (is_string($data) or
447  is_integer($data))
448  {
449  // write starttag
450  $this->xmlStartTag($tag, $attrs, FALSE, $encode, $escape);
451 
452  // write text
453  $this->xmlData($data, $encode, $escape);
454 
455  // write endtag
456  $this->xmlEndTag($tag);
457  }
458  else // no data
459  {
460  // write starttag (= empty tag)
461  $this->xmlStartTag($tag, $attrs, TRUE, $encode, $escape);
462  }
463  }
464 
471  function xmlDumpFile($file, $format = TRUE)
472  {
473  // open file
474  if (!($fp = @fopen($file,"w+")))
475  {
476  die ("<b>Error</b>: Could not open \"".$file."\" for writing".
477  " in <b>".__FILE__."</b> on line <b>".__LINE__."</b><br />");
478  }
479 
480  // set file permissions
481  chmod($file, 0770);
482 
483  // format xml data
484  if ($format)
485  {
486  $xmlStr = $this->xmlFormatData($this->xmlStr);
487  }
488  else
489  {
491  }
492 
493  // write xml data into the file
494  fwrite($fp, $xmlStr);
495 
496  // close file
497  fclose($fp);
498  }
499 
506  function xmlDumpMem($format = TRUE)
507  {
508  // format xml data
509  if ($format)
510  {
511  $xmlStr = $this->xmlFormatData($this->xmlStr);
512  }
513  else
514  {
516  }
517 
518  return $xmlStr;
519  }
520 
524  function appendXML($a_str)
525  {
526  $this->xmlStr .= $a_str;
527  }
528 
533  function xmlClear ()
534  {
535  // reset xml string
536  $this->xmlStr = "";
537  }
538 
539 }
540 
541 ?>