123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549 |
- /**
- * XML Serialization
- * Simple and lightweight xml serialization class
- *
- * Original code by Lothar Perr
- *
- * This software is provided 'as-is', without any express or implied
- * warranty. In no event will the authors be held liable for any
- * damages arising from the use of this software.
- *
- * Permission is granted to anyone to use this software for any
- * purpose, including commercial applications, and to alter it and
- * redistribute it freely
- */
- #pragma once
- #include "stdafx.h"
- #include <algorithm>
- #include <string>
- #include <iostream>
- #include <sstream>
- #include "XMLSerialization.h"
- /**
- XML Serialization namespace
- */
- namespace xmls
- {
- /**
- Assign a value to the xint member
- @return void
- */
- void xInt::AssignValue(int value)
- {
- m_sValue =std::to_string(value);
-
- };
- /**
- Returns the int-value of the xint member
- @return current value
- */
- int xInt::value()
- {
- int value;
-
- value = std::stoi(m_sValue);
- return value;
- };
- /**
- Returns the bool-value of the xbool member
- @return current value
- */
- bool xBool::value()
- {
- bool value=false;
- std::string sHelp = m_sValue;
- transform(sHelp.begin(), sHelp.end(), sHelp.begin(), ::toupper);
- if (sHelp=="TRUE") return true;
- return value;
- };
- /**
- Assign a value to the xTime_t member
- @return void
- */
- void xTime_t::AssignValue(const time_t value)
- {
-
- m_sValue = std::to_string(value);
- }
- time_t xTime_t::value()
- {
-
- time_t t;
- t = std::stoi(m_sValue);
- return t;
- }
- /**
- Assign a value to the xOleDateTime member
- @return void
- */
- void xOleDateTime::AssignValue(const COleDateTime value)
- {
-
-
- const CString MSR_RESULT_INFOR_YMD = _T("%Y-%m-%d %H:%M:%S");
- m_sValue = value.Format (MSR_RESULT_INFOR_YMD);
- }
- COleDateTime xOleDateTime::value()
- {
- COleDateTime t;
- t.ParseDateTime(m_sValue.c_str());
- return t;
- }
- void xOleDateTimeSpan::AssignValue(const COleDateTimeSpan a_TimeSpan)
- {
- CString strRet;
- strRet.Format("%f", a_TimeSpan.m_span);
- m_sValue = strRet;
- }
- COleDateTimeSpan xOleDateTimeSpan::value()
- {
- COleDateTimeSpan t;
- t.m_span = atof(m_sValue.c_str());
- return t;
- }
- /**
- Delete all (self created) collection-elements
- @return void
- */
- class Slo;
- void CollectionBase::Clear()
- {
- if (m_vCollection.size()>0)
- {
- m_vCollection.clear();
- }
- }
- /**
- SerializableBase Constructor
- */
- Slo::Slo()
- {
- m_sClassName.clear ();
- m_sVersion.clear();
- m_sXML.clear();
- m_AttributeMappings.clear();
- m_MemberMappings.clear();
- m_MemberCollections.clear();
- }
- /**
- SerializableBase Destructor
- Clean Collections, Mappings and Subclass-Mappings
- */
- Slo::~Slo()
- {
- if (m_MemberCollections.size()>0)
- {
- m_MemberCollections.clear();
- }
- if (m_MemberMappings.size() > 0)
- {
- m_MemberMappings.clear();
- }
-
- if (m_AttributeMappings.size() > 0)
- {
- m_AttributeMappings.clear();
- }
- m_sXML.clear();
- m_sClassName.clear ();
- m_sVersion.clear();
-
- }
- /**
- Register a member
- @MemberName XML-Description/Name for the member
- @Member Member to register
- @return void
- */
- void Slo::Register(std::string MemberName, MemberBase *Member, std::string DefaultValue)
- {
- m_AttributeMappings[MemberName] = Member;
- }
- /**
- Register a member-subclass
- @MemberName XML-Description/Name for the member-class
- @Member Member-class to register
- @return void
- */
- void Slo::Register(std::string MemberName, ISlo *Member)
- {
- m_MemberMappings[MemberName] = Member;
- }
- /**
- Register a class-collection
- @CollectionName XML-Description/Name for the collection
- @SubclassCollection Collection to register
- @return void
- */
- void Slo::Register(std::string CollectionName, CollectionBase *SubclassCollection)
- {
- SubclassCollection->setCollectionName(CollectionName);
- m_MemberCollections[CollectionName] = SubclassCollection;
- }
- /**
- Perform serialization
- @classDoc tinyxml Class Document
- @rootNode tinyxml Element rootNode
- @return void
- */
- void Slo::toXML(tinyxml2::XMLDocument *classDoc, tinyxml2::XMLElement *rootNode)
- {
- for (auto it_member = m_AttributeMappings.begin() ; it_member != m_AttributeMappings.end(); ++it_member)
- {
- rootNode->SetAttribute(it_member->first.c_str(), it_member->second->c_str ());
- }
- for (auto it_subclass = m_MemberMappings.begin() ; it_subclass != m_MemberMappings.end(); ++it_subclass)
- {
- ISlo *subMember = it_subclass->second;
- tinyxml2::XMLElement *subClassNode = classDoc->NewElement("Member");
- subClassNode->SetAttribute("RegName", it_subclass->first.c_str());
- rootNode->InsertEndChild(subClassNode);
- subMember->Serialize(true,classDoc, subClassNode);
- }
- for (CollectionIterator it_collection = m_MemberCollections.begin() ; it_collection != m_MemberCollections.end(); ++it_collection)
- {
- tinyxml2::XMLElement *listNode = classDoc->NewElement("Collection");
- listNode->SetAttribute("RegName", (*it_collection).second ->getCollectionName().c_str());
- for (size_t c=0;c<(*it_collection).second->size();c++)
- {
- ISlo *item = (*it_collection).second ->getItem(c);
- tinyxml2::XMLElement *elementNode = classDoc->NewElement("Member");
- item->Serialize(true,classDoc, elementNode);
- listNode->InsertEndChild(elementNode);
- }
- rootNode->InsertEndChild(listNode);
- }
- }
- /**
- Perform deserialization
- @classItem destination object
- @classDoc tinyxml Class Document
- @rootNode tinyxml Element rootNode
- @return void
- */
- void Slo::fromXML( tinyxml2::XMLDocument *classDoc, tinyxml2::XMLElement *rootNode)
- {
-
- for (auto it = this->m_AttributeMappings.begin(); it != this->m_AttributeMappings.end(); ++it)
- {
- if (rootNode->Attribute(it->first.c_str() ))
- {
- *(it->second->getStringPtr ()) = rootNode->Attribute(it->first.c_str());// *(*it)->getField() = memberNode->GetText();
- }
- }
- tinyxml2::XMLElement *classNode = rootNode->FirstChildElement("Member");
- while(classNode!=NULL)
- {
- std::string className = classNode->Attribute("RegName");
- for (auto it_subclass = this->m_MemberMappings.begin(); it_subclass != this->m_MemberMappings.end(); ++it_subclass)
- {
- if (it_subclass->first == className)
- {
- it_subclass->second ->Serialize( false,classDoc, classNode);
- break;
- }
- }
- classNode = classNode->NextSiblingElement("Member");
- }
- tinyxml2::XMLElement *collectionNode = rootNode->FirstChildElement("Collection");
- while (collectionNode!=NULL)
- {
- std::string collectionName = collectionNode->Attribute("RegName");
- for (CollectionIterator it_collection = this->m_MemberCollections.begin() ; it_collection != this->m_MemberCollections.end(); ++it_collection)
- {
- if ((*it_collection).second ->getCollectionName()==collectionName)
- {
- (*it_collection).second ->Clear();
- tinyxml2::XMLElement *classNode = collectionNode->FirstChildElement("Member");
- while (classNode!=NULL)
- {
- ISlo *newItem = (*it_collection).second ->newElement();
- newItem->Serialize(false,classDoc, classNode);
- classNode = classNode->NextSiblingElement("Member");
- }
- }
- }
- collectionNode = collectionNode->NextSiblingElement("Collection");
- }
- }
- /**
- Get Class-Type of XML-Source
- @XMLSource XML-Input
- @return class name
- */
- std::string Slo::IdentifyClass(std::string XMLSource)
- {
- tinyxml2::XMLDocument doc;
- doc.Parse(XMLSource.c_str(), (size_t)XMLSource.length());
- tinyxml2::XMLElement* rootNode;
- rootNode = doc.FirstChildElement(RootClassName);
- if (rootNode)
- {
- const char* value = rootNode->Attribute("RegName");
- if (value) return std::string(value);
- }
- return Empty_String;
- }
- /**
- Get Class-Version of XML-Source
- @XMLSource XML-Input
- @return class version
- */
- std::string Slo::IdentifyClassVersion(std::string XMLSource)
- {
- tinyxml2::XMLDocument doc;
- doc.Parse(XMLSource.c_str(), (size_t)XMLSource.length());
- tinyxml2::XMLElement* rootNode;
- rootNode = doc.FirstChildElement(RootClassName);
- if (rootNode)
- {
- const char* value = rootNode->Attribute("Version");
- if (value) return std::string(value);
- }
- return Empty_String;
- }
-
- /**
- Search/Replace in a string
- @source source the source string
- @searchFor search for
- @replaceWith replace with
- @return the resulting string
- */
- std::string Slo::strReplaceAll(std::string source, const std::string searchFor, const std::string replaceWith)
- {
- if(searchFor.empty())
- return source;
- size_t start_pos = 0;
- while((start_pos = source.find(searchFor, start_pos)) != std::string::npos) {
- source.replace(start_pos, searchFor.length(), replaceWith);
- start_pos += replaceWith.length();
- }
- return source;
- }
-
- void Slo::Clear()
- {
- m_AttributeMappings.clear ();
- m_MemberMappings.clear ();
- m_MemberCollections.clear ();
- }
- void xDouble::AssignValue(const double value)
- {
-
- m_sValue = std::to_string(value);
-
- }
- double xDouble::value()
- {
-
- double value;
- value = std::stod(m_sValue);
- return value;
- }
- void xLong::AssignValue(const long value)
- {
-
- m_sValue = std::to_string(value);
- }
- long xLong::value()
- {
-
- long value;
- value = std::stol(m_sValue);
- return value;
- }
- void xDWORD::AssignValue(const DWORD value)
- {
-
- m_sValue = std::to_string(value);
- }
- DWORD xDWORD::value()
- {
-
- DWORD value;
- value = std::stol(m_sValue);
- return value;
- }
-
- void xRect::AssignValue(const CRect value, int shape)
- {// domain text body
- CString strDomainTextBody = _T("");
-
- // value 1 -- shape
- CString strValue;
-
- // value 2 -- center x
- // domain center
-
- strValue.Format("%d", (int)value.CenterPoint().x);
- strDomainTextBody += strValue + _T(",");
- // value 3 -- center y
- strValue.Format("%d", (int)value.CenterPoint().y);
- strDomainTextBody += strValue + _T(",");
- if (shape==0)
- {
- // value 4 -- diameter
- strValue.Format("%d", (int)value.Width ());
- strDomainTextBody += strValue+ _T(",");
- // value 5 -- 0
- strDomainTextBody += _T("0");
- }
- else
- {
- // value 4 -- width
- strValue.Format("%d", (int)value.Width());
- strDomainTextBody += strValue + _T(",");
- // value 5 -- height
- strValue.Format("%d", (int)value.Height());
- strDomainTextBody += strValue ;
- }
- // return domain text body
- m_sValue= strDomainTextBody;
- }
- CRect xRect::value()
- {//here,we use the rectangle's left,top,right,bottom to memorize these four numbers.Infact they are centreX, centreY , diameter(width),0(height) if the shape is circle.
- //so when we get these numbers we must get left,top,right,bottom because they are just four numbers memory here temporarily.
- CRect rectangle;
- std::vector<std::string> point;
- SplitString(m_sValue, point, ",");
-
- rectangle.left = stoi(point[0]);
- rectangle.top = stoi(point[1]);
- rectangle.right = stoi( point[2]);
- rectangle.bottom = stoi(point[3]);
- return rectangle;
- }
- void xPoint::AssignValue(const CPoint value)
- {
- int X = value.x;
- int Y= value.y;
- std::string OutString;
- std::string sX = std::to_string(X);
-
- std::string sY = std::to_string(Y);
-
- OutString = sX + "," + sY ;
- m_sValue = OutString;
- }
- CPoint xPoint::value()
- {
- CPoint p;
- std::vector<std::string> point;
- SplitString(m_sValue, point, ",");
-
- p.x = stoi(point[0], 0, 0);
- p.y = stoi(point[1], 0, 0);
-
- return p;
- }
- void SplitString(const std::string& s, std::vector<std::string>& v, const std::string& c)
- {
- std::string::size_type pos1, pos2;
- pos2 = s.find(c);
- pos1 = 0;
- while (std::string::npos != pos2)
- {
- v.push_back(s.substr(pos1, pos2 - pos1));
- pos1 = pos2 + c.size();
- pos2 = s.find(c, pos1);
- }
- if (pos1 != s.length())
- v.push_back(s.substr(pos1));
- }
- std::vector<std::string> SplitString(const std::string& s, const std::string& c)
- {
- std::string::size_type pos1, pos2;
- pos2 = s.find(c);
- pos1 = 0;
- std::vector<std::string> v;
- while (std::string::npos != pos2)
- {
- v.push_back(s.substr(pos1, pos2 - pos1));
- pos1 = pos2 + c.size();
- pos2 = s.find(c, pos1);
- }
- if (pos1 != s.length())
- v.push_back(s.substr(pos1));
- return v;
- }
- void ReplaceAll(std::string& content, std::string searchFor, std::string replaceWith)
- {
- if (searchFor.empty())
- return ;
- size_t start_pos = 0;
- while ((start_pos = content.find(searchFor, start_pos)) != std::string::npos) {
- content.replace(start_pos, searchFor.length(), replaceWith);
- start_pos += replaceWith.length();
- }
- return ;
- /*while (content.find(substr1) > 0)
- {
- content.replace(content.find(substr1), substr1.size(), substr2);
- }*/
- }
- void ReplaceFirst(std::string& content, std::string searchFor, std::string replaceWith)
- {
- if (searchFor.empty())
- return;
- size_t start_pos = 0;
- start_pos = content.find(searchFor, start_pos);
- if (start_pos != std::string::npos)
- {
- content.replace(start_pos, searchFor.length(), replaceWith);
- //start_pos += replaceWith.length();
- }
- return;
- /*while (content.find(substr1) > 0)
- {
- content.replace(content.find(substr1), substr1.size(), substr2);
- }*/
- }
- }
|