Source for file XMLDocument.php

Documentation is available at XMLDocument.php

  1. <?php
  2. /**
  3.  * $Source: /cvsroot/svggrapher/svggrapher/XMLDocument.php,v $
  4.  *
  5.  * SvgDocument
  6.  *
  7.  * @version CVS: $Id: XMLDocument.php,v 1.4 2007/10/23 09:22:51 geelweb Exp $
  8.  * @filesource
  9.  * @author Guillaume Luchet. <guillaume@geelweb.org>
  10.  * @copyright Copyright (c) 2007, Guillaume Luchet.
  11.  * @license http://opensource.org/licenses/bsd-license.php BSD License
  12.  * @package SvgGrapher
  13.  */
  14.  
  15. /**
  16.  *  
  17.  */
  18. require_once SVGGRAPHER_CLASS_PATH '/XMLElement.php';
  19.  
  20. /**
  21.  * SvgDocument
  22.  * 
  23.  * @version CVS: $Id: XMLDocument.php,v 1.4 2007/10/23 09:22:51 geelweb Exp $
  24.  * @copyright Copyright (c) 2007, Guillaume Luchet
  25.  * @author guillaume luchet <guillaume@geelweb.org>
  26.  * @license http://opensource.org/licenses/bsd-license.php BSD License
  27.  * @package SvgGrapher
  28.  */
  29. class XMLDocument 
  30. {
  31.     // poperties {{{
  32.  
  33.     /**
  34.      * version
  35.      * 
  36.      * @var string 
  37.      * @access public
  38.      */
  39.     public $version;
  40.  
  41.     /**
  42.      * encoding
  43.      * 
  44.      * @var string 
  45.      * @access public
  46.      */
  47.     public $encoding;
  48.     
  49.     /**
  50.      * standalone
  51.      * 
  52.      * @var bool 
  53.      * @access public
  54.      */
  55.     public $standalone;
  56.  
  57.     /**
  58.      * qualifiedName
  59.      * 
  60.      * @var string 
  61.      * @access public
  62.      */
  63.     public $qualifiedName = '';
  64.     
  65.     /**
  66.      * publicId
  67.      * 
  68.      * @var string 
  69.      * @access public
  70.      */
  71.     public $publicId = NULL;
  72.     
  73.     /**
  74.      * systemId
  75.      * 
  76.      * @var string 
  77.      * @access public
  78.      */
  79.     public $systemId = NULL;
  80.  
  81.     /**
  82.      * rootElement
  83.      * 
  84.      * @var XMLElement 
  85.      * @access public
  86.      */
  87.     public $rootElement;
  88.  
  89.     // }}}
  90.     // SvgDocument::__construct() {{{
  91.  
  92.     /**
  93.      * __construct
  94.      * 
  95.      * @param string $version 
  96.      * @param string $encoding 
  97.      * @param string $standalone 
  98.      * @access public
  99.      * @return void 
  100.      */
  101.     public function __construct($version='1.0'$encoding=NULL$standalone=NULL
  102.     {
  103.         $this->version = $version;
  104.         $this->encoding = $encoding;
  105.         $this->standalone = $standalone;
  106.     }
  107.  
  108.     // }}}
  109.     // SvgDocument::createDocumentType() {{{
  110.  
  111.     /**
  112.      * createDocumentType
  113.      * 
  114.      * @param string $qualifiedName 
  115.      * @param string $publicId 
  116.      * @param string $systemId 
  117.      * @access public
  118.      * @return void 
  119.      */
  120.     public function createDocumentType($qualifiedName=''$publicId=NULL$systemId=NULL
  121.     {
  122.         $this->qualifiedName = $qualifiedName;
  123.         $this->publicId = $publicId;
  124.         $this->systemId = $systemId;
  125.     }
  126.  
  127.     // }}}
  128.     // SvgDocument::createRootElement() {{{
  129.     
  130.     /**
  131.      * createRootElement
  132.      * 
  133.      * @param string $name 
  134.      * @access public
  135.      * @return XMLElement 
  136.      */
  137.     public function createRootElement($name
  138.     {
  139.         $this->rootElement = new XMLElement($name);
  140.         return $this->rootElement;
  141.     }
  142.     
  143.     // }}}
  144.     // SvgDocument::getXMLDeclaration() {{{
  145.  
  146.     /**
  147.      * getXMLDeclaration
  148.      * 
  149.      * @access public
  150.      * @return string 
  151.      */
  152.     public function getXMLDeclaration($pretty=false
  153.     {
  154.         $decl '<?xml';
  155.         $decl .= ' version="' $this->version . '"';
  156.         if(!is_null($this->encoding)) {
  157.             $str 'encoding="' $this->encoding . '"';
  158.             $padding ' ';
  159.             if((strlen($declstrlen($str))>MAX_CHAR_PER_LINE{
  160.                 $padding XML_END_LINE XML_INDENT;
  161.             }
  162.             $decl .= $padding $str;
  163.         }
  164.         if(!is_null($this->standalone)) {
  165.             $str ' standalone="' $this->standalone ? 'yes' 'no' '"';
  166.             $padding ' ';
  167.             if((strlen($declstrlen($str))>MAX_CHAR_PER_LINE{
  168.                 $padding XML_END_LINE XML_INDENT;
  169.             }
  170.             $decl .= $padding $str;
  171.         }
  172.         $decl .= '?>';
  173.         return $decl;
  174.     }
  175.  
  176.     // }}}
  177.     // SvgDocument::getDocumentTypeDeclaration() {{{
  178.  
  179.     /**
  180.      * getDocumentTypeDeclaration
  181.      * 
  182.      * @access public
  183.      * @return string 
  184.      */
  185.     public function getDocumentTypeDeclaration($pretty=false
  186.     {
  187.         $dtd '<!DOCTYPE ' $this->qualifiedName;
  188.         if(!is_null($this->publicId)) {
  189.             $str 'PUBLIC "' $this->publicId . '"';
  190.             $padding ' ';
  191.             if((strlen($dtdstrlen($str))>MAX_CHAR_PER_LINE{
  192.                 $padding XML_END_LINE XML_INDENT;
  193.             }
  194.             $dtd .= $padding $str;
  195.         }
  196.         if(!is_null($this->systemId)) {
  197.             $str '"' $this->systemId . '"';
  198.             $padding ' ';
  199.             if((strlen($dtdstrlen($str))>MAX_CHAR_PER_LINE{
  200.                 $padding XML_END_LINE XML_INDENT;
  201.             }
  202.             $dtd .= $padding $str;
  203.         }
  204.         $dtd .= '>';
  205.         return $dtd;
  206.     }
  207.  
  208.     // }}}
  209.     // SvgDocument::asXML() {{{
  210.     
  211.     /**
  212.      * asXML
  213.      * 
  214.      * @access public
  215.      * @return string 
  216.      */
  217.     public function asXML(
  218.     {
  219.         $xml $this->getXMLDeclaration($this->getDocumentTypeDeclaration();
  220.         if($this->rootElement instanceof XMLElement{
  221.             $xml .= $this->rootElement->asXML();
  222.         }
  223.         return $xml;
  224.     }
  225.     
  226.     // }}}
  227.     // SvgDocument::asPrettyXML() {{{
  228.     
  229.     /**
  230.      * asPrettyXML
  231.      * 
  232.      * @access public
  233.      * @return string 
  234.      */
  235.     public function asPrettyXML(
  236.     {
  237.         $xml $this->getXMLDeclaration(trueXML_END_LINE;
  238.         $xml .= $this->getDocumentTypeDeclaration(trueXML_END_LINE;
  239.         if($this->rootElement instanceof XMLElement{
  240.             $xml .= $this->rootElement->asPrettyXML();
  241.         }
  242.         return $xml;
  243.     }
  244.     
  245.     // }}}
  246.     // SvgDocument::saveXML() {{{
  247.  
  248.     /**
  249.      * saveXML
  250.      * 
  251.      * @param string $filename 
  252.      * @param bool $compress 
  253.      * @access public
  254.      * @return boolean 
  255.      */
  256.     public function saveXML($filename$compress=false
  257.     {
  258.         $xml $compress $this->asXML($this->asPrettyXML();
  259.         $fh fopen($filename'w');
  260.         if($fh != false{
  261.             $return fwrite($fh$xml);
  262.             fclose($fh);
  263.             return $return !== false;
  264.         }
  265.         return false;
  266.     }
  267.     
  268.     // }}}
  269. }
  270.  
  271. ?>

Documentation generated on Tue, 23 Oct 2007 11:32:21 +0200 by phpDocumentor 1.4.0