perl-XML-LibXML / xpath.c

pa...@9ae0c189-c… 893c4b6 







ma...@9ae0c189-c… 7f5ccab 


ma...@9ae0c189-c… a7b2a55 
ma...@9ae0c189-c… 7f5ccab 
ph...@9ae0c189-c… 4924887 

ma...@9ae0c189-c… 7f5ccab 
pa...@9ae0c189-c… e34900c 
ma...@9ae0c189-c… 7f5ccab 
ma...@9ae0c189-c… a7b2a55 

ph...@9ae0c189-c… 317ff50 

ma...@9ae0c189-c… a7b2a55 
ph...@9ae0c189-c… 7dfc74b 
ma...@9ae0c189-c… a7b2a55 

















ph...@9ae0c189-c… 7dfc74b 




ma...@9ae0c189-c… a7b2a55 





























ph...@9ae0c189-c… 7dfc74b 


ma...@9ae0c189-c… a7b2a55 









ph...@9ae0c189-c… 7dfc74b 



ma...@9ae0c189-c… a7b2a55 











































ph...@9ae0c189-c… 7dfc74b 


ma...@9ae0c189-c… a7b2a55 


ma...@9ae0c189-c… 7f5ccab 
ma...@9ae0c189-c… fc9c0ec 
ma...@9ae0c189-c… 7f5ccab 




ma...@9ae0c189-c… 60e23e3 
pa...@9ae0c189-c… d4f6610 
ma...@9ae0c189-c… 60e23e3 
pa...@9ae0c189-c… e34900c 




pa...@9ae0c189-c… d4f6610 
pa...@9ae0c189-c… e34900c 




pa...@9ae0c189-c… d4f6610 
pa...@9ae0c189-c… e34900c 
ma...@9ae0c189-c… 60e23e3 
pa...@9ae0c189-c… e34900c 
ma...@9ae0c189-c… 60e23e3 
ph...@9ae0c189-c… 4924887 























pa...@9ae0c189-c… 85a4db1 


ph...@9ae0c189-c… 4924887 

ma...@9ae0c189-c… 60e23e3 


ma...@9ae0c189-c… fc9c0ec 
ma...@9ae0c189-c… c7a9949 
ph...@9ae0c189-c… 2bbfa54 

ma...@9ae0c189-c… c7a9949 



ma...@9ae0c189-c… fc9c0ec 





ph...@9ae0c189-c… d8b0de9 


pa...@9ae0c189-c… d4f6610 
pa...@9ae0c189-c… 4ad0a81 
pa...@9ae0c189-c… d4f6610 

pa...@9ae0c189-c… 4ad0a81 







pa...@9ae0c189-c… d4f6610 


ph...@9ae0c189-c… d8b0de9 



ma...@9ae0c189-c… 60e23e3 
ph...@9ae0c189-c… 4924887 



pa...@9ae0c189-c… 85a4db1 
pa...@9ae0c189-c… c385561 

ph...@9ae0c189-c… 4924887 

ph...@9ae0c189-c… d9af518 

ph...@9ae0c189-c… 4924887 


ma...@9ae0c189-c… 60e23e3 



pa...@9ae0c189-c… e34900c 
ma...@9ae0c189-c… 7f5ccab 

ph...@9ae0c189-c… 317ff50 

ma...@9ae0c189-c… 7f5ccab 
pa...@9ae0c189-c… d4f6610 
ma...@9ae0c189-c… 60e23e3 









ma...@9ae0c189-c… 7f5ccab 
ma...@9ae0c189-c… 60e23e3 
ma...@9ae0c189-c… 7f5ccab 
ma...@9ae0c189-c… 60e23e3 
ma...@9ae0c189-c… 7f5ccab 
pa...@9ae0c189-c… a298949 
pa...@9ae0c189-c… e34900c 





pa...@9ae0c189-c… d4f6610 
pa...@9ae0c189-c… e34900c 















pa...@9ae0c189-c… a298949 







pa...@9ae0c189-c… d4f6610 
pa...@9ae0c189-c… a298949 






pa...@9ae0c189-c… d4f6610 
pa...@9ae0c189-c… e34900c 





pa...@9ae0c189-c… d4f6610 
pa...@9ae0c189-c… e34900c 




pa...@9ae0c189-c… a298949 





pa...@9ae0c189-c… e34900c 
pa...@9ae0c189-c… a298949 









pa...@9ae0c189-c… 85a4db1 


pa...@9ae0c189-c… a298949 
pa...@9ae0c189-c… d4f6610 
pa...@9ae0c189-c… 4ad0a81 
pa...@9ae0c189-c… d4f6610 

