3 for (var i = 0; i < n; i++)
8 var CQLModifier = function () {
14 CQLModifier.prototype = {
15 toString: function () {
16 return this.name + this.relation + this.value;
19 toXCQL: function (n) {
20 var s = indent(n+1) + "<modifier>\n";
21 s = s + indent(n+2) + "<name>" + this.name + "</name>\n";
22 if (this.relation != null)
24 + "<relation>" + this.relation + "</relation>\n";
25 if (this.value != null)
27 + "<value>" + this.value +"</value>\n";
28 s = s + indent(n+1) + "</modifier>\n";
33 //we ignore modifier relation symbol, for value-less modifiers
35 var value = this.value.length > 0 ? this.value : "true";
36 var s = '"'+this.name+'": "'+value+'"';
42 var CQLSearchClause = function (field, fielduri, relation, relationuri,
45 this.fielduri = fielduri;
46 this.relation = relation;
47 this.relationuri = relationuri;
48 this.modifiers = modifiers;
52 CQLSearchClause.prototype = {
53 toString: function () {
54 return (this.field ? this.field + ' ' : '') +
55 (this.relation ? this.relation : '') +
56 (this.modifiers.length > 0 ? '/' + this.modifiers.join('/') : '') +
57 (this.relation || this.modifiers.length ? ' ' : '') +
58 '"' + this.term + '"';
61 toXCQL: function (n) {
62 var s = indent(n) + "<searchClause>\n";
63 if (this.fielduri.length > 0)
65 s = s + indent(n+1) + "<prefixes>\n" +
66 indent(n+2) + "<prefix>\n" +
67 indent(n+3) + "<identifier>" + this.fielduri +
69 indent(n+2) + "</prefix>\n" +
70 indent(n+1) + "</prefixes>\n";
72 s = s + indent(n+1) + "<index>" + this.field + "</index>\n";
73 s = s + indent(n+1) + "<relation>\n";
74 if (this.relationuri.length > 0) {
76 "<identifier>" + this.relationuri + "</identifier>\n";
78 s = s + indent(n+2) + "<value>" + this.relation + "</value>\n";
79 if (this.modifiers.length > 0) {
80 s = s + indent(n+2) + "<modifiers>\n";
81 for (var i = 0; i < this.modifiers.length; i++)
82 s = s + this.modifiers[i].toXCQL(n+2);
83 s = s + indent(n+2) + "</modifiers>\n";
85 s = s + indent(n+1) + "</relation>\n";
86 s = s + indent(n+1) + "<term>" + this.term + "</term>\n";
87 s = s + indent(n) + "</searchClause>\n";
92 var s = '{ "term": "'+this.term+'"';
93 if (this.field.length > 0 && this.field != 'cql.serverChoice')
94 s+= ', "field": "'+this.field+'"';
95 if (this.relation.length > 0 && this.relation != 'scr')
96 s+= ', "relation": "'+this._mapRelation(this.relation)+'"';
97 for (var i = 0; i < this.modifiers.length; i++) {
98 //since modifiers are mapped to keys, ignore the reserved ones
99 if (this.modifiers[i].name == "term"
100 ||this.modifiers[i].name == "field"
101 ||this.modifiers[i].name == "relation")
103 s += ', ' + this.modifiers[i].toFQ();
109 _mapRelation: function (rel) {
111 case "<" : return "lt";
112 case ">" : return "gt";
113 case "=" : return "eq";
114 case "<>" : return "ne";
115 case ">=" : return "ge";
116 case "<=" : return "le";
121 _remapRelation: function (rel) {
123 case "lt" : return "<";
124 case "gt" : return ">";
125 case "eq" : return "=";
126 case "ne" : return "<>";
127 case "ge" : return ">=";
128 case "le" : return "<=";
135 var CQLBoolean = function() {
137 this.modifiers = null;
142 CQLBoolean.prototype = {
143 toString: function () {
144 return (this.left.op ? '(' + this.left + ')' : this.left) + ' ' +
145 this.op.toUpperCase() +
146 (this.modifiers.length > 0 ? '/' + this.modifiers.join('/') : '') +
147 ' ' + (this.right.op ? '(' + this.right + ')' : this.right);;
149 toXCQL: function (n) {
150 var s = indent(n) + "<triple>\n";
151 s = s + indent(n+1) + "<boolean>\n" +
152 indent(n+2) + "<value>" + this.op + "</value>\n";
153 if (this.modifiers.length > 0) {
154 s = s + indent(n+2) + "<modifiers>\n";
155 for (var i = 0; i < this.modifiers.length; i++)
156 s = s + this.modifiers[i].toXCQL(n+2);
157 s = s + indent(n+2) + "</modifiers>\n";
159 s = s + indent(n+1) + "</boolean>\n";
160 s = s + indent(n+1) + "<leftOperand>\n" +
161 this.left.toXCQL(n+2) + indent(n+1) + "</leftOperand>\n";
163 s = s + indent(n+1) + "<rightOperand>\n" +
164 this.right.toXCQL(n+2) + indent(n+1) + "</rightOperand>\n";
165 s = s + indent(n) + "</triple>\n";
170 var s = ' { "op": "'+this.op+'"';
171 //proximity modifiers
172 for (var i = 0; i < this.modifiers.length; i++)
173 s += ', ' + this.modifiers[i].toFQ();
174 s += ', "s1": '+this.left.toFQ();
175 s += ', "s2": '+this.right.toFQ();
182 var CQLParser = function () {
189 this.prefixes = new Object();
193 CQLParser.prototype = {
194 parse: function (query) {
196 throw new Error("The query to be parsed cannot be empty");
199 this.ql = this.qs.length;
202 this.tree = this._parseQuery("cql.serverChoice", "scr", new Array());
204 throw new Error("EOF expected");
206 parseFromFQ: function (query) {
208 throw new Error("The query to be parsed cannot be empty");
209 if (typeof query == 'string')
210 query = JSON.parse(query);
211 this.tree = this._parseFromFQ(query);
213 _parseFromFQ: function (fq) {
215 if (fq.hasOwnProperty('op')
216 && fq.hasOwnProperty('s1')
217 && fq.hasOwnProperty('s2')) {
218 var node = new CQLBoolean();
220 node.left = this._parseFromFQ(fq.s1);
221 node.right = this._parseFromFQ(fq.s2);
222 //include all other members as modifiers
224 for (var key in fq) {
225 if (key == 'op' || key == 's1' || key == 's2')
227 var mod = new CQLModifier();
231 node.modifiers.push(mod);
236 if (fq.hasOwnProperty('term')) {
237 var node = new CQLSearchClause();
239 node.field = fq.hasOwnProperty('field')
240 ? fq.field : 'cql.serverChoice';
241 node.relation = fq.hasOwnProperty('relation')
242 ? node._remapRelation(fq.relation) : 'scr';
243 //include all other members as modifiers
244 node.relationuri = '';
247 for (var key in fq) {
248 if (key == 'term' || key == 'field' || key == 'relation')
250 var mod = new CQLModifier();
254 node.modifiers.push(mod);
258 throw new Error('Unknow node type; '+JSON.stringify(fq));
260 toXCQL: function () {
261 return this.tree.toXCQL();
264 return this.tree.toFQ();
266 toString: function () {
267 return this.tree.toString();
269 _parseQuery: function(field, relation, modifiers) {
270 var left = this._parseSearchClause(field, relation, modifiers);
271 while (this.look == "s" && (
272 this.lval == "and" ||
274 this.lval == "not" ||
275 this.lval == "prox")) {
276 var b = new CQLBoolean();
279 b.modifiers = this._parseModifiers();
281 b.right = this._parseSearchClause(field, relation, modifiers);
286 _parseModifiers: function() {
287 var ar = new Array();
288 while (this.look == "/") {
290 if (this.look != "s" && this.look != "q")
291 throw new Error("Invalid modifier.")
293 var name = this.lval;
295 if (this.look.length > 0
296 && this._strchr("<>=", this.look.charAt(0))) {
299 if (this.look != "s" && this.look != "q")
300 throw new Error("Invalid relation within the modifier.");
302 var m = new CQLModifier();
309 var m = new CQLModifier();
318 _parseSearchClause: function(field, relation, modifiers) {
319 if (this.look == "(") {
321 var b = this._parseQuery(field, relation, modifiers);
322 if (this.look == ")")
325 throw new Error("Missing closing parenthesis.");
328 } else if (this.look == "s" || this.look == "q") {
329 var first = this.val; // dont know if field or term yet
331 if (this.look == "q" ||
333 this.lval != "and" &&
335 this.lval != "not" &&
336 this.lval != "prox")) {
337 var rel = this.val; // string relation
339 return this._parseSearchClause(first, rel,
340 this._parseModifiers());
341 } else if (this.look.length > 0
342 && this._strchr("<>=", this.look.charAt(0))) {
343 var rel = this.look; // other relation <, = ,etc
345 return this._parseSearchClause(first, rel,
346 this._parseModifiers());
348 // it's a search term
349 var pos = field.indexOf('.');
352 pre = field.substring(0, pos);
354 var uri = this._lookupPrefix(pre);
356 field = field.substring(pos+1);
358 pos = relation.indexOf('.');
362 pre = relation.substring(0, pos);
364 var reluri = this._lookupPrefix(pre);
365 if (reluri.Length > 0)
366 relation = relation.Substring(pos+1);
368 var sc = new CQLSearchClause(field,
377 } else if (this.look == ">") {
379 if (this.look != "s" && this.look != "q")
380 throw new Error("Expecting string or a quoted expression.");
382 var first = this.lval;
384 if (this.look == "=")
387 if (this.look != "s" && this.look != "q")
388 throw new Error("Expecting string or a quoted expression.");
390 this._addPrefix(first, this.lval);
392 return this._parseQuery(field, relation, modifiers);
394 this._addPrefix("default", first);
395 return this._parseQuery(field, relation, modifiers);
398 throw new Error("Invalid search clause.");
403 while (this.qi < this.ql
404 && this._strchr(" \t\r\n", this.qs.charAt(this.qi)))
406 if (this.qi == this.ql) {
410 var c = this.qs.charAt(this.qi);
411 if (this._strchr("()/", c)) {
414 } else if (this._strchr("<>=", c)) {
417 while (this.qi < this.ql
418 && this._strchr("<>=", this.qs.charAt(this.qi))) {
419 this.look = this.look + this.qs.charAt(this.qi);
422 } else if (this._strchr("\"'", c)) {
427 while (this.qi < this.ql
428 && this.qs.charAt(this.qi) != mark) {
429 if (this.qs.charAt(this.qi) == '\\'
430 && this.qi < this.ql-1)
432 this.val = this.val + this.qs.charAt(this.qi);
435 this.lval = this.val.toLowerCase();
436 if (this.qi < this.ql)
441 while (this.qi < this.ql
442 && !this._strchr("()/<>= \t\r\n", this.qs.charAt(this.qi))) {
443 this.val = this.val + this.qs.charAt(this.qi);
446 this.lval = this.val.toLowerCase();
449 _strchr: function (s, ch) {
450 return s.indexOf(ch) >= 0
452 _lookupPrefix: function(name) {
453 return this.prefixes[name] ? this.prefixes[name] : "";
455 _addPrefix: function(name, value) {
456 //overwrite existing items
457 this.prefixes[name] = value;