Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Xml;
- using System.Xml.XPath;
- using System.Xml.Schema;
- using System.IO;
- using System.Data.SqlClient;
- using System.Data;
- using System.Data.SqlTypes;
- using DocumentRepository.Visitors;
- using System.Windows.Forms;
- namespace DocumentRepository
- {
- public class Document : DocumentNode
- {
- public Document(XmlDocument doc)
- {
- Initialize();
- ReadFromXMLDocument(doc);
- }
- public Document(string filename)
- {
- Initialize();
- XmlDocument doc = new XmlDocument();
- try
- {
- doc.Load(filename);
- ReadFromXMLDocument(doc);
- }
- catch (System.Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- public Document(Stream istream)
- {
- Initialize();
- XmlDocument doc = new XmlDocument();
- try
- {
- doc.Load(istream);
- ReadFromXMLDocument(doc);
- }
- catch (System.Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- private void Initialize()
- {
- child_nodes = new List<DocumentNode>();
- attributes = new Dictionary<string, Dictionary<string, string>>();
- parent = null;
- }
- private void ReadFromXMLDocument(XmlDocument doc)
- {
- // ValidateXMLDocument(doc, "F:\\Users\\admin\\Documents\\Visual Studio 10\\Projects\\RSOI\\DocumentRepository\\xml\\schema.xsd");
- try
- {
- //////////////////////////////////////////////////////////
- // АТРИБУТЫ
- //////////////////////////////////////////////////////////
- XPathNavigator nav = doc.CreateNavigator();
- {
- XPathNodeIterator groups = (XPathNodeIterator)nav.Evaluate("document/attributes/group");
- while (groups.MoveNext())
- {
- string group = groups.Current.GetAttribute("name", "");
- attributes.Add(group, new Dictionary<string, string>());
- XmlReader attrs_reader = groups.Current.ReadSubtree();
- while (attrs_reader.Read())
- {
- if (attrs_reader.NodeType == XmlNodeType.Element)
- {
- if (attrs_reader.Name == "attribute")
- {
- attributes[group].Add(attrs_reader.GetAttribute("name"), attrs_reader.GetAttribute("value"));
- }
- }
- }
- }
- }
- XPathNodeIterator guid_node = (XPathNodeIterator)nav.Evaluate("document/attributes/guid");
- if (guid_node.MoveNext()) guid = guid_node.Current.GetAttribute("value", "");
- XPathNodeIterator timestamp_node = (XPathNodeIterator)nav.Evaluate("document/attributes/timestamp");
- if (timestamp_node.MoveNext()) timestamp = DateTime.Parse(timestamp_node.Current.GetAttribute("value", ""));
- //////////////////////////////////////////////////////////
- // ДОКУМЕНТ
- //////////////////////////////////////////////////////////
- // заголовок
- if (doc.DocumentElement.Name == "document")
- {
- title = doc.DocumentElement.Attributes["title"].Value;
- }
- // блоки
- {
- XPathNodeIterator blocks = (XPathNodeIterator)nav.Evaluate("document/blocks/block");
- ReadBlocks(blocks, this);
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- private void ReadBlocks(XPathNodeIterator blocks, DocumentNode node)
- {
- while(blocks.MoveNext())
- {
- string title = "";
- string type = "";
- if(blocks.Current.HasAttributes)
- {
- title = blocks.Current.GetAttribute("title", "");
- type = blocks.Current.GetAttribute("type", "");
- }
- if (blocks.Current.SelectChildren("block", "").Count > 0)
- {
- ReadBlocks(blocks.Current.SelectChildren("block", ""), node.AddChildNode(new CompositeNode(title)));
- }
- else
- {
- switch (type)
- {
- case "image":
- byte[] image = new byte[0];
- int size = ReadImage(blocks.Current.ReadSubtree(), ref image);
- node.AddChildNode(new ImageNode(title, ref image, size));
- break;
- case "text":
- node.AddChildNode(new TextNode(title, blocks.Current.Value));
- break;
- }
- }
- }
- }
- private int ReadImage(XmlReader image_reader, ref byte[] image)
- {
- image_reader.Read();
- int size = 1000;
- image = new byte[size];
- int pos = image_reader.ReadElementContentAsBase64(image, 0, 1000);
- while (pos % 1000 == 0)
- {
- Array.Resize<byte>(ref image, size + 1000);
- pos += image_reader.ReadElementContentAsBase64(image, pos, 1000);
- size += 1000;
- }
- return pos;
- }
- /* private void ValidateXMLDocument(XmlDocument doc, string schema)
- {
- doc.Schemas.Add("", schema);
- doc.Validate(ValidationCallBack);
- }
- private static void ValidationCallBack(object sender, ValidationEventArgs args)
- {
- if (args.Severity == XmlSeverityType.Warning)
- MessageBox.Show("Warning: Matching schema not found. No validation occurred. " + args.Message);
- else
- MessageBox.Show("Validation error: " + args.Message);
- }*/
- public override void Accept(NodeVisitor visitor)
- {
- visitor.VisitDocument(this);
- }
- public DateTime Timestamp
- {
- get { return timestamp; }
- }
- public String GUID
- {
- get { return guid; }
- }
- public Dictionary<string, Dictionary<string, string> > Attributes
- {
- get { return attributes; }
- }
- protected Dictionary<string, Dictionary<string, string> > attributes;
- protected DateTime timestamp;
- protected string guid;
- }
- }
Add Comment
Please, Sign In to add comment