public void TestGraphWithEnumNode()
        {
            Node rootNode = new Node(1);
            Node intNode = new Node(42);
            Node enumNode = new Node(TestEnum.Two);

            rootNode.Nodes.Add(intNode);
            rootNode.Nodes.Add(enumNode);

            intNode.Nodes.Add(rootNode);

            Graph g = new Graph(rootNode);

            XmlSerializer s = new XmlSerializer();
            string result = s.Serialize(g);

            XmlDeserializer d = new XmlDeserializer();
            Graph newGraph = d.Deserialize<Graph>(result);

            Assert.IsNotNull(newGraph);
            Assert.AreEqual(g.Root.Data, newGraph.Root.Data);
            Assert.AreEqual(g.Root.Nodes[0].Data, newGraph.Root.Nodes[0].Data);
            Assert.AreEqual(g.Root.Nodes[1].Data, newGraph.Root.Nodes[1].Data);
            Assert.AreEqual(g.Root.Nodes[0].Nodes[0].Data, newGraph.Root.Nodes[0].Nodes[0].Data);
            Assert.AreSame(newGraph.Root, newGraph.Root.Nodes[0].Nodes[0]);
        }
示例#2
0
        /// <summary>
        /// Saves the layout to the specified fileName
        /// </summary>
        /// <param name="fileName"></param>
        public void SaveLayout(string fileName)
        {
            //Creates the model xml document
            XmlDocument layoutXmlDoc = new XmlDocument();
            XmlElement root = layoutXmlDoc.CreateElement("DotSpatialLayout");
            layoutXmlDoc.AppendChild(root);

            //Creates a serializer to handle the backgrounds
            XmlSerializer backSerializer = new XmlSerializer();

            //Saves the printer paper settings
            XmlElement paperElement = layoutXmlDoc.CreateElement("Paper");
            paperElement.SetAttribute("Name", _printerSettings.DefaultPageSettings.PaperSize.PaperName);
            paperElement.SetAttribute("Landscape", TypeDescriptor.GetConverter(typeof(bool)).ConvertToInvariantString(_printerSettings.DefaultPageSettings.Landscape));
            paperElement.SetAttribute("Margins", TypeDescriptor.GetConverter(typeof(Margins)).ConvertToInvariantString(_printerSettings.DefaultPageSettings.Margins));
            root.AppendChild(paperElement);

            //Saves the Tools and their output configuration to the model
            foreach (LayoutElement le in _layoutElements)
            {
                XmlElement element = layoutXmlDoc.CreateElement("Element");
                element.SetAttribute("Name", le.Name);
                if (le is LayoutBitmap)
                {
                    LayoutBitmap lb = le as LayoutBitmap;
                    XmlElement bitmap = layoutXmlDoc.CreateElement("Bitmap");
                    bitmap.SetAttribute("Filename", lb.Filename);
                    bitmap.SetAttribute("PreserveAspectRatio", lb.PreserveAspectRatio.ToString());
                    bitmap.SetAttribute("Draft", lb.Draft.ToString());
                    bitmap.SetAttribute("Brightness", TypeDescriptor.GetConverter(typeof(int)).ConvertToInvariantString(lb.Brightness));
                    bitmap.SetAttribute("Contrast", TypeDescriptor.GetConverter(typeof(int)).ConvertToInvariantString(lb.Contrast));
                    element.AppendChild(bitmap);
                }
                else if (le is LayoutLegend)
                {
                    LayoutLegend ll = le as LayoutLegend;
                    XmlElement legend = layoutXmlDoc.CreateElement("Legend");
                    legend.SetAttribute("TextHint", ll.TextHint.ToString());
                    legend.SetAttribute("Color", TypeDescriptor.GetConverter(typeof(Color)).ConvertToInvariantString(ll.Color));
                    legend.SetAttribute("Font", TypeDescriptor.GetConverter(typeof(Font)).ConvertToInvariantString(ll.Font));
                    legend.SetAttribute("Map", _layoutElements.IndexOf(ll.Map).ToString());
                    string layerString = string.Empty;
                    foreach (int i in ll.Layers)
                        layerString = layerString + TypeDescriptor.GetConverter(typeof(int)).ConvertToInvariantString(i) + "|";
                    legend.SetAttribute("Layers", layerString);
                    legend.SetAttribute("NumColumns", TypeDescriptor.GetConverter(typeof(int)).ConvertToInvariantString(ll.NumColumns));
                    element.AppendChild(legend);
                }
                else if (le is LayoutMap)
                {
                    LayoutMap lm = le as LayoutMap;
                    XmlElement map = layoutXmlDoc.CreateElement("Map");
                    map.SetAttribute("EnvelopeXmin", TypeDescriptor.GetConverter(typeof(double)).ConvertToInvariantString(lm.Envelope.Minimum.X));
                    map.SetAttribute("EnvelopeYmin", TypeDescriptor.GetConverter(typeof(double)).ConvertToInvariantString(lm.Envelope.Minimum.Y));
                    map.SetAttribute("EnvelopeXmax", TypeDescriptor.GetConverter(typeof(double)).ConvertToInvariantString(lm.Envelope.Maximum.X));
                    map.SetAttribute("EnvelopeYmax", TypeDescriptor.GetConverter(typeof(double)).ConvertToInvariantString(lm.Envelope.Maximum.Y));
                    element.AppendChild(map);
                }
                else if (le is LayoutNorthArrow)
                {
                    LayoutNorthArrow na = le as LayoutNorthArrow;
                    XmlElement northArrow = layoutXmlDoc.CreateElement("NorthArrow");
                    northArrow.SetAttribute("Color", TypeDescriptor.GetConverter(typeof(Color)).ConvertToInvariantString(na.Color));
                    northArrow.SetAttribute("Style", na.NorthArrowStyle.ToString());
                    northArrow.SetAttribute("Rotation", TypeDescriptor.GetConverter(typeof(float)).ConvertToInvariantString(na.Rotation));
                    element.AppendChild(northArrow);
                }
                else if (le is LayoutRectangle)
                {
                    // is this missing a few SetAttribute commands?
                    //LayoutRectangle lr = le as LayoutRectangle;
                    XmlElement rectangle = layoutXmlDoc.CreateElement("Rectangle");
                    element.AppendChild(rectangle);
                }
                else if (le is LayoutScaleBar)
                {
                    LayoutScaleBar lsc = le as LayoutScaleBar;
                    XmlElement scaleBar = layoutXmlDoc.CreateElement("ScaleBar");
                    scaleBar.SetAttribute("TextHint", lsc.TextHint.ToString());
                    scaleBar.SetAttribute("Color", TypeDescriptor.GetConverter(typeof(Color)).ConvertToInvariantString(lsc.Color));
                    scaleBar.SetAttribute("Font", TypeDescriptor.GetConverter(typeof(Font)).ConvertToInvariantString(lsc.Font));
                    scaleBar.SetAttribute("BreakBeforeZero", lsc.BreakBeforeZero.ToString());
                    scaleBar.SetAttribute("NumberOfBreaks", lsc.NumberOfBreaks.ToString());
                    scaleBar.SetAttribute("Unit", lsc.Unit.ToString());
                    scaleBar.SetAttribute("UnitText", lsc.UnitText);
                    scaleBar.SetAttribute("Map", _layoutElements.IndexOf(lsc.Map).ToString());
                    element.AppendChild(scaleBar);
                }
                else if (le is LayoutText)
                {
                    LayoutText lt = le as LayoutText;
                    XmlElement layoutText = layoutXmlDoc.CreateElement("Text");
                    layoutText.SetAttribute("TextHint", lt.TextHint.ToString());
                    layoutText.SetAttribute("Color", TypeDescriptor.GetConverter(typeof(Color)).ConvertToInvariantString(lt.Color));
                    layoutText.SetAttribute("Font", TypeDescriptor.GetConverter(typeof(Font)).ConvertToInvariantString(lt.Font));
                    layoutText.SetAttribute("ContentAlignment", lt.ContentAlignment.ToString());
                    layoutText.SetAttribute("Text", lt.Text);
                    element.AppendChild(layoutText);
                }

                element.SetAttribute("RectangleX", TypeDescriptor.GetConverter(typeof(float)).ConvertToInvariantString(le.Rectangle.X));
                element.SetAttribute("RectangleY", TypeDescriptor.GetConverter(typeof(float)).ConvertToInvariantString(le.Rectangle.Y));
                element.SetAttribute("RectangleWidth", TypeDescriptor.GetConverter(typeof(float)).ConvertToInvariantString(le.Rectangle.Width));
                element.SetAttribute("RectangleHeight", TypeDescriptor.GetConverter(typeof(float)).ConvertToInvariantString(le.Rectangle.Height));
                element.SetAttribute("Background", backSerializer.Serialize(le.Background));
                element.SetAttribute("ResizeStyle", le.ResizeStyle.ToString());
                root.AppendChild(element);
            }

            layoutXmlDoc.Save(fileName);
        }
        public void TestGraphWithStringNode()
        {
            Node rootNode = new Node(1);
            Node intNode = new Node(42);
            Node stringNode = new Node("test string with <invalid> characters!");

            rootNode.Nodes.Add(intNode);
            rootNode.Nodes.Add(stringNode);

            intNode.Nodes.Add(rootNode);

            Graph g = new Graph(rootNode);

            XmlSerializer s = new XmlSerializer();
            string result = s.Serialize(g);

            XmlDeserializer d = new XmlDeserializer();
            Graph newGraph = d.Deserialize<Graph>(result);

            Assert.IsNotNull(newGraph);
            Assert.AreEqual(g.Root.Data, newGraph.Root.Data);
            Assert.AreEqual(g.Root.Nodes[0].Data, newGraph.Root.Nodes[0].Data);
            Assert.AreEqual(g.Root.Nodes[1].Data, newGraph.Root.Nodes[1].Data);
            Assert.AreEqual(g.Root.Nodes[0].Nodes[0].Data, newGraph.Root.Nodes[0].Nodes[0].Data);
            Assert.AreSame(newGraph.Root, newGraph.Root.Nodes[0].Nodes[0]);
        }
        public void TestRectangleSerializationMap()
        {
            var rectangle = new Rectangle(1, 1, 2, 2);
            XmlSerializer s = new XmlSerializer();
            string xml = s.Serialize(rectangle);

            XmlDeserializer d = new XmlDeserializer();
            Rectangle result = d.Deserialize<Rectangle>(xml);

            Assert.AreEqual(1, result.X);
            Assert.AreEqual(1, result.Y);
            Assert.AreEqual(2, result.Width);
            Assert.AreEqual(2, result.Height);
        }
        public void TestPointSerializationMap()
        {
            var pt = new Point(1, 2);
            XmlSerializer s = new XmlSerializer();
            string xml = s.Serialize(pt);

            XmlDeserializer d = new XmlDeserializer();
            Point result = d.Deserialize<Point>(xml);

            Assert.AreEqual(1, result.X);
            Assert.AreEqual(2, result.Y);
        }
        public void TestFormatter()
        {
            ObjectWithIntMember obj = new ObjectWithIntMember(0xBEEF);
            XmlSerializer s = new XmlSerializer();
            string xml = s.Serialize(obj);

            XmlDeserializer d = new XmlDeserializer();
            ObjectWithIntMember result1 = d.Deserialize<ObjectWithIntMember>(xml);
            Assert.IsNotNull(result1);
            Assert.AreEqual(0xBEEF, result1.Number);

            ObjectWithIntMember result2 = new ObjectWithIntMember(0);
            d.Deserialize(result2, xml);
            Assert.IsNotNull(result2);
            Assert.AreEqual(0xBEEF, result2.Number);
        }
        public void TestMapPointLayer()
        {
            const string filename = @".\TestFiles\test-RandomPts.shp";

            IFeatureSet fs = FeatureSet.Open(filename);
            MapPointLayer l = new MapPointLayer(fs);
            XmlSerializer s = new XmlSerializer();
            string result = s.Serialize(l);

            XmlDeserializer d = new XmlDeserializer();
            MapPointLayer newPointLayer = d.Deserialize<MapPointLayer>(result);

            Assert.IsNotNull(newPointLayer);
            Assert.AreEqual(filename, newPointLayer.DataSet.Filename);
        }
        public void TestDictionary()
        {
            Dictionary<int, object> dictionary = new Dictionary<int, object>();
            dictionary.Add(1, new Node(42));
            dictionary.Add(2, "Hello <insert name here>!");

            XmlSerializer s = new XmlSerializer();
            string result = s.Serialize(dictionary);

            XmlDeserializer d = new XmlDeserializer();
            Dictionary<int, object> newDictionary = d.Deserialize<Dictionary<int, object>>(result);

            foreach (var key in dictionary.Keys)
            {
                Assert.AreEqual(dictionary[key], newDictionary[key]);
            }
        }
        public void TestMapPointLayer()
        {
            string filename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Data", "test-RandomPts.shp");

            IFeatureSet fs = FeatureSet.Open(filename);
            MapPointLayer l = new MapPointLayer(fs);
            XmlSerializer s = new XmlSerializer();
            string result = s.Serialize(l);

            XmlDeserializer d = new XmlDeserializer();
            MapPointLayer newPointLayer = d.Deserialize<MapPointLayer>(result);

            Assert.IsNotNull(newPointLayer);
            Assert.True(filename.Contains(newPointLayer.DataSet.Filename));
        }