From e44a7e37b6c7b5961adaffc62b9042b8d442938e Mon Sep 17 00:00:00 2001 From: mensonge Date: Thu, 13 Nov 2008 09:49:11 +0000 Subject: New feature: basic Ajax suggestion for tags and implementation of Dojo toolkit git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@151 b3834d28-1941-0410-a4f8-b48e95affb8f --- includes/js/dojox/jsonPath/README | 125 +++++++++++++++ includes/js/dojox/jsonPath/query.js | 163 +++++++++++++++++++ includes/js/dojox/jsonPath/tests/jsonPath.js | 211 +++++++++++++++++++++++++ includes/js/dojox/jsonPath/tests/module.js | 12 ++ includes/js/dojox/jsonPath/tests/runTests.html | 9 ++ 5 files changed, 520 insertions(+) create mode 100644 includes/js/dojox/jsonPath/README create mode 100644 includes/js/dojox/jsonPath/query.js create mode 100644 includes/js/dojox/jsonPath/tests/jsonPath.js create mode 100644 includes/js/dojox/jsonPath/tests/module.js create mode 100644 includes/js/dojox/jsonPath/tests/runTests.html (limited to 'includes/js/dojox/jsonPath') diff --git a/includes/js/dojox/jsonPath/README b/includes/js/dojox/jsonPath/README new file mode 100644 index 0000000..2da2b31 --- /dev/null +++ b/includes/js/dojox/jsonPath/README @@ -0,0 +1,125 @@ +------------------------------------------------------------------------------- +dojox.jsonPath +------------------------------------------------------------------------------- +Version 1.0 +Release date: 11/14/2007 +------------------------------------------------------------------------------- +Project state: beta +------------------------------------------------------------------------------- +Project authors + Dustin Machi + Kris Zyp +------------------------------------------------------------------------------- +Project description + +jsonPath is a query system similar in idea to xpath, but for use against +javascript objects. This code is a port of the jsonPath code at +http://code.google.com/p/jsonpath/. It was contributed under CLA by Stefan +Goessner. Thanks Stefan! +------------------------------------------------------------------------------- +Dependencies: + +Dojo Core (package loader). +------------------------------------------------------------------------------- +Documentation + +Usage: + +var matches = dojox.jsonPath.query(objectToQuery, jsonPathExpresson) + +Expressions: + + $ The Root Object + @ The current object/element + . or [] The child operator + .. Recursive descent + * all objects + [] subscript operator + [,] Union operator + [start:end:step] array slice operator + ?() applies a filter/script expression + () script expresions + + some examples: + + Given the following test data set: + + var json = + { "store": { + "book": [ + { "category": "reference", + "author": "Nigel Rees", + "title": "Sayings of the Century", + "price": 8.95 + }, + { "category": "fiction", + "author": "Evelyn Waugh", + "title": "Sword of Honour", + "price": 12.99 + }, + { "category": "fiction", + "author": "Herman Melville", + "title": "Moby Dick", + "isbn": "0-553-21311-3", + "price": 8.99 + }, + { "category": "fiction", + "author": "J. R. R. Tolkien", + "title": "The Lord of the Rings", + "isbn": "0-395-19395-8", + "price": 22.99 + } + ], + "bicycle": { + "color": "red", + "price": 19.95 + } + } + }; + + Here are some example queries and their output: + + $.store.book[*].author + ["Nigel Rees","Evelyn Waugh","Herman Melville","J. R. R. Tolkien"] + + $..author + ["Nigel Rees","Evelyn Waugh","Herman Melville","J. R. R. Tolkien"] + + $.store.* + [[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}],{"color":"red","price":19.95}] + + $.store..price + [8.95,12.99,8.99,22.99,19.95] + + $..book[(@.length-1)] + [{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}] + + $..book[-1] + [{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}] + + $..book[0,1] + [{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99}] + + $..book[:2] + [{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99}] + + $..book[?(@.isbn)] + [{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}] + + $..book[?(@.price<10)] + [{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99}] + + $..* + [{"book":[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}],"bicycle":{"color":"red","price":19.95}},[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}],{"color":"red","price":19.95},{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99},"reference","Nigel Rees","Sayings of the Century",8.95,"fiction","Evelyn Waugh","Sword of Honour",12.99,"fiction","Herman Melville","Moby Dick","0-553-21311-3",8.99,"fiction","J. R. R. Tolkien","The Lord of the Rings","0-395-19395-8",22.99,"red",19.95] + + +------------------------------------------------------------------------------- +Installation instructions + +Grab the following from the Dojo SVN Repository: +http://svn.dojotoolkit.org/var/src/dojo/dojox/trunk/jsonPath + +Install into the following directory structure: +/dojox/jsonPath/ + +...which should be at the same level as your Dojo checkout. diff --git a/includes/js/dojox/jsonPath/query.js b/includes/js/dojox/jsonPath/query.js new file mode 100644 index 0000000..4e24309 --- /dev/null +++ b/includes/js/dojox/jsonPath/query.js @@ -0,0 +1,163 @@ +if(!dojo._hasResource["dojox.jsonPath.query"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code. +dojo._hasResource["dojox.jsonPath.query"] = true; +dojo.provide("dojox.jsonPath.query"); + +dojox.jsonPath.query = function(/*Object*/obj, /*String*/expr, /*Object*/arg){ + // summaRy + // Perform jsonPath query `expr` on javascript object or json string `obj` + // obj - object || json string to perform query on + // expr - jsonPath expression (string) to be evaluated + // arg - {}special arugments. + // resultType: "VALUE"||"BOTH"||"PATH"} (defaults to value) + // evalType: "RESULT"||"ITEM"} (defaults to ?) + + var re = dojox.jsonPath._regularExpressions; + if (!arg){arg={};} + + var strs = []; + function _str(i){ return strs[i];} + var acc; + if (arg.resultType == "PATH" && arg.evalType == "RESULT") throw Error("RESULT based evaluation not supported with PATH based results"); + var P = { + resultType: arg.resultType || "VALUE", + normalize: function(expr){ + var subx = []; + expr = expr.replace(/'([^']|'')*'/g, function(t){return "_str("+(strs.push(eval(t))-1)+")";}); + var ll = -1; + while(ll!=subx.length){ + ll=subx.length;//TODO: Do expression syntax checking + expr = expr.replace(/(\??\([^\(\)]*\))/g, function($0){return "#"+(subx.push($0)-1);}); + } + expr = expr.replace(/[\['](#[0-9]+)[\]']/g,'[$1]') + .replace(/'?\.'?|\['?/g, ";") + .replace(/;;;|;;/g, ";..;") + .replace(/;$|'?\]|'$/g, ""); + ll = -1; + while(ll!=expr){ + ll=expr; + expr = expr.replace(/#([0-9]+)/g, function($0,$1){return subx[$1];}); + } + return expr.split(";"); + }, + asPaths: function(paths){ + for (var j=0;j + + + Dojox Unit Test Runner + + + Redirecting to D.O.H runner. + + -- cgit v1.2.3-54-g00ecf