summaryrefslogtreecommitdiffstats
path: root/axiom/src/xpath/xpath.c
blob: 38f422c700af8fdda42dc86f5fd3d095ce0e5d56 (plain)
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <axiom_xpath.h>
#include "xpath_internals.h"
#include "xpath_internals_parser.h"
#include "xpath_internals_engine.h"
#include "xpath_functions.h"
#include "xpath_streaming.h"

/* Create XPath context */
AXIS2_EXTERN axiom_xpath_context_t * AXIS2_CALL
axiom_xpath_context_create(
    const axutil_env_t *env,
    axiom_node_t * root_node)
{
    axiom_xpath_context_t* context;

    /*HACK: xpath impl requires a dummy root node in order to process properly.*/
    axiom_node_t * dummy_root;
    dummy_root = axiom_node_create(env);
    axiom_node_add_child(dummy_root, env, root_node);

    context = AXIS2_MALLOC(env->allocator,
        sizeof(axiom_xpath_context_t));

    context->env = env;
    context->root_node = dummy_root;
    context->node = dummy_root;
    context->expr = NULL;
    context->attribute = NULL;
    context->namespaces = NULL;
    context->functions = NULL;

    axiom_xpath_register_default_functions_set(context);

    return context;
}

/* Compile XPath expression */
AXIS2_EXTERN axiom_xpath_expression_t * AXIS2_CALL
axiom_xpath_compile_expression(
    const axutil_env_t *env,
    const axis2_char_t* xpath_expr)
{
    axiom_xpath_expression_t* expr;

    expr = AXIS2_MALLOC(env->allocator,
        sizeof(axiom_xpath_expression_t));

    expr->expr_str = axutil_strdup(env, xpath_expr);

    if (axiom_xpath_compile(env, expr) == AXIOM_XPATH_PARSE_ERROR)
    {
        AXIS2_FREE(env->allocator, expr->expr_str);
        AXIS2_FREE(env->allocator, expr);

        return NULL;
    }
    else
    {
        return expr;
    }
}

/* Evaluate compiled XPath expression */
AXIS2_EXTERN axiom_xpath_result_t * AXIS2_CALL
axiom_xpath_evaluate(
    axiom_xpath_context_t *context,
    axiom_xpath_expression_t *xpath_expr)
{
    axiom_xpath_expression_copy(context, xpath_expr);

    context->streaming = AXIS2_FALSE;

    return axiom_xpath_run(context);
}

AXIS2_EXTERN axiom_xpath_result_t * AXIS2_CALL
axiom_xpath_evaluate_streaming(
    axiom_xpath_context_t *context,
    axiom_xpath_expression_t *xpath_expr)
{
    axiom_xpath_result_t *res;

    axiom_xpath_expression_copy(context, xpath_expr);

    if (axiom_xpath_streaming_check(context->env, xpath_expr))
    {
        context->streaming = AXIS2_TRUE;
        return axiom_xpath_run(context);
    }
    else
    {
        res = AXIS2_MALLOC(
            context->env->allocator, sizeof(axiom_xpath_result_t));
        res->nodes = NULL;
        res->flag = AXIOM_XPATH_ERROR_STREAMING_NOT_SUPPORTED;

        return res;
    }
}

AXIS2_EXTERN void AXIS2_CALL
axiom_xpath_register_default_functions_set(
    axiom_xpath_context_t *context)
{
    axiom_xpath_register_function(
        context, "count", axiom_xpath_function_count);
}

AXIS2_EXTERN void AXIS2_CALL
axiom_xpath_register_function(
    axiom_xpath_context_t *context,
    axis2_char_t *name,
    axiom_xpath_function_t func)
{
    if (name && func)
    {
        if (!context->functions)
        {
            context->functions = axutil_hash_make(context->env);
        }

        axutil_hash_set(context->functions, name, AXIS2_HASH_KEY_STRING, func);
    }
}

