Changeset 319

Show
Ignore:
Timestamp:
02/26/08 22:54:13 (4 years ago)
Author:
nperriault
Message:

Clever Svg: Silly validation exception handling fixed

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • cleversvg/trunk/document/csDocument.class.php

    r318 r319  
    22/** 
    33 * Base SVG Document class 
    4  *  
     4 * 
    55 * @author     Nicolas Perriault <nperriault@gmail.com> 
    66 * @package    cleversvg 
     
    1010{ 
    1111 
    12   protected  
     12  protected 
    1313    $attributes       = array(), 
    1414    $definitions      = array(), 
     
    2828  /** 
    2929   * Class constructor 
    30    *  
     30   * 
    3131   * @param  mixed  $width  Width 
    3232   * @param  mixed  $height Height 
     
    4242    $this->initDefaults(); 
    4343  } 
    44    
     44 
    4545  /** 
    4646   * Adds a SVG shape or group to current SVG Document at a certain depth 
    47    *  
     47   * 
    4848   * @param  mixed  $element  SVG element to add 
    4949   * @param  mixed  $depth    Depth (default: auto when set to NULL) 
     
    6464    } 
    6565    $this->elements[$depth] = $element; 
    66      
     66 
    6767    // Add DOM id to document list, if any 
    6868    $id = $element->getId(); 
     
    7272    } 
    7373  } 
    74    
     74 
    7575  /** 
    7676   * Returns an array containing SVG Document Elements 
    77    *  
    78    * @return array  
     77   * 
     78   * @return array 
    7979   */ 
    8080  public function getElements() 
     
    8282    return $this->elements; 
    8383  } 
    84    
     84 
    8585  /** 
    8686   * Gets the namespace prefix if this is an embedded document 
    87    *  
     87   * 
    8888   * @return stringPie 
    8989   */ 
     
    9797    return $prefix; 
    9898  } 
    99    
     99 
    100100  /** 
    101101   * Adds a script tag in global document array of scripts 
    102    *  
     102   * 
    103103   * @param  string  $content 
    104104   * @param  string  $content_type 
     
    108108    $this->scripts[] = array($content, $type); 
    109109  } 
    110    
     110 
    111111  /** 
    112112   * Adds a style tag in global document array of styles 
    113    *  
     113   * 
    114114   * @param  string  $content       Style declaration content 
    115    * @param  string  $content_type   
    116    * @param  mixed   $media          
    117    * @param  mixed   $title          
     115   * @param  string  $content_type 
     116   * @param  mixed   $media 
     117   * @param  mixed   $title 
    118118   */ 
    119119  public function addStyleDeclaration($content, $type='text/css', $media=null, $title=null) 
    120120  { 
    121     $this->embed_styles[] = array('content'   => $content,  
    122                                   'type'      => $type,  
    123                                   'media'     => $media,  
    124                                   'title'     => $title);  
    125   } 
    126    
     121    $this->embed_styles[] = array('content'   => $content, 
     122                                  'type'      => $type, 
     123                                  'media'     => $media, 
     124                                  'title'     => $title); 
     125  } 
     126 
    127127  /** 
    128128   * Adds a linked stylesheet global document 
    129    *  
    130    * @param  string  $href  
    131    * @param  string  $content_type   
    132    * @param  mixed   $media          
    133    * @param  mixed   $title          
     129   * 
     130   * @param  string  $href 
     131   * @param  string  $content_type 
     132   * @param  mixed   $media 
     133   * @param  mixed   $title 
    134134   */ 
    135135  public function addStylesheetLink($href, $type='text/css', $alternate='no', $media=null, $title=null) 
    136136  { 
    137     $this->external_styles[] = array('href'      => $href,  
    138                                      'type'      => $type,  
    139                                      'alternate' => $alternate,  
    140                                      'media'     => $media,  
     137    $this->external_styles[] = array('href'      => $href, 
     138                                     'type'      => $type, 
     139                                     'alternate' => $alternate, 
     140                                     'media'     => $media, 
    141141                                     'title'     => $title); 
    142142  } 
    143    
     143 
    144144  /** 
    145145   * Adds a definition to current SVG Document 
    146    *  
     146   * 
    147147   * @param  mixed  $definition  SVG definition to add 
    148148   * @param  string $id          Override element DOM id 
     
    173173    } 
    174174  } 
    175    
     175 
    176176  /** 
    177177   * Returns an array containing SVG Document definitions 
    178    *  
     178   * 
    179179   * @return array 
    180180   */ 
     
    183183    return $this->definitions; 
    184184  } 
    185    
     185 
    186186  /** 
    187187   * Returns an array containing used definitions for current document 
    188    *  
     188   * 
    189189   * @return array 
    190190   */ 
     
    193193    return $this->used_definitions; 
    194194  } 
    195    
     195 
    196196  /** 
    197197   * Returns a defined element alias (a &lt;use/&gt; tag) 
    198    *  
     198   * 
    199199   * @return array 
    200200   */ 
     
    206206                  sprintf('Unable to find id "%s" in current document.')); 
    207207    } 
    208     else  
     208    else 
    209209    { 
    210210      return $this->definitions[$id]; 
    211211    } 
    212212  } 
    213    
     213 
    214214  /** 
    215215   * Returns current DOMDOcument 
    216    *  
     216   * 
    217217   * @return DOMDocument 
    218218   */ 
     
    256256    } 
    257257  } 
    258    
     258 
    259259  /** 
    260260   * Uses a definition in current SVG Document 
    261    *  
     261   * 
    262262   * @param  mixed  $element 
    263263   * @param  array  $attrs    More atributes to add to &lt;use/&gt; tag 
     
    267267    $this->used_definitions[] = array($this->getDefinition($id), $attrs); 
    268268  } 
    269    
     269 
    270270  /** 
    271271   * Swap respective depths of two elements 
    272    *  
     272   * 
    273273   * @param  sfSvgElement  $element1 
    274274   * @param  sfSvgElement  $element2 
     
    283283    $element1->setDepth($e2depth); 
    284284  } 
    285    
     285 
    286286  /** 
    287287   * Checks if a DOM id already exists in current SVG Document 
    288    *  
     288   * 
    289289   * @return boolean 
    290290   */ 
     
    293293    return in_array($dom_id, $this->dom_ids); 
    294294  } 
    295    
     295 
    296296  /** 
    297297   * Initialize object with default required values if they're not present in 
    298298   * the attributes array 
    299    *  
     299   * 
    300300   */ 
    301301  public function initDefaults() 
     
    314314    } 
    315315  } 
    316    
     316 
    317317  /** 
    318318   * Sets a description describing the SVG Document 
    319    *  
    320    * @param  string  $description   
     319   * 
     320   * @param  string  $description 
    321321   */ 
    322322  public function setDescription($description) 
     
    324324    $this->description = $description; 
    325325  } 
    326    
     326 
    327327  /** 
    328328   * Sets if the SVG document will be embedded or not 
    329    *  
     329   * 
    330330   * @param  boolean  $flag 
    331331   */ 
     
    334334    $this->embedded = (boolean) $flag; 
    335335  } 
    336    
     336 
    337337  /** 
    338338   * Sets if the SVG document will be stricly validated 
    339    *  
     339   * 
    340340   * @param  boolean  $flag 
    341341   */ 
     
    344344    $this->strict_mode = (boolean) $flag; 
    345345  } 
    346    
     346 
    347347  /** 
    348348   * Sets if the SVG document title 
    349    *  
     349   * 
    350350   * @param  string  $title 
    351351   */ 
     
    354354    $this->title = trim($title) != '' ? $title : 'Untitled SVG Document'; 
    355355  } 
    356    
     356 
    357357  /** 
    358358   * Standard XML attribute for identifying an XML namespace. 
    359    *  
     359   * 
    360360   */ 
    361361  public function setXmlns($xmlns='http://www.w3.org/2000/svg') 
     
    363363    $this->attributes['xmlns'] = $xmlns; 
    364364  } 
    365    
     365 
    366366  /** 
    367367   * The x-axis coordinate of one corner of the rectangular region into which an 
    368368   * embedded 'svg' element is placed. 
    369    *  
     369   * 
    370370   */ 
    371371  public function setX($x) 
     
    373373    $this->attributes['x'] = (string)$x; 
    374374  } 
    375    
     375 
    376376  /** 
    377377   * The y-axis coordinate of one corner of the rectangular region into which an 
    378378   * embedded 'svg' element is placed. 
    379    *  
     379   * 
    380380   */ 
    381381  public function setY($y) 
     
    383383    $this->attributes['y'] = (string)$y; 
    384384  } 
    385    
    386   /** 
    387    * For outermost 'svg'  elements, the intrinsic width of the SVG document  
    388    * fragment. For embedded 'svg'  elements, the width of the rectangular  
     385 
     386  /** 
     387   * For outermost 'svg'  elements, the intrinsic width of the SVG document 
     388   * fragment. For embedded 'svg'  elements, the width of the rectangular 
    389389   * region into which the 'svg' element is placed. 
    390    *  
     390   * 
    391391   */ 
    392392  public function setWidth($width) 
     
    394394    $this->attributes['width'] = (string)$width; 
    395395  } 
    396    
    397   /** 
    398    * For outermost 'svg'  elements, the intrinsic height of the SVG document  
    399    * fragment. For embedded 'svg' elements, the height of the rectangular  
     396 
     397  /** 
     398   * For outermost 'svg'  elements, the intrinsic height of the SVG document 
     399   * fragment. For embedded 'svg' elements, the height of the rectangular 
    400400   * region into which the 'svg' element is placed. 
    401    *  
     401   * 
    402402   */ 
    403403  public function setHeight($height) 
     
    405405    $this->attributes['height'] = (string)$height; 
    406406  } 
    407    
    408   /** 
    409    * Indicates the SVG language version to which this document fragment  
    410    * conforms. In SVG 1.0, this attribute was fixed to the value "1.0". For  
     407 
     408  /** 
     409   * Indicates the SVG language version to which this document fragment 
     410   * conforms. In SVG 1.0, this attribute was fixed to the value "1.0". For 
    411411   * SVG 1.1, the attribute should have the value "1.1". 
    412    *  
     412   * 
    413413   */ 
    414414  public function setVersion($version='1.1') 
     
    416416    $this->attributes['version'] = (string)$version; 
    417417  } 
    418    
    419   /** 
    420    * This attribute provides a convenient way to design SVG documents to  
     418 
     419  /** 
     420   * This attribute provides a convenient way to design SVG documents to 
    421421   * scale-to-fit into an arbitrary viewport. 
    422    *  
     422   * 
    423423   */ 
    424424  public function setViewBox($x, $y, $width, $height) 
    425425  { 
    426     $this->attributes['viewBox'] = sprintf('%d %d %d %d',  
     426    $this->attributes['viewBox'] = sprintf('%d %d %d %d', 
    427427                                           $x, $y, $width, $height); 
    428428  } 
    429    
    430   /** 
    431    * Describes the minimum SVG language profile that the author believes is  
     429 
     430  /** 
     431   * Describes the minimum SVG language profile that the author believes is 
    432432   * necessary to correctly render the content. This can be considered metadata. 
    433    *  
     433   * 
    434434   */ 
    435435  public function setBaseProfile($base_profile) 
     
    437437    $this->attributes['baseProfile'] = $base_profile; 
    438438  } 
    439    
     439 
    440440  /** 
    441441   * Gets SVG XML rendering output. Never forget to send image/svg+xml headers 
    442442   * before outputing. 
    443    *  
     443   * 
    444444   * @param  boolean  $embedded  Is SVG Document embedded ? 
    445445   * @return string 
     
    451451      $this->setEmbedded($embedded); 
    452452    } 
    453      
     453 
    454454    $xml_prepend = ''; 
    455      
    456     $dom = $this->getDomDocument(); 
    457      
     455 
     456    $dom = $this->getDomDocument(); 
     457 
    458458    // External stylesheets 
    459459    $this->processExternalStyleSheets(); 
    460      
     460 
    461461    // Root node creation 
    462462    $this->processRootNode(); 
     
    467467    $this->processElements(); 
    468468    $this->processUsedDefinitions(); 
    469          
     469 
    470470    // Document normalization 
    471471    $dom->normalizeDocument(); 
    472      
     472 
    473473    // Validation 
    474474    $this->validate($dom); 
    475      
     475 
    476476    // Output 
    477477    if ($this->embedded === true) 
     
    484484    } 
    485485  } 
    486    
     486 
    487487  /** 
    488488   * Process definitionsn if any 
    489    *  
     489   * 
    490490   */ 
    491491  protected function processDefinitions() 
     
    506506    } 
    507507  } 
    508    
     508 
    509509  /** 
    510510   * Process Document elements 
    511    *  
     511   * 
    512512   */ 
    513513  protected function processElements() 
     
    521521    } 
    522522  } 
    523    
     523 
    524524  /** 
    525525   * Process embedded styles, if any 
    526    *  
     526   * 
    527527   */ 
    528528  protected function processEmbeddedStyles() 
     
    544544    } 
    545545  } 
    546    
     546 
    547547  /** 
    548548   * Process externals stylesheets, if any 
    549    *  
     549   * 
    550550   */ 
    551551  protected function processExternalStyleSheets() 
     
    557557      { 
    558558        $style_declaration = $dom->createProcessingInstruction( 
    559                                'xml-stylesheet',  
    560                                sprintf('href="%s" type="%s"',  
    561                                        $stylesheet['href'],  
     559                               'xml-stylesheet', 
     560                               sprintf('href="%s" type="%s"', 
     561                                       $stylesheet['href'], 
    562562                                       $stylesheet['type'])); 
    563563        $dom->appendChild($style_declaration); 
     
    565565    } 
    566566  } 
    567    
     567 
    568568  /** 
    569569   * Process meta description 
    570    *  
     570   * 
    571571   */ 
    572572  protected function processMetas() 
    573573  { 
    574574    $dom = $this->getDomDocument(); 
    575      
     575 
    576576    // Title node 
    577577    if (!is_null($this->title)) 
    578578    { 
    579       $title_node = $dom->createElement($this->getNodeNS().'title',  
     579      $title_node = $dom->createElement($this->getNodeNS().'title', 
    580580                                        strip_tags($this->title)); 
    581581      $dom->documentElement->appendChild($title_node); 
    582582    } 
    583      
    584      
     583 
     584 
    585585    // Description node 
    586586    if (!is_null($this->description)) 
    587587    { 
    588       $desc_node = $dom->createElement($this->getNodeNS().'desc',  
     588      $desc_node = $dom->createElement($this->getNodeNS().'desc', 
    589589                                       strip_tags($this->description)); 
    590590      $dom->documentElement->appendChild($desc_node); 
    591591    } 
    592592  } 
    593    
     593 
    594594  /** 
    595595   * Process root node 
    596    *  
     596   * 
    597597   */ 
    598598  protected function processRootNode() 
     
    609609    } 
    610610  } 
    611    
     611 
    612612  /** 
    613613   * Process scripts, if any 
    614    *  
     614   * 
    615615   */ 
    616616  protected function processScripts() 
     
    630630    } 
    631631  } 
    632    
     632 
    633633  /** 
    634634   * Process used definitions, if any 
    635    *  
     635   * 
    636636   */ 
    637637  protected function processUsedDefinitions() 
     
    654654    } 
    655655  } 
    656    
     656 
    657657  /** 
    658658   * Validates document 
    659    *  
     659   * 
    660660   * @throws csException 
    661661   */ 
     
    665665    { 
    666666      set_error_handler(array('csDocument', 'handleValidationError'), E_ALL); 
    667       try 
    668       { 
    669         $dom->validate(); 
    670       } 
    671       catch (csException $e) 
    672       { 
    673         throw 'This SVG Document does not validate: '.$e; 
     667      if (!$dom->validate()) 
     668      { 
     669        throw new csException('This SVG Document does not validate'); 
    674670      } 
    675671      restore_error_handler(); 
    676672    } 
    677673  } 
    678    
     674 
    679675  /** 
    680676   * Handle validation errors 
    681    *  
     677   * 
    682678   * @throws csException 
    683679   */ 
     
    688684    exit; 
    689685  } 
    690    
     686 
    691687}