2009-04-06 Atsushi Enomoto <atsushi@ximian.com>
authorAtsushi Eno <atsushieno@gmail.com>
Mon, 6 Apr 2009 16:43:50 +0000 (16:43 -0000)
committerAtsushi Eno <atsushieno@gmail.com>
Mon, 6 Apr 2009 16:43:50 +0000 (16:43 -0000)
* ServiceDocumentFormatter.cs
  Workspace.cs
  Atom10FeedFormatter.cs
  AtomPub10ServiceDocumentFormatter.cs
  ResourceCollectionInfo.cs
  CategoriesDocument.cs
  ServiceDocument.cs : implemented most of reader parts.

* AtomPub10ServiceDocumentFormatterTest.cs : add ReadFrom() test.

svn path=/trunk/mcs/; revision=131133

mcs/class/System.ServiceModel.Web/System.ServiceModel.Syndication/Atom10FeedFormatter.cs
mcs/class/System.ServiceModel.Web/System.ServiceModel.Syndication/AtomPub10ServiceDocumentFormatter.cs
mcs/class/System.ServiceModel.Web/System.ServiceModel.Syndication/CategoriesDocument.cs
mcs/class/System.ServiceModel.Web/System.ServiceModel.Syndication/ChangeLog
mcs/class/System.ServiceModel.Web/System.ServiceModel.Syndication/ResourceCollectionInfo.cs
mcs/class/System.ServiceModel.Web/System.ServiceModel.Syndication/ServiceDocument.cs
mcs/class/System.ServiceModel.Web/System.ServiceModel.Syndication/ServiceDocumentFormatter.cs
mcs/class/System.ServiceModel.Web/System.ServiceModel.Syndication/Workspace.cs
mcs/class/System.ServiceModel.Web/Test/System.ServiceModel.Syndication/AtomPub10ServiceDocumentFormatterTest.cs
mcs/class/System.ServiceModel.Web/Test/System.ServiceModel.Syndication/ChangeLog

index c9e32b3fbf63c8121449ab74d2a4840ca22cd745..ffbf6d50ee3be5ba41dc767d7ef711e1c8a4017a 100644 (file)
@@ -251,7 +251,7 @@ namespace System.ServiceModel.Syndication
                        reader.ReadEndElement ();
                }
 
