diff options
author | Henri Herscher <henri@oreka.org> | 2005-10-20 13:40:58 +0000 |
---|---|---|
committer | Henri Herscher <henri@oreka.org> | 2005-10-20 13:40:58 +0000 |
commit | 7e1d63dd9fd149e4934bf77095c8610fac786b04 (patch) | |
tree | 5fe486a1b0300c3b84fb559107a868e5cc2c95da /orkbasecxx/serializers | |
parent | 467768fc956fc3e5a253373f26c71c681b31b6b8 (diff) |
First checkin
git-svn-id: https://oreka.svn.sourceforge.net/svnroot/oreka/trunk@2 09dcff7a-b715-0410-9601-b79a96267cd0
Diffstat (limited to 'orkbasecxx/serializers')
-rw-r--r-- | orkbasecxx/serializers/DomSerializer.cpp | 157 | ||||
-rw-r--r-- | orkbasecxx/serializers/DomSerializer.h | 81 | ||||
-rw-r--r-- | orkbasecxx/serializers/Makefile.am | 10 | ||||
-rw-r--r-- | orkbasecxx/serializers/Serializer.cpp | 312 | ||||
-rw-r--r-- | orkbasecxx/serializers/Serializer.h | 86 | ||||
-rw-r--r-- | orkbasecxx/serializers/SingleLineSerializer.cpp | 203 | ||||
-rw-r--r-- | orkbasecxx/serializers/SingleLineSerializer.h | 44 | ||||
-rw-r--r-- | orkbasecxx/serializers/UrlSerializer.cpp | 161 | ||||
-rw-r--r-- | orkbasecxx/serializers/UrlSerializer.h | 44 | ||||
-rw-r--r-- | orkbasecxx/serializers/XmlRpcSerializer.cpp | 47 | ||||
-rw-r--r-- | orkbasecxx/serializers/XmlRpcSerializer.h | 45 |
11 files changed, 1190 insertions, 0 deletions
diff --git a/orkbasecxx/serializers/DomSerializer.cpp b/orkbasecxx/serializers/DomSerializer.cpp new file mode 100644 index 0000000..348819e --- /dev/null +++ b/orkbasecxx/serializers/DomSerializer.cpp @@ -0,0 +1,157 @@ +/* + * Oreka -- A media capture and retrieval platform + * + * Copyright (C) 2005, orecx LLC + * + * http://www.orecx.com + * + * This program is free software, distributed under the terms of + * the GNU General Public License. + * Please refer to http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "xercesc/util/XMLString.hpp" +#include <xercesc/util/PlatformUtils.hpp> +#include <xercesc/dom/DOM.hpp> +#include <xercesc/dom/DOMElement.hpp> +#include <xercesc/dom/DOMImplementation.hpp> +#include <xercesc/dom/DOMImplementationLS.hpp> +#include <xercesc/dom/DOMWriter.hpp> +#include <xercesc/framework/MemBufFormatTarget.hpp> + +#include "DomSerializer.h" + +void DomSerializer::ObjectValue(const char* key, Object& value, bool required) +{ + if (m_deSerialize == true) + { + GetObject(key, value, required); + } + else + { + AddObject(key, value); + } +} + +void DomSerializer::GetString(const char* key, CStdString& value, bool required) +{ + // Find the right node + DOMNode* stringNode = FindElementByName(m_node, CStdString(key)); + + if(stringNode) + { + // Now, the string associated to element should be the first child (text element) + DOMNode* textNode = stringNode->getFirstChild(); + if (textNode && textNode->getNodeType() == DOMNode::TEXT_NODE) + { + value = XMLStringToLocal(textNode->getNodeValue()); + } + } + else if (required) + { + throw(CStdString("DomSerializer::GetString: required parameter missing:") + key); + } +} + +void DomSerializer::GetObject(const char* key, Object& value, bool required) +{ + // Find the node corresponding to the object wanting to be populated + DOMNode* objectNode = FindElementByName(m_node, CStdString(key)); + + // Create a new serializer and affect it to this object + if (objectNode) + { + DomSerializer serializer(&value); + serializer.DeSerialize(objectNode); + } + else if (required) + { + throw(CStdString("DomSerializer::GetObject: required node missing:") + key); + } +} + +void DomSerializer::AddObject(const char* key, Object& value) +{ + ; +} + +void DomSerializer::AddString(const char* key, CStdString& value) +{ + DOMElement* newElem = m_document->createElement(XStr(key).unicodeForm()); + m_node->appendChild(newElem); + + DOMText* newText = m_document->createTextNode(XStr((PCSTR)value).unicodeForm()); + newElem->appendChild(newText); +} + +void DomSerializer::Serialize(XERCES_CPP_NAMESPACE::DOMDocument* doc) +{ + if(doc) + { + m_document = doc; + m_node = m_document->getDocumentElement(); + m_deSerialize = false; // Set Serialize mode + m_object->Define(this); + } + else + { + throw(CStdString("DomSerializer::Serialize: input DOM document is NULL")); + } +} + +void DomSerializer::DeSerialize(DOMNode* node) +{ + m_node = node; + m_deSerialize = true; // Set DeSerialize mode + m_object->Define(this); + m_object->Validate(); +} + +CStdString DomSerializer::XMLStringToLocal(const XMLCh* const toTranscode) +{ + char* szResult = XMLString::transcode(toTranscode); + CStdString result = szResult; + XMLString::release(&szResult); + return result; +} + +DOMNode* DomSerializer::FindElementByName(DOMNode *node, CStdString name) +{ + DOMNode *child = node->getFirstChild(); + while(child) + { + if (XMLStringToLocal(child->getNodeName()) == name && child->getNodeType() == DOMNode::ELEMENT_NODE) + { + return child; + } + child = child->getNextSibling(); + } + return NULL; +} + +CStdString DomSerializer::DomNodeToString(DOMNode* node) +{ + CStdString output; + + DOMImplementation *impl = DOMImplementationRegistry::getDOMImplementation(XStr("LS").unicodeForm()); + DOMWriter *theSerializer = ((DOMImplementationLS*)impl)->createDOMWriter(); + // set user specified output encoding + //theSerializer->setEncoding(gOutputEncoding); + theSerializer->setFeature(XStr("format-pretty-print").unicodeForm(), true); + + XMLFormatTarget *myFormTarget; + myFormTarget = new MemBufFormatTarget (); + theSerializer->writeNode(myFormTarget, *node); + + output = (char *)((MemBufFormatTarget*)myFormTarget)->getRawBuffer(); + + // Clean up + delete theSerializer; + // + // Filter, formatTarget and error handler + // are NOT owned by the serializer. + delete myFormTarget; + + return output; +} diff --git a/orkbasecxx/serializers/DomSerializer.h b/orkbasecxx/serializers/DomSerializer.h new file mode 100644 index 0000000..4190d59 --- /dev/null +++ b/orkbasecxx/serializers/DomSerializer.h @@ -0,0 +1,81 @@ +/* + * Oreka -- A media capture and retrieval platform + * + * Copyright (C) 2005, orecx LLC + * + * http://www.orecx.com + * + * This program is free software, distributed under the terms of + * the GNU General Public License. + * Please refer to http://www.gnu.org/copyleft/gpl.html + * + */ + +#ifndef __DOMSERIALIZER_H__ +#define __DOMSERIALIZER_H__ + +#include "Serializer.h" +#include "Object.h" + +#include "xercesc/dom/DOMNode.hpp" +#include "xercesc/util/XMLString.hpp" + +using namespace XERCES_CPP_NAMESPACE; + +/** Serializer class for Document Object Model. + This class allows a nested object to be serialized or deserialized + to or from a xerces DOM object. +*/ +class DLL_IMPORT_EXPORT DomSerializer : public Serializer +{ +public: + DomSerializer(Object* object) : Serializer(object){}; + + void ObjectValue(const char* key, Object& value, bool required = false); + + void AddInt(const char* key, int value); + void AddString(const char* key, CStdString& value); + void AddObject(const char* key, Object& value); + void Serialize(XERCES_CPP_NAMESPACE::DOMDocument* node); + + void GetString(const char* key, CStdString& value, bool required = false); + void GetObject(const char* key, Object& value, bool required = false); + void DeSerialize(DOMNode* node); + + static CStdString XMLStringToLocal(const XMLCh* const toTranscode); + static XMLCh* LocalStringToXML(CStdString& toTranscode); + + DOMNode* FindElementByName(DOMNode *node, CStdString name); + + static CStdString DomNodeToString(DOMNode *); +protected: + DOMNode* m_node; + XERCES_CPP_NAMESPACE::DOMDocument* m_document; +}; + +/** Container for xerces unicode string initialized with char* string */ +class DLL_IMPORT_EXPORT XStr +{ +public : + inline XStr(const char* const toTranscode) + { + fUnicodeForm = XMLString::transcode(toTranscode); + } + + inline ~XStr() + { + XMLString::release(&fUnicodeForm); + } + + inline const XMLCh* unicodeForm() const + { + return fUnicodeForm; + } + +private : + XMLCh* fUnicodeForm; +}; + + +#endif + diff --git a/orkbasecxx/serializers/Makefile.am b/orkbasecxx/serializers/Makefile.am new file mode 100644 index 0000000..33bd167 --- /dev/null +++ b/orkbasecxx/serializers/Makefile.am @@ -0,0 +1,10 @@ +METASOURCES = AUTO +noinst_LTLIBRARIES = libserializers.la +libserializers_la_SOURCES = DomSerializer.cpp DomSerializer.h Serializer.cpp\ + Serializer.h SingleLineSerializer.cpp SingleLineSerializer.h\ + UrlSerializer.cpp UrlSerializer.h XmlRpcSerializer.cpp\ + XmlRpcSerializer.h +#libserializers_la_LIBADD = -L/projects/ext/xmlrpc++/xmlrpc++0.7/ -lXmlRpc +INCLUDES = -I@top_srcdir@ + +AM_CXXFLAGS = -D_REENTRANT diff --git a/orkbasecxx/serializers/Serializer.cpp b/orkbasecxx/serializers/Serializer.cpp new file mode 100644 index 0000000..8e571ed --- /dev/null +++ b/orkbasecxx/serializers/Serializer.cpp @@ -0,0 +1,312 @@ +/* + * Oreka -- A media capture and retrieval platform + * + * Copyright (C) 2005, orecx LLC + * + * http://www.orecx.com + * + * This program is free software, distributed under the terms of + * the GNU General Public License. + * Please refer to http://www.gnu.org/copyleft/gpl.html + * + */ + +#define _WINSOCKAPI_ // prevents the inclusion of winsock.h + +#include "ace/OS_NS_time.h" +#include "Object.h" +#include "Serializer.h" +#include "Utils.h" + +using namespace XERCES_CPP_NAMESPACE; + +Serializer::Serializer(Object* object) +{ + m_object = object; +} + +void Serializer::IntValue(const char* key, int& value, bool required) +{ + if (m_deSerialize == true) + { + GetInt(key, value, required); + } + else + { + AddInt(key, value); + } +} + +void Serializer::DoubleValue(const char* key, double& value, bool required) +{ + if (m_deSerialize == true) + { + GetDouble(key, value, required); + } + else + { + AddDouble(key, value); + } +} + +void Serializer::BoolValue(const char* key, bool& value, bool required) +{ + if (m_deSerialize == true) + { + GetBool(key, value, required); + } + else + { + AddBool(key, value); + } +} + +void Serializer::StringValue(const char* key, CStdString& value, bool required) +{ + if (m_deSerialize == true) + { + GetString(key, value, required); + } + else + { + AddString(key, value); + } +} + +void Serializer::EnumValue(const char* key, int& value, StringToEnumFunction toEnum, EnumToStringFunction toString, bool required) +{ + if (m_deSerialize == true) + { + GetEnum(key, value, toEnum, required); + } + else + { + AddEnum(key, value, toString); + } +} + +void Serializer::CsvValue(const char* key, std::list<CStdString>& value, bool required) +{ + if (m_deSerialize == true) + { + GetCsv(key, value, required); + } + else + { + AddCsv(key, value); + } +} + +void Serializer::DateValue(const char* key, time_t& value, bool required) +{ + if (m_deSerialize == true) + { + GetDate(key, value, required); + } + else + { + AddDate(key, value); + } +} + + +//===================================== +void Serializer::AddInt(const char* key, int value) +{ + CStdString valueString = IntToString(value); + AddString(key, valueString); +} + +void Serializer::AddDouble(const char* key, double value) +{ + CStdString valueString = DoubleToString(value); + AddString(key, valueString); +} + +void Serializer::AddBool(const char* key, bool value) +{ + if(value) + { + CStdString trueString("true"); + AddString(key, trueString); + } + else + { + CStdString falseString("false"); + AddString(key, falseString); + } +} + +void Serializer::AddEnum(const char* key, int value, EnumToStringFunction function) +{ + if(!function) + { + throw(CStdString("Serializer: wrong enumerated type conversion function for parameter:") + key); + } + else + { + CStdString valueString = function(value); + AddString(key, valueString); + } +} + +void Serializer::AddCsv(const char* key, std::list<CStdString>& value) +{ + CStdString csvString; + bool first = true; + for(std::list<CStdString>::iterator it = value.begin(); it!=value.end(); it++) + { + if(!first) + { + csvString += ","; + } + first = false; + csvString += *it; + } + AddString(key, csvString); +} + +void Serializer::AddDate(const char* key, time_t value) +{ + struct tm date; + ACE_OS::localtime_r(&value ,&date); + int month = date.tm_mon + 1; // january=0, decembre=11 + int year = date.tm_year + 1900; + CStdString dateString; + dateString.Format("%.4d-%.2d-%.2d_%.2d-%.2d-%.2d", year, month, date.tm_mday, date.tm_hour, date.tm_min, date.tm_sec); + AddString(key, dateString); +} + + +//==================================================================== +void Serializer::GetInt(const char* key, int&value, bool required) +{ + CStdString stringValue; + GetString(key, stringValue, required); + if(!stringValue.IsEmpty()) + { + value = StringToInt(stringValue); + } +} + +void Serializer::GetDouble(const char* key, double&value, bool required) +{ + CStdString stringValue; + GetString(key, stringValue, required); + if(!stringValue.IsEmpty()) + { + value = StringToDouble(stringValue); + } +} + +void Serializer::GetBool(const char* key, bool&value, bool required) +{ + CStdString stringValue; + GetString(key, stringValue, required); + stringValue.ToLower(); + if( stringValue == "true" || stringValue == "yes" || stringValue == "1" ) + { + value = true; + } + else if( stringValue == "false" || stringValue == "no" || stringValue == "0" ) + { + value = false; + } + else if( stringValue.IsEmpty() ) + { + ; // do not touch default value + } + else + { + throw(CStdString("Serializer: Invalid boolean value:") + stringValue + " for parameter:" + key); + } +} + +void Serializer::GetEnum(const char* key, int& value, StringToEnumFunction function, bool required) +{ + if(!function) + { + throw(CStdString("Serializer: missing enumerated type conversion function for parameter:") + key); + } + else + { + CStdString enumStringValue; + GetString(key, enumStringValue, required); + if (!enumStringValue.IsEmpty()) + { + value = function(enumStringValue); + } + } + +} + +void Serializer::GetCsv(const char* key, std::list<CStdString>& value, bool required) +{ + CStdString stringValue; + stringValue.Trim(); + CStdString element; + bool first = true; + GetString(key, stringValue, required); + for(int i=0; i<stringValue.length(); i++) + { + TCHAR c = stringValue[i]; + if(c == ',') + { + if(first) + { + first = false; // only erase default value if something found + value.clear(); + } + element.Trim(); + value.push_back(element); + element.Empty(); + } + else + { + element += c; + } + } + if (!element.IsEmpty()) + { + if(first) + { + first = false; // only erase default value if something found + value.clear(); + } + element.Trim(); + value.push_back(element); + } +} + +void Serializer::GetDate(const char* key, time_t& value, bool required) +{ + throw (CStdString("DeSerializer: GetDate: not implemented yet")); + + CStdString stringValue; + GetString(key, stringValue, required); + if(!stringValue.IsEmpty()) + { + //value = ; + } +} + +//========================================================== + +void KeyValueSerializer::GetString(const char* key, CStdString& value, bool required) +{ + std::map<CStdString, CStdString>::iterator it = m_map.find(CStdString(key)); + if (it != m_map.end()) + { + value = it->second; + } + else if (required == true) + { + throw CStdString(CStdString("Serializer::GetString: required parameter missing:") + key); + } +} + +void KeyValueSerializer::ObjectValue(const char* key, Object& value, bool required) +{ + throw CStdString(CStdString("KeyValueSerializer::ObjectValue: Nested objects not allowed for key-value serializers")); +} + diff --git a/orkbasecxx/serializers/Serializer.h b/orkbasecxx/serializers/Serializer.h new file mode 100644 index 0000000..a63af1a --- /dev/null +++ b/orkbasecxx/serializers/Serializer.h @@ -0,0 +1,86 @@ +/* + * Oreka -- A media capture and retrieval platform + * + * Copyright (C) 2005, orecx LLC + * + * http://www.orecx.com + * + * This program is free software, distributed under the terms of + * the GNU General Public License. + * Please refer to http://www.gnu.org/copyleft/gpl.html + * + */ + +#ifndef __SERIALIZER_H__ +#define __SERIALIZER_H__ + +#pragma warning( disable: 4786 ) // disables truncated symbols in browse-info warning + +#include "OrkBase.h" +#include <map> +#include <list> +#include "boost/shared_ptr.hpp" + +typedef int (*StringToEnumFunction)(CStdString&); +typedef CStdString (*EnumToStringFunction)(int); + +class Object; + +/** Base class for serializing Objects. +*/ +class DLL_IMPORT_EXPORT Serializer +{ +public: + Serializer(Object* object); + + void IntValue(const char* key, int& value, bool required = false); + void DoubleValue(const char* key, double& value, bool required = false); + void StringValue(const char* key, CStdString& value, bool required = false); + void BoolValue(const char* key, bool& value, bool required = false); + void EnumValue(const char* key, int& value, StringToEnumFunction, EnumToStringFunction, bool required = false); + virtual void ObjectValue(const char* key, Object& value, bool required = false) = 0; + void CsvValue(const char* key, std::list<CStdString>& value, bool required = false); + void DateValue(const char* key, time_t& value, bool required = false); + + void AddInt(const char* key, int value); + void AddDouble(const char* key, double value); + void AddBool(const char* key, bool value); + void AddEnum(const char* key, int value, EnumToStringFunction); + void AddCsv(const char* key, std::list<CStdString>& value); + void AddDate(const char* key, time_t value); + virtual void AddString(const char* key, CStdString& value) = 0; + + void GetInt(const char* key, int& value, bool required = false); + void GetDouble(const char* key, double& value, bool required = false); + void GetBool(const char* key, bool& value, bool required = false); + void GetEnum(const char* key, int& value, StringToEnumFunction, bool required = false); + void GetCsv(const char* key, std::list<CStdString>& value, bool required = false); + void GetDate(const char* key, time_t& value, bool required = false); + virtual void GetString(const char* key, CStdString& value, bool required = false) = 0; + +protected: + + Object* m_object; + bool m_deSerialize; +}; + +typedef boost::shared_ptr<Serializer> SerializerRef; + +/** Base class for all Key-Value pair based serializers. +*/ +class DLL_IMPORT_EXPORT KeyValueSerializer : public Serializer +{ +public: + KeyValueSerializer(Object* object) : Serializer(object), m_numParams(0){}; + + void ObjectValue(const char* key, Object& value, bool required = false); + + void GetString(const char* key, CStdString& value, bool required = false); + +protected: + int m_numParams; + std::map<CStdString, CStdString> m_map; +}; + +#endif + diff --git a/orkbasecxx/serializers/SingleLineSerializer.cpp b/orkbasecxx/serializers/SingleLineSerializer.cpp new file mode 100644 index 0000000..11b60d9 --- /dev/null +++ b/orkbasecxx/serializers/SingleLineSerializer.cpp @@ -0,0 +1,203 @@ +/* + * Oreka -- A media capture and retrieval platform + * + * Copyright (C) 2005, orecx LLC + * + * http://www.orecx.com + * + * This program is free software, distributed under the terms of + * the GNU General Public License. + * Please refer to http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "ace/OS_NS_ctype.h" +#include "Utils.h" +#include "SingleLineSerializer.h" + + +void SingleLineSerializer::AddString(const char* key, CStdString& value) +{ + CStdString pair; + CStdString escapedValue; + EscapeSingleLine(value, escapedValue); + pair.Format("%s=%s ", key, (PCSTR)escapedValue); + m_output += pair; +} + +CStdString SingleLineSerializer::Serialize() +{ + m_deSerialize = false; // Set Serialize mode + m_object->Define(this); + return m_output; +} + +typedef enum {SingleLineStartState, SingleLineKeyState, SingleLineValueState, SingleLineErrorState} SingleLineState; + +void SingleLineSerializer::DeSerialize(CStdString& input) +{ + // read string and extract values into map + SingleLineState state = SingleLineStartState; + CStdString key; + CStdString value; + // #### Additionally, errorDescription should be converted to Exceptions + CStdString errorDescription; + + input.Trim(); + + for(int i=0; i<input.length() && state!= SingleLineErrorState; i++) + { + TCHAR character = input[i]; + + switch(state) + { + case SingleLineStartState: + if(character == ' ') + { + ; // ignore spaces + } + else if ( ACE_OS::ace_isalnum(character) ) + { + state = SingleLineKeyState; + key = character; + } + else + { + state = SingleLineErrorState; + errorDescription = "Cannot find key start, keys must be alphanum"; + } + break; + case SingleLineKeyState: + if( ACE_OS::ace_isalnum(character) ) + { + key += character; + } + else if (character == '=') + { + state = SingleLineValueState; + value.Empty(); + } + else + { + state = SingleLineErrorState; + errorDescription = "Invalid key character, keys must be alphanum"; + } + break; + case SingleLineValueState: + if( character == '=') + { + state = SingleLineErrorState; + errorDescription = "Value followed by = sign, value should always be followed by space sign"; + } + else if (character == ' ') + { + state = SingleLineStartState; + } + else + { + value += character; + } + break; + default: + state = SingleLineErrorState; + errorDescription = "Non-existing state"; + } // switch(state) + + if ( (state == SingleLineStartState) || (i == (input.length()-1)) ) + { + if (!key.IsEmpty()) + { + // SingleLine unescape + CStdString unescapedValue; + UnEscapeSingleLine(value, unescapedValue); + + // Add pair to key-value map + m_map.insert(std::make_pair(key, unescapedValue)); + key.Empty(); + value.Empty(); + } + } + } // for(int i=0; i<input.length() && state!= SingleLineErrorState; i++) + + m_deSerialize = true; // Set DeSerialize mode + m_object->Define(this); + m_object->Validate(); +} + +// Escape the space, colon and percent characters for serializing to Key-Value-Pair text +void SingleLineSerializer::EscapeSingleLine(CStdString& in, CStdString& out) +{ + for(int i=0; i<in.length();i++) + { + TCHAR c = in[i]; + if (c == ' ') + { + out+= "%s"; + } + else if (c == ':') + { + out+= "%c"; + } + else if (c == '%') + { + out+= "%p"; + } + else + { + out+= c; + } + } +} + +// Unescape the space, colon and percent characters for serializing to Key-Value-Pair text +void SingleLineSerializer::UnEscapeSingleLine(CStdString& in, CStdString& out) +{ + int iin = 0; + + while(iin<in.length()) + { + if ( in[iin] == '%') + { + iin++; + + switch (in[iin]) + { + case 's': + out += ' '; + break; + case 'c': + out += ':'; + break; + case 'p': + out += '%'; + break; + } + } + else + { + out += in[iin]; + } + iin++; + } +} + +CStdString SingleLineSerializer::FindClass(CStdString& input) +{ + CStdString result; + int equalsPostion = input.Find('='); + if (equalsPostion != -1) + { + int spacePostion = input.Find(' '); + if (spacePostion > equalsPostion) + { + result = input.Mid(equalsPostion+1, spacePostion-equalsPostion-1); + } + else + { + result = input.Mid(equalsPostion+1, input.GetLength() - equalsPostion - 1); + } + } + result.ToLower(); + return result; +} + diff --git a/orkbasecxx/serializers/SingleLineSerializer.h b/orkbasecxx/serializers/SingleLineSerializer.h new file mode 100644 index 0000000..62ada27 --- /dev/null +++ b/orkbasecxx/serializers/SingleLineSerializer.h @@ -0,0 +1,44 @@ +/* + * Oreka -- A media capture and retrieval platform + * + * Copyright (C) 2005, orecx LLC + * + * http://www.orecx.com + * + * This program is free software, distributed under the terms of + * the GNU General Public License. + * Please refer to http://www.gnu.org/copyleft/gpl.html + * + */ + +#ifndef __SINGLELINESERIALIZER_H__ +#define __SINGLELINESERIALIZER_H__ + +#include "messages/Message.h" +#include "serializers/Serializer.h" + +/** Serializer that generates and parses objects to and from a single line of text. + key-value pairs are separated by spaces. + key and values are separated by equal signs. + example: message=doit what=run +*/ +class DLL_IMPORT_EXPORT SingleLineSerializer : public KeyValueSerializer +{ +public: + SingleLineSerializer(Object* object) : KeyValueSerializer(object){}; + + void AddString(const char* key, CStdString& value); + CStdString Serialize(); + + void DeSerialize(CStdString& input); + + void EscapeSingleLine(CStdString& in, CStdString& out); + void UnEscapeSingleLine(CStdString& in, CStdString& out); + + static CStdString FindClass(CStdString& input); +private: + CStdString m_output; +}; + +#endif + diff --git a/orkbasecxx/serializers/UrlSerializer.cpp b/orkbasecxx/serializers/UrlSerializer.cpp new file mode 100644 index 0000000..1f33c18 --- /dev/null +++ b/orkbasecxx/serializers/UrlSerializer.cpp @@ -0,0 +1,161 @@ +/* + * Oreka -- A media capture and retrieval platform + * + * Copyright (C) 2005, orecx LLC + * + * http://www.orecx.com + * + * This program is free software, distributed under the terms of + * the GNU General Public License. + * Please refer to http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "ace/OS_NS_ctype.h" +#include "Utils.h" +#include "UrlSerializer.h" + + +void UrlSerializer::AddString(const char* key, CStdString& value) +{ + CStdString pair; + CStdString escapedValue; + EscapeUrl(value, escapedValue); + pair.Format("%s=%s&", key, (PCSTR)escapedValue); + m_output += pair; +} + +CStdString UrlSerializer::Serialize() +{ + m_deSerialize = false; // Set Serialize mode + m_object->Define(this); + return m_output; +} + +typedef enum {UrlStartState, UrlKeyState, UrlValueState, UrlErrorState} UrlState; + +void UrlSerializer::DeSerialize(CStdString& input) +{ + // read string and extract values into map + UrlState state = UrlStartState; + CStdString key; + CStdString value; + CStdString errorDescription; + + input.Trim(); + input.ToLower(); + + for(int i=0; i<input.length() && state!= UrlErrorState; i++) + { + TCHAR character = input[i]; + + switch(state) + { + case UrlStartState: + if(character == '&') + { + ; // ignore ampersands + } + else if ( ACE_OS::ace_isalnum(character) ) + { + state = UrlKeyState; + key = character; + } + else + { + state = UrlErrorState; + errorDescription = "Cannot find key start, keys must be alphanum"; + } + break; + case UrlKeyState: + if( ACE_OS::ace_isalnum(character) ) + { + key += character; + } + else if (character == '=') + { + state = UrlValueState; + value.Empty(); + } + else + { + state = UrlErrorState; + errorDescription = "Invalid key character, keys must be alphanum"; + } + break; + case UrlValueState: + if( character == '=') + { + state = UrlErrorState; + errorDescription = "Value followed by = sign, value should always be followed by space sign"; + } + else if (character == '&') + { + state = UrlStartState; + } + else + { + value += character; + } + break; + default: + state = UrlErrorState; + errorDescription = "Non-existing state"; + } // switch(state) + + if ( (state == UrlStartState) || (i == (input.length()-1)) ) + { + if (!key.IsEmpty()) + { + // Url unescape + CStdString unescapedValue; + UnEscapeUrl(value, unescapedValue); + + // Add pair to key-value map + m_map.insert(std::make_pair(key, unescapedValue)); + key.Empty(); + value.Empty(); + } + } + } // for(int i=0; i<input.length() && state!= UrlErrorState; i++) + + m_deSerialize = true; // Set DeSerialize mode + m_object->Define(this); + m_object->Validate(); +} + +// Escape the space, colon and percent characters for serializing to Key-Value-Pair text +void UrlSerializer::EscapeUrl(CStdString& in, CStdString& out) +{ + //####### fixme, need unescaping + out = in; +} + +// Unescape the space, colon and percent characters for serializing to Key-Value-Pair text +void UrlSerializer::UnEscapeUrl(CStdString& in, CStdString& out) +{ + // ###### fixme, need escaping + out = in; +} + +CStdString UrlSerializer::FindClass(CStdString& input) +{ + CStdString result; + int equalsPostion = input.Find('='); + if (equalsPostion != -1) + { + int ampersandPostion = input.Find('&'); + if (ampersandPostion > equalsPostion) + { + // there is at least one parameter + result = input.Mid(equalsPostion+1, ampersandPostion-equalsPostion-1); + } + else + { + // there is no parameter + result = input.Mid(equalsPostion+1, input.GetLength() - equalsPostion - 1); + } + } + result.ToLower(); + return result; +}
\ No newline at end of file diff --git a/orkbasecxx/serializers/UrlSerializer.h b/orkbasecxx/serializers/UrlSerializer.h new file mode 100644 index 0000000..5c2ce21 --- /dev/null +++ b/orkbasecxx/serializers/UrlSerializer.h @@ -0,0 +1,44 @@ +/* + * Oreka -- A media capture and retrieval platform + * + * Copyright (C) 2005, orecx LLC + * + * http://www.orecx.com + * + * This program is free software, distributed under the terms of + * the GNU General Public License. + * Please refer to http://www.gnu.org/copyleft/gpl.html + * + */ + +#ifndef __URLSERIALIZER_H__ +#define __URLSERIALIZER_H__ + +#include "messages/Message.h" +#include "serializers/Serializer.h" + +/** Serializer that generates and parses URLs from and to objects. + key-value pairs are separated by ampersands + keys and values are separated by equal signs + example: message=doit&what=run +*/ +class DLL_IMPORT_EXPORT UrlSerializer : public KeyValueSerializer +{ +public: + UrlSerializer(Object* object) : KeyValueSerializer(object){}; + + void AddString(const char* key, CStdString& value); + CStdString Serialize(); + + void DeSerialize(CStdString& input); + + void EscapeUrl(CStdString& in, CStdString& out); + void UnEscapeUrl(CStdString& in, CStdString& out); + + static CStdString FindClass(CStdString& input); +private: + CStdString m_output; +}; + +#endif + diff --git a/orkbasecxx/serializers/XmlRpcSerializer.cpp b/orkbasecxx/serializers/XmlRpcSerializer.cpp new file mode 100644 index 0000000..b9ffd88 --- /dev/null +++ b/orkbasecxx/serializers/XmlRpcSerializer.cpp @@ -0,0 +1,47 @@ +/* + * Oreka -- A media capture and retrieval platform + * + * Copyright (C) 2005, orecx LLC + * + * http://www.orecx.com + * + * This program is free software, distributed under the terms of + * the GNU General Public License. + * Please refer to http://www.gnu.org/copyleft/gpl.html + * + */ + +/* +#include "XmlRpcSerializer.h" + +#define XMLRPC_METHOD_NAME "exec" + + +void XmlRpcSerializer::AddInt(const char* key, int value) +{ + XmlRpcValue pair; + pair[0] = key; + pair[1] = value; + m_array[m_numParams++] = pair; +} + +void XmlRpcSerializer::AddString(const char* key, CStdString& value) +{ + XmlRpcValue pair; + pair[0] = key; + pair[1] = (PCSTR)value; + m_array[m_numParams++] = pair; +} + +bool XmlRpcSerializer::Invoke(CStdString& hostname, int tcpPort) +{ + m_object->Define(this); + m_hostname = hostname; + m_tcpPort = tcpPort; + m_params[0] = m_array; + XmlRpcClient client(m_hostname, m_tcpPort); + client.execute(XMLRPC_METHOD_NAME, m_params, m_ret); + client.close(); + return m_ret.valid(); +} +*/ diff --git a/orkbasecxx/serializers/XmlRpcSerializer.h b/orkbasecxx/serializers/XmlRpcSerializer.h new file mode 100644 index 0000000..07bb59f --- /dev/null +++ b/orkbasecxx/serializers/XmlRpcSerializer.h @@ -0,0 +1,45 @@ +/* + * Oreka -- A media capture and retrieval platform + * + * Copyright (C) 2005, orecx LLC + * + * http://www.orecx.com + * + * This program is free software, distributed under the terms of + * the GNU General Public License. + * Please refer to http://www.gnu.org/copyleft/gpl.html + * + */ + +#ifndef __XMLRPCSERIALIZER_H__ +#define __XMLRPCSERIALIZER_H__ +/* +#include "XmlRpc.h" +#include "messages/Message.h" +#include "serializers/Serializer.h" + +using namespace XmlRpc; +*/ +/** Serializer that generates and parses XMLRPC parameters from and to objects. +*/ +/* +class DLL_IMPORT_EXPORT XmlRpcSerializer : public KeyValueSerializer +{ +public: + //XmlRpcSerializer(Message* message, CStdString& hostname, int tcpPort); + XmlRpcSerializer(Object* object) : KeyValueSerializer(object){}; + + void AddInt(const char* key, int value); + void AddString(const char* key, CStdString& value); + + bool Invoke(CStdString& hostname, int tcpPort); +private: + CStdString m_hostname; + int m_tcpPort; + XmlRpcValue m_params; + XmlRpcValue m_array; + XmlRpcValue m_ret; +}; +*/ +#endif + |