/* * 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_atom_hpp #define tuscany_atom_hpp /** * ATOM data conversion functions. */ #include "string.hpp" #include "list.hpp" #include "value.hpp" #include "monad.hpp" #include "xml.hpp" namespace tuscany { namespace atom { /** * Convert a list of elements to a list of values representing an ATOM 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, "id")), e); const value i = isNil(li)? value(emptyString) : elementValue(car(li)); const list lc = filter(selector(mklist(element, "content")), e); return mklist(t, i, elementValue(car(lc))); } /** * Convert a list of elements to a list of values representing ATOM 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 isATOMFeed(const list& ls) { if (!isXML(ls)) return false; return contains(car(ls), " > readATOMEntry(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 ATOM 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 ATOM feed. */ const failable > readATOMFeed(const list& ilist) { const list f = readXML(ilist); if (isNil(f)) return mkfailure >("Empty feed"); const list t = filter(selector(mklist(element, "title")), car(f)); const list i = filter(selector(mklist(element, "id")), car(f)); const list e = filter(selector(mklist(element, "entry")), car(f)); if (isNil(e)) return mklist(elementValue(car(t)), elementValue(car(i))); return cons(elementValue(car(t)), cons(elementValue(car(i)), entriesElementsToValues(e))); } /** * Convert an ATOM feed containing elements to an ATOM 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 ATOM 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 + "entry" + (list() + attribute + "xmlns" + "http://www.w3.org/2005/Atom") + (list() + element + "title" + (list() + attribute + "type" + "text") + car(l)) + (list() + element + "id" + cadr(l)) + (list() + element + "content" + (list() + attribute + "type" + (isList(caddr(l))? "application/xml" : "text")) + caddr(l)) + (list() + element + "link" + (list() + attribute + "href" + cadr(l))); } /** * Convert a list of values representing ATOM 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 ATOM entry to an ATOM entry. * The first two values in the list are the entry id and title. */ template const failable writeATOMEntry(const lambda& reduce, const R& initial, const list& l) { return writeXML(reduce, initial, mklist(entryElement(l))); } const failable > writeATOMEntry(const list& l) { const failable > ls = writeATOMEntry >(rcons, list(), l); if (!hasContent(ls)) return ls; return reverse(list(content(ls))); } /** * Convert a list of values representing an ATOM feed to an ATOM feed. * The first two values in the list are the feed id and title. */ template const failable writeATOMFeed(const lambda& reduce, const R& initial, const list& l) { const list f = list() + element + "feed" + (list() + attribute + "xmlns" + "http://www.w3.org/2005/Atom") + (list() + element + "title" + (list() + attribute + "type" + "text") + car(l)) + (list() + element + "id" + cadr(l)); if (isNil(cddr(l))) return writeXML(reduce, initial, mklist(f)); const list fe = append(f, entriesElements(cddr(l))); return writeXML(reduce, initial, mklist(fe)); } /** * Convert a list of values representing an ATOM feed to a list of strings. * The first two values in the list are the feed id and title. */ const failable > writeATOMFeed(const list& l) { const failable > ls = writeATOMFeed>(rcons, list(), l); if (!hasContent(ls)) return ls; return reverse(list(content(ls))); } /** * Convert an ATOM entry containing a value to an ATOM 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 ATOM feed containing values to an ATOM 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_atom_hpp */