Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame^] | 1 | var KorAP = KorAP || {}; |
| 2 | /* |
| 3 | 704: "Operation needs operand list" |
| 4 | 701: "JSON-LD group has no @type attribute" |
| 5 | |
| 6 | 802: "Match type is not supported by value type" |
| 7 | 804: "Unknown value type" |
| 8 | 805: "Value is invalid" |
| 9 | 806: "Value is not a valid date string" |
| 10 | 807: "Value is not a valid regular expression" |
| 11 | |
| 12 | // new: |
| 13 | 810: "Unknown document group operation" (like 711) |
| 14 | 811: "Document group expects operation" (like 703) |
| 15 | 812: "Operand not supported in document group" (like 744) |
| 16 | */ |
| 17 | |
| 18 | (function (KorAP) { |
| 19 | "use strict"; |
| 20 | |
| 21 | // Default log message |
| 22 | KorAP.log = KorAP.log || function (type, msg) { |
| 23 | console.log(type + ": " + msg); |
| 24 | }; |
| 25 | |
| 26 | KorAP._validStringMatchRE = new RegExp("^(?:eq|ne|contains)$"); |
| 27 | KorAP._validRegexMatchRE = new RegExp("^(?:eq|ne)$"); |
| 28 | KorAP._validDateMatchRE = new RegExp("^(?:since|until|eq)$"); |
| 29 | KorAP._validDateRE = new RegExp("^(?:\\d{4})(?:-\\d\\d(?:-\\d\\d)?)?$"); |
| 30 | KorAP._validGroupOpRE = new RegExp("^(?:and|or)$"); |
| 31 | |
| 32 | /* |
| 33 | KorAP.VirtualCollection = { |
| 34 | create : function () { |
| 35 | return Object.create(KorAP.VirtualCollection); |
| 36 | }, |
| 37 | fromJson : function (json) { |
| 38 | }, |
| 39 | render : function (element) { |
| 40 | // ... |
| 41 | } |
| 42 | }; |
| 43 | */ |
| 44 | // Make KorAP.DocElement inherit from KorAP.Doc |
| 45 | KorAP.DocElement = { |
| 46 | create : function (param) { |
| 47 | return Object.create(Korap.DocElement)._init(param); |
| 48 | }, |
| 49 | _init : function () { |
| 50 | KorAP.Doc.create(param) |
| 51 | } |
| 52 | }; |
| 53 | |
| 54 | KorAP.DocGroup = { |
| 55 | create : function (type) { |
| 56 | var docGroup = Object.create(KorAP.DocGroup); |
| 57 | docGroup.operation = type; |
| 58 | docGroup._operands = []; |
| 59 | docGroup.ldType = "docGroup"; |
| 60 | return docGroup; |
| 61 | }, |
| 62 | |
| 63 | // Deserialize from json |
| 64 | fromJson : function (json) { |
| 65 | return Object.create(KorAP.DocGroup)._init(json); |
| 66 | }, |
| 67 | |
| 68 | _init : function (json) { |
| 69 | |
| 70 | if (json === undefined) |
| 71 | return KorAP.DocGroup.create("and"); |
| 72 | |
| 73 | if (json["@type"] != "korap:docGroup") { |
| 74 | KorAP.log(701, "JSON-LD group has no @type attribute"); |
| 75 | return; |
| 76 | }; |
| 77 | |
| 78 | this.ldType = "docGroup"; |
| 79 | |
| 80 | if (json["operation"] === undefined || |
| 81 | typeof json["operation"] !== 'string') { |
| 82 | KorAP.log(811, "Document group expects operation"); |
| 83 | return; |
| 84 | }; |
| 85 | |
| 86 | var operation = json["operation"]; |
| 87 | this.operation = operation.replace(/^operation:/,''); |
| 88 | |
| 89 | this._operands = []; |
| 90 | |
| 91 | if (json["operands"] === undefined || |
| 92 | !(json["operands"] instanceof Array)) { |
| 93 | KorAP.log(704, "Operation needs operand list") |
| 94 | return; |
| 95 | }; |
| 96 | |
| 97 | var operands = json["operands"]; |
| 98 | |
| 99 | // Add all documents |
| 100 | for (var i in operands) { |
| 101 | var operand = operands[i]; |
| 102 | |
| 103 | switch (operand["@type"]) { |
| 104 | case undefined: |
| 105 | KorAP.log(701, "JSON-LD group has no @type attribute"); |
| 106 | return; |
| 107 | case "korap:doc": |
| 108 | var doc = KorAP.Doc.fromJson(operand); |
| 109 | if (doc === undefined) |
| 110 | return; |
| 111 | this.appendOperand(doc); |
| 112 | break; |
| 113 | case "korap:docGroup": |
| 114 | var docGroup = KorAP.DocGroup.fromJson(operand); |
| 115 | if (docGroup === undefined) |
| 116 | return; |
| 117 | this.appendOperand(docGroup); |
| 118 | break; |
| 119 | default: |
| 120 | KorAP.log(812, "Operand not supported in document group"); |
| 121 | return; |
| 122 | }; |
| 123 | }; |
| 124 | return this; |
| 125 | }, |
| 126 | set operation (op) { |
| 127 | if (KorAP._validGroupOpRE.test(op)) { |
| 128 | this._op = op; |
| 129 | } |
| 130 | else { |
| 131 | KorAP.log(810, "Unknown operation type"); |
| 132 | return; |
| 133 | }; |
| 134 | }, |
| 135 | get operation () { |
| 136 | return this._op || 'and'; |
| 137 | }, |
| 138 | get operands () { |
| 139 | return this._operands; |
| 140 | }, |
| 141 | appendOperand : function (obj) { |
| 142 | this._operands.push(obj) |
| 143 | return obj; |
| 144 | }, |
| 145 | getOperand : function (index) { |
| 146 | return this._operands[index]; |
| 147 | }, |
| 148 | set ldType (type) { |
| 149 | this._ldtype = type; |
| 150 | }, |
| 151 | get ldType () { |
| 152 | return this._ldType || "docGroup"; |
| 153 | }, |
| 154 | toJson : function () { |
| 155 | var operands = new Array(); |
| 156 | for (var i in this._operands) { |
| 157 | operands.push(this._operands[i].toJson()); |
| 158 | }; |
| 159 | return { |
| 160 | "@type" : "korap:" + this.ldType, |
| 161 | "operation" : "operation:" + this.operation, |
| 162 | "operands" : operands |
| 163 | }; |
| 164 | } |
| 165 | }; |
| 166 | |
| 167 | /** |
| 168 | * Virtual collection doc criterion. |
| 169 | */ |
| 170 | KorAP.Doc = { |
| 171 | |
| 172 | // Create new |
| 173 | create : function () { |
| 174 | var doc = Object.create(KorAP.Doc); |
| 175 | doc.ldType = "doc"; |
| 176 | return doc; |
| 177 | }, |
| 178 | |
| 179 | // Deserialize from json |
| 180 | fromJson : function (json) { |
| 181 | return Object.create(KorAP.Doc)._init(json); |
| 182 | }, |
| 183 | |
| 184 | // Init new doc criterion or deserialize fro JSON-LD |
| 185 | _init : function (json) { |
| 186 | if (json === undefined) |
| 187 | return this.create(); |
| 188 | |
| 189 | if (json["@type"] !== "korap:doc") { |
| 190 | KorAP.log(701, "JSON-LD group has no @type attribute"); |
| 191 | return; |
| 192 | }; |
| 193 | |
| 194 | this.ldType = "doc"; |
| 195 | |
| 196 | if (json["value"] === undefined || |
| 197 | typeof json["value"] != 'string') { |
| 198 | KorAP.log(805, "Value is invalid"); |
| 199 | return; |
| 200 | }; |
| 201 | |
| 202 | // There is a defined key |
| 203 | if (json["key"] !== undefined && |
| 204 | typeof json["key"] === 'string') { |
| 205 | |
| 206 | // Set key |
| 207 | this.key = json["key"]; |
| 208 | |
| 209 | // Set match operation |
| 210 | if (json["match"] !== undefined) { |
| 211 | if (typeof json["match"] === 'string') |
| 212 | this.matchop = json["match"]; |
| 213 | else { |
| 214 | KorAP.log(802, "Match type is not supported by value type"); |
| 215 | return; |
| 216 | }; |
| 217 | }; |
| 218 | |
| 219 | // Key is a string |
| 220 | if (json["type"] === undefined || |
| 221 | json["type"] == "type:string") { |
| 222 | this.type = "string"; |
| 223 | |
| 224 | // Check match type |
| 225 | if (!KorAP._validStringMatchRE.test(this.matchop)) { |
| 226 | KorAP.log(802, "Match type is not supported by value type"); |
| 227 | return; |
| 228 | }; |
| 229 | |
| 230 | // Set string value |
| 231 | this.value = json["value"]; |
| 232 | } |
| 233 | |
| 234 | // Key is a date |
| 235 | else if (json["type"] === "type:date") { |
| 236 | this.type = "date"; |
| 237 | |
| 238 | if (json["value"] !== undefined && |
| 239 | KorAP._validDateRE.test(json["value"])) { |
| 240 | |
| 241 | if (!KorAP._validDateMatchRE.test(this.matchop)) { |
| 242 | KorAP.log(802, "Match type is not supported by value type"); |
| 243 | return; |
| 244 | }; |
| 245 | |
| 246 | // Set value |
| 247 | this.value = json["value"]; |
| 248 | } |
| 249 | else { |
| 250 | KorAP.log(806, "Value is not a valid date string"); |
| 251 | return; |
| 252 | }; |
| 253 | } |
| 254 | |
| 255 | // Key is a regular expression |
| 256 | else if (json["type"] === "type:regex") { |
| 257 | this.type = "regex"; |
| 258 | |
| 259 | try { |
| 260 | |
| 261 | // Try to create a regular expression |
| 262 | var check = new RegExp(json["value"]); |
| 263 | |
| 264 | if (!KorAP._validRegexMatchRE.test(this.matchop)) { |
| 265 | KorAP.log(802, "Match type is not supported by value type"); |
| 266 | return; |
| 267 | }; |
| 268 | |
| 269 | this.value = json["value"]; |
| 270 | } |
| 271 | catch (e) { |
| 272 | KorAP.log(807, "Value is not a valid regular expression"); |
| 273 | return; |
| 274 | }; |
| 275 | this.type = "regex"; |
| 276 | } |
| 277 | |
| 278 | else { |
| 279 | KorAP.log(804, "Unknown value type"); |
| 280 | return; |
| 281 | }; |
| 282 | }; |
| 283 | |
| 284 | return this; |
| 285 | }, |
| 286 | set key (value) { |
| 287 | this._key = value; |
| 288 | }, |
| 289 | get key () { |
| 290 | return this._key; |
| 291 | }, |
| 292 | set matchop (match) { |
| 293 | this._matchop = match.replace(/^match:/, ''); |
| 294 | }, |
| 295 | get matchop () { |
| 296 | return this._matchop || "eq"; |
| 297 | }, |
| 298 | set type (type) { |
| 299 | this._type = type; |
| 300 | }, |
| 301 | get type () { |
| 302 | return this._type || "string"; |
| 303 | }, |
| 304 | set value (value) { |
| 305 | this._value = value; |
| 306 | }, |
| 307 | get value () { |
| 308 | return this._value; |
| 309 | }, |
| 310 | |
| 311 | // Todo: Redundant and should be inherited |
| 312 | set ldType (type) { |
| 313 | this._ldtype = type; |
| 314 | }, |
| 315 | get ldType () { |
| 316 | return this._ldType || "doc"; |
| 317 | }, |
| 318 | toJson : function () { |
| 319 | if (!this.matchop || !this.key) |
| 320 | return {}; |
| 321 | |
| 322 | return { |
| 323 | "@type" : "korap:" + this.ldType, |
| 324 | "key" : this.key, |
| 325 | "match" : "match:" + this.matchop, |
| 326 | "value" : this.value || '', |
| 327 | "type" : "type:" + this.type |
| 328 | }; |
| 329 | } |
| 330 | }; |
| 331 | }(this.KorAP)); |