diff options
Diffstat (limited to 'cpp/sca/modules/atom')
-rw-r--r-- | cpp/sca/modules/atom/atom-test.cpp | 72 | ||||
-rw-r--r-- | cpp/sca/modules/atom/atom.hpp | 50 |
2 files changed, 85 insertions, 37 deletions
diff --git a/cpp/sca/modules/atom/atom-test.cpp b/cpp/sca/modules/atom/atom-test.cpp index 0cef6b3b60..618f534001 100644 --- a/cpp/sca/modules/atom/atom-test.cpp +++ b/cpp/sca/modules/atom/atom-test.cpp @@ -31,8 +31,9 @@ #include "atom.hpp" namespace tuscany { +namespace atom { -std::ostringstream* atomWriter(std::ostringstream* os, const std::string& s) { +std::ostringstream* writer(std::ostringstream* os, const std::string& s) { (*os) << s; return os; } @@ -49,22 +50,49 @@ std::string itemEntry("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" "<link href=\"cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b\"/>" "</entry>\n"); -bool testATOMEntry() { +std::string incompleteEntry("<entry xmlns=\"http://www.w3.org/2005/Atom\">" + "<title>item</title><content type=\"text/xml\">" + "<Item xmlns=\"http://services/\">" + "<name xmlns=\"\">Orange</name>" + "<price xmlns=\"\">3.55</price>" + "</Item>" + "</content>" + "</entry>"); + +std::string completedEntry("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<entry xmlns=\"http://www.w3.org/2005/Atom\">" + "<title type=\"text\">item</title>" + "<id></id>" + "<content type=\"application/xml\">" + "<Item xmlns=\"http://services/\">" + "<name xmlns=\"\">Orange</name>" + "<price xmlns=\"\">3.55</price>" + "</Item>" + "</content><link href=\"\"/>" + "</entry>\n"); + +bool testEntry() { { const list<value> i = list<value>() << element << "item" << (list<value>() << element << "name" << std::string("Apple")) << (list<value>() << element << "price" << std::string("$2.99")); const list<value> a = mklist<value>(std::string("item"), std::string("cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b"), i); std::ostringstream os; - writeATOMEntry<std::ostringstream*>(atomWriter, &os, a); + writeEntry<std::ostringstream*>(writer, &os, a); assert(os.str() == itemEntry); } { - const list<value> a = readATOMEntry(mklist(itemEntry)); + const list<value> a = readEntry(mklist(itemEntry)); std::ostringstream os; - writeATOMEntry<std::ostringstream*>(atomWriter, &os, a); + writeEntry<std::ostringstream*>(writer, &os, a); assert(os.str() == itemEntry); } + { + const list<value> a = readEntry(mklist(incompleteEntry)); + std::ostringstream os; + writeEntry<std::ostringstream*>(writer, &os, a); + assert(os.str() == completedEntry); + } return true; } @@ -100,16 +128,16 @@ std::string itemFeed("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" "</entry>" "</feed>\n"); -bool testATOMFeed() { +bool testFeed() { { std::ostringstream os; - writeATOMFeed<std::ostringstream*>(atomWriter, &os, mklist<value>("Feed", "1234")); + writeFeed<std::ostringstream*>(writer, &os, mklist<value>("Feed", "1234")); assert(os.str() == emptyFeed); } { - const list<value> a = readATOMFeed(mklist(emptyFeed)); + const list<value> a = readFeed(mklist(emptyFeed)); std::ostringstream os; - writeATOMFeed<std::ostringstream*>(atomWriter, &os, a); + writeFeed<std::ostringstream*>(writer, &os, a); assert(os.str() == emptyFeed); } { @@ -124,25 +152,41 @@ bool testATOMFeed() { << (list<value>() << element << "price" << "$3.55"))); const list<value> a = cons<value>("Feed", cons<value>("1234", i)); std::ostringstream os; - writeATOMFeed<std::ostringstream*>(atomWriter, &os, a); + writeFeed<std::ostringstream*>(writer, &os, a); assert(os.str() == itemFeed); } { - const list<value> a = readATOMFeed(mklist(itemFeed)); + const list<value> i = list<value>() + << (list<value>() << "item" << "cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b" + << valueToElement(list<value>() << "item" + << (list<value>() << "name" << "Apple") + << (list<value>() << "price" << "$2.99"))) + << (list<value>() << "item" << "cart-53d67a61-aa5e-4e5e-8401-39edeba8b83c" + << valueToElement(list<value>() << "item" + << (list<value>() << "name" << "Orange") + << (list<value>() << "price" << "$3.55"))); + const list<value> a = cons<value>("Feed", cons<value>("1234", i)); std::ostringstream os; - writeATOMFeed<std::ostringstream*>(atomWriter, &os, a); + writeFeed<std::ostringstream*>(writer, &os, a); + assert(os.str() == itemFeed); + } + { + const list<value> a = readFeed(mklist(itemFeed)); + std::ostringstream os; + writeFeed<std::ostringstream*>(writer, &os, a); assert(os.str() == itemFeed); } return true; } } +} int main() { std::cout << "Testing..." << std::endl; - tuscany::testATOMEntry(); - tuscany::testATOMFeed(); + tuscany::atom::testEntry(); + tuscany::atom::testFeed(); std::cout << "OK" << std::endl; diff --git a/cpp/sca/modules/atom/atom.hpp b/cpp/sca/modules/atom/atom.hpp index 5bb8d45e38..78b60da70f 100644 --- a/cpp/sca/modules/atom/atom.hpp +++ b/cpp/sca/modules/atom/atom.hpp @@ -33,40 +33,43 @@ #include "xml.hpp" namespace tuscany { +namespace atom { /** * Convert a list of elements to a list of values representing an ATOM entry. */ -const list<value> atomEntry(const list<value>& e) { - const list<value> t = filter<value>(selector(mklist<value>(element, "title")), e); - const list<value> i = filter<value>(selector(mklist<value>(element, "id")), e); - const list<value> c = filter<value>(selector(mklist<value>(element, "content")), e); - return mklist<value>(elementValue(car(t)), elementValue(car(i)), cadr(elementChildren(car(c)))); +const list<value> entry(const list<value>& e) { + const list<value> lt = filter<value>(selector(mklist<value>(element, "title")), e); + const value t = isNil(lt)? value(std::string("")) : elementValue(car(lt)); + const list<value> li = filter<value>(selector(mklist<value>(element, "id")), e); + const value i = isNil(li)? value(std::string("")) : elementValue(car(li)); + const list<value> lc = filter<value>(selector(mklist<value>(element, "content")), e); + return mklist<value>(t, i, cadr(elementChildren(car(lc)))); } /** * Convert a list of elements to a list of values representing ATOM entries. */ -const list<value> atomEntries(const list<value>& e) { +const list<value> entries(const list<value>& e) { if (isNil(e)) return list<value>(); - return cons<value>(atomEntry(car(e)), atomEntries(cdr(e))); + return cons<value>(entry(car(e)), entries(cdr(e))); } /** * Convert a list of strings to a list of values representing an ATOM entry. */ -const failable<list<value>, std::string> readATOMEntry(const list<std::string>& ilist) { +const failable<list<value>, std::string> readEntry(const list<std::string>& ilist) { const list<value> e = readXML(ilist); if (isNil(e)) return std::string("Empty entry"); - return atomEntry(car(e)); + return entry(car(e)); } /** * Convert a list of strings to a list of values representing an ATOM feed. */ -const failable<list<value>, std::string> readATOMFeed(const list<std::string>& ilist) { +const failable<list<value>, std::string> readFeed(const list<std::string>& ilist) { const list<value> f = readXML(ilist); if (isNil(f)) return std::string("Empty feed"); @@ -75,14 +78,14 @@ const failable<list<value>, std::string> readATOMFeed(const list<std::string>& i const list<value> e = filter<value>(selector(mklist<value>(element, "entry")), car(f)); if (isNil(e)) return mklist<value>(elementValue(car(t)), elementValue(car(i))); - return cons<value>(elementValue(car(t)), cons(elementValue(car(i)), atomEntries(e))); + return cons<value>(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<value> atomEntryElement(const list<value>& l) { +const list<value> entryElement(const list<value>& l) { return list<value>() << element << "entry" << (list<value>() << attribute << "xmlns" << "http://www.w3.org/2005/Atom") << (list<value>() << element << "title" << (list<value>() << attribute << "type" << "text") << car(l)) @@ -94,30 +97,30 @@ const list<value> atomEntryElement(const list<value>& l) { /** * Convert a list of values representing ATOM entries to a list of elements. */ -const list<value> atomEntriesElements(const list<value>& l) { +const list<value> entriesElements(const list<value>& l) { if (isNil(l)) return list<value>(); - return cons<value>(atomEntryElement(car(l)), atomEntriesElements(cdr(l))); + return cons<value>(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<typename R> const failable<R, std::string> writeATOMEntry(const lambda<R(R, std::string)>& reduce, const R& initial, const list<value>& l) { - return writeXML<R>(reduce, initial, mklist<value>(atomEntryElement(l))); +template<typename R> const failable<R, std::string> writeEntry(const lambda<R(R, std::string)>& reduce, const R& initial, const list<value>& l) { + return writeXML<R>(reduce, initial, mklist<value>(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<std::string> writeATOMList(const list<std::string>& listSoFar, const std::string& s) { +const list<std::string> writeStrings(const list<std::string>& listSoFar, const std::string& s) { return cons(s, listSoFar); } -const failable<list<std::string>, std::string> writeATOMEntry(const list<value>& l) { - const failable<list<std::string>, std::string> ls = writeATOMEntry<list<std::string> >(writeATOMList, list<std::string>(), l); +const failable<list<std::string>, std::string> writeEntry(const list<value>& l) { + const failable<list<std::string>, std::string> ls = writeEntry<list<std::string> >(writeStrings, list<std::string>(), l); if (!hasValue(ls)) return ls; return reverse(list<std::string>(ls)); @@ -127,14 +130,14 @@ const failable<list<std::string>, std::string> writeATOMEntry(const list<value>& * 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<typename R> const failable<R, std::string> writeATOMFeed(const lambda<R(R, std::string)>& reduce, const R& initial, const list<value>& l) { +template<typename R> const failable<R, std::string> writeFeed(const lambda<R(R, std::string)>& reduce, const R& initial, const list<value>& l) { const list<value> f = list<value>() << element << "feed" << (list<value>() << attribute << "xmlns" << "http://www.w3.org/2005/Atom") << (list<value>() << element << "title" << (list<value>() << attribute << "type" << "text") << car(l)) << (list<value>() << element << "id" << cadr(l)); if (isNil(cddr(l))) return writeXML<R>(reduce, initial, mklist<value>(f)); - const list<value> fe = append(f, atomEntriesElements(cddr(l))); + const list<value> fe = append(f, entriesElements(cddr(l))); return writeXML<R>(reduce, initial, mklist<value>(fe)); } @@ -142,13 +145,14 @@ template<typename R> const failable<R, std::string> writeATOMFeed(const lambda<R * 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<list<std::string>, std::string> writeATOMFeed(const list<value>& l) { - const failable<list<std::string>, std::string> ls = writeATOMFeed<list<std::string> >(writeATOMList, list<std::string>(), l); +const failable<list<std::string>, std::string> writeFeed(const list<value>& l) { + const failable<list<std::string>, std::string> ls = writeFeed<list<std::string> >(writeStrings, list<std::string>(), l); if (!hasValue(ls)) return ls; return reverse(list<std::string>(ls)); } } +} #endif /* tuscany_atom_hpp */ |