pa...@9ae0c189-c… 4ad0a81 







pa...@9ae0c189-c… d4f6610 


pa...@9ae0c189-c… a298949 


pa...@9ae0c189-c… 85a4db1 
pa...@9ae0c189-c… a298949 
pa...@9ae0c189-c… c385561 
pa...@9ae0c189-c… a298949 

pa...@9ae0c189-c… 22f5aa0 


pa...@9ae0c189-c… a298949 










pa...@9ae0c189-c… d4f6610 
pa...@9ae0c189-c… a298949 













  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/* $Id$
 *
 * This is free software, you may use it and distribute it under the same terms as
 * Perl itself.
 *
 * Copyright 2001-2003 AxKit.com Ltd., 2002-2006 Christian Glahn, 2006-2009 Petr Pajas
*/

#include <libxml/tree.h>
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>
#include <libxml/uri.h>

#include "EXTERN.h"

#include "dom.h"
#include "xpath.h"

void
perlDocumentFunction(xmlXPathParserContextPtr ctxt, int nargs){
    xmlXPathObjectPtr obj = NULL, obj2 = NULL;
    xmlChar *base = NULL, *URI = NULL;


    if ((nargs < 1) || (nargs > 2)) {
        ctxt->error = XPATH_INVALID_ARITY;
        return;
    }
    if (ctxt->value == NULL) {
        ctxt->error = XPATH_INVALID_TYPE;
        return;
    }

    if (nargs == 2) {
        if (ctxt->value->type != XPATH_NODESET) {
            ctxt->error = XPATH_INVALID_TYPE;
            return;
        }

        obj2 = valuePop(ctxt);
    }


    /* first assure the XML::LibXML error handler is deactivated 
       otherwise strange things might happen
     */

    if (ctxt->value->type == XPATH_NODESET) {
        int i;
        xmlXPathObjectPtr newobj, ret;

        obj = valuePop(ctxt);
        ret = xmlXPathNewNodeSet(NULL);

        if (obj->nodesetval) {
            for (i = 0; i < obj->nodesetval->nodeNr; i++) {
                valuePush(ctxt,
                          xmlXPathNewNodeSet(obj->nodesetval->nodeTab[i]));
                xmlXPathStringFunction(ctxt, 1);
                if (nargs == 2) {
                    valuePush(ctxt, xmlXPathObjectCopy(obj2));
                } else {
                    valuePush(ctxt,
                              xmlXPathNewNodeSet(obj->nodesetval->nodeTab[i]));
                }
                perlDocumentFunction(ctxt, 2);
                newobj = valuePop(ctxt);
                ret->nodesetval = xmlXPathNodeSetMerge(ret->nodesetval,
                                                       newobj->nodesetval);
                xmlXPathFreeObject(newobj);
            }
        }

        xmlXPathFreeObject(obj);
        if (obj2 != NULL)
            xmlXPathFreeObject(obj2);
        valuePush(ctxt, ret);

        /* reset the error old error handler before leaving
         */
        return;
    }
    /*
     * Make sure it's converted to a string
     */
    xmlXPathStringFunction(ctxt, 1);
    if (ctxt->value->type != XPATH_STRING) {
        ctxt->error = XPATH_INVALID_TYPE;
        if (obj2 != NULL)
            xmlXPathFreeObject(obj2);

        /* reset the error old error handler before leaving
         */

        return;
    }
    obj = valuePop(ctxt);
    if (obj->stringval == NULL) {
        valuePush(ctxt, xmlXPathNewNodeSet(NULL));
    } else {
        if ((obj2 != NULL) && (obj2->nodesetval != NULL) &&
            (obj2->nodesetval->nodeNr > 0)) {
            xmlNodePtr target;

            target = obj2->nodesetval->nodeTab[0];
            if (target->type == XML_ATTRIBUTE_NODE) {
                target = ((xmlAttrPtr) target)->parent;
            }
            base = xmlNodeGetBase(target->doc, target);
        } else {
            base = xmlNodeGetBase(ctxt->context->node->doc, ctxt->context->node);
        }
        URI = xmlBuildURI(obj->stringval, base);
        if (base != NULL)
            xmlFree(base);
        if (URI == NULL) {
            valuePush(ctxt, xmlXPathNewNodeSet(NULL));
        } else {
            if (xmlStrEqual(ctxt->context->node->doc->URL, URI)) {
                valuePush(ctxt, xmlXPathNewNodeSet((xmlNodePtr)ctxt->context->node->doc));
            }
            else {
                xmlDocPtr doc;
                doc = xmlParseFile((const char *)URI);
                if (doc == NULL)
                    valuePush(ctxt, xmlXPathNewNodeSet(NULL));
                else {
                    /* TODO: use XPointer of HTML location for fragment ID */
                    /* pbm #xxx can lead to location sets, not nodesets :-) */
                    valuePush(ctxt, xmlXPathNewNodeSet((xmlNodePtr) doc));
                }
            }
            xmlFree(URI);
        }
    }
    xmlXPathFreeObject(obj);
    if (obj2 != NULL)
        xmlXPathFreeObject(obj2);

    /* reset the error old error handler before leaving
     */
}