AXIS2_EXTERN axiom_xpath_function_t AXIS2_CALL
axiom_xpath_get_function(
    axiom_xpath_context_t *context,
    axis2_char_t *name)
{
    axiom_xpath_function_t func = NULL;

    if(context->functions)
    {
        func = axutil_hash_get(context->functions, name, AXIS2_HASH_KEY_STRING);
    }

    return func;
}

AXIS2_EXTERN void AXIS2_CALL
axiom_xpath_register_namespace(
    axiom_xpath_context_t *context,
    axiom_namespace_t *ns)
{
    axis2_char_t *prefix = NULL;

    if (!context->namespaces)
    {
        context->namespaces = axutil_hash_make(context->env);
    }

    prefix = axiom_namespace_get_prefix(ns, context->env);

    if (prefix)
    {
        axutil_hash_set(
            context->namespaces, prefix, AXIS2_HASH_KEY_STRING, ns);
    }
}

AXIS2_EXTERN axiom_namespace_t * AXIS2_CALL
axiom_xpath_get_namespace(
    axiom_xpath_context_t *context,
    axis2_char_t *prefix)
{
    axiom_namespace_t *ns = NULL;

    if (context->namespaces)
    {
        ns = axutil_hash_get(context->namespaces, prefix, AXIS2_HASH_KEY_STRING);
    }

    return ns;
}

AXIS2_EXTERN void AXIS2_CALL
axiom_xpath_clear_namespaces(
    axiom_xpath_context_t *context)
{
    axutil_hash_index_t *hi;
    void *val;

    if (context->namespaces)
    {
        for (hi = axutil_hash_first(context->namespaces, context->env);
            hi;
            hi = axutil_hash_next(context->env, hi))
        {
            axutil_hash_this(hi, NULL, NULL, &val);
            axiom_namespace_free((axiom_namespace_t *)val, context->env);
        }

        axutil_hash_free(context->namespaces, context->env);
    }

    context->namespaces = NULL;
}

/* Cast to boolean */
AXIS2_EXTERN axis2_bool_t AXIS2_CALL
axiom_xpath_cast_node_to_boolean(
    const axutil_env_t *env,
    axiom_xpath_result_node_t * node)
{
    if(node->type == AXIOM_XPATH_TYPE_BOOLEAN)
    {
        return *(axis2_bool_t *)node->value;
    }
    else if(node->type == AXIOM_XPATH_TYPE_NUMBER)
    {
        /* Cannot evaluate as *(double *)(node->value) == 1e-12
         since there might be an precision error */
        if(*(double *)(node->value) > 1e-12 || *(double *)(node->value) < -1e-12)
        {
            return AXIS2_TRUE;
        }
        else
        {
            return AXIS2_FALSE;
        }
    }
    else if(node->value)
    {
        return AXIS2_TRUE;
    }
    else
    {
        return AXIS2_FALSE;
    }
}

/* Cast to double */
AXIS2_EXTERN double AXIS2_CALL
axiom_xpath_cast_node_to_number(
    const axutil_env_t *env,
    axiom_xpath_result_node_t * node)
{
    if (node->type == AXIOM_XPATH_TYPE_BOOLEAN)
    {
        if (*(axis2_bool_t *)(node->value) == AXIS2_TRUE)
        {
            return 1.0;
        }
        else
        {
            return 0.0;
        }
    }
    else if (node->type == AXIOM_XPATH_TYPE_NUMBER)
    {
        return *(double *)node->value;
    }
    else if (node->value)
    {
        return 1.0;
    }
    else
    {
        return 0.0;
    }
}

