Implement XamlReader.ReadSubtree().
authorAtsushi Eno <atsushi@ximian.com>
Wed, 2 Feb 2011 09:18:02 +0000 (18:18 +0900)
committerAtsushi Eno <atsushi@ximian.com>
Wed, 2 Feb 2011 09:18:02 +0000 (18:18 +0900)
mcs/class/System.Xaml/System.Xaml.dll.sources
mcs/class/System.Xaml/System.Xaml/XamlReader.cs
mcs/class/System.Xaml/System.Xaml/XamlSubtreeReader.cs [new file with mode: 0644]
mcs/class/System.Xaml/System.Xaml_test.dll.sources
mcs/class/System.Xaml/Test/System.Xaml/XamlReaderTest.cs [new file with mode: 0644]

index e46183b5a225158a3e8a1199687075c592cee84e..57dc4a170f6fcbb6a1389d235b1b3bb383233fd3 100644 (file)
@@ -111,6 +111,7 @@ System.Xaml/XamlSchemaContext.cs
 System.Xaml/XamlSchemaContextSettings.cs
 System.Xaml/XamlSchemaException.cs
 System.Xaml/XamlServices.cs
+System.Xaml/XamlSubtreeReader.cs
 System.Xaml/XamlType.cs
 System.Xaml/XamlWriter.cs
 System.Xaml/XamlWriterInternalBase.cs
index 3ff163a3d130cacdabf3804296b5056226610739..638c3df51e351d60276d104d5ebe7ac08d4be0b6 100644 (file)
@@ -54,10 +54,9 @@ namespace System.Xaml
                
                public abstract bool Read ();
                
-               [MonoTODO]
                public virtual XamlReader ReadSubtree ()
                {
-                       throw new NotImplementedException ();
+                       return new XamlSubtreeReader (this);
                }
                
                public virtual void Skip ()
diff --git a/mcs/class/System.Xaml/System.Xaml/XamlSubtreeReader.cs b/mcs/class/System.Xaml/System.Xaml/XamlSubtreeReader.cs
new file mode 100644 (file)
index 0000000..f396901
--- /dev/null
@@ -0,0 +1,107 @@
+//
+// Copyright (C) 2010 Novell Inc. http://novell.com
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+using System;
+using System.Collections.Generic;
+
+namespace System.Xaml
+{
+       internal class XamlSubtreeReader : XamlReader
+       {
+               internal XamlSubtreeReader (XamlReader source)
+               {
+                       this.source = source;
+               }
+               
+               XamlReader source;
+
+               public override bool IsEof {
+                       get { return started && (nest == 0 || source.IsEof); }
+               }
+               public override XamlMember Member {
+                       get { return started ? source.Member : null; }
+               }
+               
+               public override NamespaceDeclaration Namespace {
+                       get { return started ? source.Namespace : null; }
+               }
+               
+               public override XamlNodeType NodeType {
+                       get { return started ? source.NodeType : XamlNodeType.None; }
+               }
+               
+               public override XamlSchemaContext SchemaContext {
+                       get { return source.SchemaContext; }
+               }
+               
+               public override XamlType Type {
+                       get { return started ? source.Type : null; }
+               }
+               
+               public override object Value {
+                       get { return started ? source.Value : null; }
+               }
+               
+               public void Close ()
+               {
+                       Dispose (true);
+               }
+               
+               protected override void Dispose (bool disposing)
+               {
+                       while (nest > 0)
+                               if (!Read ())
+                                       break;
+                       base.Dispose (disposing);
+               }
+               
+               bool started;
+               int nest;
+               
+               public override bool Read ()
+               {
+                       if (started) {
+                               if (nest == 0) {
+                                       source.Read ();
+                                       return false; // already consumed
+                               }
+                               if (!source.Read ())
+                                       return false;
+                       }
+                       else
+                               started = true;
+
+                       switch (source.NodeType) {
+                       case XamlNodeType.StartObject:
+                       case XamlNodeType.GetObject:
+                       case XamlNodeType.StartMember:
+                               nest++;
+                               break;
+                       case XamlNodeType.EndObject:
+                       case XamlNodeType.EndMember:
+                               nest--;
+                               break;
+                       }
+                       return true;
+               }
+       }
+}
index 884e9e77a4757c6b80743ba3effccd7aa2c4dc19..7ff9a23002a001e011d6856710904df55f8b20c1 100644 (file)
@@ -29,6 +29,7 @@ System.Xaml/XamlObjectReaderTest.cs
 System.Xaml/XamlObjectWriterSettingsTest.cs
 System.Xaml/XamlObjectWriterTest.cs
 System.Xaml/XamlReaderSettingsTest.cs
