From 7808f187b68261a58de9e348a722b6d0e32dcbe9 Mon Sep 17 00:00:00 2001 From: jsdelfino Date: Mon, 31 May 2010 02:29:45 +0000 Subject: Add support for RSS feeds and minor fixes to ATOM support. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@949652 13f79535-47bb-0310-9956-ffa450edef68 --- sca-cpp/trunk/modules/rss/rss.hpp | 201 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 201 insertions(+) create mode 100644 sca-cpp/trunk/modules/rss/rss.hpp (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 new file mode 100644 index 0000000000..506d1f4a6d --- /dev/null +++ b/sca-cpp/trunk/modules/rss/rss.hpp @@ -0,0 +1,201 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* $Rev$ $Date$ */ + +#ifndef tuscany_rss_hpp +#define tuscany_rss_hpp + +/** + * RSS data conversion functions. + */ + +#include "string.hpp" +#include "list.hpp" +#include "value.hpp" +#include "monad.hpp" +#include "xml.hpp" + +namespace tuscany { +namespace rss { + +/** + * Convert a list of elements to a list of values representing an RSS entry. + */ +const list entryElementsToValues(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, elementValue(car(ld))); +} + +/** + * Convert a list of elements to a list of values representing RSS entries. + */ +const list entriesElementsToValues(const list& e) { + if (isNil(e)) + return e; + return cons(entryElementsToValues(car(e)), entriesElementsToValues(cdr(e))); +} + +/** + * Return true if a list of strings contains an RSS feed. + */ +const bool isRSSFeed(const list& ls) { + if (!isXML(ls)) + return false; + return contains(car(ls), " > 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))? (value)cdr(car(v)) : car(v))); +} + +/** + * Convert a list of strings to a list of values representing an RSS feed. + */ +const failable > readRSSFeed(const list& ilist) { + const list f = readXML(ilist); + if (isNil(f)) + return mkfailure >("Empty feed"); + const list c = filter(selector(mklist(element, "channel")), car(f)); + 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)))); +} + +/** + * 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. + */ +const list entryElement(const list& l) { + return list() + + element + "item" + + (list() + element + "title" + car(l)) + + (list() + element + "link" + cadr(l)) + + (list() + element + "description" + caddr(l)); +} + +/** + * Convert a list of values representing RSS entries to a list of elements. + */ +const list entriesElements(const list& l) { + if (isNil(l)) + return l; + return cons(entryElement(car(l)), entriesElements(cdr(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) { + return writeXML(reduce, initial, mklist(entryElement(l))); +} + +const failable > writeRSSEntry(const list& l) { + const failable > ls = writeRSSEntry >(rcons, list(), l); + if (!hasContent(ls)) + return ls; + return reverse(list(content(ls))); +} + +/** + * 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) { + 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))); + const list fe = list() + + element + "rss" + (list() + attribute + "version" + "2.0") + + append(list() + element + "channel", ce); + return writeXML(reduce, initial, mklist(fe)); +} + +/** + * Convert a list of values representing an RSS feed to a list of strings. + * The first two values in the list are the feed id and title. + */ +const failable > writeRSSFeed(const list& l) { + const failable > ls = writeRSSFeed>(rcons, list(), l); + if (!hasContent(ls)) + return ls; + 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)))); +} + +} +} + +#endif /* tuscany_rss_hpp */ -- cgit v1.2.3