Source for file Xslt.php

Documentation is available at Xslt.php

  1. <?php
  2. // vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 fdm=marker encoding=utf8 :
  3. /**
  4.  * Pxxo - build self-supported and interoperable Web graphical components
  5.  *
  6.  * Copyright (c) 2008, Nicolas Thouvenin
  7.  *
  8.  * All rights reserved.
  9.  *
  10.  * Redistribution and use in source and binary forms, with or without
  11.  * modification, are permitted provided that the following conditions are met:
  12.  *
  13.  *     * Redistributions of source code must retain the above copyright
  14.  *       notice, this list of conditions and the following disclaimer.
  15.  *     * Redistributions in binary form must reproduce the above copyright
  16.  *       notice, this list of conditions and the following disclaimer in the
  17.  *       documentation and/or other materials provided with the distribution.
  18.  *     * Neither the name of the author nor the names of its contributors may be
  19.  *       used to endorse or promote products derived from this software without
  20.  *       specific prior written permission.
  21.  *
  22.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
  23.  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  24.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  25.  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
  26.  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  27.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  28.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  29.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32.  
  33.  * @package    Pxxo
  34.  * @copyright  Copyright (c) 2008 Nicolas Thouvenin
  35.  * @license    http://opensource.org/licenses/bsd-license.php
  36.  * @version    $Id: Xslt.php,v 1.11 2008/02/29 13:35:22 thouveni Exp $
  37.  */
  38. require_once 'Pxxo.php';
  39.  
  40. /**
  41.  * Encapsule une  transformation XSL
  42.  *
  43.  * Exemple :
  44.  *    $x = new Pxxo_Xslt('exemple01.xml', 'exemple01.xsl');
  45.  *    $x->transform();
  46.  *    $ret = $x->get();
  47.  *
  48.  * @package    Pxxo
  49.  * @copyright  Copyright (c) 2008 Nicolas Thouvenin
  50.  * @license    http://opensource.org/licenses/bsd-license.php
  51.  */
  52. class Pxxo_Xslt extends Pxxo
  53. {
  54.     // {{{ Variables
  55.     /**
  56.      * Fichier ou buffer XML
  57.      * @var        mixed 
  58.      */
  59.     private $_XML = '';
  60.     /**
  61.      * Fichier ou buffer XSL
  62.      * @var        mixed 
  63.      */
  64.     private $_XSL = '';
  65.     /**
  66.      * Parametres pour la feuille
  67.      * @var      array 
  68.      */
  69.     private $_PAR = array();
  70.     /**
  71.      * buffer de sortie
  72.      * @var        string 
  73.      */
  74.     private $_out;
  75.     /**
  76.      * active ou non l'execution du PHP généré par le XSL
  77.      * @var boolean 
  78.      */
  79.     public $eval_result = false;
  80.     /**
  81.      * active ou non les fonctions PHP à l'intetrieur du XSL
  82.      * @var boolean 
  83.      */
  84.     public $register_functions = true;
  85.     /**
  86.      * active si possible xsltcache
  87.      * @var boolean 
  88.      */
  89.     public $xsl_cache = true;
  90.     // }}}
  91.     // {{{ Constructeur
  92.     /**
  93.      * Constructeur
  94.      *
  95.      * @param       string      Chemin du fichier xml
  96.      * @param       string      Chemin du fichier xsl
  97.      * @param       mixed       Tableau de partametres
  98.      */
  99.     function __construct($a ''$b ''$c array())
  100.     {
  101.         parent::__construct();
  102.         $this->setXML($a);
  103.         $this->setXSL($b);
  104.         $this->setPAR($c);
  105.     }
  106.     // }}}
  107.     // {{{ factory
  108.     /**
  109.      * Construction automatique d'un objet
  110.      *
  111.      * @param    string   which handler you want to use
  112.      * @deprecated
  113.      * @return    Object     Pxxo_Xslt
  114.      */
  115.     public static function factory($s '')
  116.     {
  117.         $x new Pxxo_Xslt();
  118.         return $x;
  119.     }
  120.     // }}}
  121.     // {{{ transform
  122.     /**
  123.      * Effectue la transformation
  124.      *
  125.      * @param array options de tranformation
  126.      * @return 
  127.      */
  128.     public function transform($options array())
  129.     {
  130.         $this->_out = $this->getinCache($this->getCacheID()P_C_XSLT);
  131.  
  132.         if ($this->_out === false {
  133.  
  134.             $xmldom new DOMDocument;
  135.  
  136.             // assign user's options
  137.             if (isset($options['xmldom'])) foreach($options['xmldom'as $k => $v$xmldom->$k $v;
  138.  
  139.             $this->_XML = trim($this->_XML);
  140.             if (substr($this->_XML01== '<'{
  141.                 if(!$xmldom->loadXML($this->_XML)) {
  142.                     return trigger_error("XSLT initialization failed : unable to open XML Buffer"E_USER_ERROR);
  143.                 }
  144.             }
  145.             else {
  146.                 if (!$xmldom->load($this->_XML)) {
  147.                     return trigger_error("XSLT initialization failed : unable to open XML file"E_USER_ERROR);
  148.                 }
  149.             }
  150.             $this->_XSL = trim($this->_XSL);
  151.  
  152.             if (substr($this->_XSL01== '<'{
  153.                 $xslfile false;
  154.                 $this->xsl_cache = false;
  155.             }
  156.             else {
  157.                 $xslfile true;
  158.             }
  159.  
  160.             if ($this->xsl_cache and extension_loaded('xslcache')) {
  161.                 $proc new xsltCache;
  162.                 $proc->importStyleSheet($this->_XSL);
  163.             }
  164.             else {
  165.                 $xsldom new DOMDocument;
  166.                 if ($xslfile{
  167.                     if (!$xsldom->load($this->_XSL)) {
  168.                         return trigger_error("XSLT initialization failed : unable to open XSL file"E_USER_ERROR);
  169.                     }
  170.                 }
  171.                 else {
  172.                     if(!$xsldom->loadXML($this->_XSL)) {
  173.                         return trigger_error("XSLT initialization failed : unable to open XSL Buffer"E_USER_ERROR);
  174.                     }
  175.                 }
  176.                 if (isset($options['xsldom'])) foreach($options['xsldom'as $k => $v$xsldom->$k $v;
  177.                 $proc new XSLTProcessor;
  178.                 $proc->importStyleSheet($xsldom);
  179.             }
  180.  
  181.             if ($this->register_functions and method_exists($proc'registerPHPFunctions')) {
  182.                 $proc->registerPHPFunctions();
  183.             }
  184.  
  185.             if(is_array($this->_PAR)) {
  186.                 foreach($this->_PAR as $k => $v{
  187.                     $proc->setParameter(''$k$v);
  188.                 }
  189.             }
  190.  
  191.             $this->_out = $proc->transformToXML($xmldom);
  192.             if(!$this->_out{
  193.                 return trigger_error("XSLT transform failed : unknown error "E_USER_ERROR);
  194.             }
  195.  
  196.             if ($this->eval_result{
  197.                 ob_start();
  198.                 eval('?'.'>'.$this->_out);
  199.                 $this->_out = ob_get_contents();
  200.                 ob_end_clean();
  201.             }
  202.             $this->setinCache($this->_out$this->getCacheID()P_C_XSLT);
  203.         }
  204.         return true;
  205.     }
  206.     // }}}
  207.     // {{{ get
  208.     /**
  209.      * Sortie du résultat
  210.      *
  211.      * @return    string 
  212.      */
  213.     public function get()
  214.     {
  215.         return $this->_out;
  216.     }
  217.     // }}}
  218.     // {{{ dump
  219.     /**
  220.      * Affiche le résultat
  221.      *
  222.      * @return    string 
  223.      */
  224.     public function dump()
  225.     {
  226.         echo $this->get();
  227.     }
  228.     // }}}
  229.     // {{{ enablePHP
  230.     /**
  231.      * active l'exécution de PHP dans les modèles XSL
  232.      * @deprecated
  233.      */
  234.     public function enablePHP()
  235.     {
  236.         $this->eval_result = true;
  237.     }
  238.     // }}}
  239.     // {{{ disablePHP
  240.     /**
  241.      * désactive l'exécution de PHP dans les modèles XSL
  242.      * @deprecated
  243.      */
  244.     public function disablePHP()
  245.     {
  246.         $this->eval_result = false;
  247.     }
  248.     // }}}
  249.     // {{{ setXML
  250.     /**
  251.      * Fixe une chaine ou un fichier XML
  252.      *
  253.      * @param    string 
  254.      */
  255.     public function setXML($s)
  256.     {
  257.         $this->_XML = $s;
  258.         $this->addCacheID($s);
  259.     }
  260.     // }}}
  261.     // {{{ setXSL
  262.     /**
  263.      * Fixe une chaine ou un fichier XSL
  264.      *
  265.      * @param    string 
  266.      */
  267.     public function setXSL($s)
  268.     {
  269.         $this->_XSL = $s;
  270.         $this->addCacheID($s);
  271.     }
  272.     // }}}
  273.     // {{{ setPar
  274.     /**
  275.      * Ajout des paramètres
  276.      *
  277.      * Passe des paramètres à Sablotron qui seront intégrés comme variables
  278.      * dans le fichier XSL
  279.      *
  280.      * @param    array 
  281.      */
  282.     public function setPAR($t)
  283.     {
  284.         if (is_array($t)) {
  285.             $this->_PAR = array();
  286.             foreach($t as $key => $val{
  287.                 $this->_PAR[$key$val;
  288.                 $this->addCacheID($val);
  289.             }
  290.         }
  291.     }
  292.     // }}}
  293. }

Documentation generated on Thu, 13 Mar 2008 22:03:36 +0100 by phpDocumentor 1.4.1