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

Source for file Anchor.php

Documentation is available at Anchor.php

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

Documentation generated on Mon, 11 Mar 2019 15:48:09 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.