/* * 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 #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 entry(const list& e) { const list lt = filter(selector(mklist(element, "title")), e); const value t = isNil(lt)? value(std::string("")) : elementValue(car(lt)); const list li = filter(selector(mklist(element, "id")), e); const value i = isNil(li)? value(std::string("")) : elementValue(car(li)); const list lc = filter(selector(mklist(element, "content")), e); return mklist(t, i, cadr(elementChildren(car(lc)))); } /** * Convert a list of elements to a list of values representing ATOM entries. */ const list entries(const list& e) { if (isNil(e)) return list(); return cons(entry(car(e)), entries(cdr(e))); } /** * Convert a list of strings to a list of values representing an ATOM entry. */ const failable, std::string> readEntry(const list& ilist) { const list e = readXML(ilist); if (isNil(e)) return std::string("Empty entry"); return entry(car(e)); } /** * Convert a list of strings to a list of values representing an ATOM feed. */ const failable, std::string> readFeed(const list& ilist) { const list f = readXML(ilist); if (isNil(f)) return std::string("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)), entries(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" << "application/xml") << 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 list(); 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 writeEntry(const lambda& reduce, const R& initial, const list& l) { return writeXML(reduce, initial, mklist(entryElement(l))); } /** * Convert a list of values representing an ATOM entry to a list of strings. * The first two values in the list are the entry id and title. */ const list writeStrings(const list& listSoFar, const std::string& s) { return cons(s, listSoFar); } const failable, std::string> writeEntry(const list& l) { const failable, std::string> ls = writeEntry >(writeStrings, list(), l); if (!hasValue(ls)) return ls; return reverse(list(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 writeFeed(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, std::string> writeFeed(const list& l) { const failable, std::string> ls = writeFeed >(writeStrings, list(), l); if (!hasValue(ls)) return ls; return reverse(list(ls)); } } } #endif /* tuscany_atom_hpp */