00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00037 class ilXmlWriter
00038 {
00044 var $xmlStr;
00045
00051 var $version;
00052
00058 var $outEnc;
00059
00065 var $inEnc;
00066
00072 var $dtdDef = "";
00073
00079 var $stSheet = "";
00080
00086 var $genCmt = "Generated by ILIAS XmlWriter";
00087
00095 function ilXmlWriter ($version = "1.0", $outEnc = "utf-8", $inEnc = "utf-8")
00096 {
00097
00098 $this->xmlStr = "";
00099
00100
00101 $this->version = $version;
00102 $this->outEnc = $outEnc;
00103 $this->inEnc = $inEnc;
00104 }
00105
00110 function _ilXmlWriter ()
00111 {
00112
00113 unset($this->xmlStr);
00114 }
00115
00121 function xmlSetDtdDef ($dtdDef)
00122 {
00123 $this->dtdDef = $dtdDef;
00124 }
00125
00131 function xmlSetStSheet ($stSheet)
00132 {
00133 $this->stSheet = $stSheet;
00134 }
00135
00141 function xmlSetGenCmt ($genCmt)
00142 {
00143 $this->genCmt = $genCmt;
00144 }
00145
00152 function _xmlEscapeData($data)
00153 {
00154 $position = 0;
00155 $length = strlen($data);
00156 $escapedData = "";
00157
00158 for(; $position < $length;)
00159 {
00160 $character = substr($data, $position, 1);
00161 $code = Ord($character);
00162
00163 switch($code)
00164 {
00165 case 34:
00166 $character = """;
00167 break;
00168
00169 case 38:
00170 $character = "&";
00171 break;
00172
00173 case 39:
00174 $character = "'";
00175 break;
00176
00177 case 60:
00178 $character = "<";
00179 break;
00180
00181 case 62:
00182 $character = ">";
00183 break;
00184
00185 default:
00186 if ($code < 32)
00187 {
00188 $character = ("&#".strval($code).";");
00189 }
00190 break;
00191 }
00192
00193 $escapedData .= $character;
00194 $position ++;
00195 }
00196 return $escapedData;
00197 }
00198
00205 function xmlEncodeData($data)
00206 {
00207 if ($this->inEnc == $this->outEnc)
00208 {
00209 $encodedData = $data;
00210 }
00211 else
00212 {
00213 switch(strtolower($this->outEnc))
00214 {
00215 case "utf-8":
00216 if(strtolower($this->inEnc) == "iso-8859-1")
00217 {
00218 $encodedData = utf8_encode($data);
00219 }
00220 else
00221 {
00222 die ("<b>Error</b>: Cannot encode iso-8859-1 data in ".$this->outEnc.
00223 " in <b>".__FILE__."</b> on line <b>".__LINE__."</b><br />");
00224 }
00225 break;
00226
00227 case "iso-8859-1":
00228 if(strtolower($this->inEnc) == "utf-8")
00229 {
00230 $encodedData = utf8_decode($data);
00231 }
00232 else
00233 {
00234 die ("<b>Error</b>: Cannot encode utf-8 data in ".$this->outEnc.
00235 " in <b>".__FILE__."</b> on line <b>".__LINE__."</b><br />");
00236 }
00237 break;
00238
00239 default:
00240 die ("<b>Error</b>: Cannot encode ".$this->inEnc." data in ".$this->outEnc.
00241 " in <b>".__FILE__."</b> on line <b>".__LINE__."</b><br />");
00242 }
00243 }
00244 return $encodedData;
00245 }
00246
00253 function xmlFormatData($data)
00254 {
00255
00256 $formatedXml = preg_replace_callback("|<[^>]*>[^<]*|", array($this, "xmlFormatElement"), $data);
00257
00258 return $formatedXml;
00259 }
00260
00267 function xmlFormatElement($array)
00268 {
00269 $found = trim($array[0]);
00270
00271 static $indent;
00272
00273
00274 $nl = "\n";
00275
00276 $tab = str_repeat(" ", $indent * 2);
00277
00278
00279 if (substr($found, 0, 2) == "</")
00280 {
00281 if($indent)
00282 {
00283 $indent --;
00284 }
00285 $tab = str_repeat(" ", $indent * 2);
00286 }
00287 elseif (substr($found, -2, 1) == "/" or
00288 strpos($found, "/>") or
00289 substr($found, 0, 2) == "<!")
00290 {
00291
00292 }
00293 elseif (substr($found, 0, 2) == "<?")
00294 {
00295
00296
00297 $nl = "";
00298 }
00299 else
00300 {
00301 $indent ++;
00302 }
00303
00304
00305 if (substr($found, -1) != ">")
00306 {
00307 $found = str_replace(">", ">\n".str_repeat(" ", ($indent + 0) * 2), $found);
00308 }
00309
00310 return $nl.$tab.$found;
00311 }
00312
00317 function xmlHeader()
00318 {
00319
00320 $this->xmlStr .= "<?xml version=\"".$this->version."\" encoding=\"".$this->outEnc."\"?>";
00321
00322
00323 if ($this->dtdDef <> "")
00324 {
00325 $this->xmlStr .= $this->dtdDef;
00326 }
00327
00328
00329 if ($this->stSheet <> "")
00330 {
00331 $this->xmlStr .= $this->stSheet;
00332 }
00333
00334
00335 if ($this->genCmt <> "")
00336 {
00337 $this->xmlComment($this->genCmt);
00338 }
00339
00340 return $xmlStr;
00341 }
00342
00352 function xmlStartTag ($tag, $attrs = NULL, $empty = FALSE, $encode = TRUE, $escape = TRUE)
00353 {
00354
00355 $this->xmlStr .= "<".$tag;
00356
00357
00358 if (is_array($attrs))
00359 {
00360
00361 foreach ($attrs as $name => $value)
00362 {
00363
00364 if ($encode)
00365 {
00366 $value = $this->xmlEncodeData($value);
00367 }
00368
00369
00370 if ($escape)
00371 {
00372 $value = ilXmlWriter::_xmlEscapeData($value);
00373 }
00374
00375 $this->xmlStr .= " ".$name."=\"".$value."\"";
00376 }
00377 }
00378
00379
00380 if ($empty)
00381 {
00382 $this->xmlStr .= "/>";
00383 }
00384 else
00385 {
00386 $this->xmlStr .= ">";
00387 }
00388 }
00389
00395 function xmlEndTag ($tag)
00396 {
00397 $this->xmlStr .= "</".$tag.">";
00398 }
00399
00405 function xmlComment ($comment)
00406 {
00407 $this->xmlStr .= "<!--".$comment."-->";
00408 }
00409
00417 function xmlData ($data, $encode = TRUE, $escape = TRUE)
00418 {
00419
00420 if ($encode)
00421 {
00422 $data = $this->xmlEncodeData($data);
00423 }
00424
00425
00426 if ($escape)
00427 {
00428 $data = ilXmlWriter::_xmlEscapeData($data);
00429 }
00430
00431 $this->xmlStr .= $data;
00432 }
00433
00443 function xmlElement ($tag, $attrs = NULL, $data = Null, $encode = TRUE, $escape = TRUE)
00444 {
00445
00446 if (is_string($data) or
00447 is_integer($data))
00448 {
00449
00450 $this->xmlStartTag($tag, $attrs, FALSE, $encode, $escape);
00451
00452
00453 $this->xmlData($data, $encode, $escape);
00454
00455
00456 $this->xmlEndTag($tag);
00457 }
00458 else
00459 {
00460
00461 $this->xmlStartTag($tag, $attrs, TRUE, $encode, $escape);
00462 }
00463 }
00464
00471 function xmlDumpFile($file, $format = TRUE)
00472 {
00473
00474 if (!($fp = @fopen($file,"w+")))
00475 {
00476 die ("<b>Error</b>: Could not open \"".$file."\" for writing".
00477 " in <b>".__FILE__."</b> on line <b>".__LINE__."</b><br />");
00478 }
00479
00480
00481 chmod($file, 0770);
00482
00483
00484 if ($format)
00485 {
00486 $xmlStr = $this->xmlFormatData($this->xmlStr);
00487 }
00488 else
00489 {
00490 $xmlStr = $this->xmlStr;
00491 }
00492
00493
00494 fwrite($fp, $xmlStr);
00495
00496
00497 fclose($fp);
00498 }
00499
00506 function xmlDumpMem($format = TRUE)
00507 {
00508
00509 if ($format)
00510 {
00511 $xmlStr = $this->xmlFormatData($this->xmlStr);
00512 }
00513 else
00514 {
00515 $xmlStr = $this->xmlStr;
00516 }
00517
00518 return $xmlStr;
00519 }
00520
00524 function appendXML($a_str)
00525 {
00526 $this->xmlStr .= $a_str;
00527 }
00528
00533 function xmlClear ()
00534 {
00535
00536 $this->xmlStr = "";
00537 }
00538
00539 }
00540
00541 ?>