From 7905926c413e843d9497948cb824059f2c017f6a Mon Sep 17 00:00:00 2001 From: jsdelfino Date: Mon, 26 Aug 2013 03:04:23 +0000 Subject: Support sorting database search results by rank, and add a rank attribute to ATOM entries. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1517412 13f79535-47bb-0310-9956-ffa450edef68 --- sca-cpp/trunk/modules/js/htdocs/atomutil.js | 48 ++++++++++++++++++++--------- sca-cpp/trunk/modules/js/htdocs/elemutil.js | 26 ++++++++-------- 2 files changed, 48 insertions(+), 26 deletions(-) (limited to 'sca-cpp/trunk/modules/js/htdocs') diff --git a/sca-cpp/trunk/modules/js/htdocs/atomutil.js b/sca-cpp/trunk/modules/js/htdocs/atomutil.js index 4b57b36fb4..8fe95c16a1 100644 --- a/sca-cpp/trunk/modules/js/htdocs/atomutil.js +++ b/sca-cpp/trunk/modules/js/htdocs/atomutil.js @@ -27,24 +27,27 @@ var atom = {}; */ atom.entryElementValues = function(e) { var lt = filter(selector(mklist(element, "'title")), e); - var t = mklist(element, "'title", isNull(lt)? '' : elementValue(car(lt))); + var t = mklist(element, "'title", isNull(lt) || !elementHasValue(car(lt))? '' : elementValue(car(lt))); var li = filter(selector(mklist(element, "'id")), e); var i = mklist(element, "'id", isNull(li)? '' : elementValue(car(li))); var la = filter(selector(mklist(element, "'author")), e); - var lan = isNull(la)? mklist() : filter(selector(mklist(element, "'name")), car(la)); - var lae = isNull(la)? mklist() : filter(selector(mklist(element, "'email")), car(la)); + var lan = isNull(la)? nil : filter(selector(mklist(element, "'name")), car(la)); + var lae = isNull(la)? nil : filter(selector(mklist(element, "'email")), car(la)); var laa = isNull(lan)? lae : lan; - var a = isNull(laa)? mklist() : mklist(mklist(element, "'author", elementValue(car(laa)))); + var a = isNull(laa)? nil : mklist(mklist(element, "'author", elementValue(car(laa)))); var lu = filter(selector(mklist(element, "'updated")), e); - var u = isNull(lu)? mklist() : mklist(mklist(element, "'updated", elementValue(car(lu)))); + var u = isNull(lu)? nil : mklist(mklist(element, "'updated", elementValue(car(lu)))); + + var lr = filter(selector(mklist(element, "'rank")), e); + var r = isNull(lr)? nil : mklist(mklist(element, "'rank", elementValue(car(lr)))); var lc = filter(selector(mklist(element, "'content")), e); - var c = isNull(lc)? mklist() : isAttribute(elementValue(car(lc)))? mklist() : mklist(mklist(element, "'content", elementValue(car(lc)))); + var c = isNull(lc)? nil : isAttribute(elementValue(car(lc)))? nil : mklist(mklist(element, "'content", elementValue(car(lc)))); - return append(append(append(mklist(element, "'entry", t, i), a), u), c); + return append(append(append(append(mklist(element, "'entry", t, i), a), u), r), c); }; /** @@ -71,7 +74,7 @@ atom.isATOMEntry = function(l) { atom.readATOMEntryDocument = function(doc) { var e = readXMLDocument(doc); if (isNull(e)) - return mklist(); + return nil; return mklist(atom.entryElementValues(car(e))); }; @@ -97,7 +100,7 @@ atom.isATOMFeed = function(l) { atom.readATOMFeedDocument = function(doc) { var f = readXMLDocument(doc); if (isNull(f)) - return mklist(); + return nil; var t = filter(selector(mklist(element, "'title")), car(f)); var i = filter(selector(mklist(element, "'id")), car(f)); var e = filter(selector(mklist(element, "'entry")), car(f)); @@ -117,20 +120,23 @@ atom.readATOMFeed = function(l) { * Convert a list of values representy an ATOM entry to a list of elements. */ atom.entryElement = function(l) { - var title = elementValue(namedElementChild("'title", l)); + var etitle = namedElementChild("'title", l); + var title = elementHasValue(etitle)? elementValue(etitle) : ''; var id = elementValue(namedElementChild("'id", l)); var author = namedElementChild("'author", l); var email = isNull(author)? false : (elementValue(author).indexOf('@') != -1); var updated = namedElementChild("'updated", l); + var rank = namedElementChild("'rank", l); var content = namedElementChild("'content", l); var text = isNull(content)? false : elementHasValue(content); - return append(append(append(append( + return append(append(append(append(append( mklist(element, "'entry", mklist(attribute, "'xmlns", "http://www.w3.org/2005/Atom"), mklist(element, "'title", mklist(attribute, "'type", "text"), title), mklist(element, "'id", id)), - isNull(author)? mklist() : mklist(mklist(element, "'author", + isNull(author)? nil : mklist(mklist(element, "'author", (email? mklist(element, "'email", elementValue(author)) : mklist(element, "'name", elementValue(author)))))), - isNull(updated)? mklist() : mklist(mklist(element, "'updated", elementValue(updated)))), - isNull(content)? mklist() : + isNull(updated)? nil : mklist(mklist(element, "'updated", elementValue(updated)))), + isNull(rank)? nil : mklist(mklist(element, "'rank", elementValue(rank)))), + isNull(content)? nil : mklist(append(mklist(element, "'content", mklist(attribute, "'type", text? "text" : "application/xml")), text? mklist(elementValue(content)) : elementChildren(content)))), mklist(mklist(element, "'link", mklist(attribute, "'href", id)))); @@ -182,3 +188,17 @@ atom.writeATOMFeed = function(ll) { return writeXML(mklist(fe), true); }; +/** + * Uncomment to test. + */ +/* +(function testAtom() { + console.log('Testing...'); + var entry = mklist("'entry", mklist("'title", 'test'), mklist("'id", 'test'), mklist("'content", mklist("'rating", '1'))); + var entryxml = car(atom.writeATOMEntry(valuesToElements(mklist(entry)))); + assert(entryxml == '\n' + + 'testtest1\n'); + console.log('OK'); +})(); +*/ + diff --git a/sca-cpp/trunk/modules/js/htdocs/elemutil.js b/sca-cpp/trunk/modules/js/htdocs/elemutil.js index 62050f5953..01dfcf2664 100644 --- a/sca-cpp/trunk/modules/js/htdocs/elemutil.js +++ b/sca-cpp/trunk/modules/js/htdocs/elemutil.js @@ -42,45 +42,47 @@ function isAttribute(v) { /** * Return the name of an attribute. */ -attributeName = cadr; +var attributeName = cadr; /** * Return the value of an attribute. */ -attributeValue = caddr; +var attributeValue = caddr; /** * Return the name of an element. */ -elementName = cadr; +var elementName = cadr; /** * Return true if an element has children. */ function elementHasChildren(l) { - return !isNull(cddr(l)); + return !isNull(l) && !isNull(cddr(l)); } /** * Return the children of an element. */ -elementChildren = cddr; +var elementChildren = cddr; /** * Return true if an element has a value. */ function elementHasValue(l) { - var r = reverse(l); - if (isSymbol(car(r))) + if (isNull(l)) + return false; + var v = last(l); + if (isSymbol(v)) return false; - return (!(isList(car(r)) && !isNull(car(r)) && isSymbol(car(car(r))))) + return (!(isList(v) && !isNull(v) && isSymbol(car(v)))) } /** * Return the value of an element. */ function elementValue(l) { - return car(reverse(l)); + return last(l); } /** @@ -140,7 +142,7 @@ function elementsToValues(e) { function valueToElement(t) { if (isList(t) && !isNull(t) && isSymbol(car(t))) { var n = car(t); - var v = isNull(cdr(t))? mklist() : cadr(t); + var v = isNull(cdr(t))? nil : cadr(t); if (!isList(v)) { if (n.substring(0, 2) == atsign) return mklist(attribute, "'" + n.substring(2), v); @@ -171,7 +173,7 @@ function selector(s) { function evalSelect(s, v) { if (isNull(s)) return true; - if (isNull(v)) + if (!isList(v) || isNull(v)) return false; if (car(s) != car(v)) return false; @@ -230,7 +232,7 @@ function namedElementChild(name, l) { * Set the contents of an element. */ function setElement(l, e) { - setlist(l, e); + setList(l, e); l.memo = {}; } -- cgit v1.2.3