XMLSerialization.cs 20 KB

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