-               TextSyndicationContent ReadTextSyndicationContent (XmlReader reader)
+               internal static TextSyndicationContent ReadTextSyndicationContent (XmlReader reader)
                {
                        TextSyndicationContentKind kind = TextSyndicationContentKind.Plaintext;
                        switch (reader.GetAttribute ("type")) {
index 19649dff2fda12b84e98b41fc931a664e338f2a0..f80a6c3412560fc1bf1de7f6e30fac611741b7b6 100644 (file)
@@ -76,22 +76,15 @@ namespace System.ServiceModel.Syndication
 
                protected override ServiceDocument CreateDocumentInstance ()
                {
-                       return doc_type != null ? (ServiceDocument) Activator.CreateInstance (doc_type, new object [0]) : base.CreateDocumentInstance ();
+                       var doc = doc_type != null ? (ServiceDocument) Activator.CreateInstance (doc_type, new object [0]) : base.CreateDocumentInstance ();
+                       doc.InternalFormatter = this;
+                       return doc;
                }
 
-               [MonoTODO]
                public override void ReadFrom (XmlReader reader)
                {
-                       if (reader == null)
-                               throw new ArgumentNullException ("reader");
-
-                       for (int i = 0; i < reader.AttributeCount; i++) {
-                               reader.MoveToAttribute (i);
-                               Document.TryParseAttribute (reader.LocalName, reader.NamespaceURI, reader.Value, Version);
-                       }
-                       reader.MoveToElement ();
-
-                       throw new NotImplementedException ();
+                       if (!Document.TryParseElement (reader, Version))
+                               throw new XmlException (String.Format ("Unexpected element '{0}' in namespace '{1}'", reader.LocalName, reader.NamespaceURI));
                }
 
                public override void WriteTo (XmlWriter writer)
index 94144517ed6a384f35613b0f450adc30f02a4e26..0962fe95701842335f92ff143bfba2e8081bdb7d 100644 (file)
@@ -101,16 +101,34 @@ namespace System.ServiceModel.Syndication
                        GetFormatter ().WriteTo (writer);
                }
 
-               [MonoTODO]
                protected internal virtual bool TryParseAttribute (string name, string ns, string value, string version)
                {
-                       throw new NotImplementedException ();
+                       var inline = this as InlineCategoriesDocument;
+                       if (name == "lang" && ns == Namespaces.Xml)
+                               Language = value;
+                       else if (name == "base" && ns == Namespaces.Xml)
+                               BaseUri = new Uri (value, UriKind.RelativeOrAbsolute);
+                       else if (name == "href" && ns == String.Empty && this is ReferencedCategoriesDocument)
+                               ((ReferencedCategoriesDocument) this).Link = new Uri (value, UriKind.RelativeOrAbsolute);
+                       else if (name == "fixed" && ns == String.Empty && inline != null && value == "true")
+                               inline.IsFixed = true;
+                       else if (name == "scheme" && ns == String.Empty && inline != null)
+                               inline.Scheme = value;
+                       else
+                               return false;
+                       return true;
                }
 
-               [MonoTODO]
                protected internal virtual bool TryParseElement (XmlReader reader, string version)
                {
-                       throw new NotImplementedException ();
+                       if (reader == null)
+                               throw new ArgumentNullException ("reader");
+
+                       var f = GetFormatter ();
+                       if (!f.CanRead (reader))
+                               return false;
+                       f.ReadFrom (reader);
+                       return true;
                }
 
                protected internal virtual void WriteAttributeExtensions (XmlWriter writer, string version)
index 2e7efa08cd31b411e7d7c47b23fbd93f618c646c..f2bd10e1f709a033d6cae6c919095ad032176639 100644 (file)
@@ -1,3 +1,13 @@
+2009-04-06  Atsushi Enomoto  <atsushi@ximian.com>
+
+       * ServiceDocumentFormatter.cs
+         Workspace.cs
+         Atom10FeedFormatter.cs
+         AtomPub10ServiceDocumentFormatter.cs
+         ResourceCollectionInfo.cs
+         CategoriesDocument.cs
+         ServiceDocument.cs : implemented most of reader parts.
+
 2009-04-06  Atsushi Enomoto  <atsushi@ximian.com>
 
        * Workspace.cs, ResourceCollectionInfo.cs,
index d033eebd18d2c46404c8e576a4a8e58c869bd126..c05d37b5868faa77e9e765b901704ae3ed520487 100644 (file)
@@ -107,21 +107,54 @@ namespace System.ServiceModel.Syndication
                        return new ReferencedCategoriesDocument ();
                }
 
-               [MonoTODO]
                protected internal virtual bool TryParseAttribute (string name, string ns, string value, string version)
                {
-                       throw new NotImplementedException ();
+                       if (name == "base" && ns == Namespaces.Xml)
+                               BaseUri = new Uri (value, UriKind.RelativeOrAbsolute);
+                       else if (name == "href" && ns == String.Empty)
+                               Link = new Uri (value, UriKind.RelativeOrAbsolute);
+                       else
+                               return false;
+                       return true;
                }
 
-               [MonoTODO]
                protected internal virtual bool TryParseElement (XmlReader reader, string version)
                {
-                       throw new NotImplementedException ();
+                       if (reader == null)
+                               throw new ArgumentNullException ("reader");
+                       reader.MoveToContent ();
+
+                       if (reader.LocalName != "collection" || reader.NamespaceURI != version)
+                               return false;
+
+                       for (int i = 0; i < reader.AttributeCount; i++) {
+                               reader.MoveToAttribute (i);
+                               if (!TryParseAttribute (reader.LocalName, reader.NamespaceURI, reader.Value, version))
+                                       AttributeExtensions.Add (new XmlQualifiedName (reader.LocalName, reader.NamespaceURI), reader.Value);
+                       }
+                       reader.MoveToElement ();
+
+                       if (!reader.IsEmptyElement) {
+                               reader.Read ();
+                               for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
+                                       if (reader.LocalName == "title" && reader.NamespaceURI == Namespaces.Atom10)
+                                               Title = Atom10FeedFormatter.ReadTextSyndicationContent (reader);
+                                       else
+                                               ElementExtensions.Add (new SyndicationElementExtension (reader));
+                               }
+                       }
+                       reader.Read ();
+                       return true;
                }
 
                protected internal virtual void WriteAttributeExtensions (XmlWriter writer, string version)
                {
                        extensions.WriteAttributeExtensions (writer, version);
                }
