home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / Anchor.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  6.1 KB  |  275 lines

  1. <?php
  2. //
  3. // +---------------------------------------------------------------------------+
  4. // | PEAR :: XML :: Transformer :: Anchor Namespace Handler                    |
  5. // +---------------------------------------------------------------------------+
  6. // | Copyright (c) 2002-2004 Sebastian Bergmann <sb@sebastian-bergmann.de> and |
  7. // |                         Kristian K÷hntopp <kris@koehntopp.de>.            |
  8. // +---------------------------------------------------------------------------+
  9. // | This source file is subject to version 3.00 of the PHP License,           |
  10. // | that is available at http://www.php.net/license/3_0.txt.                  |
  11. // | If you did not receive a copy of the PHP license and are unable to        |
  12. // | obtain it through the world-wide-web, please send a note to               |
  13. // | license@php.net so we can mail you a copy immediately.                    |
  14. // +---------------------------------------------------------------------------+
  15. //
  16. // $Id: Anchor.php,v 1.12 2004/01/01 10:31:55 sebastian Exp $
  17. //
  18.  
  19. require_once 'XML/Transformer/Namespace.php';
  20. require_once 'XML/Util.php';
  21.  
  22. /**
  23. * Handler for the Anchor Namespace.
  24. *
  25. * This namespace maintains an anchor database, a database of
  26. * named links. These links can be referenced using the iref
  27. * tag within this namespace.
  28. *
  29. * This allows for a central storage of links, changing links
  30. * need only be changed in one locations. Designers can reference
  31. * the link through the symbolic name.
  32. *
  33. * Example:
  34. *
  35. * ...
  36. *   $n = XML_Transformer_Namespace_Anchor;
  37. *   $t->overloadNamespace("a", $n);
  38. *
  39. *   $n->setDatabase(
  40. *         array(
  41. *           "pear" => array(
  42. *             "href"  => "http://pear.php.net",
  43. *             "title" => "PEAR Homepage"
  44. *           )
  45. *         )
  46. *   );
  47. * ?>
  48. * <p>The <a:iref iref="pear">PEAR Homepage</a:iref> is now online.</p>
  49. *
  50. *
  51. * Output:
  52. * <p>The <a href="http://www.pear.net" title="PEAR Homepage">PEAR
  53. * Homepage</a> is now online.</p>
  54. *
  55. *
  56. * @author  Sebastian Bergmann <sb@sebastian-bergmann.de>
  57. * @author  Kristian K÷hntopp <kris@koehntopp.de>
  58. * @version $Revision: 1.12 $
  59. * @access  public
  60. */
  61. class XML_Transformer_Namespace_Anchor extends XML_Transformer_Namespace {
  62.     // {{{ Members
  63.  
  64.     /**
  65.     * @var    boolean
  66.     * @access public
  67.     */
  68.     var $defaultNamespacePrefix = 'a';
  69.  
  70.     /**
  71.     * @var    array
  72.     * @access private
  73.     */
  74.     var $_anchorDatabase = array();
  75.  
  76.     /**
  77.     * @var    array
  78.     * @access private
  79.     */
  80.     var $_irefAttributes = array();
  81.  
  82.     // {{{ function setDatabase($db)
  83.  
  84.     /**
  85.     * Install a complete link database array.
  86.     *
  87.     * @param  array
  88.     * @return boolean
  89.     * @access public
  90.     */
  91.     function setDatabase($db) {
  92.         $this->_anchorDatabase = $db;
  93.  
  94.         return true;
  95.     }
  96.  
  97.     // }}}
  98.     // {{{ function getDatabase($db)
  99.  
  100.     /**
  101.     * Return the link database array.
  102.     *
  103.     * @return array
  104.     * @access public
  105.     */
  106.     function getDatabase() {
  107.         return $this->_anchorDatabase;
  108.     }
  109.  
  110.     // }}}
  111.     // {{{ function addItem($item, $attr)
  112.  
  113.     /**
  114.     * Add an item $item with the attributes $attr to the link database array.
  115.     *
  116.     * @param  string
  117.     * @param  array
  118.     * @return boolean
  119.     * @access public
  120.     */
  121.     function addItem($item, $attr) {
  122.         $this->_anchorDatabase[$item] = $attr;
  123.  
  124.         return true;
  125.     }
  126.  
  127.     // }}}
  128.     // {{{ function dropItem($item)
  129.  
  130.     /**
  131.     * Drop an item $item drom the link database array.
  132.     *
  133.     * @param  string
  134.     * @return boolean
  135.     * @access public
  136.     */
  137.     function dropItem($item) {
  138.         if (!isset($this->_anchorDatabase[$item]))
  139.             return false;
  140.  
  141.         unset($this->_anchorDatabase[$item]);
  142.  
  143.         return true;
  144.     }
  145.  
  146.     // }}}
  147.     // {{{ function getItem($item)
  148.  
  149.     /**
  150.     * Get an item $item from the link database array.
  151.     *
  152.     * @param  string
  153.     * @return mixed
  154.     * @access public
  155.     */
  156.     function getItem($item) {
  157.         if (!isset($this->_anchorDatabase[$item])) {
  158.             return false;
  159.         }
  160.  
  161.         return $this->_anchorDatabase[$item];
  162.     }
  163.  
  164.     // }}}
  165.     // {{{ function start_iref($attributes)
  166.  
  167.     /**
  168.     * @param  array
  169.     * @return string
  170.     * @access public
  171.     */
  172.     function start_iref($attributes) {
  173.         $this->_irefAttributes = $attributes;
  174.  
  175.         return '';
  176.     }
  177.  
  178.     // }}}
  179.     // {{{ function end_iref($cdata)
  180.  
  181.     /**
  182.     * @param  string
  183.     * @return string
  184.     * @access public
  185.     */
  186.     function end_iref($cdata) {
  187.         if (!isset($this->_irefAttributes['iref']))
  188.             return '';
  189.  
  190.         $name = $this->_irefAttributes['iref'];
  191.         if (!isset($this->_anchorDatabase[$name]))
  192.             return sprintf('<span>(undefined reference %s)%s</span>',
  193.                 $name,
  194.                 $cdata
  195.             );
  196.  
  197.         return sprintf('<a %s>%s</a>',
  198.             XML_Util::attributesToString($this->_anchorDatabase[$name]),
  199.             $cdata
  200.         );
  201.     }
  202.  
  203.     // }}}
  204.     // {{{ function start_random($attributes)
  205.  
  206.     /**
  207.     * @param  array
  208.     * @return string
  209.     * @access public
  210.     */
  211.     function start_random($attributes) {
  212.         return '';
  213.     }
  214.  
  215.     // }}}
  216.     // {{{ function end_random($cdata)
  217.  
  218.     /**
  219.     * @param  string
  220.     * @return string
  221.     * @access public
  222.     */
  223.     function end_random($cdata) {
  224.         srand((double)microtime()*1000000);
  225.  
  226.         $keys = array_keys($this->_anchorDatabase);
  227.         $pos  = rand(0, count($keys)-1);
  228.         $name = $keys[$pos];
  229.  
  230.         return sprintf('<a %s>%s</a>',
  231.             XML_Util::attributesToString($this->_anchorDatabase[$name]),
  232.             $cdata
  233.         );
  234.     }
  235.  
  236.     // }}}
  237.     // {{{ function start_link($attributes)
  238.  
  239.     /**
  240.     * @param  array
  241.     * @return string
  242.     * @access public
  243.     */
  244.     function start_link($attributes) {
  245.         if (!isset($attributes['name']))
  246.             return '';
  247.  
  248.         $name = $attributes['name'];
  249.         unset($attributes['name']);
  250.  
  251.         $this->addItem($name, $attributes);
  252.         return '';
  253.     }
  254.  
  255.     // }}}
  256.     // {{{ function end_link($cdata)
  257.  
  258.     /**
  259.     * @param  string
  260.     * @return string
  261.     * @access public
  262.     */
  263.     function end_link($cdata) {
  264.         return '';
  265.     }
  266.  
  267.     // }}}
  268. }
  269.  
  270. /*
  271.  * vim600:  et sw=2 ts=2 fdm=marker
  272.  * vim<600: et sw=2 ts=2
  273.  */
  274. ?>
  275.