tinyows 1.2.2
fe_logical_ops.c
Go to the documentation of this file.
1/*
2 Copyright (c) <2007-2012> <Barbara Philippot - Olivier Courtin>
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20 IN THE SOFTWARE.
21*/
22
23
24#include <stdbool.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <assert.h>
29
30#include "../ows/ows.h"
31
32
33/*
34 * Check if the string is a logical operator
35 */
36bool fe_is_logical_op(char *name)
37{
38 assert(name);
39
40 /* NOTA: GML is case sensitive */
41 if (!strcmp(name, "And") || !strcmp(name, "Or") || !strcmp(name, "Not")) return true;
42
43 return false;
44}
45
46
47/*
48 * If the logical operator is binary, go through the nodes recursively
49 * and fill the SQL request buffer
50 */
51static buffer *fe_binary_logical_op(ows * o, buffer * typename, filter_encoding * fe, xmlNodePtr n)
52{
53 xmlNodePtr node;
54 assert(o);
55 assert(typename);
56 assert(fe);
57 assert(n);
58
59 buffer_add_str(fe->sql, "(");
60
61 node = n->children;
62 while (node->type != XML_ELEMENT_NODE) node = node->next; /* Jump to next element if spaces */
63
64 /* Execute the matching function's type */
65 if (fe_is_logical_op((char *) node->name)) fe->sql = fe_logical_op(o, typename, fe, node);
66 else if (fe_is_spatial_op((char *) node->name)) fe->sql = fe_spatial_op(o, typename, fe, node);
67 else if (fe_is_comparison_op((char *) node->name)) fe->sql = fe_comparison_op(o, typename, fe, node);
68
69 /* We could have severals terms in a logical */
70 for ( node = node->next ; node ; node = node->next ) {
71 if (node->type != XML_ELEMENT_NODE) continue;
72
73 /* Revert boolean logical if inside a Not */
74 if (!fe->in_not) {
75 if (!strcmp((char *) n->name, "And")) buffer_add_str(fe->sql, " AND ");
76 else if (!strcmp((char *) n->name, "Or")) buffer_add_str(fe->sql, " OR ");
77 } else {
78 if (!strcmp((char *) n->name, "And")) buffer_add_str(fe->sql, " OR ");
79 else if (!strcmp((char *) n->name, "Or")) buffer_add_str(fe->sql, " AND ");
80 }
81
82 /* Execute the matching function's type */
83 if (fe_is_logical_op((char *) node->name)) fe->sql = fe_logical_op(o, typename, fe, node);
84 else if (fe_is_spatial_op((char *) node->name)) fe->sql = fe_spatial_op(o, typename, fe, node);
85 else if (fe_is_comparison_op((char *) node->name)) fe->sql = fe_comparison_op(o, typename, fe, node);
86 }
87 buffer_add_str(fe->sql, ")");
88
89 return fe->sql;
90}
91
92
93/*
94 * If the logical operator is unary, go through the nodes recursively
95 * and fill the SQL request buffer
96 */
97static buffer *fe_unary_logical_op(ows * o, buffer * typename, filter_encoding * fe, xmlNodePtr n)
98{
99 assert(typename);
100 assert(o);
101 assert(fe);
102 assert(n);
103
104 buffer_add_str(fe->sql, "not(");
105 fe->in_not = true;
106
107 n = n->children;
108 while (n->type != XML_ELEMENT_NODE) n = n->next;
109
110 /* Execute the matching function's type */
111 if (fe_is_logical_op((char *) n->name)) fe->sql = fe_logical_op(o, typename, fe, n);
112 else if (fe_is_spatial_op((char *) n->name)) fe->sql = fe_spatial_op(o, typename, fe, n);
113 else if (fe_is_comparison_op((char *) n->name)) fe->sql = fe_comparison_op(o, typename, fe, n);
114
115 buffer_add_str(fe->sql, ")");
116 fe->in_not = false;
117
118 return fe->sql;
119}
120
121
122/*
123 * Execute the matching function (And, Or or Not)
124 * Warning : before calling this function,
125 * Check if the node name is a logical operator with is_logical_op()
126 */
127buffer *fe_logical_op(ows * o, buffer * typename, filter_encoding * fe, xmlNodePtr n)
128{
129 assert(typename);
130 assert(o);
131 assert(fe);
132 assert(n);
133
134 /* case sensitive comparison because the gml standard specifies
135 strictly the name of the operator */
136 if (!strcmp((char *) n->name, "And") || !strcmp((char *) n->name, "Or"))
137 fe->sql = fe_binary_logical_op(o, typename, fe, n);
138 else if (!strcmp((char *) n->name, "Not"))
139 fe->sql = fe_unary_logical_op(o, typename, fe, n);
140 else
142
143 return fe->sql;
144}
145
146
147/*
148 * vim: expandtab sw=4 ts=4
149 */
buffer * fe_logical_op(ows *o, buffer *typename, filter_encoding *fe, xmlNodePtr n)
bool fe_is_logical_op(char *name)
static buffer * fe_unary_logical_op(ows *o, buffer *typename, filter_encoding *fe, xmlNodePtr n)
static buffer * fe_binary_logical_op(ows *o, buffer *typename, filter_encoding *fe, xmlNodePtr n)
bool fe_is_comparison_op(char *name)
void buffer_add_str(buffer *buf, const char *str)
Definition buffer.c:254
bool fe_is_spatial_op(char *name)
buffer * fe_comparison_op(ows *o, buffer *typename, filter_encoding *fe, xmlNodePtr n)
buffer * fe_spatial_op(ows *o, buffer *typename, filter_encoding *fe, xmlNodePtr n)
struct Buffer buffer
struct Ows ows
@ FE_ERROR_FILTER
Definition ows_struct.h:330
struct Filter_encoding filter_encoding
enum fe_error_code error_code
Definition ows_struct.h:346

Generated for tinyows by doxygen 1.13.2