Subversion Repositories Applications.annuaire

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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