/* * 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/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 = elementChildren("title", e); const list t = nilListValue + element + value("title") + (isNull(lt) || !elementHasValue(car(lt))? value(emptyString) : elementValue(car(lt))); const list li = elementChildren("id", e); const list i = nilListValue + element + value("id") + (isNull(li)? value(emptyString) : elementValue(car(li))); const list la = elementChildren("author", e); const list lan = isNull(la)? nilListValue : elementChildren("name", car(la)); const list lae = isNull(la)? nilListValue : elementChildren("email", car(la)); const list laa = isNull(lan)? lae : lan; const list a = isNull(laa)? nilListValue : mklist(nilListValue + element + value("author") + elementValue(car(laa))); const list lu = elementChildren("updated", e); const list u = isNull(lu)? nilListValue : mklist(nilListValue + element + value("updated") + elementValue(car(lu))); const list lr = elementChildren("rank", e); const list r = isNull(lr)? nilListValue : mklist(nilListValue + element + value("rank") + elementValue(car(lr))); const list lc = elementChildren("content", e); const list c = isNull(lc)? nilListValue : isAttribute(elementValue(car(lc)))? nilListValue : mklist(nilListValue + element + value("content") + elementValue(car(lc))); return append(append(append(append(nilListValue + element + entry + value(t) + value(i), a), u), r), c); } /** * Convert a list of elements to a list of element values representing ATOM entries. */ const list entriesElementValues(const list& e) { if (isNull(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 (!xml::isXML(ls)) return false; return contains(car(ls), "& ls) { if (!xml::isXML(ls)) return false; return contains(car(ls), " > readATOMEntry(const list& ilist) { const list e = content(xml::readElements(ilist)); if (isNull(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 = content(xml::readElements(ilist)); if (isNull(f)) return mkfailure >("Empty feed"); const list t = elementChildren("title", car(f)); const list i = elementChildren("id", car(f)); const list e = elementChildren(entry, car(f)); return mklist(append(nilListValue + element + feed + value(nilListValue + element + value("title") + elementValue(car(t))) + value(nilListValue + element + value("id") + elementValue(car(i))), entriesElementValues(e))); } /** * Returns children of an ATOM content element. */ const list contentElementChildren(const value& content) { return filter([](const value& v) { return !(isAttribute(v) && attributeName((list)v) == "type"); }, elementChildren(content)); } /** * Convert a list of element values representing an ATOM entry to a list of elements. */ const list entryElement(const list& l) { const value title = elementChild("title", l); const value id = elementChild("id", l); const value author = elementChild("author", l); const bool email = isNull(author)? false : contains(elementValue(author), "@"); const value updated = elementChild("updated", l); const value rank = elementChild("rank", l); const value content = elementChild("content", l); const bool text = isNull(content)? false : elementHasValue(content); return nilListValue + element + entry + (nilListValue + attribute + "xmlns" + "http://www.w3.org/2005/Atom") + (nilListValue + element + "title" + (nilListValue + attribute + "type" + "text") + (elementHasValue(title)? elementValue(title) : value(""))) + (nilListValue + element + "id" + elementValue(id)) + (isNull(author)? nilListValue : (nilListValue + element + "author" + (email? (nilListValue + element + "email" + elementValue(author)) : (nilListValue + element + "name" + elementValue(author))))) + (isNull(updated)? nilListValue : (nilListValue + element + "updated" + elementValue(updated))) + (isNull(rank)? nilListValue : (nilListValue + element + "rank" + elementValue(rank))) + (isNull(content)? nilListValue : append(nilListValue + element + "content" + (nilListValue + attribute + "type" + (text? "text" : "application/xml")), text? mklist(elementValue(content)) : contentElementChildren(content))) + (nilListValue + element + "link" + (nilListValue + attribute + "href" + elementValue(id))); } /** * Convert a list of element values representing ATOM entries to a list of elements. */ const list entriesElements(const list& l) { if (isNull(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 = isNull(ll)? ll : (list)car(ll); return xml::writeElements(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 = isNull(ll)? ll : (list)car(ll); const list lt = elementChildren("title", l); const value t = isNull(lt)? value(emptyString) : elementValue(car(lt)); const list li = elementChildren("id", l); const value i = isNull(li)? value(emptyString) : elementValue(car(li)); const list f = nilListValue + element + feed + (nilListValue + attribute + "xmlns" + "http://www.w3.org/2005/Atom") + (nilListValue + element + "title" + (nilListValue + attribute + "type" + "text") + t) + (nilListValue + element + "id" + i); // Write ATOM entries const list le = elementChildren(entry, l); if (isNull(le)) return xml::writeElements(reduce, initial, mklist(f)); // Write a single ATOM entry element with a list of values if (!isNull(le) && !isNull(car(le)) && isList(car(caddr(car(le))))) { const list fe = append(f, entriesElements(caddr(car(le)))); return xml::writeElements(reduce, initial, mklist(fe)); } // Write separate ATOM entry elements const list fe = append(f, entriesElements(le)); return xml::writeElements(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 */