/**
 * Most of the code is stolen from testXPath. 
 * The almost only thing I added, is the storeing of the data, so
 * we can access the data easily - or say more easiely than through
 * libxml2.
 **/

xmlXPathObjectPtr
domXPathFind( xmlNodePtr refNode, xmlChar * path, int to_bool ) {
    xmlXPathObjectPtr res = NULL;
    xmlXPathCompExprPtr comp;
    comp = xmlXPathCompile( path );
    if ( comp == NULL ) {
        return NULL;
    }
    res = domXPathCompFind(refNode,comp,to_bool);
    xmlXPathFreeCompExpr(comp);
    return res;
}

xmlXPathObjectPtr
domXPathCompFind( xmlNodePtr refNode, xmlXPathCompExprPtr comp, int to_bool ) {
    xmlXPathObjectPtr res = NULL;
  
    if ( refNode != NULL && comp != NULL ) {
        xmlXPathContextPtr ctxt;

        xmlDocPtr tdoc = NULL;
        xmlNodePtr froot = refNode;

        if ( comp == NULL ) {
            return NULL;
        }
        
        if ( refNode->doc == NULL ) {
            /* if one XPaths a node from a fragment, libxml2 will
               refuse the lookup. this is not very usefull for XML
               scripters. thus we need to create a temporary document
               to make libxml2 do it's job correctly.
             */
            tdoc = xmlNewDoc( NULL );

            /* find refnode's root node */
            while ( froot != NULL ) {
                if ( froot->parent == NULL ) {
                    break;
                }
                froot = froot->parent;
            }
            xmlAddChild((xmlNodePtr)tdoc, froot);
            xmlSetTreeDoc(froot, tdoc); /* probably no need to clean psvi */
	    froot->doc = tdoc;
            /* refNode->doc = tdoc; */
        }

        /* prepare the xpath context */
        ctxt = xmlXPathNewContext( refNode->doc );
        ctxt->node = refNode;
        /* get the namespace information */
        if (refNode->type == XML_DOCUMENT_NODE) {
            ctxt->namespaces = xmlGetNsList( refNode->doc,
                                             xmlDocGetRootElement( refNode->doc ) );
        }
        else {
            ctxt->namespaces = xmlGetNsList(refNode->doc, refNode);
        }
        ctxt->nsNr = 0;
        if (ctxt->namespaces != NULL) {
            while (ctxt->namespaces[ctxt->nsNr] != NULL)
            ctxt->nsNr++;
        }

        xmlXPathRegisterFunc(ctxt,
                             (const xmlChar *) "document",
                             perlDocumentFunction);
	if (to_bool) {
#if LIBXML_VERSION >= 20627
	  int val = xmlXPathCompiledEvalToBoolean(comp, ctxt);
	  res = xmlXPathNewBoolean(val);
#else
	  res = xmlXPathCompiledEval(comp, ctxt);
	  if (res!=NULL) {
	    int val = xmlXPathCastToBoolean(res);
            xmlXPathFreeObject(res);
	    res = xmlXPathNewBoolean(val);
	  }
#endif
	} else {
	  res = xmlXPathCompiledEval(comp, ctxt);
	}
        if (ctxt->namespaces != NULL) {
            xmlFree( ctxt->namespaces );
        }

        xmlXPathFreeContext(ctxt);

        if ( tdoc != NULL ) {
            /* after looking through a fragment, we need to drop the
               fake document again */
            xmlSetTreeDoc(froot, NULL); /* probably no need to clean psvi */
	    froot->doc = NULL;
	    froot->parent = NULL;
            tdoc->children = NULL;
            tdoc->last     = NULL;
            /* next line is not required anymore */
            /* refNode->doc = NULL; */

            xmlFreeDoc( tdoc );
        }
    }
    return res;
}