+
+               protected internal virtual void WriteElementExtensions (XmlWriter writer, string version)
+               {
+                       extensions.WriteElementExtensions (writer, version);
+               }
        }
 }
index c1b5bfb950e9fa5977c9ceb11816fb1ec4f5feb2..7ca79b71090f14817df2ed66f7886cb87f217a82 100644 (file)
@@ -52,17 +52,17 @@ namespace System.ServiceModel.Syndication
 
        public class ServiceDocument
        {
-               [MonoTODO]
                public static TServiceDocument Load<TServiceDocument> (XmlReader reader)
                        where TServiceDocument : ServiceDocument, new()
                {
-                       throw new NotImplementedException ();
+                       var doc = new TServiceDocument ();
+                       new AtomPub10ServiceDocumentFormatter<TServiceDocument> (doc).ReadFrom (reader);
+                       return doc;
                }
 
-               [MonoTODO]
                public static ServiceDocument Load (XmlReader reader)
                {
-                       throw new NotImplementedException ();
+                       return Load<ServiceDocument> (reader);
                }
 
 
@@ -82,6 +82,10 @@ namespace System.ServiceModel.Syndication
                ServiceDocumentFormatter formatter;
                SyndicationExtensions extensions = new SyndicationExtensions ();
 
+               internal ServiceDocumentFormatter InternalFormatter {
+                       set { formatter = value; }
+               }
+
                public Dictionary<XmlQualifiedName, string> AttributeExtensions {
                        get { return extensions.Attributes; }
                }
@@ -134,10 +138,42 @@ namespace System.ServiceModel.Syndication
                        return false;
                }
 
-               [MonoTODO]
                protected internal virtual bool TryParseElement (XmlReader reader, string version)
                {
-                       throw new NotImplementedException ();
+                       if (reader == null)
+                               throw new ArgumentNullException ("reader");
+
+                       reader.MoveToContent ();
+
+                       if (reader.LocalName != "service" || reader.NamespaceURI != version)
+                               return false;
+
+                       for (int i = 0; i < reader.AttributeCount; i++) {
+                               reader.MoveToAttribute (i);
+                               if (!TryParseAttribute (reader.LocalName, reader.NamespaceURI, reader.Value, version))
+                                       AttributeExtensions.Add (new XmlQualifiedName (reader.LocalName, reader.NamespaceURI), reader.Value);
+                       }
+                       reader.MoveToElement ();
+
+                       if (reader.IsEmptyElement)
+                               throw new XmlException ("AtomPP service element requires at least one workspace element");
+
+                       reader.ReadStartElement ();
+
+                       for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
+                               if (reader.LocalName == "workspace" && reader.NamespaceURI == version) {
+                                       var ws = CreateWorkspace ();
+                                       if (ws.TryParseElement (reader, version)) {
+                                               Workspaces.Add (ws);
+                                               continue;
+                                       }
+                               }
+                               ElementExtensions.Add (new SyndicationElementExtension (reader));
+                       }
+
+                       reader.ReadEndElement ();
+
+                       return true;
                }
 
                protected internal virtual void WriteAttributeExtensions (XmlWriter writer, string version)
