Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: Yadis

Developer Network License

The Joomla! Developer Network content is © copyright 2006 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution- NonCommercial- ShareAlike 2.5
Source code for file /openid/Services/Yadis/XML.php

Documentation is available at XML.php

  1. <?php
  2.  
  3. /**
  4.  * XML-parsing classes to wrap the domxml and DOM extensions for PHP 4
  5.  * and 5, respectively.
  6.  *
  7.  * @package Yadis
  8.  */
  9.  
  10. /**
  11.  * The base class for wrappers for available PHP XML-parsing
  12.  * extensions.  To work with this Yadis library, subclasses of this
  13.  * class MUST implement the API as defined in the remarks for this
  14.  * class.  Subclasses of Services_Yadis_XMLParser are used to wrap
  15.  * particular PHP XML extensions such as 'domxml'.  These are used
  16.  * internally by the library depending on the availability of
  17.  * supported PHP XML extensions.
  18.  *
  19.  * @package Yadis
  20.  */
  21.     /**
  22.      * Initialize an instance of Services_Yadis_XMLParser with some
  23.      * XML and namespaces.  This SHOULD NOT be overridden by
  24.      * subclasses.
  25.      *
  26.      * @param string $xml_string A string of XML to be parsed.
  27.      * @param array $namespace_map An array of ($ns_name => $ns_uri)
  28.      *  to be registered with the XML parser.  May be empty.
  29.      * @return boolean $result True if the initialization and
  30.      *  namespace registration(s) succeeded; false otherwise.
  31.      */
  32.     function init($xml_string$namespace_map)
  33.     {
  34.         if (!$this->setXML($xml_string)) {
  35.             return false;
  36.         }
  37.  
  38.         foreach ($namespace_map as $prefix => $uri{
  39.             if (!$this->registerNamespace($prefix$uri)) {
  40.                 return false;
  41.             }
  42.         }
  43.  
  44.         return true;
  45.     }
  46.  
  47.     /**
  48.      * Register a namespace with the XML parser.  This should be
  49.      * overridden by subclasses.
  50.      *
  51.      * @param string $prefix The namespace prefix to appear in XML tag
  52.      *  names.
  53.      *
  54.      * @param string $uri The namespace URI to be used to identify the
  55.      *  namespace in the XML.
  56.      *
  57.      * @return boolean $result True if the registration succeeded;
  58.      *  false otherwise.
  59.      */
  60.     function registerNamespace($prefix$uri)
  61.     {
  62.         // Not implemented.
  63.     }
  64.  
  65.     /**
  66.      * Set this parser object's XML payload.  This should be
  67.      * overridden by subclasses.
  68.      *
  69.      * @param string $xml_string The XML string to pass to this
  70.      *  object's XML parser.
  71.      *
  72.      * @return boolean $result True if the initialization succeeded;
  73.      *  false otherwise.
  74.      */
  75.     function setXML($xml_string)
  76.     {
  77.         // Not implemented.
  78.     }
  79.  
  80.     /**
  81.      * Evaluate an XPath expression and return the resulting node
  82.      * list.  This should be overridden by subclasses.
  83.      *
  84.      * @param string $xpath The XPath expression to be evaluated.
  85.      *
  86.      * @param mixed $node A node object resulting from a previous
  87.      *  evalXPath call.  This node, if specified, provides the context
  88.      *  for the evaluation of this xpath expression.
  89.      *
  90.      * @return array $node_list An array of matching opaque node
  91.      *  objects to be used with other methods of this parser class.
  92.      */
  93.     function evalXPath($xpath$node null)
  94.     {
  95.         // Not implemented.
  96.     }
  97.  
  98.     /**
  99.      * Return the textual content of a specified node.
  100.      *
  101.      * @param mixed $node A node object from a previous call to
  102.      *  $this->evalXPath().
  103.      *
  104.      * @return string $content The content of this node.
  105.      */
  106.     function content($node)
  107.     {
  108.         // Not implemented.
  109.     }
  110.  
  111.     /**
  112.      * Return the attributes of a specified node.
  113.      *
  114.      * @param mixed $node A node object from a previous call to
  115.      *  $this->evalXPath().
  116.      *
  117.      * @return array $attrs An array mapping attribute names to
  118.      *  values.
  119.      */
  120.     function attributes($node)
  121.     {
  122.         // Not implemented.
  123.     }
  124. }
  125.  
  126. /**
  127.  * This concrete implementation of Services_Yadis_XMLParser implements
  128.  * the appropriate API for the 'domxml' extension which is typically
  129.  * packaged with PHP 4.  This class will be used whenever the 'domxml'
  130.  * extension is detected.  See the Services_Yadis_XMLParser class for
  131.  * details on this class's methods.
  132.  *
  133.  * @package Yadis
  134.  */
  135.     function Services_Yadis_domxml()
  136.     {
  137.         $this->xml null;
  138.         $this->doc null;
  139.         $this->xpath null;
  140.         $this->errors array();
  141.     }
  142.  
  143.     function setXML($xml_string)
  144.     {
  145.         $this->xml $xml_string;
  146.         $this->doc @domxml_open_mem($xml_stringDOMXML_LOAD_PARSING,
  147.                                       $this->errors);
  148.  
  149.         if (!$this->doc{
  150.             return false;
  151.         }
  152.  
  153.         $this->xpath $this->doc->xpath_new_context();
  154.  
  155.         return true;
  156.     }
  157.  
  158.     function registerNamespace($prefix$uri)
  159.     {
  160.         return xpath_register_ns($this->xpath$prefix$uri);
  161.     }
  162.  
  163.     function &evalXPath($xpath$node null)
  164.     {
  165.         if ($node{
  166.             $result @$this->xpath->xpath_eval($xpath$node);
  167.         else {
  168.             $result @$this->xpath->xpath_eval($xpath);
  169.         }
  170.  
  171.         if (!$result->nodeset{
  172.             $n array();
  173.             return $n;
  174.         }
  175.  
  176.         return $result->nodeset;
  177.     }
  178.  
  179.     function content($node)
  180.     {
  181.         if ($node{
  182.             return $node->get_content();
  183.         }
  184.     }
  185.  
  186.     function attributes($node)
  187.     {
  188.         if ($node{
  189.             $arr $node->attributes();
  190.             $result array();
  191.  
  192.             if ($arr{
  193.                 foreach ($arr as $attrnode{
  194.                     $result[$attrnode->name$attrnode->value;
  195.                 }
  196.             }
  197.  
  198.             return $result;
  199.         }
  200.     }
  201. }
  202.  
  203. /**
  204.  * This concrete implementation of Services_Yadis_XMLParser implements
  205.  * the appropriate API for the 'dom' extension which is typically
  206.  * packaged with PHP 5.  This class will be used whenever the 'dom'
  207.  * extension is detected.  See the Services_Yadis_XMLParser class for
  208.  * details on this class's methods.
  209.  *
  210.  * @package Yadis
  211.  */
  212.     function Services_Yadis_dom()
  213.     {
  214.         $this->xml null;
  215.         $this->doc null;
  216.         $this->xpath null;
  217.         $this->errors array();
  218.     }
  219.  
  220.     function setXML($xml_string)
  221.     {
  222.         $this->xml $xml_string;
  223.         $this->doc new DOMDocument;
  224.  
  225.         if (!$this->doc{
  226.             return false;
  227.         }
  228.  
  229.         if (!@$this->doc->loadXML($xml_string)) {
  230.             return false;
  231.         }
  232.  
  233.         $this->xpath new DOMXPath($this->doc);
  234.  
  235.         if ($this->xpath{
  236.             return true;
  237.         else {
  238.             return false;
  239.         }
  240.     }
  241.  
  242.     function registerNamespace($prefix$uri)
  243.     {
  244.         return $this->xpath->registerNamespace($prefix$uri);
  245.     }
  246.  
  247.     function &evalXPath($xpath$node null)
  248.     {
  249.         if ($node{
  250.             $result @$this->xpath->query($xpath$node);
  251.         else {
  252.             $result @$this->xpath->query($xpath);
  253.         }
  254.  
  255.         $n array();
  256.  
  257.         for ($i 0$i $result->length$i++{
  258.             $n[$result->item($i);
  259.         }
  260.  
  261.         return $n;
  262.     }
  263.  
  264.     function content($node)
  265.     {
  266.         if ($node{
  267.             return $node->textContent;
  268.         }
  269.     }
  270.  
  271.     function attributes($node)
  272.     {
  273.         if ($node{
  274.             $arr $node->attributes;
  275.             $result array();
  276.  
  277.             if ($arr{
  278.                 for ($i 0$i $arr->length$i++{
  279.                     $node $arr->item($i);
  280.                     $result[$node->nodeName$node->nodeValue;
  281.                 }
  282.             }
  283.  
  284.             return $result;
  285.         }
  286.     }
  287. }
  288.  
  289. $__Services_Yadis_defaultParser null;
  290.  
  291. /**
  292.  * Set a default parser to override the extension-driven selection of
  293.  * available parser classes.  This is helpful in a test environment or
  294.  * one in which multiple parsers can be used but one is more
  295.  * desirable.
  296.  *
  297.  * @param Services_Yadis_XMLParser $parser An instance of a
  298.  *  Services_Yadis_XMLParser subclass.
  299.  */
  300. {
  301.     global $__Services_Yadis_defaultParser;
  302.     $__Services_Yadis_defaultParser =$parser;
  303. }
  304.  
  305. $GLOBALS['__Services_Yadis_xml_extensions'array(
  306.     'dom' => array('classname' => 'Services_Yadis_dom',
  307.                    'libname' => array('dom.so''dom.dll')),
  308.     'domxml' => array('classname' => 'Services_Yadis_domxml',
  309.                       'libname' => array('domxml.so''php_domxml.dll')),
  310.     );
  311.  
  312. /**
  313.  * Returns an instance of a Services_Yadis_XMLParser subclass based on
  314.  * the availability of PHP extensions for XML parsing.  If
  315.  * Services_Yadis_setDefaultParser has been called, the parser used in
  316.  * that call will be returned instead.
  317.  */
  318. {
  319.     global $__Services_Yadis_defaultParser,
  320.         $__Services_Yadis_xml_extensions;
  321.  
  322.     if (isset($__Services_Yadis_defaultParser)) {
  323.         return $__Services_Yadis_defaultParser;
  324.     }
  325.  
  326.     $p null;
  327.     $classname null;
  328.  
  329.     // Return a wrapper for the resident implementation, if any.
  330.     foreach ($__Services_Yadis_xml_extensions as $name => $params)
  331.     {
  332.         echo $name;
  333.         if (!extension_loaded($name)) {
  334.             foreach ($params['libname'as $libname{
  335.                 if (@dl($libname)) {
  336.                     $classname $params['classname'];
  337.                 }
  338.             }
  339.         else {
  340.             $classname $params['classname'];
  341.         }
  342.         if (isset($classname)) {
  343.             $p new $classname();
  344.             return $p;
  345.         }
  346.     }
  347.  
  348.     if (!isset($p)) {
  349.         trigger_error('No XML parser was found'E_USER_ERROR);
  350.     else {
  351.         Services_Yadis_setDefaultParser($p);
  352.     }
  353.  
  354.     return $p;
  355. }
  356.  
  357. ?>

Documentation generated on Mon, 05 Mar 2007 21:31:47 +0000 by phpDocumentor 1.3.1