+System.Xaml/XamlReaderTest.cs
 System.Xaml/XamlReaderTestBase.cs
 System.Xaml/XamlSchemaContextSettingsTest.cs
 System.Xaml/XamlSchemaContextTest.cs
diff --git a/mcs/class/System.Xaml/Test/System.Xaml/XamlReaderTest.cs b/mcs/class/System.Xaml/Test/System.Xaml/XamlReaderTest.cs
new file mode 100644 (file)
index 0000000..88306e9
--- /dev/null
@@ -0,0 +1,100 @@
+//
+// Copyright (C) 2010 Novell Inc. http://novell.com
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.ComponentModel;
+using System.IO;
+using System.Linq;
+using System.Reflection;
+using System.Windows.Markup;
+using System.Xaml;
+using System.Xaml.Schema;
+using System.Xml;
+using NUnit.Framework;
+
+using CategoryAttribute = NUnit.Framework.CategoryAttribute;
+
+namespace MonoTests.System.Xaml
+{
+       [TestFixture]
+       public partial class XamlReaderTest
+       {
+               [Test]
+               public void ReadSubtree1 ()
+               {
+                       var xr = new XamlObjectReader (5);
+                       var sr = xr.ReadSubtree ();
+                       Assert.AreEqual (XamlNodeType.None, sr.NodeType, "#1-2");
+                       Assert.AreEqual (XamlNodeType.None, xr.NodeType, "#1-3");
+                       Assert.IsTrue (sr.Read (), "#2");
+                       Assert.AreEqual (XamlNodeType.None, sr.NodeType, "#2-2");
+                       Assert.AreEqual (XamlNodeType.None, xr.NodeType, "#2-3");
+                       Assert.IsFalse (sr.Read (), "#3");
+                       Assert.AreEqual (XamlNodeType.NamespaceDeclaration, xr.NodeType, "#3-2");
+               }
+
+               [Test]
+               public void ReadSubtree2 ()
+               {
+                       var xr = new XamlObjectReader (5);
+                       xr.Read ();
+                       var sr = xr.ReadSubtree ();
+                       Assert.AreEqual (XamlNodeType.None, sr.NodeType, "#1-2");
+                       Assert.AreEqual (XamlNodeType.NamespaceDeclaration, xr.NodeType, "#1-3");
+                       Assert.IsTrue (sr.Read (), "#2");
+                       Assert.AreEqual (XamlNodeType.NamespaceDeclaration, sr.NodeType, "#2-2");
+                       Assert.AreEqual (XamlNodeType.NamespaceDeclaration, xr.NodeType, "#2-3");
+                       Assert.IsFalse (sr.Read (), "#3");
+                       Assert.AreEqual (XamlNodeType.StartObject, xr.NodeType, "#3-2");
+               }
+
+               [Test]
+               public void ReadSubtree3 ()
+               {
+                       var xr = new XamlObjectReader (5);
+                       xr.Read ();
+                       xr.Read ();
+                       var sr = xr.ReadSubtree ();
+                       Assert.AreEqual (XamlNodeType.None, sr.NodeType, "#1-2");
+                       Assert.AreEqual (XamlNodeType.StartObject, xr.NodeType, "#1-3");
+                       Assert.IsTrue (sr.Read (), "#2");
+                       Assert.AreEqual (XamlNodeType.StartObject, sr.NodeType, "#2-2");
+                       Assert.AreEqual (XamlNodeType.StartObject, xr.NodeType, "#2-3");
+                       Assert.IsTrue (sr.Read (), "#3");
+                       Assert.AreEqual (XamlNodeType.StartMember, sr.NodeType, "#3-2");
+                       Assert.AreEqual (XamlNodeType.StartMember, xr.NodeType, "#3-3");
+                       Assert.IsTrue (sr.Read (), "#4");
+                       Assert.AreEqual (XamlNodeType.Value, sr.NodeType, "#4-2");
+                       Assert.AreEqual (XamlNodeType.Value, xr.NodeType, "#4-3");
+                       Assert.IsTrue (sr.Read (), "#5");
+                       Assert.AreEqual (XamlNodeType.EndMember, sr.NodeType, "#5-2");
+                       Assert.AreEqual (XamlNodeType.EndMember, xr.NodeType, "#5-3");
+                       Assert.IsTrue (sr.Read (), "#6");
+                       Assert.AreEqual (XamlNodeType.EndObject, sr.NodeType, "#6-2");
+                       Assert.AreEqual (XamlNodeType.EndObject, xr.NodeType, "#6-3");
+                       Assert.IsFalse (sr.Read (), "#7");
+                       Assert.AreEqual (XamlNodeType.None, xr.NodeType, "#7-2");
+               }
+       }
+}