XMLSerialization.cs 19 KB

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