/* * 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 { /** * 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 ATOM 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, "id")), e); const value i = isNil(li)? value(emptyString) : elementValue(car(li)); const list lc = filter(selector(mklist(element, "content")), e); return append(list() + element + entry + value(list() + element + value("title") + t) + value(list() + element + value("id") + i), isNil(lc)? list() : mklist(value(list() + element + value("content") + elementValue(car(lc))))); } /** * 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 ATOM feed. */ const bool isATOMFeed(const list& ls) { if (!isXML(ls)) return false; return contains(car(ls), "& 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 mklist(entryElementValues(car(e))); } /** * 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)); 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 ATOM 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 list() + element + entry + (list() + attribute + "xmlns" + "http://www.w3.org/2005/Atom") + (list() + element + "title" + (list() + attribute + "type" + "text") + title) + (list() + element + "id" + id) + (isNil(content)? list() : append(list() + element + "content" + (list() + attribute + "type" + (text? "text" : "application/xml")), text? mklist(elementValue(content)) : elementChildren(content))) + (list() + element + "link" + (list() + attribute + "href" + id)); } /** * Convert a list of element 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 element values representing an ATOM entry to an ATOM entry. */ template const failable writeATOMEntry(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 > 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 element values representing an ATOM feed to an ATOM feed. */ template const failable writeATOMFeed(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 f = list() + element + feed + (list() + attribute + "xmlns" + "http://www.w3.org/2005/Atom") + (list() + element + "title" + (list() + attribute + "type" + "text") + t) + (list() + element + "id" + i); // Write ATOM entries const list le = filter(selector(mklist(element, entry)), l); if (isNil(le)) return writeXML(reduce, initial, mklist(f)); // Write a single ATOM entry element with a list of values if (!isNil(le) && !isNil(car(le)) && isList(car(caddr(car(le))))) { const list fe = append(f, entriesElements(caddr(car(le)))); return writeXML(reduce, initial, mklist(fe)); } // Write separate ATOM entry elements const list fe = append(f, entriesElements(le)); return writeXML(reduce, initial, mklist(fe)); } /** * Convert a list of element values representing an ATOM feed to a list of strings. */ const failable > writeATOMFeed(const list& l) { const failable > ls = writeATOMFeed>(rcons, list(), l); if (!hasContent(ls)) return ls; return reverse(list(content(ls))); } } } #endif /* tuscany_atom_hpp */