Source for file XMLElement.php

Documentation is available at XMLElement.php

  1. <?php
  2. /**
  3.  * $Source: /cvsroot/svggrapher/svggrapher/XMLElement.php,v $
  4.  *
  5.  * XMLElement
  6.  *
  7.  * @version CVS: $Id: XMLElement.php,v 1.5 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. // constants {{{
  16.  
  17. if(!defined('XML_END_LINE')) {
  18.     /**
  19.      * end line character
  20.      * @var char 
  21.      */
  22.     define('XML_END_LINE'"\n");
  23. }
  24.  
  25. if(!defined('XML_INDENT')) {
  26.     /**
  27.      * indentation, 4 space default
  28.      * @var string 
  29.      */
  30.     define('XML_INDENT''    ');
  31. }
  32.  
  33. if(!defined('MAX_CHAR_PER_LINE')) {
  34.     /**
  35.      * max char per line
  36.      */
  37.     define('MAX_CHAR_PER_LINE'80);
  38. }
  39.  
  40. // }}}
  41.  
  42. /**
  43.  * XMLElement
  44.  * 
  45.  * @version CVS: $Id: XMLElement.php,v 1.5 2007/10/23 09:22:51 geelweb Exp $
  46.  * @copyright Copyright (c) 2007, Guillaume Luchet
  47.  * @author guillaume luchet <guillaume@geelweb.org>
  48.  * @license http://opensource.org/licenses/bsd-license.php BSD License
  49.  * @package SvgGrapher
  50.  */
  51. class XMLElement 
  52. {
  53.     // properties {{{
  54.     
  55.     /**
  56.      * name
  57.      * 
  58.      * @var string 
  59.      * @access protected
  60.      */
  61.     protected $name = NULL;
  62.     
  63.     /**
  64.      * content
  65.      * 
  66.      * @var string 
  67.      * @access protected
  68.      */
  69.     protected $content = NULL;
  70.     
  71.     /**
  72.      * attributes
  73.      * 
  74.      * @var array 
  75.      * @access protected
  76.      */
  77.     protected $attributes = array();
  78.     
  79.     /**
  80.      * children
  81.      * 
  82.      * @var array 
  83.      * @access protected
  84.      */
  85.     protected $children = array();
  86.     
  87.     /**
  88.      * parent
  89.      * 
  90.      * @var XMLElement 
  91.      * @access protected
  92.      */
  93.     private $parent = false;
  94.  
  95.     // }}}
  96.     // XMLElement::__construct() {{{
  97.     
  98.     /**
  99.      * __construct
  100.      * 
  101.      * @param string $name 
  102.      * @param string $content 
  103.      * @param array $atts 
  104.      * @access public
  105.      * @return void 
  106.      */
  107.     public function __construct($name$atts=array()$content=NULL
  108.     {
  109.         $this->name = $name;
  110.         $this->content = $content;
  111.         foreach($atts as $name=>$value{
  112.             $this->setAttribute($name$value);
  113.         }
  114.     }
  115.  
  116.     // }}}
  117.     // XMLElement::__set() {{{
  118.  
  119.     /**
  120.      * __set
  121.      * 
  122.      * @param string $name 
  123.      * @param mixed $value 
  124.      * @access public
  125.      * @return mixed 
  126.      */
  127.     public function __set($name$value
  128.     {
  129.         $setter 'set' $name;
  130.         if(method_exists($this$setter)) {
  131.             return $this->$setter($value);
  132.         }
  133.         return $this->$name $value;
  134.     }
  135.  
  136.     // }}}
  137.     // XMLElement::__get() {{{
  138.  
  139.     /**
  140.      * __get
  141.      * 
  142.      * @param string $name 
  143.      * @access public
  144.      * @return mixed 
  145.      */
  146.     public function __get($name
  147.     {
  148.         $getter 'get' $name;
  149.         if(method_exists($this$getter)) {
  150.             return $this->$getter();
  151.         }
  152.         return $this->$name;
  153.     }
  154.  
  155.     // }}}
  156.     // XMLElement::setAttribute() {{{
  157.  
  158.     /**
  159.      * setAttribute
  160.      * 
  161.      * @param string $name 
  162.      * @param string $value 
  163.      * @access public
  164.      * @return void 
  165.      */
  166.     public function setAttribute($name$value=NULL
  167.     {
  168.         if(is_string($name&& !is_null($value)) {
  169.             $this->attributes[$name$value;
  170.         }
  171.     }
  172.  
  173.     // }}}
  174.     // XMLElement::getAttribute() {{{
  175.  
  176.     /**
  177.      * getAttribute
  178.      * 
  179.      * @param string $name 
  180.      * @access public
  181.      * @return string 
  182.      */
  183.     public function getAttribute($name
  184.     {
  185.         $getter '_get' $name 'Attribute';
  186.         if(method_exists($this$getter)) {
  187.             return $this->$getter();
  188.         }
  189.         if(isset($this->attributes[$name])) {
  190.             return $this->attributes[$name];
  191.         }
  192.         return NULL;
  193.     }
  194.  
  195.     // }}}
  196.     // XMLElement::appendChild() {{{
  197.  
  198.     /**
  199.      * appendChild
  200.      * 
  201.      * @param XMLElement $child 
  202.      * @access public
  203.      * @return void 
  204.      */
  205.     public function appendChild($child
  206.     {
  207.         if(false === $child->parent{
  208.             $child->parent $this;
  209.             $this->children[$child;
  210.         
  211.     }
  212.  
  213.     // }}}
  214.     // XMLElement::asXML() {{{
  215.  
  216.     /**
  217.      * asXML
  218.      * 
  219.      * @access public
  220.      * @return string 
  221.      */
  222.     public function asXML(
  223.     {
  224.         $tagTpl '<{$TAG}{$ATTRIBUTES}>{$CONTENT}</{$TAG}>';
  225.         $atts '';
  226.         $content is_null($this->content'' $this->content;
  227.         foreach($this->attributes as $key=>$value{
  228.             $atts .= ' ' $key '="' $value '"';
  229.         }
  230.         foreach($this->children as $child{
  231.             $content .= $child->asXML();
  232.         }
  233.         $xml str_replace(
  234.             array('{$TAG}''{$ATTRIBUTES}''{$CONTENT}'),
  235.             array($this->name$atts$content)$tagTpl);
  236.         return $xml;
  237.     }
  238.  
  239.     // }}}
  240.     // XMLElement::asPrettyXML() {{{
  241.  
  242.     /**
  243.      * asPrettyXML
  244.      * 
  245.      * @param int $level node level
  246.      * @access public
  247.      * @return string 
  248.      */
  249.     public function asPrettyXML($level=0
  250.     {
  251.         $padding str_repeat(XML_INDENT$level);
  252.         $string array();
  253.         $line 0;
  254.         $string[$line$padding '<' $this->name;
  255.         foreach($this->attributes as $key=>$value{
  256.             $attr ' ' $key '="' $value '"';
  257.             if((strlen($string[$line]strlen($attr)) MAX_CHAR_PER_LINE{
  258.                 $line++;
  259.                 $string[$line$padding XML_INDENT trim($attr);
  260.             else {
  261.                 $string[$line.= $attr;
  262.             }
  263.         }
  264.         $xml implode(XML_END_LINE$string'>';
  265.         $content is_null($this->content'' 
  266.             XML_END_LINE .$padding XML_INDENT $this->content;
  267.         foreach($this->children as $child{
  268.             $content .= XML_END_LINE $child->asPrettyXML($level+1);
  269.         }
  270.         if(!empty($content)) {
  271.             $xml .= $content XML_END_LINE $padding '</' $this->name . '>';
  272.         else {
  273.             $xml .= '</' $this->name . '>';
  274.         }
  275.         return $xml;
  276.     }
  277.  
  278.     // }}}
  279.     // XMLElement::saveXML() {{{
  280.  
  281.     /**
  282.      * saveXML
  283.      * 
  284.      * @param mixed $filename 
  285.      * @param mixed $compress 
  286.      * @access public
  287.      * @return boolean 
  288.      */
  289.     public function saveXML($filename$compress=false
  290.     {
  291.         $xml $compress $this->asXML($this->asPrettyXML();
  292.         $fh fopen($filename'w');
  293.         if($fh != false{
  294.             $return fwrite($fh$xml);
  295.             fclose($fh);
  296.             return $return !== false;
  297.         }
  298.         return false;
  299.     }
  300.     
  301.     // }}}
  302.     // XMLElement::_getStyleAttribute() {{{
  303.  
  304.     /**
  305.      * _getStyleAttribute
  306.      *
  307.      * Return the element style
  308.      * 
  309.      * @access private
  310.      * @return SvgStyle 
  311.      */
  312.     private function _getStyleAttribute(
  313.     {
  314.         require_once SVGGRAPHER_CLASS_PATH '/mains/SvgStyle.php';
  315.         $style new SvgStyle(isset($this->attributes['style'])?$this->attributes['style']:'');
  316.         return $style;
  317.     }
  318.  
  319.     // }}}
  320.     // XMLElement::_getTransformAttribute() {{{
  321.  
  322.     /**
  323.      * _getTransformAttribute
  324.      *
  325.      * Return the element transformation
  326.      * 
  327.      * @access private
  328.      * @return SvgTransform 
  329.      */
  330.     private function _getTransformAttribute(
  331.     {
  332.         require_once SVGGRAPHER_CLASS_PATH '/mains/SvgTransform.php';
  333.         $transform new SvgTransform(isset($this->attributes['transform'])?$this->attributes['transform']:'');
  334.         return $transform;
  335.     }
  336.  
  337.     // }}}
  338. }
  339.  
  340. ?>

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