Guest User

Untitled

a guest
Aug 20th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.05 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml;
  6. using System.Xml.XPath;
  7. using System.Xml.Schema;
  8. using System.IO;
  9. using System.Data.SqlClient;
  10. using System.Data;
  11. using System.Data.SqlTypes;
  12. using DocumentRepository.Visitors;
  13. using System.Windows.Forms;
  14.  
  15. namespace DocumentRepository
  16. {
  17. public class Document : DocumentNode
  18. {
  19. public Document(XmlDocument doc)
  20. {
  21. Initialize();
  22. ReadFromXMLDocument(doc);
  23. }
  24. public Document(string filename)
  25. {
  26. Initialize();
  27. XmlDocument doc = new XmlDocument();
  28. try
  29. {
  30. doc.Load(filename);
  31. ReadFromXMLDocument(doc);
  32. }
  33. catch (System.Exception ex)
  34. {
  35. MessageBox.Show(ex.Message);
  36. }
  37. }
  38. public Document(Stream istream)
  39. {
  40. Initialize();
  41. XmlDocument doc = new XmlDocument();
  42. try
  43. {
  44. doc.Load(istream);
  45. ReadFromXMLDocument(doc);
  46. }
  47. catch (System.Exception ex)
  48. {
  49. MessageBox.Show(ex.Message);
  50. }
  51. }
  52. private void Initialize()
  53. {
  54. child_nodes = new List<DocumentNode>();
  55. attributes = new Dictionary<string, Dictionary<string, string>>();
  56. parent = null;
  57. }
  58. private void ReadFromXMLDocument(XmlDocument doc)
  59. {
  60. // ValidateXMLDocument(doc, "F:\\Users\\admin\\Documents\\Visual Studio 10\\Projects\\RSOI\\DocumentRepository\\xml\\schema.xsd");
  61. try
  62. {
  63. //////////////////////////////////////////////////////////
  64. // АТРИБУТЫ
  65. //////////////////////////////////////////////////////////
  66. XPathNavigator nav = doc.CreateNavigator();
  67. {
  68. XPathNodeIterator groups = (XPathNodeIterator)nav.Evaluate("document/attributes/group");
  69. while (groups.MoveNext())
  70. {
  71. string group = groups.Current.GetAttribute("name", "");
  72. attributes.Add(group, new Dictionary<string, string>());
  73. XmlReader attrs_reader = groups.Current.ReadSubtree();
  74.  
  75. while (attrs_reader.Read())
  76. {
  77. if (attrs_reader.NodeType == XmlNodeType.Element)
  78. {
  79. if (attrs_reader.Name == "attribute")
  80. {
  81. attributes[group].Add(attrs_reader.GetAttribute("name"), attrs_reader.GetAttribute("value"));
  82. }
  83. }
  84. }
  85. }
  86. }
  87.  
  88. XPathNodeIterator guid_node = (XPathNodeIterator)nav.Evaluate("document/attributes/guid");
  89. if (guid_node.MoveNext()) guid = guid_node.Current.GetAttribute("value", "");
  90.  
  91. XPathNodeIterator timestamp_node = (XPathNodeIterator)nav.Evaluate("document/attributes/timestamp");
  92. if (timestamp_node.MoveNext()) timestamp = DateTime.Parse(timestamp_node.Current.GetAttribute("value", ""));
  93. //////////////////////////////////////////////////////////
  94. // ДОКУМЕНТ
  95. //////////////////////////////////////////////////////////
  96. // заголовок
  97. if (doc.DocumentElement.Name == "document")
  98. {
  99. title = doc.DocumentElement.Attributes["title"].Value;
  100. }
  101.  
  102. // блоки
  103. {
  104. XPathNodeIterator blocks = (XPathNodeIterator)nav.Evaluate("document/blocks/block");
  105. ReadBlocks(blocks, this);
  106. }
  107. }
  108. catch (Exception ex)
  109. {
  110. MessageBox.Show(ex.Message);
  111. }
  112. }
  113. private void ReadBlocks(XPathNodeIterator blocks, DocumentNode node)
  114. {
  115. while(blocks.MoveNext())
  116. {
  117. string title = "";
  118. string type = "";
  119.  
  120. if(blocks.Current.HasAttributes)
  121. {
  122. title = blocks.Current.GetAttribute("title", "");
  123. type = blocks.Current.GetAttribute("type", "");
  124. }
  125.  
  126. if (blocks.Current.SelectChildren("block", "").Count > 0)
  127. {
  128. ReadBlocks(blocks.Current.SelectChildren("block", ""), node.AddChildNode(new CompositeNode(title)));
  129. }
  130. else
  131. {
  132. switch (type)
  133. {
  134. case "image":
  135. byte[] image = new byte[0];
  136. int size = ReadImage(blocks.Current.ReadSubtree(), ref image);
  137. node.AddChildNode(new ImageNode(title, ref image, size));
  138. break;
  139. case "text":
  140. node.AddChildNode(new TextNode(title, blocks.Current.Value));
  141. break;
  142. }
  143. }
  144. }
  145. }
  146. private int ReadImage(XmlReader image_reader, ref byte[] image)
  147. {
  148. image_reader.Read();
  149.  
  150. int size = 1000;
  151.  
  152. image = new byte[size];
  153. int pos = image_reader.ReadElementContentAsBase64(image, 0, 1000);
  154.  
  155. while (pos % 1000 == 0)
  156. {
  157. Array.Resize<byte>(ref image, size + 1000);
  158. pos += image_reader.ReadElementContentAsBase64(image, pos, 1000);
  159. size += 1000;
  160. }
  161.  
  162. return pos;
  163. }
  164.  
  165. /* private void ValidateXMLDocument(XmlDocument doc, string schema)
  166. {
  167. doc.Schemas.Add("", schema);
  168. doc.Validate(ValidationCallBack);
  169. }
  170. private static void ValidationCallBack(object sender, ValidationEventArgs args)
  171. {
  172. if (args.Severity == XmlSeverityType.Warning)
  173. MessageBox.Show("Warning: Matching schema not found. No validation occurred. " + args.Message);
  174. else
  175. MessageBox.Show("Validation error: " + args.Message);
  176. }*/
  177.  
  178. public override void Accept(NodeVisitor visitor)
  179. {
  180. visitor.VisitDocument(this);
  181. }
  182.  
  183. public DateTime Timestamp
  184. {
  185. get { return timestamp; }
  186. }
  187. public String GUID
  188. {
  189. get { return guid; }
  190. }
  191. public Dictionary<string, Dictionary<string, string> > Attributes
  192. {
  193. get { return attributes; }
  194. }
  195.  
  196. protected Dictionary<string, Dictionary<string, string> > attributes;
  197. protected DateTime timestamp;
  198. protected string guid;
  199. }
  200. }
Add Comment
Please, Sign In to add comment