1 /*
2  * Copyright (C) 2000-2003 Chris Ross and various contributors
3  * Copyright (C) 1999-2000 Chris Ross
4  * Copyright (C) 2004 Christian M. Stamgren
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies of the Software, its documentation and marketing & publicity
15  * materials, and acknowledgment shall be given in the documentation, materials
16  * and software packages that this Software was used.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
22  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25 #include "tree_handlers.h"
26 #include "xpath_handlers.h"
27 
ParseXPath(FeriteScript * script,XMLDoc * tree,const char * str)28 FeriteVariable *ParseXPath( FeriteScript *script, XMLDoc *tree, const char *str )
29 {
30     xmlXPathObjectPtr res;
31     xmlNodeSetPtr cur;
32     xmlXPathCompExprPtr comp;
33     xmlChar *string;
34     FeriteVariable *array,*element;
35     register int i;
36 
37     xmlXPathContextPtr ctxt = xmlXPathNewContext( tree->doc );
38     ctxt->node = tree->node;
39 
40     array = ferite_create_uarray_variable( script, "xpath_result", FE_ARRAY_DEFAULT_SIZE, FE_STATIC );
41     comp = xmlXPathCompile(BAD_CAST str);
42 
43     if (comp != NULL)
44     {
45         res = xmlXPathCompiledEval(comp, ctxt);
46         xmlXPathFreeCompExpr(comp);
47 
48         switch( res->type )
49         {
50 
51           case XPATH_UNDEFINED:
52             ferite_error(NULL, 0, "Object is uninitialized\n");
53             break;
54 
55           case XPATH_NODESET:
56 
57             cur = res->nodesetval;
58             for (i = 0 ; i < cur->nodeNr ; i++)
59             {
60                 element = create_element_node( script, tree->doc, cur->nodeTab[i] );
61                 ferite_uarray_add( script, VAUA( array ), element, NULL, FE_ARRAY_ADD_AT_END );
62             }
63             break;
64 
65           case XPATH_NUMBER:
66 
67             if (xmlXPathIsNaN(res->floatval))
68               element = fe_new_str_static("xpath_result", "NaN", 3, FE_CHARSET_DEFAULT);
69             else
70               element = fe_new_dbl_static("xpath_result", res->floatval);
71 
72             ferite_uarray_add( script, VAUA( array ), element, NULL, FE_ARRAY_ADD_AT_END );
73             break;
74 
75           case XPATH_STRING:
76             element = fe_new_str_static("xpath_result", res->stringval, 0, FE_CHARSET_DEFAULT);
77             ferite_uarray_add( script, VAUA( array ), element, NULL, FE_ARRAY_ADD_AT_END );
78             break;
79 
80           case XPATH_BOOLEAN:
81 
82             if (res->boolval)
83               element = fe_new_str_static("xpath_result", "true", 0, FE_CHARSET_DEFAULT);
84             else
85               element = fe_new_str_static("xpath_result", "false", 0, FE_CHARSET_DEFAULT);
86 
87             ferite_uarray_add( script, VAUA( array ), element, NULL, FE_ARRAY_ADD_AT_END );
88             break;
89 
90           default:
91             ferite_error( script, 0, "Unimplemeted result type");
92             break;
93         }
94     }
95     xmlXPathFreeObject(res);
96     xmlXPathFreeContext(ctxt);
97 
98     return array;
99 }
100