XMLSerialization.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Xml;
  7. using System.Drawing;
  8. namespace OTSDataType
  9. {
  10. public interface Convertinterface
  11. {
  12. void Serialize(bool isStoring, XmlDocument xml, XmlNode rootNode);
  13. }
  14. public class MemberBase
  15. {
  16. const string RootClassName = "XMLData";
  17. const string XMLClassEndTag = "</" + RootClassName + ">";
  18. const string Empty_String = "";
  19. protected string m_sValue;
  20. //public virtual ~MemberBase() { };
  21. public virtual void Dispose() { }
  22. public string toString() { return m_sValue; }
  23. //public const char* c_str() { return m_sValue.c_str(); }
  24. public string getStringPtr(string str) { return m_sValue = str; } //c++原代码为:std::string *getStringPtr() { return &m_sValue; };
  25. }
  26. /**
  27. serializable string
  28. */
  29. public class xString : MemberBase
  30. {
  31. public void AssignValue(string value) { m_sValue = value; } //因为c#无=重载,所以修改为public
  32. public xString() { }
  33. public xString(string value) { AssignValue(value); }
  34. public string value() { return m_sValue; }
  35. }
  36. public class xInt : MemberBase
  37. {
  38. public void AssignValue(int value) { m_sValue = value.ToString(); } //因为c#无=重载,所以修改为public
  39. public xInt() { AssignValue(0); }
  40. public xInt(int value) { AssignValue(value); }
  41. public int value()
  42. {
  43. int value;
  44. value = int.Parse(m_sValue);
  45. return value;
  46. }
  47. }
  48. public class xDouble : MemberBase
  49. {
  50. public void AssignValue(double value) { m_sValue = value.ToString(); } //因为c#无=重载,所以修改为public
  51. public xDouble() { AssignValue(0); }
  52. public xDouble(double value) { AssignValue(value); }
  53. public double value()
  54. {
  55. double value;
  56. value = double.Parse(m_sValue);
  57. return value;
  58. }
  59. }
  60. public class xLong : MemberBase
  61. {
  62. public void AssignValue(long value) { m_sValue = value.ToString(); } //因为c#无=重载,所以修改为public public xLong() { AssignValue(0); }
  63. public xLong() { AssignValue(0); }
  64. public xLong(long value) { AssignValue(value); }
  65. public long value()
  66. {
  67. long value;
  68. value = long.Parse(m_sValue);
  69. return value;
  70. }
  71. }
  72. public class xDWORD : MemberBase
  73. {
  74. public void AssignValue(uint value) { m_sValue = value.ToString(); } //因为c#无=重载,所以修改为public
  75. public xDWORD() { AssignValue(0); }
  76. public xDWORD(uint value) { AssignValue(value); }
  77. public uint value()
  78. {
  79. uint value;
  80. value = uint.Parse(m_sValue);
  81. return value;
  82. }
  83. }
  84. public class xBool : MemberBase
  85. {
  86. public void AssignValue(bool value) { m_sValue = value ? "true" : "false"; } //因为c#无=重载,所以修改为public
  87. public xBool() { AssignValue(false); }
  88. public xBool(bool value) { AssignValue(value); }
  89. public bool value()
  90. {
  91. bool value = false;
  92. string sHelp = m_sValue;
  93. //transform(sHelp.GetEnumerator(), sHelp.end(), sHelp.GetEnumerator(), toupper);
  94. sHelp = sHelp.ToUpper();
  95. if (sHelp == "TRUE")
  96. return true;
  97. return value;
  98. }
  99. }
  100. public class xTime_t : MemberBase
  101. {
  102. public void AssignValue(DateTime value) //因为c#无=重载,所以修改为public
  103. {
  104. m_sValue = value.ToString();
  105. }
  106. public xTime_t()
  107. {
  108. DateTime t = Convert.ToDateTime("1000-01-01 00:00:00");
  109. AssignValue(t);
  110. }
  111. public xTime_t(DateTime value) { AssignValue(value); }
  112. public DateTime value()
  113. {
  114. return Convert.ToDateTime(m_sValue);
  115. }
  116. }
  117. public class xOleDateTimeSpan : MemberBase
  118. {
  119. public void AssignValue(TimeSpan value) //因为c#无=重载,所以修改为public
  120. {
  121. m_sValue = value.ToString();
  122. }
  123. public xOleDateTimeSpan()
  124. {
  125. TimeSpan timeSpan = DateTime.Now - DateTime.Now;
  126. AssignValue(timeSpan);
  127. }
  128. public xOleDateTimeSpan(TimeSpan value) { AssignValue(value); }
  129. public TimeSpan value()
  130. {
  131. return TimeSpan.Parse(m_sValue);
  132. }
  133. //xOleDateTimeSpan operator=(const COleDateTimeSpan value) { AssignValue(value); return *this; };
  134. };
  135. public class xRect : MemberBase
  136. {
  137. public void AssignValue(RectangleF value, int shape = 1) //因为c#无=重载,所以修改为public
  138. {
  139. //Rect x = new Rect();
  140. // domain text body
  141. string strDomainTextBody = "";
  142. // value 1 -- shape
  143. string strValue = "";
  144. // value 2 -- center x
  145. // domain center
  146. int centerX = (int)(value.Left + value.Right) / 2;
  147. strValue = centerX.ToString();
  148. strDomainTextBody += strValue + ",";
  149. // value 3 -- center y
  150. int centerY = (int)(value.Top + value.Bottom) / 2;
  151. strValue = centerY.ToString();
  152. strDomainTextBody += strValue + ",";
  153. if (shape == 0)
  154. {
  155. // value 4 -- diameter
  156. int diameter = (int)value.Width;
  157. strValue = diameter.ToString();
  158. strDomainTextBody += strValue + ",";
  159. // value 5 -- 0
  160. strDomainTextBody += "0";
  161. }
  162. else
  163. {
  164. // value 4 -- width
  165. int width = (int)value.Width;
  166. strValue = width.ToString();
  167. strDomainTextBody += strValue + ",";
  168. // value 5 -- height
  169. int height = (int)value.Height;
  170. strValue = height.ToString();
  171. strDomainTextBody += strValue;
  172. }
  173. // return domain text body
  174. m_sValue = strDomainTextBody;
  175. }
  176. public xRect() //xRect() { AssignValue(0); };???
  177. {
  178. Rectangle value = new Rectangle();
  179. value.X = 0;
  180. value.Y = 0;
  181. value.Width = 1;
  182. value.Height = 1;
  183. AssignValue(value, 1);
  184. }
  185. public xRect(Rectangle value, int shape) { AssignValue(value, shape); }
  186. public void SplitString(string s, ref List<string> v, string c)
  187. {
  188. string[] sArray = System.Text.RegularExpressions.Regex.Split(s, c, System.Text.RegularExpressions.RegexOptions.None);
  189. foreach (string i in sArray)
  190. v.Add(i);
  191. }
  192. public Rectangle value()
  193. {
  194. List<string> point = new List<string>();
  195. SplitString(m_sValue, ref point, ",");
  196. System.Drawing.Point pReftTop = new System.Drawing.Point();
  197. pReftTop.X = int.Parse(point[0]);
  198. pReftTop.Y = int.Parse(point[1]);
  199. System.Drawing.Size pRightBottom = new System.Drawing.Size();
  200. pRightBottom.Width = int.Parse(point[2]);
  201. pRightBottom.Height = int.Parse(point[3]);
  202. Rectangle rectangle = new Rectangle(pReftTop, pRightBottom);
  203. return rectangle;
  204. }
  205. }
  206. public class xPoint : MemberBase
  207. {
  208. public void AssignValue(System.Drawing.Point value) //因为c#无=重载,所以修改为public
  209. {
  210. int X = value.X;
  211. int Y = value.Y;
  212. string OutString;
  213. string sX = X.ToString();
  214. string sY = Y.ToString();
  215. OutString = sX + "," + sY;
  216. m_sValue = OutString;
  217. }
  218. public xPoint()
  219. {
  220. System.Drawing.Point value = new System.Drawing.Point();
  221. value.X = 0;
  222. value.Y = 0;
  223. AssignValue(value);
  224. }
  225. public xPoint(System.Drawing.Point value) { AssignValue(value); }
  226. public void SplitString(string s, ref List<string> v, string c)
  227. {
  228. string[] sArray = System.Text.RegularExpressions.Regex.Split(s, c, System.Text.RegularExpressions.RegexOptions.None);
  229. foreach (string i in sArray)
  230. v.Add(i);
  231. }
  232. public System.Drawing.Point value()
  233. {
  234. System.Drawing.Point p = new System.Drawing.Point();
  235. List<string> point = new List<string>();
  236. SplitString(m_sValue, ref point, ",");
  237. p.X = int.Parse(point[0]);
  238. p.Y = int.Parse(point[1]);
  239. return p;
  240. }
  241. };
  242. public abstract class CollectionBase<ISlo>
  243. {
  244. private string m_sCollectionName;
  245. private string m_sCollectionClassType;
  246. public CollectionBase() { m_sCollectionName = ""; m_sCollectionClassType = ""; }
  247. public List<ISlo> m_vCollection = new List<ISlo>(); //vector<ISlo*> m_vCollection; ISlo*转??
  248. public SortedDictionary<ISlo, bool> m_mOwner = new SortedDictionary<ISlo, bool>(); //map<ISlo*, bool> ??
  249. public void setCollectionName(string value) { m_sCollectionName = value; }
  250. public void setCollectionClassType(string value) { m_sCollectionClassType = value; }
  251. public abstract ISlo newElement();
  252. public string getCollectionName() { return m_sCollectionName; }
  253. public int size() { return m_vCollection.Count(); }
  254. public ISlo getItem(int itemID) { return m_vCollection[itemID]; }
  255. public void Clear()
  256. {
  257. if (m_vCollection.Count() > 0)
  258. {
  259. m_vCollection.Clear();
  260. }
  261. }
  262. }
  263. public class Collection<T> : CollectionBase<ISlo>
  264. {
  265. /**
  266. create new element of type T
  267. @return empty object of type T
  268. */
  269. public override ISlo newElement()
  270. {
  271. T newItem = System.Activator.CreateInstance<T>();
  272. //T newItem = default(T);
  273. object temp = newItem;
  274. ISlo sIo = (ISlo)temp;
  275. //Slo sIo = new Slo();
  276. m_vCollection.Add(sIo);
  277. //I change this value to be false forever(gsp).No matter what case there's no need to set the object's owner to the collection
  278. //after we created the object we'll put them to in a smartpointer.then the smartpointer will manage it.
  279. //m_mOwner[sIo] = false;//m_mOwner[newItem]=true
  280. //Type type = newItem.GetType();
  281. return sIo;
  282. }
  283. public void addItem(T item)
  284. {
  285. object temp = item;
  286. ISlo sIo = (ISlo)temp;
  287. m_vCollection.Add(sIo);/* m_mOwner[item] = false;*/
  288. }
  289. public T getItem(int itemID)
  290. {
  291. object temp = m_vCollection[itemID];
  292. T item = (T)temp;
  293. return item;
  294. }
  295. };
  296. public abstract class ISlo : Convertinterface
  297. {
  298. public abstract void Serialize(bool isStoring, XmlDocument classDoc, XmlNode rootNode);
  299. }
  300. public class Slo: ISlo
  301. {
  302. public string strReplaceAll(string source, string searchFor, string replaceWith)
  303. {
  304. if ((searchFor == null) || (searchFor == ""))
  305. {
  306. return source;
  307. }
  308. source.Replace(searchFor, replaceWith);
  309. return source;
  310. }
  311. public string m_sXML;
  312. public string m_sClassName;
  313. public string m_sVersion;
  314. public SortedDictionary<string, MemberBase> m_AttributeMappings = new SortedDictionary<string, MemberBase>();
  315. public SortedDictionary<string, ISlo> m_MemberMappings=new SortedDictionary<string, ISlo>();
  316. public SortedDictionary<string, CollectionBase<ISlo>> m_MemberCollections=new SortedDictionary<string, CollectionBase<ISlo>>();
  317. public void setClassName(string ClassName) { m_sClassName = ClassName; }
  318. public Slo()
  319. {
  320. m_sClassName = null;
  321. m_sVersion = null;
  322. m_sXML = null;
  323. m_AttributeMappings.Clear();
  324. m_MemberMappings.Clear();
  325. m_MemberCollections.Clear();
  326. }
  327. /**
  328. Register a member
  329. @MemberName XML-Description/Name for the member
  330. @Member Member to register
  331. @return void
  332. */
  333. public void Register(string MemberName, MemberBase Member) // public void Register(string MemberName, MemberBase Member, string DefaultValue) DefaultValue无引用,c#无自动补充功能,故方法调整为双参数
  334. {
  335. m_AttributeMappings[MemberName] = Member;
  336. }
  337. /**
  338. Register a member-subclass
  339. @MemberName XML-Description/Name for the member-class
  340. @Member Member-class to register
  341. @return void
  342. */
  343. public void Register(string MemberName, ISlo Member)
  344. {
  345. m_MemberMappings[MemberName] = Member;
  346. }
  347. /**
  348. Register a class-collection
  349. @CollectionName XML-Description/Name for the collection
  350. @SubclassCollection Collection to register
  351. @return void
  352. */
  353. public void Register(string CollectionName, CollectionBase<ISlo> SubclassCollection)
  354. {
  355. SubclassCollection.setCollectionName(CollectionName);
  356. m_MemberCollections[CollectionName] = SubclassCollection;
  357. }
  358. public override void Serialize(bool isStoring, XmlDocument xml, XmlNode rootNode)
  359. {
  360. if (isStoring)
  361. {
  362. if (xml.DocumentElement != null)
  363. {
  364. toXML(xml, rootNode);
  365. }
  366. }
  367. else
  368. {
  369. fromXML(xml, rootNode);
  370. }
  371. }
  372. public void toXML(XmlDocument xml, XmlNode rootNode)
  373. {
  374. for (int it_member = 0; it_member < m_AttributeMappings.Count; it_member++)
  375. {
  376. var element = m_AttributeMappings.ElementAt(it_member);
  377. if (rootNode.Attributes[element.Key] == null)
  378. {
  379. XmlElement XmlEle = (XmlElement)rootNode;
  380. XmlEle.SetAttribute(element.Key, element.Value.toString());
  381. }
  382. else
  383. {
  384. rootNode.Attributes[element.Key].InnerText = element.Value.toString();
  385. }
  386. }
  387. for (int it_subclass = 0; it_subclass < m_MemberMappings.Count; it_subclass++)
  388. {
  389. var element = m_MemberMappings.ElementAt(it_subclass);
  390. ISlo subMember = element.Value;
  391. XmlNode subClassNode = xml.CreateElement("Member");
  392. rootNode.AppendChild(subClassNode);
  393. XmlElement XmlEle_subclass = (XmlElement)subClassNode;
  394. XmlEle_subclass.SetAttribute("RegName", element.Key);
  395. subMember.Serialize(true, xml, subClassNode);
  396. }
  397. for (int it_collection = 0; it_collection < m_MemberCollections.Count; it_collection++)
  398. {
  399. var element = m_MemberCollections.ElementAt(it_collection);
  400. XmlNode listNode = xml.CreateElement("Collection");
  401. XmlElement XmlEle = (XmlElement)listNode;
  402. XmlEle.SetAttribute("RegName", element.Value.getCollectionName());
  403. for (int c = 0; c < element.Value.size(); c++)
  404. {
  405. ISlo item = element.Value.getItem(c);
  406. XmlNode elementNode = xml.CreateElement("Member");
  407. item.Serialize(true, xml, elementNode);
  408. listNode.AppendChild(elementNode);
  409. }
  410. rootNode.AppendChild(listNode);
  411. }
  412. }
  413. public void fromXML(XmlDocument xml, XmlNode rootNode)
  414. {
  415. for (int count = 0; count < m_AttributeMappings.Count; count++)
  416. {
  417. var element = m_AttributeMappings.ElementAt(count);
  418. if (rootNode.Attributes[element.Key] != null)
  419. {
  420. //string aa = rootNode.Attributes[element.Key].Value;
  421. element.Value.getStringPtr(rootNode.Attributes[element.Key].Value);
  422. }
  423. else
  424. {
  425. NLog.LogManager.GetCurrentClassLogger().Error("cann't find " + element.Key + " in config file!");
  426. }
  427. }
  428. XmlNodeList classNodeList = rootNode.SelectNodes("Member");
  429. if (classNodeList.Count != 0)
  430. {
  431. for (int i = 0; i < classNodeList.Count; i++)
  432. {
  433. string className = classNodeList[i].Attributes["RegName"].Value;
  434. for (int count = 0; count < m_MemberMappings.Count; count++)
  435. {
  436. var element = m_MemberMappings.ElementAt(count);
  437. if (element.Key == className)
  438. {
  439. element.Value.Serialize(false, xml, classNodeList[i]);
  440. break;
  441. }
  442. }
  443. }
  444. }
  445. XmlNodeList collectionNodeList = rootNode.SelectNodes("Collection");
  446. if (collectionNodeList.Count != 0)
  447. {
  448. for (int i = 0; i < collectionNodeList.Count; i++)
  449. {
  450. string collectionName = collectionNodeList[i].Attributes["RegName"].Value;
  451. for (int count = 0; count < m_MemberCollections.Count; count++)
  452. {
  453. var element = m_MemberCollections.ElementAt(count);
  454. if (element.Value.getCollectionName() == collectionName)
  455. {
  456. element.Value.Clear();
  457. XmlNodeList classNode2List = collectionNodeList[i].SelectNodes("Member");
  458. if (classNode2List.Count != 0)
  459. {
  460. for (int j = 0; j < classNode2List.Count; j++)
  461. {
  462. Convertinterface newItem = (Convertinterface)element.Value.newElement();
  463. newItem.Serialize(false, xml, classNode2List[j]);
  464. }
  465. }
  466. }
  467. }
  468. }
  469. }
  470. }
  471. public string IdentifyClass(XmlNode rootNode, string XMLSource)
  472. {
  473. return rootNode.Attributes["RegName"].Value;
  474. }
  475. public string IdentifyClassVersions(XmlNode rootNode, string XMLSource) //同 IdentifyClass ???
  476. {
  477. return rootNode.Attributes["RegName"].Value;
  478. }
  479. public string getClassName() { return m_sClassName; }
  480. public void setVersion(string value) { m_sVersion = value; }
  481. public string getVersion() { return m_sVersion; }
  482. public void Clear()
  483. {
  484. m_AttributeMappings.Clear();
  485. m_MemberMappings.Clear();
  486. m_MemberCollections.Clear();
  487. }
  488. internal void Register(string v, bool m_obShowAreaUp)
  489. {
  490. throw new NotImplementedException();
  491. }
  492. }
  493. }