/* Cast to text */
AXIS2_EXTERN axis2_char_t * AXIS2_CALL
axiom_xpath_cast_node_to_string(
    const axutil_env_t *env,
    axiom_xpath_result_node_t * node)
{
    axiom_element_t *ele;
    axis2_char_t *res;

    if (!node->value)
    {
        return NULL;
    }

    if (node->type == AXIOM_XPATH_TYPE_BOOLEAN)
    {
        if (*(axis2_bool_t *)(node->value) == AXIS2_TRUE)
        {
            return axutil_strdup(env, "true");
        }
        else
        {
            return axutil_strdup(env, "false");
        }
    }
    else if (node->type == AXIOM_XPATH_TYPE_NUMBER)
    {
        /* Allocate 50 bytes */
        res = AXIS2_MALLOC(env->allocator, sizeof(axis2_char_t) * 50);

        sprintf(res, "%lf", *(double *)(node->value));

        return res;
    }
    else if (node->type == AXIOM_XPATH_TYPE_TEXT)
    {
        return (axis2_char_t *)node->value;
    }
    else if (node->type == AXIOM_XPATH_TYPE_NODE)
    {
        ele = (axiom_element_t *)axiom_node_get_data_element(
            (axiom_node_t *)(node->value), env);

        if (ele)
        {
            return axiom_element_get_text(
                ele, env, (axiom_node_t *)(node->value));
        }
        else
        {
            return NULL;
        }
    }
    else if (node->type == AXIOM_XPATH_TYPE_ATTRIBUTE)
    {
        return axiom_attribute_get_value(
            (axiom_attribute_t *)(node->value), env);
    }
    else if (node->type == AXIOM_XPATH_TYPE_NAMESPACE)
    {
        return axiom_namespace_get_prefix(
            (axiom_namespace_t *)(node->value), env);
    }

    return NULL;
}

/* Cast to axiom node */
AXIS2_EXTERN axiom_node_t * AXIS2_CALL
axiom_xpath_cast_node2axiom_node(
    const axutil_env_t *env,
    axiom_xpath_result_node_t * node)
{
    if (node->type == AXIOM_XPATH_TYPE_NODE && node->value)
    {
        return (axiom_node_t *)node->value;
    }
    else
    {
        return NULL;
    }
}

/* Free context */
AXIS2_EXTERN void AXIS2_CALL
axiom_xpath_free_context(
    const axutil_env_t *env,
    axiom_xpath_context_t *context)
{
    if (context)
    {
        /* Free the expression if not freed */
        if (context->expr)
        {
            /* axiom_xpath_free_expression(env, context->expr); */

            context->expr = NULL;
        }

        if (context->root_node)
        {
            axiom_node_detach(axiom_node_get_first_child(context->root_node, context->env), context->env);
            axiom_node_free_tree(context->root_node, context->env);
            context->root_node = NULL;
        }

        if (context->functions)
        {
            axutil_hash_free(context->functions, context->env);
            context->functions = NULL;
        }

        if(context->namespaces)
        {
            axiom_xpath_clear_namespaces(context);
            context->namespaces = NULL;
        }

        AXIS2_FREE(env->allocator, context);
    }
}

/* Free expression */
AXIS2_EXTERN void AXIS2_CALL
axiom_xpath_free_expression(
    const axutil_env_t *env,
    axiom_xpath_expression_t * xpath_expr)
{
    if (xpath_expr)
    {
        if (xpath_expr->expr_str)
        {
            AXIS2_FREE(env->allocator, xpath_expr->expr_str);

            xpath_expr->expr_str = NULL;
        }

        if (xpath_expr->operations)
        {
            axutil_array_list_free(xpath_expr->operations, env);
            xpath_expr->operations = NULL;
        }

        AXIS2_FREE(env->allocator, xpath_expr);
    }
}

/* Free result set */
AXIS2_EXTERN void AXIS2_CALL
axiom_xpath_free_result(
    const axutil_env_t *env,
    axiom_xpath_result_t* result)
{
    if (result)
    {
        if (result->nodes)
        {
            axutil_array_list_free(result->nodes, env);
        }

        AXIS2_FREE(env->allocator, result);
    }
}

/* Check if the expression can be evaluated on streaming XML */
AXIS2_EXTERN axis2_bool_t AXIS2_CALL
axiom_xpath_streaming_check(
    const axutil_env_t *env,
    axiom_xpath_expression_t* expr)
{
    axiom_xpath_streaming_t r = AXIOM_XPATH_CHECK(expr->start);

    if(r == AXIOM_XPATH_STREAMING_NOT_SUPPORTED)
    {
        return AXIS2_FALSE;
    }
    else
    {
        return AXIS2_TRUE;
    }
}