From 49b878b1b0f2e52bbd5282c22ac32a68e1e8736c Mon Sep 17 00:00:00 2001 From: jsdelfino Date: Tue, 8 Mar 2011 08:18:07 +0000 Subject: Change ATOM and RSS feed representations to use name value pairs instead of just strings, to allow support for all ATOM and RSS attributes and avoid confusion with non-feed string results. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1079292 13f79535-47bb-0310-9956-ffa450edef68 --- sca-cpp/trunk/modules/rss/rss.hpp | 127 +++++++++++++++++++------------------- 1 file changed, 64 insertions(+), 63 deletions(-) (limited to 'sca-cpp/trunk/modules/rss/rss.hpp') diff --git a/sca-cpp/trunk/modules/rss/rss.hpp b/sca-cpp/trunk/modules/rss/rss.hpp index 646a8fd030..c62d137adc 100644 --- a/sca-cpp/trunk/modules/rss/rss.hpp +++ b/sca-cpp/trunk/modules/rss/rss.hpp @@ -36,24 +36,33 @@ namespace tuscany { namespace rss { /** - * Convert a list of elements to a list of values representing an RSS entry. + * Tags used to tag feed and entry elements. */ -const list entryElementsToValues(const list& e) { +const value feed("feed"); +const value entry("entry"); + +/** + * Convert a list of elements to a list of element values representing an RSS entry. + */ +const list entryElementValues(const list& e) { const list lt = filter(selector(mklist(element, "title")), e); const value t = isNil(lt)? value(emptyString) : elementValue(car(lt)); const list li = filter(selector(mklist(element, "link")), e); const value i = isNil(li)? value(emptyString) : elementValue(car(li)); const list ld = filter(selector(mklist(element, "description")), e); - return mklist(t, i, isNil(ld)? (value)list() : elementValue(car(ld))); + return append(list() + element + entry + + value(list() + element + value("title") + t) + + value(list() + element + value("id") + i), + isNil(ld)? list() : mklist(value(list() + element + value("content") + elementValue(car(ld))))); } /** - * Convert a list of elements to a list of values representing RSS entries. + * Convert a list of elements to a list of element values representing ATOM entries. */ -const list entriesElementsToValues(const list& e) { +const list entriesElementValues(const list& e) { if (isNil(e)) return e; - return cons(entryElementsToValues(car(e)), entriesElementsToValues(cdr(e))); + return cons(entryElementValues(car(e)), entriesElementValues(cdr(e))); } /** @@ -72,15 +81,7 @@ const failable > readRSSEntry(const list& ilist) { const list e = readXML(ilist); if (isNil(e)) return mkfailure >("Empty entry"); - return entryElementsToValues(car(e)); -} - -/** - * Convert a list of values representing an RSS entry to a value. - */ -const value entryValue(const list& e) { - const list v = elementsToValues(mklist(caddr(e))); - return cons(car(e), mklist(cadr(e), isList(car(v))? (isNil((list)car(v))? car(v) : (value)cdr(car(v))) : car(v))); + return mklist(entryElementValues(car(e))); } /** @@ -94,34 +95,28 @@ const failable > readRSSFeed(const list& ilist) { const list t = filter(selector(mklist(element, "title")), car(c)); const list i = filter(selector(mklist(element, "link")), car(c)); const list e = filter(selector(mklist(element, "item")), car(c)); - if (isNil(e)) - return mklist(elementValue(car(t)), elementValue(car(i))); - return cons(elementValue(car(t)), cons(elementValue(car(i)), entriesElementsToValues(e))); -} - -/** - * Convert an RSS feed containing elements to an RSS feed containing values. - */ -const list feedValuesLoop(const list e) { - if (isNil(e)) - return e; - return cons(entryValue(car(e)), feedValuesLoop(cdr(e))); -} - -const list feedValues(const list& e) { - return cons(car(e), cons(cadr(e), feedValuesLoop(cddr(e)))); + return mklist(append(list() + element + feed + + value(list() + element + value("title") + elementValue(car(t))) + + value(list() + element + value("id") + elementValue(car(i))), + entriesElementValues(e))); } /** - * Convert a list of values representing an RSS entry to a list of elements. - * The first two values in the list are the entry title and id. + * Convert a list of element values representing an RSS entry to a list of elements. */ const list entryElement(const list& l) { - return list() + const value title = elementValue(elementChild("title", l)); + const value id = elementValue(elementChild("id", l)); + const value content = elementChild("content", l); + const bool text = isNil(content)? false : elementHasValue(content); + return append(list() + element + "item" - + (list() + element + "title" + car(l)) - + (list() + element + "link" + cadr(l)) - + (isNil(cddr(l))? list() : list() + element + "description" + caddr(l)); + + (list() + element + "title" + title) + + (list() + element + "link" + id), + isNil(content)? + list() : + mklist(append(list() + element + "description", + text? mklist(elementValue(content)) : elementChildren(content)))); } /** @@ -137,7 +132,8 @@ const list entriesElements(const list& l) { * Convert a list of values representing an RSS entry to an RSS entry. * The first two values in the list are the entry id and title. */ -template const failable writeRSSEntry(const lambda& reduce, const R& initial, const list& l) { +template const failable writeRSSEntry(const lambda& reduce, const R& initial, const list& ll) { + const list l = isNil(ll)? ll : (list)car(ll); return writeXML(reduce, initial, mklist(entryElement(l))); } @@ -152,12 +148,37 @@ const failable > writeRSSEntry(const list& l) { * Convert a list of values representing an RSS feed to an RSS feed. * The first two values in the list are the feed id and title. */ -template const failable writeRSSFeed(const lambda& reduce, const R& initial, const list& l) { +template const failable writeRSSFeed(const lambda& reduce, const R& initial, const list& ll) { + const list l = isNil(ll)? ll : (list)car(ll); + const list lt = filter(selector(mklist(element, "title")), l); + const value t = isNil(lt)? value(emptyString) : elementValue(car(lt)); + const list li = filter(selector(mklist(element, "id")), l); + const value i = isNil(li)? value(emptyString) : elementValue(car(li)); const list c = list() - + (list() + element + "title" + car(l)) - + (list() + element + "link" + cadr(l)) - + (list() + element + "description" + car(l)); - const list ce = isNil(cddr(l))? c : append(c, entriesElements(cddr(l))); + + (list() + element + "title" + t) + + (list() + element + "link" + i) + + (list() + element + "description" + t); + + // Write RSS entries + const list le = filter(selector(mklist(element, entry)), l); + if (isNil(le)) { + const list fe = list() + + element + "rss" + (list() + attribute + "version" + "2.0") + + append(list() + element + "channel", c); + return writeXML(reduce, initial, mklist(fe)); + } + + // Write a single RSS entry element with a list of values + if (!isNil(le) && !isNil(car(le)) && isList(car(caddr(car(le))))) { + const list ce = append(c, entriesElements(caddr(car(le)))); + const list fe = list() + + element + "rss" + (list() + attribute + "version" + "2.0") + + append(list() + element + "channel", ce); + return writeXML(reduce, initial, mklist(fe)); + } + + // Write separate RSS entry elements + const list ce = append(c, entriesElements(le)); const list fe = list() + element + "rss" + (list() + attribute + "version" + "2.0") + append(list() + element + "channel", ce); @@ -175,26 +196,6 @@ const failable > writeRSSFeed(const list& l) { return reverse(list(content(ls))); } -/** - * Convert an RSS entry containing a value to an RSS entry containing an item element. - */ -const list entryValuesToElements(const list val) { - return cons(car(val), cons(cadr(val), valuesToElements(mklist(cons("item", (list)caddr(val)))))); -} - -/** - * Convert an RSS feed containing values to an RSS feed containing elements. - */ -const list feedValuesToElementsLoop(const list val) { - if (isNil(val)) - return val; - return cons(entryValuesToElements(car(val)), feedValuesToElementsLoop(cdr(val))); -} - -const list feedValuesToElements(const list& val) { - return cons(car(val), cons(cadr(val), feedValuesToElementsLoop(cddr(val)))); -} - } } -- cgit v1.2.3