Remove SOAP support
This commit is contained in:
@@ -1,147 +0,0 @@
|
||||
#include "examples/common/book_xml.h"
|
||||
|
||||
#include <cassert>
|
||||
#include <functional>
|
||||
#include <sstream>
|
||||
|
||||
#include "examples/common/book.h"
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
// Print a XML node to string.
|
||||
static std::string PrintXml(pugi::xml_node xnode, bool format_raw = true,
|
||||
const char* indent = "") {
|
||||
std::stringstream ss;
|
||||
unsigned int flags = format_raw ? pugi::format_raw : pugi::format_indent;
|
||||
xnode.print(ss, indent, flags);
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
bool XmlToBook(pugi::xml_node xbook, Book* book) {
|
||||
assert(xbook.name() == std::string("book"));
|
||||
|
||||
book->id = xbook.child("id").text().as_string();
|
||||
book->title = xbook.child("title").text().as_string();
|
||||
book->price = xbook.child("price").text().as_double();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void BookToXml(const Book& book, pugi::xml_node* xparent) {
|
||||
pugi::xml_node xbook = xparent->append_child("book");
|
||||
|
||||
xbook.append_child("id").text().set(book.id.c_str());
|
||||
xbook.append_child("title").text().set(book.title.c_str());
|
||||
xbook.append_child("price").text().set(book.price);
|
||||
}
|
||||
|
||||
bool XmlToBookList(pugi::xml_node xbooks, std::list<Book>* books) {
|
||||
assert(xbooks.name() == std::string("books"));
|
||||
|
||||
pugi::xml_node xbook = xbooks.child("book");
|
||||
|
||||
while (xbook) {
|
||||
Book book{
|
||||
xbook.child("id").text().as_string(),
|
||||
xbook.child("title").text().as_string(),
|
||||
xbook.child("price").text().as_double()
|
||||
};
|
||||
books->push_back(book);
|
||||
|
||||
xbook = xbook.next_sibling("book");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void BookListToXml(const std::list<Book>& books, pugi::xml_node* xparent) {
|
||||
pugi::xml_node xbooks = xparent->append_child("books");
|
||||
|
||||
for (const Book& book : books) {
|
||||
BookToXml(book, &xbooks);
|
||||
}
|
||||
}
|
||||
|
||||
bool XmlStringToBook(const std::string& xml_string, Book* book) {
|
||||
pugi::xml_document xdoc;
|
||||
if (!xdoc.load_string(xml_string.c_str())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
pugi::xml_node xbook = xdoc.document_element();
|
||||
if (!xbook) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (xbook.name() != std::string("book")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return XmlToBook(xbook, book);
|
||||
}
|
||||
|
||||
std::string BookToXmlString(const Book& book, bool format_raw,
|
||||
const char* indent) {
|
||||
pugi::xml_document xdoc;
|
||||
BookToXml(book, &xdoc);
|
||||
return PrintXml(xdoc);
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
std::string NewRequestXml(const Book& book) {
|
||||
pugi::xml_document xdoc;
|
||||
|
||||
pugi::xml_node xwebcc = xdoc.append_child("webcc");
|
||||
xwebcc.append_attribute("type") = "request";
|
||||
|
||||
BookToXml(book, &xwebcc);
|
||||
|
||||
return PrintXml(xdoc, false, " ");
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
static std::string __NewResultXml(int code, const char* message,
|
||||
std::function<void(pugi::xml_node*)> callback) {
|
||||
pugi::xml_document xdoc;
|
||||
|
||||
pugi::xml_node xwebcc = xdoc.append_child("webcc");
|
||||
xwebcc.append_attribute("type") = "response";
|
||||
|
||||
pugi::xml_node xstatus = xwebcc.append_child("status");
|
||||
xstatus.append_attribute("code") = code;
|
||||
xstatus.append_attribute("message") = message;
|
||||
|
||||
if (callback) {
|
||||
callback(&xwebcc);
|
||||
}
|
||||
|
||||
return PrintXml(xdoc, false, " ");
|
||||
}
|
||||
|
||||
std::string NewResultXml(int code, const char* message) {
|
||||
return __NewResultXml(code, message, {});
|
||||
}
|
||||
|
||||
std::string NewResultXml(int code, const char* message, const char* node,
|
||||
const char* key, const char* value) {
|
||||
auto callback = [node, key, value](pugi::xml_node* xparent) {
|
||||
pugi::xml_node xnode = xparent->append_child(node);
|
||||
xnode.append_child(key).text() = value;
|
||||
};
|
||||
return __NewResultXml(code, message, callback);
|
||||
}
|
||||
|
||||
std::string NewResultXml(int code, const char* message, const Book& book) {
|
||||
return __NewResultXml(code, message,
|
||||
std::bind(BookToXml, book, std::placeholders::_1));
|
||||
}
|
||||
|
||||
std::string NewResultXml(int code, const char* message,
|
||||
const std::list<Book>& books) {
|
||||
return __NewResultXml(code, message,
|
||||
std::bind(BookListToXml, books, std::placeholders::_1));
|
||||
}
|
||||
@@ -1,111 +0,0 @@
|
||||
#ifndef EXAMPLE_COMMON_BOOK_XML_H_
|
||||
#define EXAMPLE_COMMON_BOOK_XML_H_
|
||||
|
||||
#include <list>
|
||||
#include <string>
|
||||
|
||||
#include "pugixml/pugixml.hpp"
|
||||
|
||||
struct Book;
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
// Convert the following XML node to a book object.
|
||||
// <book>
|
||||
// <id>1</id>
|
||||
// <title>1984</title>
|
||||
// <price>12.3</price>
|
||||
// </book>
|
||||
bool XmlToBook(pugi::xml_node xbook, Book* book);
|
||||
|
||||
// Convert a book object to XML and append to the given parent.
|
||||
void BookToXml(const Book& book, pugi::xml_node* xparent);
|
||||
|
||||
// Convert the following XML node to a list of book objects.
|
||||
// <books>
|
||||
// <book>
|
||||
// <id>1</id>
|
||||
// <title>1984</title>
|
||||
// <price>12.3</price>
|
||||
// </book>
|
||||
// ...
|
||||
// </books>
|
||||
bool XmlToBookList(pugi::xml_node xbooks, std::list<Book>* books);
|
||||
|
||||
// Convert a list of book objects to XML and append to the given parent.
|
||||
void BookListToXml(const std::list<Book>& books, pugi::xml_node* xparent);
|
||||
|
||||
// Convert the following XML string to a book object.
|
||||
// <book>
|
||||
// <id>1</id>
|
||||
// <title>1984</title>
|
||||
// <price>12.3</price>
|
||||
// </book>
|
||||
bool XmlStringToBook(const std::string& xml_string, Book* book);
|
||||
|
||||
// Convert a book object to XML string.
|
||||
std::string BookToXmlString(const Book& book, bool format_raw = true,
|
||||
const char* indent = "");
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
// This example defines its own result XML which will be embedded into the SOAP
|
||||
// envolope as CDATA. The general schema of this result XML is:
|
||||
// <webcc type = "result">
|
||||
// <status code = "{code}" message = "{message}">
|
||||
// </webcc>
|
||||
// The "status" node is mandatory, you should define proper status codes and
|
||||
// messages according to your needs.
|
||||
// Additional data is attached as the sibling of "status" node, e.g.,
|
||||
// <webcc type = "result">
|
||||
// <status code = "{code}" message = "{message}">
|
||||
// <book>
|
||||
// <id>{book.id}</id>
|
||||
// <title>{book.title}</title>
|
||||
// <price>{book.price}</price>
|
||||
// </book>
|
||||
// </webcc>
|
||||
|
||||
// Create a result XML as below:
|
||||
// <webcc type = "result">
|
||||
// <status code = "{code}" message = "{message}">
|
||||
// </webcc>
|
||||
std::string NewResultXml(int code, const char* message);
|
||||
|
||||
// Create a result XML as below:
|
||||
// <webcc type = "result">
|
||||
// <status code = "{code}" message = "{message}">
|
||||
// <{node}>
|
||||
// <{key}>{value}</{key}>
|
||||
// </{node}>
|
||||
// </webcc>
|
||||
std::string NewResultXml(int code, const char* message, const char* node,
|
||||
const char* key, const char* value);
|
||||
|
||||
// Create a result XML as below:
|
||||
// <webcc type = "result">
|
||||
// <status code = "{code}" message = "{message}">
|
||||
// <book>
|
||||
// <id>{book.id}</id>
|
||||
// <title>{book.title}</title>
|
||||
// <price>{book.price}</price>
|
||||
// </book>
|
||||
// </webcc>
|
||||
std::string NewResultXml(int code, const char* message, const Book& book);
|
||||
|
||||
// Create a result XML as below:
|
||||
// <webcc type = "result">
|
||||
// <status code = "{code}" message = "{message}">
|
||||
// <books>
|
||||
// <book>
|
||||
// <id>{book.id}</id>
|
||||
// <title>{book.title}</title>
|
||||
// <price>{book.price}</price>
|
||||
// </book>
|
||||
// ...
|
||||
// </books>
|
||||
// </webcc>
|
||||
std::string NewResultXml(int code, const char* message,
|
||||
const std::list<Book>& books);
|
||||
|
||||
#endif // EXAMPLE_COMMON_BOOK_XML_H_
|
||||
Reference in New Issue
Block a user