/* this function is not actually used: */
xmlNodeSetPtr
domXPathSelect( xmlNodePtr refNode, xmlChar * path ) {
    xmlNodeSetPtr rv = NULL;
    xmlXPathObjectPtr res = NULL;
  
    res = domXPathFind( refNode, path, 0 );
    
    if (res != NULL) {
            /* here we have to transfer the result from the internal
               structure to the return value */
        	/* get the result from the query */
        	/* we have to unbind the nodelist, so free object can 
        	   not kill it */
        rv = res->nodesetval;  
        res->nodesetval = 0 ;
    }

    xmlXPathFreeObject(res);

    return rv;
}

/* this function is not actually used: */
xmlNodeSetPtr
domXPathCompSelect( xmlNodePtr refNode, xmlXPathCompExprPtr comp ) {
    xmlNodeSetPtr rv = NULL;
    xmlXPathObjectPtr res = NULL;
  
    res = domXPathCompFind( refNode, comp, 0 );
    
    if (res != NULL) {
            /* here we have to transfer the result from the internal
               structure to the return value */
        	/* get the result from the query */
        	/* we have to unbind the nodelist, so free object can 
        	   not kill it */
        rv = res->nodesetval;  
        res->nodesetval = 0 ;
    }

    xmlXPathFreeObject(res);

    return rv;
}

/**
 * Most of the code is stolen from testXPath. 
 * The almost only thing I added, is the storeing of the data, so
 * we can access the data easily - or say more easiely than through
 * libxml2.
 **/

xmlXPathObjectPtr
domXPathFindCtxt( xmlXPathContextPtr ctxt, xmlChar * path, int to_bool ) {
    xmlXPathObjectPtr res = NULL;
    if ( ctxt->node != NULL && path != NULL ) {
        xmlXPathCompExprPtr comp;
        comp = xmlXPathCompile( path );
        if ( comp == NULL ) {
            return NULL;
        }
        res = domXPathCompFindCtxt(ctxt,comp,to_bool);
        xmlXPathFreeCompExpr(comp);
    }
    return res;
}

xmlXPathObjectPtr
domXPathCompFindCtxt( xmlXPathContextPtr ctxt, xmlXPathCompExprPtr comp, int to_bool ) {
    xmlXPathObjectPtr res = NULL;
    if ( comp != NULL && ctxt->node != NULL && comp != NULL ) {
        xmlDocPtr tdoc = NULL;
        xmlNodePtr froot = ctxt->node;

        if ( ctxt->node->doc == NULL ) {
            /* if one XPaths a node from a fragment, libxml2 will
               refuse the lookup. this is not very usefull for XML
               scripters. thus we need to create a temporary document
               to make libxml2 do it's job correctly.
             */

            tdoc = xmlNewDoc( NULL );

            /* find refnode's root node */
            while ( froot != NULL ) {
                if ( froot->parent == NULL ) {
                    break;
                }
                froot = froot->parent;
            }
            xmlAddChild((xmlNodePtr)tdoc, froot);
	    xmlSetTreeDoc(froot,tdoc);  /* probably no need to clean psvi */
            froot->doc = tdoc;
	    /* ctxt->node->doc = tdoc; */
        }
	if (to_bool) {
#if LIBXML_VERSION >= 20627
	  int val = xmlXPathCompiledEvalToBoolean(comp, ctxt);
	  res = xmlXPathNewBoolean(val);
#else
	  res = xmlXPathCompiledEval(comp, ctxt);
	  if (res!=NULL) {
	    int val = xmlXPathCastToBoolean(res);
            xmlXPathFreeObject(res);
	    res = xmlXPathNewBoolean(val);
	  }
#endif
	} else {
	  res = xmlXPathCompiledEval(comp, ctxt);
	}
        if ( tdoc != NULL ) {
            /* after looking through a fragment, we need to drop the
               fake document again */
	    xmlSetTreeDoc(froot,NULL); /* probably no need to clean psvi */
            froot->doc = NULL;
            froot->parent  = NULL;
            tdoc->children = NULL;
            tdoc->last     = NULL;
	    if (ctxt->node) {
	      ctxt->node->doc = NULL;
	    }
            xmlFreeDoc( tdoc );
        }
    }
    return res;
}

xmlNodeSetPtr
domXPathSelectCtxt( xmlXPathContextPtr ctxt, xmlChar * path ) {
    xmlNodeSetPtr rv = NULL;
    xmlXPathObjectPtr res = NULL;
  
    res = domXPathFindCtxt( ctxt, path, 0 );
    
    if (res != NULL) {
            /* here we have to transfer the result from the internal
               structure to the return value */
        	/* get the result from the query */
        	/* we have to unbind the nodelist, so free object can 
        	   not kill it */
        rv = res->nodesetval;  
        res->nodesetval = 0 ;
    }

    xmlXPathFreeObject(res);

    return rv;
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.