summaryrefslogtreecommitdiffstats
path: root/axiom/include/axiom_xpath.h
blob: d2b0ee237137d32a9dc58e0a88b3990404928624 (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
/*
 * 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.
 */

#ifndef AXIOM_XPATH_H
#define AXIOM_XPATH_H

#include <axiom.h>
#include <axutil_env.h>
#include <axutil_stack.h>
#include <axiom_soap.h>

#ifdef __cplusplus
extern "C"
{
#endif

    /**
     * @defgroup axiom_xpath_api api
     * @ingroup axiom_xpath
     * @{
     */


    /**
      * Enable tracing
      */

#define AXIOM_XPATH_DEBUG

    /**
      * An error occured while evaluating the xpath expression
      */
#define AXIOM_XPATH_EVALUATION_ERROR 0

#define AXIOM_XPATH_ERROR_STREAMING_NOT_SUPPORTED 10

    /* Typedefs */

    /**
      * XPath expression
      * It includes the expression as a string and parsed data.
      */
    typedef struct axiom_xpath_expression axiom_xpath_expression_t;

    /**
      * The XPath context
      * Keeps a reference to the context node or attribute,
      * XPath expression, environment and result set.
      */
    typedef struct axiom_xpath_context axiom_xpath_context_t;

    /**
      * XPath result set
      * Contains the result set and other information such as
      * whether the expression was evaluated successfully.
      */
    typedef struct axiom_xpath_result axiom_xpath_result_t;

    /**
      * XPath result
      * Stores type and value of the result.
      */
    typedef struct axiom_xpath_result_node axiom_xpath_result_node_t;

    /**
      * XPath result types
      */
    typedef enum axiom_xpath_result_type_t
    {
        AXIOM_XPATH_TYPE_NODE = 0,
        AXIOM_XPATH_TYPE_ATTRIBUTE,
        AXIOM_XPATH_TYPE_NAMESPACE,
        AXIOM_XPATH_TYPE_TEXT,
        AXIOM_XPATH_TYPE_NUMBER,
        AXIOM_XPATH_TYPE_BOOLEAN
    } axiom_xpath_result_type_t;

    typedef int (*axiom_xpath_function_t)(axiom_xpath_context_t *context,
            int np);

    /**
      * XPath expression
      */
    struct axiom_xpath_expression
    {
        /** XPath expression as a string */
        axis2_char_t* expr_str;

        /** Length of the expression */
        int expr_len;

        /** A cursor pointing to the position currently being parsed */
        int expr_ptr;

        /** Parsed expression in an array list*/
        axutil_array_list_t *operations;

        /** A pointer to the start operation in operations */
        int start;
    };

    /**
      * XPath context
      */
    struct axiom_xpath_context
    {
        /** Environment */
        const axutil_env_t *env;

        /** List of namespaces */
        axutil_hash_t *namespaces;

        /** Set of functions */
        axutil_hash_t *functions;

        /** Root node */
        axiom_node_t *root_node;

        /** Context node */
        axiom_node_t *node;

        /** Context attribute */
        axiom_attribute_t *attribute;

        /** Context attribute */
        axiom_namespace_t *ns;

        /** Context position */
        int position;

        /** Context size
          * *Does not work location paths due to optimizations */
        int size;

        /** XPath expression */
        axiom_xpath_expression_t *expr;

        /** Streaming */
        axis2_bool_t streaming;

        /** Stack of processed items */
        axutil_stack_t *stack;

        /* TODO:
           functions
           variables
           etc */
    };

    /**
      * XPath result set
      */
    struct axiom_xpath_result
    {
        /** A flag indicating whether errors occured while evaluting XPath
          * expression */
        int flag;

        /** An array list containing the set of results */
        axutil_array_list_t * nodes;
    };

    /**
      * XPath result
      */
    struct axiom_xpath_result_node
    {
        /** Type of result */
        axiom_xpath_result_type_t type;

        /** Value */
        void * value;
    };

    /**
      * Compile an XPath expression
      *
      * @param env Environment must not be null
      * @param xpath_expr A pointer to the XPath expression
      * @return The parsed XPath expression. Returns NULL if an error occured
      *         while parsing.
      */
    AXIS2_EXTERN axiom_xpath_expression_t * AXIS2_CALL axiom_xpath_compile_expression(
        const axutil_env_t *env,
        const axis2_char_t* xpath_expr);

    /**
      * Create an empty XPath context
      *
      * @param env Environment must not be null
      * @param root_node A pointer to the root of the tree
      * @return The initialized XPath context.
      */
    AXIS2_EXTERN axiom_xpath_context_t * AXIS2_CALL axiom_xpath_context_create(
        const axutil_env_t *env,
        axiom_node_t * root_node);

    /**
      * Evaluate an parsed XPath expression. Different expressions could
      * be evaluated on the same context, and same expression could be
      * evaluated on multiple trees without recompiling.
      *
      * @param context XPath context must not be null
      * @param xpath_expr XPath expression to be evaluated
      * @return The set of results.
      */
    AXIS2_EXTERN axiom_xpath_result_t * AXIS2_CALL axiom_xpath_evaluate(
        axiom_xpath_context_t *context,
        axiom_xpath_expression_t *xpath_expr);


    /**
      * Convert an XPath result to a boolean.
      * If the result is a boolean the value of it is returned.
      * If the result is a number, AXIS2_TRUE is
      * returned if the value is not equal to 0 and AXIS2_FALSE otherwise.
      * Otherwise AXIS2_TRUE is returned if the result is not NULL and AXIS2_FALSE otherwise.
      *
      * @param env Environment must not be null
      * @param node A pointer to the XPath result
      * @return The boolean value.
      */
    AXIS2_EXTERN axis2_bool_t AXIS2_CALL axiom_xpath_cast_node_to_boolean(
        const axutil_env_t *env,
        axiom_xpath_result_node_t * node);

    /**
      * Convert an XPath result to a number.
      * If the result is a boolean, 1 is returned if it's true and 0 otherwise.
      * If the result is a number the value of it is returned.
      * Otherwise AXIS2_TRUE is returned if the result is not NULL and AXIS2_FALSE otherwise.
      *
      * @param env Environment must not be null
      * @param node A pointer to the XPath result
      * @return The numerical value.
      */
    AXIS2_EXTERN double AXIS2_CALL axiom_xpath_cast_node_to_number(
        const axutil_env_t *env,
        axiom_xpath_result_node_t * node);

    /**
      * Convert an XPath result to text.
      * If the result is a boolean, "true" is returned if it's true and "false" otherwise.
      * If the result is a number the text representation of it is returned.
      * If the result is a text the value of it is returned.
      * If the result is an axiom node, the text value of it is returned
      * If the result is an axiom attribue, the text value of it is returned
      *
      * @param env Environment must not be null
      * @param node A pointer to the XPath result
      * @return The text value.
      */
    AXIS2_EXTERN axis2_char_t * AXIS2_CALL axiom_xpath_cast_node_to_string(
        const axutil_env_t *env,
        axiom_xpath_result_node_t * node);

    /**
      * Convert an XPath result to an axiom node.
      * If the result is an axiom node it is returned and NULL otherwise.
      *
      * @param env Environment must not be null
      * @param node A pointer to the XPath result
      * @return The axiom node.
      */
    AXIS2_EXTERN axiom_node_t * AXIS2_CALL axiom_xpath_cast_node_to_axiom_node(
        const axutil_env_t *env,
        axiom_xpath_result_node_t * node);


    /**
      * Free XPath context
      *
      * @param env Environment must not be null
      * @param context XPath context must not be null
      */
    AXIS2_EXTERN void AXIS2_CALL axiom_xpath_free_context(
        const axutil_env_t *env,
        axiom_xpath_context_t *context);

    /**
      * Free XPath expression
      *
      * @param env Environment must not be null
      * @param xpath_expr XPath expression must not be null
      */
    AXIS2_EXTERN void AXIS2_CALL axiom_xpath_free_expression(
        const axutil_env_t *env,
        axiom_xpath_expression_t * xpath_expr);

    /**
      * Free XPath result set
      *
      * @param env Environment must not be null
      * @param result XPath result set must not be null
      */
    AXIS2_EXTERN void AXIS2_CALL axiom_xpath_free_result(
        const axutil_env_t *env,
        axiom_xpath_result_t* result);

    /**
      * Registers a XPath namespace
      *
      * @param context XPath Context, must not be null
      * @param ns AXIOM namespace, must not be null
      */
    AXIS2_EXTERN void AXIS2_CALL axiom_xpath_register_namespace(
        axiom_xpath_context_t *context,
        axiom_namespace_t *ns);

    /**
      * Get a registered namespace by the prefix.
      * If there is no namespace registered by the given prefix NULL will be returned
      *
      * @param context XPath Context, must not be null
      * @param prefix Prefix of the namespace, must not be null
      * @return The namespace corresponding to the prefix.
      */
    AXIS2_EXTERN axiom_namespace_t * AXIS2_CALL axiom_xpath_get_namespace(
        axiom_xpath_context_t *context,
        axis2_char_t *prefix);

    /**
      * Clears all registered XPath namespaces
      *
      * @param context XPath Context, must not be null
      */
    AXIS2_EXTERN void AXIS2_CALL axiom_xpath_clear_namespaces(
        axiom_xpath_context_t *context);

    /**
      * Evaluates an XPath expression on streaming XML.
      * Not all expressions can be evaluated on streaming XML.
      * If the expression cannot be evaluated on streaming XML NULL will be returned.
      *
      * @param context XPath Context, must not be null
      * @param xpath_expr XPath expression to be evaluated
      */
    AXIS2_EXTERN axiom_xpath_result_t * AXIS2_CALL axiom_xpath_evaluate_streaming(
        axiom_xpath_context_t *context,
        axiom_xpath_expression_t *xpath_expr);

    /**
      * Checks whether the given expression can be evaluated on streaming XML.
      * If it is possible AXIS2_TRUE will be retuned; AXIS2_FALSE otherwise.
      *
      * @param env Axis2 environment, must not be null
      * @param expr Complied XPath expression, must not be null
      * @return A boolean indicating whether 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);

    /**
      * Setup the XPath core function library
      *
      * @param context XPath Context, must not be null
      */
    AXIS2_EXTERN void AXIS2_CALL axiom_xpath_register_default_functions_set(
        axiom_xpath_context_t *context);

    /**
      * Registers a custom XPath function http://www.w3.org/TR/xpath#corelib
      *
      * @param context XPath Context, must not be null
      * @param name Name of the function, must not be null
      * @param func Pointer to the function, must not be null
      */
    AXIS2_EXTERN void AXIS2_CALL axiom_xpath_register_function(
        axiom_xpath_context_t *context,
        axis2_char_t *name,
        axiom_xpath_function_t func);

    /**
      * Retrive a pointer to a registered funciton by the function name.
      * If there is no function registered by the given name, NULL will be returned.
      *
      * @param context XPath Context, must not be null
      * @param name Name of the function, must not be null
      * @return The corresponding function.
      */
    AXIS2_EXTERN axiom_xpath_function_t AXIS2_CALL axiom_xpath_get_function(
        axiom_xpath_context_t *context,
        axis2_char_t *name);

    /** @} */

#ifdef __cplusplus
}
#endif

#endif