index 5e54c013702f2f45c827e7f5381d977befb624b2..0764bc2e981b4b34b982f6392168fcbd2eeaa49c 100644 (file)
@@ -38,34 +38,29 @@ namespace System.ServiceModel.Syndication
        [DataContract]
        public abstract class ServiceDocumentFormatter
        {
-               [MonoTODO]
                protected static SyndicationCategory CreateCategory (InlineCategoriesDocument inlineCategories)
                {
-                       throw new NotImplementedException ();
+                       return inlineCategories.CreateCategory ();
                }
 
-               [MonoTODO]
                protected static ResourceCollectionInfo CreateCollection (Workspace workspace)
                {
-                       throw new NotImplementedException ();
+                       return workspace.CreateResourceCollection ();
                }
 
-               [MonoTODO]
                protected static InlineCategoriesDocument CreateInlineCategories (ResourceCollectionInfo collection)
                {
-                       throw new NotImplementedException ();
+                       return collection.CreateInlineCategoriesDocument ();
                }
 
-               [MonoTODO]
                protected static ReferencedCategoriesDocument CreateReferencedCategories (ResourceCollectionInfo collection)
                {
-                       throw new NotImplementedException ();
+                       return collection.CreateReferencedCategoriesDocument ();
                }
 
-               [MonoTODO]
                protected static Workspace CreateWorkspace (ServiceDocument document)
                {
-                       throw new NotImplementedException ();
+                       return document.CreateWorkspace ();
                }
 
                [MonoTODO]
@@ -201,7 +196,7 @@ namespace System.ServiceModel.Syndication
 
                protected virtual ServiceDocument CreateDocumentInstance ()
                {
-                       return new ServiceDocument ();
+                       return new ServiceDocument () { InternalFormatter = this };
                }
 
                public abstract void ReadFrom (XmlReader reader);
index 2df8501f4a0130be70c3944b27078e61e3b6f5b2..2669ab550b6108b88eb6def61e9ff2d1b430567b 100644 (file)
@@ -77,16 +77,44 @@ namespace System.ServiceModel.Syndication
                        return new ResourceCollectionInfo ();
                }
 
-               [MonoTODO]
                protected internal virtual bool TryParseAttribute (string name, string ns, string value, string version)
                {
-                       throw new NotImplementedException ();
+                       if (name == "base" && ns == Namespaces.Xml) {
+                               BaseUri = new Uri (value, UriKind.RelativeOrAbsolute);
+                               return true;
+                       }
+                       return false;
                }
 
-               [MonoTODO]
                protected internal virtual bool TryParseElement (XmlReader reader, string version)
                {
-                       throw new NotImplementedException ();
+                       if (reader == null)
+                               throw new ArgumentNullException ("reader");
+
+                       reader.MoveToContent ();
+                       if (reader.LocalName != "workspace" || reader.NamespaceURI != version)
+                               return false;
+
+                       bool isEmpty = reader.IsEmptyElement;
+
+                       reader.ReadStartElement ();
+                       if (isEmpty)
+                               return true;
+
+                       for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
+                               if (reader.LocalName == "title" && reader.NamespaceURI == Namespaces.Atom10) {
+                                       Title = Atom10FeedFormatter.ReadTextSyndicationContent (reader);
+                                       continue;
+                               } else if (reader.LocalName == "collection" && reader.NamespaceURI == version) {
+                                       var rc = new ResourceCollectionInfo ();
+                                       if (rc.TryParseElement (reader, version)) {
+                                               Collections.Add (rc);
+                                               continue;
+                                       }
+                               }
+                               ElementExtensions.Add (new SyndicationElementExtension (reader));
+                       }
+                       return true;
                }
 
                protected internal virtual void WriteAttributeExtensions (XmlWriter writer, string version)
index 81caf129309cf37635a2b69015f0b1d2787eca6a..8866289f3a93289897a694fb2c1c954eb2304bdd 100644 (file)
@@ -39,6 +39,14 @@ using QName = System.Xml.XmlQualifiedName;
 
 namespace MonoTests.System.ServiceModel.Syndication
 {
+       class MyServiceFormatter : AtomPub10ServiceDocumentFormatter
+       {
+               public void Read (XmlReader reader)
+               {
+                       ReadFrom (reader);
+               }
+       }
+
        [TestFixture]
        public class AtomPub10ServiceDocumentFormatterTest
        {
@@ -91,5 +99,11 @@ namespace MonoTests.System.ServiceModel.Syndication
                {
                        ServiceDocument.Load (XmlReader.Create (new StringReader (app2)));
                }
+
+               [Test]
+               public void ReadFrom ()
+               {
+                       new MyServiceFormatter ().Read (XmlReader.Create (new StringReader (app2)));
+               }
        }
 }
index b1f6b89132ac50aefc997ecb63e1f9353ff5da37..d590ad27a23a81019b117071b1c2ce1f05155dd9 100644 (file)
@@ -1,3 +1,7 @@
+2009-04-06  Atsushi Enomoto  <atsushi@ximian.com>
+
+       * AtomPub10ServiceDocumentFormatterTest.cs : add ReadFrom() test.
+
 2009-04-06  Atsushi Enomoto  <atsushi@ximian.com>
 
        * AtomPub10CategoriesDocumentFormatterTest.cs : new test.