/* * 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 { /** * Tags used to tag feed and entry elements. */ 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 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 element values representing ATOM entries. */ const list entriesElementValues(const list& e) { if (isNil(e)) return e; return cons(entryElementValues(car(e)), entriesElementValues(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 mklist(entryElementValues(car(e))); } /** * 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)); 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 element values representing an RSS entry to a list of elements. */ const list entryElement(const list& l) { 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" + title) + (list() + element + "link" + id), isNil(content)? list() : mklist(append(list() + element + "description", text? mklist(elementValue(content)) : elementChildren(content)))); } /** * 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& ll) { const list l = isNil(ll)? ll : (list)car(ll); 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& 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" + 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); 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))); } } } #endif /* tuscany_rss_hpp */