• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

classes/class.ilXmlWriter.php

Go to the documentation of this file.
00001 <?php
00002 
00003 /*
00004     +-----------------------------------------------------------------------------+
00005     | ILIAS open source                                                           |
00006         +-----------------------------------------------------------------------------+
00007     | Copyright (c) 1998-2001 ILIAS open source, University of Cologne            |
00008     |                                                                             |
00009     | This program is free software; you can redistribute it and/or               |
00010     | modify it under the terms of the GNU General Public License                 |
00011     | as published by the Free Software Foundation; either version 2              |
00012     | of the License, or (at your option) any later version.                      |
00013     |                                                                             |
00014     | This program is distributed in the hope that it will be useful,             |
00015     | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
00016     | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
00017     | GNU General Public License for more details.                                |
00018     |                                                                             |
00019     | You should have received a copy of the GNU General Public License           |
00020     | along with this program; if not, write to the Free Software                 |
00021     | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
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                 // initialize xml string
00098                 $this->xmlStr = "";
00099                 
00100                 // set properties
00101                 $this->version = $version;
00102                 $this->outEnc = $outEnc;
00103                 $this->inEnc = $inEnc;
00104         }
00105         
00110         function _ilXmlWriter ()
00111         {
00112                 // terminate xml string
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 = "&quot;";
00167                                         break;
00168                                 
00169                                 case 38:
00170                                         $character = "&amp;";
00171                                         break;
00172                                 
00173                                 case 39:
00174                                         $character = "&apos;";
00175                                         break;
00176                                 
00177                                 case 60:
00178                                         $character = "&lt;";
00179                                         break;
00180                                 
00181                                 case 62:
00182                                         $character = "&gt;";
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                 // regular expression for tags
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                 // linebreak (default)
00274                 $nl = "\n";
00275                 
00276                 $tab = str_repeat(" ", $indent * 2);
00277                 
00278                 // closing tag
00279                 if (substr($found, 0, 2) == "</")
00280                 {
00281                         $indent --;
00282                         $tab = str_repeat(" ", $indent * 2);
00283                 }
00284                 elseif (substr($found, -2, 1) == "/" or // opening and closing, comment, ...
00285                                 strpos($found, "/>") or
00286                                 substr($found, 0, 2) == "<!") 
00287                 {
00288                         // do not change indent
00289                 }
00290                 elseif (substr($found, 0, 2) == "<?") 
00291                 {
00292                         // do not change indent
00293                         // no linebreak
00294                         $nl = "";
00295                 }
00296                 else // opening tag
00297                 {
00298                         $indent ++;
00299                 }
00300                 
00301                 // content
00302                 if (substr($found, -1) != ">")
00303                 {
00304                         $found = str_replace(">", ">\n".str_repeat(" ", ($indent + 0) * 2), $found);
00305                 }
00306                 
00307                 return $nl.$tab.$found;
00308         }
00309         
00314         function xmlHeader()
00315         {
00316                 // version and encoding
00317                 $this->xmlStr .= "<?xml version=\"".$this->version."\" encoding=\"".$this->outEnc."\"?>";
00318                 
00319                 // dtd definition
00320                 if ($this->dtdDef <> "")
00321                 {
00322                         $this->xmlStr .= $this->dtdDef;
00323                 }
00324                 
00325                 // stSheet
00326                 if ($this->stSheet <> "")
00327                 {
00328                         $this->xmlStr .= $this->stSheet;
00329                 }
00330                 
00331                 // generated comment
00332                 if ($this->genCmt <> "")
00333                 {
00334                         $this->xmlComment($this->genCmt);
00335                 }
00336                 
00337                 return $xmlStr;
00338         }
00339         
00349         function xmlStartTag ($tag, $attrs = NULL, $empty = FALSE, $encode = TRUE, $escape = TRUE)
00350         {
00351                 // write first part of the starttag
00352                 $this->xmlStr .= "<".$tag;
00353                 
00354                 // check for existing attributes
00355                 if (is_array($attrs))
00356                 {
00357                         // write attributes
00358                         foreach ($attrs as $name => $value)
00359                         {
00360                                 // encode
00361                                 if ($encode)
00362                                 {
00363                                 $value = $this->xmlEncodeData($value);
00364                                 }
00365                                 
00366                                 // escape
00367                                 if ($escape)
00368                                 {
00369                                  $value = $this->xmlEscapeData($value);
00370                         }
00371                                 
00372                                 $this->xmlStr .= " ".$name."=\"".$value."\"";
00373                         }
00374                 }
00375                 
00376                 // write last part of the starttag
00377                 if ($empty)
00378                 {
00379                         $this->xmlStr .= "/>";
00380                 }
00381                 else
00382                 {
00383                         $this->xmlStr .= ">";
00384                 }
00385         }
00386         
00392         function xmlEndTag ($tag)
00393         {
00394                 $this->xmlStr .= "</".$tag.">";
00395         }
00396         
00402         function xmlComment ($comment)
00403         {
00404                 $this->xmlStr .= "<!--".$comment."-->";
00405         }
00406 
00414         function xmlData ($data, $encode = TRUE, $escape = TRUE)
00415         {
00416                 // encode
00417                 if ($encode)
00418                 {
00419                     $data = $this->xmlEncodeData($data);
00420                 }
00421                 
00422                 // escape
00423                 if ($escape)
00424                 {
00425                  $data = $this->xmlEscapeData($data);
00426             }
00427                 
00428                 $this->xmlStr .= $data;
00429         }
00430         
00440         function xmlElement ($tag, $attrs = NULL, $data = Null, $encode = TRUE, $escape = TRUE)
00441         {
00442                 // check for existing data (element's content)
00443                 if (is_string($data) or
00444                         is_integer($data))
00445                 {
00446                         // write starttag
00447                         $this->xmlStartTag($tag, $attrs, FALSE, $encode, $escape);
00448                         
00449                         // write text
00450                         $this->xmlData($data, $encode, $escape);
00451                         
00452                         // write endtag
00453                         $this->xmlEndTag($tag);
00454                 }
00455                 else // no data
00456                 {
00457                         // write starttag (= empty tag)
00458                         $this->xmlStartTag($tag, $attrs, TRUE, $encode, $escape);
00459                 }
00460         }
00461         
00468         function xmlDumpFile($file, $format = TRUE)
00469         {
00470                 // open file
00471                 if (!($fp = @fopen($file,"w+")))
00472                 {
00473                         die ("<b>Error</b>: Could not open \"".$file."\" for writing".
00474                                         " in <b>".__FILE__."</b> on line <b>".__LINE__."</b><br />");
00475                 }
00476                 
00477                 // set file permissions
00478                 chmod($file, 0770);
00479                 
00480                 // format xml data
00481                 if ($format)
00482                 {
00483                         $xmlStr = $this->xmlFormatData($this->xmlStr);
00484                 }
00485                 else
00486                 {
00487                         $xmlStr = $this->xmlStr;
00488                 }
00489                 
00490                 // write xml data into the file
00491                 fwrite($fp, $xmlStr);
00492                 
00493                 // close file
00494                 fclose($fp);
00495         }
00496         
00503         function xmlDumpMem($format = TRUE)
00504         {
00505                 // format xml data
00506                 if ($format)
00507                 {
00508                         $xmlStr = $this->xmlFormatData($this->xmlStr);
00509                 }
00510                 else
00511                 {
00512                         $xmlStr = $this->xmlStr;
00513                 }
00514                 
00515                 return $xmlStr;
00516         }
00517 
00521         function appendXML($a_str)
00522         {
00523                 $this->xmlStr .= $a_str;
00524         }
00525 
00526 }
00527 
00528 ?>

Generated on Fri Dec 13 2013 09:06:35 for ILIAS Release_3_4_x_branch .rev 46804 by  doxygen 1.7.1