Ejemplo n.º 1
0
        public void PairNodes(Node node1, Node node2)
        {
            try
            {
                XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();
                xmlReaderSettings.IgnoreWhitespace = true;
                xmlReaderSettings.IgnoreComments = true;

                //if (!File.Exists(XmlFilePath)) return;
                var document = new XmlDocument();
                document.Load(XmlFilePath);

                var xmlElementList = document.GetElementsByTagName("NodePairs");
                var newXmlElement = document.CreateElement("NodePair");
                var firstNode = document.CreateElement("Node");
                var secondNode = document.CreateElement("Node");
                firstNode.SetAttribute("Id", node1.Id);
                secondNode.SetAttribute("Id", node2.Id);
                newXmlElement.AppendChild(firstNode);
                newXmlElement.AppendChild(secondNode);
                xmlElementList[0].AppendChild(newXmlElement);
                document.Save(XmlFilePath);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
Ejemplo n.º 2
0
 public IEnumerable<Node> GetPairedNodesOf(Node node)
 {
     var pairedNodeList = new List<Node>();
     var document = new XmlDocument();
     //if (!File.Exists(XmlFilePath)) return null;
     document.Load(XmlFilePath);
     var xmlNode = document.DocumentElement;
     try
     {
         var xmlNodes = xmlNode.ChildNodes;
         for (var i = 0; i < xmlNodes.Count; i++)
         {
             var currentXmlNode = xmlNodes[i];
             var firstXmlNode = currentXmlNode.ChildNodes[0];
             var secondXmlNode = currentXmlNode.ChildNodes[1];
             if (firstXmlNode.Attributes["Id"].Value.Equals(node.Id))
             {
                 pairedNodeList.Add(_nodeManager.GetNodeById(secondXmlNode.Attributes["Id"].Value));
             }
             else if (secondXmlNode.Attributes["Id"].Value.Equals(node.Id))
             {
                 pairedNodeList.Add(_nodeManager.GetNodeById(firstXmlNode.Attributes["Id"].Value));
             }
         }
     }
     catch (Exception e) { Console.WriteLine(e.Message); }
     return pairedNodeList;
 }
 private void SetUpMockNodeManager()
 {
     _mockNodeManager.Setup(m => m.GetNodeById(It.IsAny<string>()))
         .Returns<string>(id =>
                              {
                                  dynamic node = new Node(id, _nodeMetadata);
                                  node.标题 = "Title for " + id;
                                  return node;
                              });
 }
Ejemplo n.º 4
0
 private static INodeManager MockNodeManager()
 {
     var mockNodeManager = new Mock<INodeManager>();
     var nodeMetadata =
         new NodeMetadata("知识点", new[]
                                     {
                                         new NodePropertyMetadata("标题", typeof (string)),
                                         new NodePropertyMetadata("内容", typeof (MsDoc))
                                     });
     mockNodeManager.Setup(m => m.GetNodeById(It.IsAny<string>()))
         .Returns<string>(id =>
                              {
                                  dynamic node = new Node(id, nodeMetadata);
                                  node.标题 = "Title for " + id;
                                  return node;
                              });
     mockNodeManager.Setup(m => m.AddNode(It.IsAny<Node>()))
         .Callback((Node node) => { Debug.WriteLine("Add Node"); });
     mockNodeManager.Setup(m => m.UpdateNode(It.IsAny<Node>()))
         .Callback((Node node) => { Debug.WriteLine("Update Node"); });
     mockNodeManager.Setup(m => m.DeleteNode(It.IsAny<Node>()))
         .Callback((Node node) => { Debug.WriteLine("Delete Node"); });
     return mockNodeManager.Object;
 }
Ejemplo n.º 5
0
 /// <summary>
 /// 给树节点增加子节点<paramref name="node"/>。
 /// </summary>
 public TreeNode AddChild(Node node)
 {
     var child = new TreeNode(node, this);
     _children.Add(child);
     return child;
 }
Ejemplo n.º 6
0
 public void Given_id_exist_should_delete_node_ok()
 {
     var nodeManager = new NodeManager("./root");
     dynamic node = new Node("mmm", _nodeMetadata);
     node.标题 = "标题";
     node.内容 = new MsDoc { Content = "内容" };
     nodeManager.AddNode(node);
     Directory.Exists("./root/mmm").Should().BeTrue();
     nodeManager.DeleteNode(node);
     Directory.Exists("./root/mmm").Should().BeFalse();
 }
Ejemplo n.º 7
0
 public void DeleteNode(Node node)
 {
     var nodePath = Path.Combine(_rootPath, node.Id);
     string[] fileList = Directory.GetFiles(nodePath);
     //foreach (var file in fileList)
     //    File.Delete(file);
     Directory.Delete(nodePath, true);
 }
Ejemplo n.º 8
0
 /// <summary>
 /// 构造一个<see cref="TreeNode"/>,
 /// 其对应节点为<paramref name="node"/>,父节点为<paramref name="parent"/>。
 /// </summary>
 public TreeNode(Node node, TreeNode parent = null)
 {
     _node = node;
     Parent = parent;
 }
Ejemplo n.º 9
0
 public void AddNode(Node node)
 {
     var nodePath = Path.Combine(_rootPath, node.Id);
     Directory.CreateDirectory(nodePath);
     addOrUpdateNode(node, nodePath);
 }
Ejemplo n.º 10
0
 public void UpdateNode(Node node)
 {
     var nodePath = Path.Combine(_rootPath, node.Id);
     addOrUpdateNode(node, nodePath);
 }
Ejemplo n.º 11
0
 /// <summary>
 /// 复制<paramref name="node"/>的内容,
 /// 并以<paramref name="id"/>为<see cref="Id"/>创建新节点。
 /// </summary>
 public Node(string id, Node node) : this(id, node.Metadata)
 {
     _properties = new List<NodeProperty>(_properties);
 }
Ejemplo n.º 12
0
        public Node GetNodeById(string id)
        {
            var nodePath = Path.Combine(_rootPath, id);
            if (!Directory.Exists(nodePath))
                return null;
            var metaDataXmlFile = Path.Combine(nodePath, "metadata.xml");

            var xmlReaderSettings = new XmlReaderSettings();
            xmlReaderSettings.IgnoreWhitespace = true;
            xmlReaderSettings.IgnoreComments = true;

            // read metadata.xml

            var xml2NodesMetadataConverter = new Xml2NodesMetadataConverter();
            var nodeMetadata = xml2NodesMetadataConverter.Extract(metaDataXmlFile);
            dynamic node = new Node(id, nodeMetadata);


            //read data.xml
            var dataXmlFile = Path.Combine(nodePath, "data.xml");
            using (XmlReader xmlReader = XmlReader.Create(dataXmlFile, xmlReaderSettings))
            {
                bool visitNode = false;
                while (xmlReader.Read())
                {
                    if (!xmlReader.Name.Equals("Node") && visitNode)
                    {
                        if (xmlReader.HasAttributes)
                        {
                            var msdocXml = xmlReader["File"];
                            var fs = new FileStream(Path.Combine(nodePath, msdocXml), FileMode.Open, FileAccess.Read);
                            var sr = new StreamReader(fs, Encoding.UTF8);
                            var msdoc = new MsDoc();
                            msdoc.Content = sr.ReadToEnd();
                            node[xmlReader.Name]= msdoc;
                            sr.Close();
                            fs.Close();
                        }
                        else
                        {
                            Type type = NodeMetadataHelper.GetPropertyMetadata(node.Metadata, xmlReader.Name).Type;
                            if (type.Equals(typeof(int)))
                            {
                                node[xmlReader.Name] = Convert.ToInt32(xmlReader.ReadString());
                            }
                            else if (type.Equals(typeof(decimal)))
                            {
                                node[xmlReader.Name]= Convert.ToDecimal(xmlReader.ReadString());
                            }
                            else if (type.Equals(typeof(string)))
                            {
                                node[xmlReader.Name]= xmlReader.ReadString();
                            }
                        }
                    }
                    else if (xmlReader.Name.Equals("Node")) visitNode = true;
                }
            }

            return node;
        }
Ejemplo n.º 13
0
 /// <summary>
 /// 复制<paramref name="node"/>的内容。
 /// 节点<see cref="Id"/>自动生成。
 /// </summary>
 public Node(Node node)
     : this(Guid.NewGuid().ToString(), node)
 {
 }
Ejemplo n.º 14
0
 public void Given_id_exist_should_update_node_ok()
 {
     var nodeManager = new NodeManager("./root");
     dynamic node = new Node("xxx", _nodeMetadata);
     node.标题 = "标题";
     node.内容 = new MsDoc { Content = "内容" };
     nodeManager.AddNode(node);
     Directory.Exists("./root/xxx").Should().BeTrue();
     node.标题 = "update标题";
     node.内容 = new MsDoc{Content = "update"}; 
     nodeManager.UpdateNode(node);
     Directory.Exists("./root/xxx").Should().BeTrue();
     var fs = new FileStream("./root/xxx/内容.xml", FileMode.Open, FileAccess.Read);
     var sr = new StreamReader(fs);
     var s = sr.ReadToEnd();
     s.Should().Be("update");
     fs.Close(); sr.Close();
     nodeManager.DeleteNode(node);
 }
Ejemplo n.º 15
0
 public void AddChild(Node node)
 {
     AddChild(_treeNode.AddChild(node));
 }
Ejemplo n.º 16
0
 public NodeModifiedMessage(object sender, object target, Node node, NodeModifyType modifyType)
     : base(sender, target)
 {
     _node = node;
     _modifyType = modifyType;
 }
Ejemplo n.º 17
0
 public NodeModifiedMessage(Node node, NodeModifyType modifyType)
 {
     _node = node;
     _modifyType = modifyType;
 }
Ejemplo n.º 18
0
        public void UnpairNodes(Node node1, Node node2)
        {
            try
            {
                var xmlReaderSettings = new XmlReaderSettings();
                xmlReaderSettings.IgnoreWhitespace = true;
                xmlReaderSettings.IgnoreComments = true;
                //if (!File.Exists(XmlFilePath)) return;
                var document = new XmlDocument();
                document.Load(XmlFilePath);

                var xmlElement = document.GetElementsByTagName("NodePairs");
                var nodePairElementList = document.GetElementsByTagName("NodePair");
                foreach (XmlElement nodePairElement in nodePairElementList)
                {
                    if (nodePairElement.ChildNodes[0].Attributes["Id"].Value.Equals(node1.Id))
                    {
                        if (nodePairElement.ChildNodes[1].Attributes["Id"].Value.Equals(node2.Id))
                        {
                            xmlElement[0].RemoveChild(nodePairElement);
                            break;
                        }
                    }
                    if (nodePairElement.ChildNodes[0].Attributes["Id"].Value.Equals(node2.Id))
                    {
                        if (nodePairElement.ChildNodes[1].Attributes["Id"].Value.Equals(node1.Id))
                        {
                            xmlElement[0].RemoveChild(nodePairElement);
                            break;
                        }
                    }
                }
                document.Save(XmlFilePath);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
Ejemplo n.º 19
0
        private void addOrUpdateNode(Node node, string nodePath)
        {
            var xmlWriterSettings = new XmlWriterSettings {OmitXmlDeclaration = false, Indent = true};

            const string metaDataXml = "metadata.xml";
            const string dataXml = "data.xml";
            var metaDataXmlFile = Path.Combine(nodePath, metaDataXml);
            var dataXmlFile = Path.Combine(nodePath, dataXml);

            var nodePropertyMetadatas = node.Metadata.Properties;
            var nodeProperties = node.Properties;

            //write metadata.xml

            var xw = XmlWriter.Create(metaDataXmlFile, xmlWriterSettings);
            xw.WriteStartDocument();
            xw.WriteStartElement("NodeMetadata");
            xw.WriteAttributeString("Name", node.Metadata.Name);

            foreach (var npm in nodePropertyMetadatas)
            {
                xw.WriteStartElement("NodeProperty");
                xw.WriteAttributeString("Name", npm.Name);
                if (npm.Type.Equals(typeof(int)))
                {
                    xw.WriteAttributeString("Type", "int");
                }
                else if (npm.Type.Equals(typeof(decimal)))
                {
                    xw.WriteAttributeString("Type", "decimal");
                }
                else if (npm.Type.Equals(typeof(string)))
                {
                    xw.WriteAttributeString("Type", "string");
                }
                else if(npm.Type.Equals(typeof(MsDoc)))
                {
                    xw.WriteAttributeString("Type", "msdoc");
                }
                xw.WriteEndElement();
            }
            xw.WriteEndElement();
            xw.WriteEndDocument();
            xw.Close();


            //write data.xml

            xw = XmlWriter.Create(dataXmlFile, xmlWriterSettings);
            xw.WriteStartDocument();
            xw.WriteStartElement("Node");

            foreach (var nodeProperty in nodeProperties)
            {
                xw.WriteStartElement(nodeProperty.PropertyName);

                if (nodeProperty.PropertyType == typeof(MsDoc))
                {
                    xw.WriteAttributeString("File", nodeProperty.PropertyName + ".xml");
                }
                else
                {
                    //xw.WriteStartElement(nodeProperty.PropertyName);
                    xw.WriteString(nodeProperty.Value.ToString());
                    //xw.WriteEndElement();
                }
                xw.WriteEndElement();
            }

            xw.WriteEndElement();
            xw.WriteEndDocument();
            xw.Close();



            //write xml files of msdoc
            foreach (var npm in nodeProperties.Where(p => p.PropertyType == typeof(MsDoc)))
            {
                var msDocXmlFile = Path.Combine(nodePath, npm.PropertyName + ".xml");
                using (var fs = new FileStream(msDocXmlFile, FileMode.Create, FileAccess.ReadWrite))
                using (var sw = new StreamWriter(fs, Encoding.UTF8))
                    if (npm.Value != null) sw.Write(((MsDoc) (npm.Value)).Content);
            }
        }