4 * XML-parsing classes to wrap the domxml and DOM extensions for PHP 4
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 Auth_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.
21 class Auth_Yadis_XMLParser {
23 * Initialize an instance of Auth_Yadis_XMLParser with some
24 * XML and namespaces. This SHOULD NOT be overridden by
27 * @param string $xml_string A string of XML to be parsed.
28 * @param array $namespace_map An array of ($ns_name => $ns_uri)
29 * to be registered with the XML parser. May be empty.
30 * @return boolean $result True if the initialization and
31 * namespace registration(s) succeeded; false otherwise.
33 function init($xml_string, $namespace_map)
35 if (!$this->setXML($xml_string)) {
39 foreach ($namespace_map as $prefix => $uri) {
40 if (!$this->registerNamespace($prefix, $uri)) {
49 * Register a namespace with the XML parser. This should be
50 * overridden by subclasses.
52 * @param string $prefix The namespace prefix to appear in XML tag
55 * @param string $uri The namespace URI to be used to identify the
56 * namespace in the XML.
58 * @return boolean $result True if the registration succeeded;
61 function registerNamespace($prefix, $uri)
67 * Set this parser object's XML payload. This should be
68 * overridden by subclasses.
70 * @param string $xml_string The XML string to pass to this
71 * object's XML parser.
73 * @return boolean $result True if the initialization succeeded;
76 function setXML($xml_string)
82 * Evaluate an XPath expression and return the resulting node
83 * list. This should be overridden by subclasses.
85 * @param string $xpath The XPath expression to be evaluated.
87 * @param mixed $node A node object resulting from a previous
88 * evalXPath call. This node, if specified, provides the context
89 * for the evaluation of this xpath expression.
91 * @return array $node_list An array of matching opaque node
92 * objects to be used with other methods of this parser class.
94 function evalXPath($xpath, $node = null)
100 * Return the textual content of a specified node.
102 * @param mixed $node A node object from a previous call to
103 * $this->evalXPath().
105 * @return string $content The content of this node.
107 function content($node)
113 * Return the attributes of a specified node.
115 * @param mixed $node A node object from a previous call to
116 * $this->evalXPath().
118 * @return array $attrs An array mapping attribute names to
121 function attributes($node)
128 * This concrete implementation of Auth_Yadis_XMLParser implements
129 * the appropriate API for the 'domxml' extension which is typically
130 * packaged with PHP 4. This class will be used whenever the 'domxml'
131 * extension is detected. See the Auth_Yadis_XMLParser class for
132 * details on this class's methods.
136 class Auth_Yadis_domxml extends Auth_Yadis_XMLParser {
137 function Auth_Yadis_domxml()
142 $this->errors = array();
145 function setXML($xml_string)
147 $this->xml = $xml_string;
148 $this->doc = @domxml_open_mem($xml_string, DOMXML_LOAD_PARSING,
155 $this->xpath = $this->doc->xpath_new_context();
160 function registerNamespace($prefix, $uri)
162 return xpath_register_ns($this->xpath, $prefix, $uri);
165 function &evalXPath($xpath, $node = null)
168 $result = @$this->xpath->xpath_eval($xpath, $node);
170 $result = @$this->xpath->xpath_eval($xpath);
178 if (!$result->nodeset) {
183 return $result->nodeset;
186 function content($node)
189 return $node->get_content();
193 function attributes($node)
196 $arr = $node->attributes();
200 foreach ($arr as $attrnode) {
201 $result[$attrnode->name] = $attrnode->value;
211 * This concrete implementation of Auth_Yadis_XMLParser implements
212 * the appropriate API for the 'dom' extension which is typically
213 * packaged with PHP 5. This class will be used whenever the 'dom'
214 * extension is detected. See the Auth_Yadis_XMLParser class for
215 * details on this class's methods.
219 class Auth_Yadis_dom extends Auth_Yadis_XMLParser {
220 function Auth_Yadis_dom()
225 $this->errors = array();
228 function setXML($xml_string)
230 $this->xml = $xml_string;
231 $this->doc = new DOMDocument;
237 if (!@$this->doc->loadXML($xml_string)) {
241 $this->xpath = new DOMXPath($this->doc);
250 function registerNamespace($prefix, $uri)
252 return $this->xpath->registerNamespace($prefix, $uri);
255 function &evalXPath($xpath, $node = null)
258 $result = @$this->xpath->query($xpath, $node);
260 $result = @$this->xpath->query($xpath);
269 for ($i = 0; $i < $result->length; $i++) {
270 $n[] = $result->item($i);
276 function content($node)
279 return $node->textContent;
283 function attributes($node)
286 $arr = $node->attributes;
290 for ($i = 0; $i < $arr->length; $i++) {
291 $node = $arr->item($i);
292 $result[$node->nodeName] = $node->nodeValue;
301 global $__Auth_Yadis_defaultParser;
302 $__Auth_Yadis_defaultParser = null;
305 * Set a default parser to override the extension-driven selection of
306 * available parser classes. This is helpful in a test environment or
307 * one in which multiple parsers can be used but one is more
310 * @param Auth_Yadis_XMLParser $parser An instance of a
311 * Auth_Yadis_XMLParser subclass.
313 function Auth_Yadis_setDefaultParser(&$parser)
315 global $__Auth_Yadis_defaultParser;
316 $__Auth_Yadis_defaultParser =& $parser;
319 function Auth_Yadis_getSupportedExtensions()
322 'dom' => array('classname' => 'Auth_Yadis_dom',
323 'libname' => array('dom.so', 'dom.dll')),
324 'domxml' => array('classname' => 'Auth_Yadis_domxml',
325 'libname' => array('domxml.so', 'php_domxml.dll')),
330 * Returns an instance of a Auth_Yadis_XMLParser subclass based on
331 * the availability of PHP extensions for XML parsing. If
332 * Auth_Yadis_setDefaultParser has been called, the parser used in
333 * that call will be returned instead.
335 function &Auth_Yadis_getXMLParser()
337 global $__Auth_Yadis_defaultParser;
339 if (isset($__Auth_Yadis_defaultParser)) {
340 return $__Auth_Yadis_defaultParser;
346 $extensions = Auth_Yadis_getSupportedExtensions();
348 // Return a wrapper for the resident implementation, if any.
349 foreach ($extensions as $name => $params) {
350 if (!extension_loaded($name)) {
351 foreach ($params['libname'] as $libname) {
353 $classname = $params['classname'];
357 $classname = $params['classname'];
359 if (isset($classname)) {
360 $p = new $classname();
366 trigger_error('No XML parser was found', E_USER_ERROR);
368 Auth_Yadis_setDefaultParser($p);