XML_CSSML
[ class tree: XML_CSSML ] [ index: XML_CSSML ] [ all elements ]

Source for file CSSML.php

Documentation is available at CSSML.php

  1. <?php
  2. // {{{ license
  3.  
  4. // +----------------------------------------------------------------------+
  5. // | PHP version 4.0                                                      |
  6. // +----------------------------------------------------------------------+
  7. // | Copyright (c) 1997-2003 The PHP Group                                |
  8. // +----------------------------------------------------------------------+
  9. // | This source file is subject to version 2.0 of the PHP license,       |
  10. // | that is bundled with this package in the file LICENSE, and is        |
  11. // | available at through the world-wide-web at                           |
  12. // | http://www.php.net/license/2_02.txt.                                 |
  13. // | If you did not receive a copy of the PHP license and are unable to   |
  14. // | obtain it through the world-wide-web, please send a note to          |
  15. // | license@php.net so we can mail you a copy immediately.               |
  16. // +----------------------------------------------------------------------+
  17. // | Authors: Dan Allen <dan@mojavelinux.com>                             |
  18. // +----------------------------------------------------------------------+
  19.  
  20. // $Id: CSSML.php,v 1.8 2005/10/12 12:38:12 toggg Exp $
  21.  
  22. // }}}
  23. // {{{ description
  24.  
  25. // XML_CSSML is a CSSML to CSS xslt parser
  26.  
  27. // }}}
  28. // {{{ error codes
  29.  
  30. define('XML_CSSML_OK',                 0);
  31. define('XML_CSSML_ERROR',             -1);
  32. define('XML_CSSML_ALREADY_EXISTS',    -2);
  33. define('XML_CSSML_NOT_LOADED',        -3);
  34. define('XML_CSSML_INVALID_DATA',      -4);
  35. define('XML_CSSML_INVALID_DOCUMENT',  -5);
  36. define('XML_CSSML_INVALID_FILE',      -6);
  37.  
  38. // }}}
  39. // {{{ includes
  40.  
  41. require_once 'PEAR.php';
  42. require_once 'XML/CSSML/Error.php';
  43.  
  44. // }}}
  45. // {{{ functions
  46.  
  47. /**
  48.  * Replace function is_a()
  49.  *
  50.  * @category PHP
  51.  * @package  PHP_Compat
  52.  * @link     http://php.net/function.is_a
  53.  * @author   Aidan Lister <aidan@php.net>
  54.  * @version  $Revision: 1.8 $
  55.  * @since       PHP 4.2.0
  56.  * @require     PHP 4.0.0 (user_error) (is_subclass_of)
  57.  */
  58. if (!function_exists('is_a')) {
  59.     function is_a($object$class)
  60.     {
  61.         if (!is_object($object)) {
  62.            return false;
  63.         }
  64.  
  65.         if (get_class($object== strtolower($class)) {
  66.             return true;
  67.         else {
  68.             return is_subclass_of($object$class);
  69.         }
  70.     }
  71. }
  72.  
  73. // }}}
  74.  
  75. // {{{ class XML_CSSML
  76.  
  77. /**
  78.  * The XML_CSSML class provides the xsl functions
  79.  * to parse a CSSML document into a stylesheet
  80.  * with the ability to output to a file or return
  81.  *
  82.  * @author   Dan Allen <dan@mojavelinux.com>
  83.  * @version  Revision: 0.1
  84.  * @access   public
  85.  * @package  XML_CSSML
  86.  */
  87.  
  88. // }}}
  89. class XML_CSSML {
  90.     // {{{ properties
  91.  
  92.     /**
  93.      * domxml object which holds the xml document with the css information
  94.      * @var object $CSSMLDoc 
  95.      */
  96.     var $CSSMLDoc;
  97.  
  98.     /**
  99.      * domxml object which holds the xsl document which parses the cssml document
  100.      * @var object $stylesheetDoc 
  101.      */
  102.     var $stylesheetDoc;
  103.  
  104.     /**
  105.      * Redirection method for the output of the cssml (file, stdout)
  106.      * If redirection is a file, it must be absolute
  107.      * @var string $outputMethod 
  108.      */
  109.     var $output = 'STDOUT';
  110.  
  111.     /**
  112.      * Code corresponding to the user agent of the browser,
  113.      * such as is generated with Net_UserAgentDetect
  114.      * @var string $browser 
  115.      */
  116.     var $browser = '';
  117.  
  118.     /**
  119.      * Filter for the entries in the CSSML
  120.      * @var string $filter 
  121.      */
  122.     var $filter = '';
  123.  
  124.     /**
  125.      * Comment to be used at the top of the stylesheet output
  126.      * @var string $comment 
  127.      */
  128.     var $comment = '';
  129.  
  130.     /**
  131.      * Boolean which defines if the CSSML document has been loaded
  132.      * @var boolean $loaded 
  133.      */
  134.     var $loaded = false;
  135.  
  136.     // }}}
  137.     // {{{ constructor
  138.  
  139.     /**
  140.      * Class constructor, prepare the cssml document object from either a string, file or object
  141.      *
  142.      * @param mixed $in_cssml Optionally the CSSML data can be passed to the constructor
  143.      * @return void 
  144.      * @access private
  145.      */
  146.     function XML_CSSML($in_driver$in_CSSML = null$in_type 'string'$in_params = null)
  147.     {
  148.         $this $this->factory($in_driver$in_CSSML$in_type$in_params);
  149.     }
  150.  
  151.     // }}}
  152.     // {{{ factory()
  153.  
  154.     function &factory($in_driver$in_CSSML = null$in_type 'string'$in_params = null)
  155.     {
  156.         $interface_path 'CSSML/' $in_driver '.php';
  157.         $interface_class 'XML_CSSML_' $in_driver;
  158.  
  159.         @include_once $interface_path;
  160.  
  161.         $obj =new $interface_class($in_CSSML$in_type$in_params);
  162.         return $obj;
  163.     }
  164.  
  165.     // }}}
  166.     // {{{ load()
  167.  
  168.     /**
  169.      * Prepare the CSSML document object from either a string, file or object.  This
  170.      * will set the CSSMLDoc class variable which will be parsed by the xsl stylesheet
  171.      * into a CSS stylesheet
  172.      *
  173.      * @param mixed $in_CSSML The CSSML document which contains the information for
  174.      *                          generating the CSS document
  175.      *
  176.      * @return void 
  177.      *
  178.      */
  179.     function load()
  180.     {
  181.         if ($this->loaded{
  182.             return PEAR::raiseError(nullXML_CSSML_ALREADY_EXISTSnullE_USER_WARNING$this->CSSMLDoc'XML_CSSML_Error'true);
  183.         }
  184.     }
  185.  
  186.      // }}}
  187.      // {{{ setParams()
  188.  
  189.     /**
  190.      * Set the params (params) that will be used when calling the stylesheet parser.
  191.      * This pertains particularly to variables such as browser code, image path and
  192.      * the filter.  It works by passing an associative array with any number of the
  193.      * possible parameters for the stylesheet.  If a variable is not set, the default
  194.      * will be used
  195.      *
  196.      * @param array $in_params Associative array of the params
  197.      *
  198.      * @return void 
  199.      *
  200.      */
  201.     function setParams($in_params)
  202.     {
  203.         if (isset($in_params['browser'])) {
  204.             $this->browser = $in_params['browser'];
  205.         }
  206.  
  207.         if (isset($in_params['filter'])) {
  208.             $this->filter = $in_params['filter'];
  209.         }
  210.  
  211.         if (isset($in_params['comment'])) {
  212.             $this->comment = str_replace(array('/*''*/')''$in_params['comment']);
  213.         }
  214.  
  215.         if (isset($in_params['output'])) {
  216.             $this->output = $in_params['output'];
  217.             if ($in_params['output'!= 'STDOUT'{
  218.                 // check to make sure this is a file...this needs work
  219.                 if (!@file_exists($in_params['output']&& !@touch($in_params['output'])) {
  220.                     $this->output = 'STDOUT';
  221.                     return PEAR::raiseError(nullXML_CSSML_INVALID_FILEPEAR_ERROR_PRINTE_USER_NOTICE'''XML_CSSML_Error'true);
  222.                 }
  223.             }
  224.         }
  225.     }
  226.  
  227.     // }}}
  228.     // {{{ process()
  229.  
  230.     /**
  231.      * Run the transformation on the CSSML document using the CSSML xsl stylesheet.  If
  232.      * the output method is to a file, then the function will not return.  If the output
  233.      * is set to STDOUT, the xml string will be returned (really the css document) after
  234.      * some clean up of entities and domxml bugs have been fixed
  235.      *
  236.      * @return css string if output method is STDOUT, else void
  237.      * @access public
  238.      */
  239.     function process()
  240.     {
  241.         if (!$this->loaded{
  242.             return PEAR::raiseError(nullXML_CSSML_NOT_LOADEDnullE_USER_WARNING'use load() function''XML_CSSML_Error'true);
  243.         }
  244.     }
  245.  
  246.     // }}}
  247.     // {{{ isError()
  248.  
  249.     /**
  250.      * Tell whether a result code from a XML_CSSML method is an error.
  251.      *
  252.      * @param  object  $in_value object in question
  253.      *
  254.      * @access public
  255.      * @return boolean whether object is an error object
  256.      */
  257.     function isError($in_value)
  258.     {
  259.         return is_a($in_value'xml_cssml_error');
  260.     }
  261.  
  262.     // }}}
  263.     // {{{ errorMessage()
  264.  
  265.     /**
  266.      * Return a textual error message for an XML_CSSML error code.
  267.      *
  268.      * @param  int $in_value error code
  269.      *
  270.      * @access public
  271.      * @return string error message, or false if not error code
  272.      */
  273.     function errorMessage($in_value)
  274.     {
  275.         // make the variable static so that it only has to do the defining on the first call
  276.         static $errorMessages;
  277.  
  278.         // define the varies error messages
  279.         if (!isset($errorMessages)) {
  280.             $errorMessages = array(
  281.                 XML_CSSML_OK                    => 'no error',
  282.                 XML_CSSML_ERROR                 => 'unknown error',
  283.                 XML_CSSML_ALREADY_EXISTS        => 'cssml document already loaded',
  284.                 XML_CSSML_NOT_LOADED            => 'cssml document has not been loaded',
  285.                 XML_CSSML_INVALID_DATA          => 'invalid cssml data to parse',
  286.                 XML_CSSML_INVALID_DOCUMENT      => 'cssml domdocument could not be created',
  287.                 XML_CSSML_INVALID_FILE          => 'output file does not exist',
  288.             );
  289.         }
  290.  
  291.         // If this is an error object, then grab the corresponding error code
  292.         if (XML_CSSML::isError($in_value)) {
  293.             $in_value $in_value->getCode();
  294.         }
  295.  
  296.         // return the textual error message corresponding to the code
  297.         return isset($errorMessages[$in_value]$errorMessages[$in_value$errorMessages[XML_CSSML_ERROR];
  298.     }
  299.  
  300.     // }}}
  301.     // {{{ reset()
  302.  
  303.     /**
  304.      * Resets the object so it is possible to load another xml document.
  305.      *
  306.      * @access public
  307.      * @return void 
  308.      */
  309.     function reset()
  310.     {
  311.         $this->CSSMLDoc = null;
  312.         $this->loaded = false;
  313.     }
  314.  
  315.     // }}}
  316.     // {{{ free()
  317.  
  318.     /**
  319.      * Kill the class object to free memory.  Not really sure how necessary this is, but xml
  320.      * documents can be pretty big.  This will kill everything, so only use it when you are done
  321.      *
  322.      * @access public
  323.      * @return void 
  324.      */
  325.     function free()
  326.     {
  327.         $vars get_class_vars('XML_CSSML');
  328.  
  329.         foreach ($vars as $var => $value{
  330.             $this->$var = null;
  331.         }
  332.     }
  333.  
  334.     // }}}
  335. }
  336. ?>

Documentation generated on Mon, 11 Mar 2019 14:12:45 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.