public override void WriteTo(XmlWriter writer)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }

            writer.WriteStartElement("app", "categories", Version);

            if (writer.LookupPrefix(Namespaces.Atom10) != "a10")
            {
                writer.WriteAttributeString("xmlns", "a10", Namespaces.Xmlns, Namespaces.Atom10);
            }

            // xml:lang, xml:base, term, scheme, label
            if (Document.Language != null)
            {
                writer.WriteAttributeString("xml", "lang", Namespaces.Xml, Document.Language);
            }
            if (Document.BaseUri != null)
            {
                writer.WriteAttributeString("xml", "base", Namespaces.Xml, Document.BaseUri.ToString());
            }

            InlineCategoriesDocument     inline     = Document as InlineCategoriesDocument;
            ReferencedCategoriesDocument referenced = Document as ReferencedCategoriesDocument;

            // ... no term ?

            if (inline != null)
            {
                if (inline.IsFixed)
                {
                    writer.WriteAttributeString("fixed", "yes");
                }
                if (inline.Scheme != null)
                {
                    writer.WriteAttributeString("scheme", inline.Scheme);
                }
            }
            else if (referenced != null)
            {
                if (referenced.Link != null)
                {
                    writer.WriteAttributeString("href", referenced.Link.ToString());
                }
            }

            Document.WriteAttributeExtensions(writer, Version);

            Document.WriteElementExtensions(writer, Version);

            if (inline != null)
            {
                WriteInlineCategoriesContent(inline, writer);
            }
            // no (non-extension) contents for out-of-line category

            writer.WriteEndElement();
        }
Exemplo n.º 2
0
        public static CategoriesDocument Load(XmlReader reader)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            var f = new AtomPub10CategoriesDocumentFormatter();

            reader.MoveToContent();

            CategoriesDocument doc;

            if (reader.GetAttribute("href") == null)
            {
                doc = new InlineCategoriesDocument();
            }
            else
            {
                doc = new ReferencedCategoriesDocument();
            }
            doc.GetFormatter().ReadFrom(reader);

            return(doc);
        }
 private static void WriteReferencedCategoriesContent(XmlWriter writer, ReferencedCategoriesDocument categories, string version)
 {
     if (categories.Link != null)
     {
         writer.WriteAttributeString("href", FeedUtils.GetUriString(categories.Link));
     }
     ServiceDocumentFormatter.WriteAttributeExtensions(writer, categories, version);
     ServiceDocumentFormatter.WriteElementExtensions(writer, categories, version);
 }
 private static void WriteReferencedCategoriesContent(XmlWriter writer, ReferencedCategoriesDocument categories, string version)
 {
     if (categories.Link != null)
     {
         writer.WriteAttributeString(App10Constants.Href, FeedUtils.GetUriString(categories.Link));
     }
     WriteAttributeExtensions(writer, categories, version);
     WriteElementExtensions(writer, categories, version);
 }
        internal static CategoriesDocument ReadCategories(XmlReader reader, Uri baseUri, CreateInlineCategoriesDelegate inlineCategoriesFactory, CreateReferencedCategoriesDelegate referencedCategoriesFactory, string version, bool preserveElementExtensions, bool preserveAttributeExtensions, int maxExtensionSize)
        {
            string attribute = reader.GetAttribute("href", string.Empty);

            if (string.IsNullOrEmpty(attribute))
            {
                InlineCategoriesDocument inlineCategories = inlineCategoriesFactory();
                ReadInlineCategories(reader, inlineCategories, baseUri, version, preserveElementExtensions, preserveAttributeExtensions, maxExtensionSize);
                return(inlineCategories);
            }
            ReferencedCategoriesDocument referencedCategories = referencedCategoriesFactory();

            ReadReferencedCategories(reader, referencedCategories, baseUri, new Uri(attribute, UriKind.RelativeOrAbsolute), version, preserveElementExtensions, preserveAttributeExtensions, maxExtensionSize);
            return(referencedCategories);
        }
        private ResourceCollectionInfo GetPostsResourceCollectionInfo(UrlHelper url) {

            ResourceCollectionInfo posts = new ResourceCollectionInfo("Blog",
                new Uri(url.Link("DefaultApi", new { controller = "posts" })));

            posts.Accepts.Add("application/atom+xml;type=entry");

            // For WLW to work we need to include format in the categories URI.
            // Hoping to provide a better solution than this.
            var categoriesUri = new Uri(url.Link("DefaultApi", new { controller = "tags", format = "atomcat" }));
            ReferencedCategoriesDocument categories = new ReferencedCategoriesDocument(categoriesUri);
            posts.Categories.Add(categories);

            return posts;
        }
        internal static CategoriesDocument ReadCategories(XmlReader reader, Uri baseUri, CreateInlineCategoriesDelegate inlineCategoriesFactory, CreateReferencedCategoriesDelegate referencedCategoriesFactory, string version, int maxExtensionSize)
        {
            string link = reader.GetAttribute(App10Constants.Href, string.Empty);

            if (string.IsNullOrEmpty(link))
            {
                InlineCategoriesDocument inlineCategories = inlineCategoriesFactory();
                ReadInlineCategories(reader, inlineCategories, baseUri, version, maxExtensionSize);
                return(inlineCategories);
            }
            else
            {
                ReferencedCategoriesDocument referencedCategories = referencedCategoriesFactory();
                ReadReferencedCategories(reader, referencedCategories, baseUri, new Uri(link, UriKind.RelativeOrAbsolute), version, maxExtensionSize);
                return(referencedCategories);
            }
        }
Exemplo n.º 8
0
		public static CategoriesDocument Load (XmlReader reader)
		{
			if (reader == null)
				throw new ArgumentNullException ("reader");

			var f = new AtomPub10CategoriesDocumentFormatter ();
			reader.MoveToContent ();

			CategoriesDocument doc;
			if (reader.GetAttribute ("href") == null)
				doc = new InlineCategoriesDocument ();
			else
				doc = new ReferencedCategoriesDocument ();
			doc.GetFormatter ().ReadFrom (reader);

			return doc;
		}
        internal static async Task <CategoriesDocument> ReadCategories(XmlReaderWrapper reader, Uri baseUri, CreateInlineCategoriesDelegate inlineCategoriesFactory, CreateReferencedCategoriesDelegate referencedCategoriesFactory, string version, bool preserveElementExtensions, bool preserveAttributeExtensions, int maxExtensionSize)
        {
            string link = reader.GetAttribute(App10Constants.Href, string.Empty);

            if (string.IsNullOrEmpty(link))
            {
                InlineCategoriesDocument inlineCategories = inlineCategoriesFactory();
                await ReadInlineCategoriesAsync(reader, inlineCategories, baseUri, version, preserveElementExtensions, preserveAttributeExtensions, maxExtensionSize);

                return(inlineCategories);
            }
            else
            {
                ReferencedCategoriesDocument referencedCategories = referencedCategoriesFactory();
                await ReadReferencedCategoriesAsync(reader, referencedCategories, baseUri, new Uri(link, UriKind.RelativeOrAbsolute), version, preserveElementExtensions, preserveAttributeExtensions, maxExtensionSize);

                return(referencedCategories);
            }
        }
Exemplo n.º 10
0
        public HttpResponseMessage Get()
        {
            var doc = new ServiceDocument();
            var ws = new Workspace
            {
                Title = new TextSyndicationContent("My Site"),
                BaseUri = new Uri(Request.RequestUri.GetLeftPart(UriPartial.Authority))
            };

            var posts = new ResourceCollectionInfo("Blog",
                new Uri(Url.Link("DefaultApi", new { controller = "posts" })));

            posts.Accepts.Add("application/atom+xml;type=entry");

            // For WLW to work we need to include format in the categories URI.
            // Hoping to provide a better solution than this.
            var categoriesUri = new Uri(Url.Link("DefaultApi", new { controller = "tags", format = "atomcat" }));
            var categories = new ReferencedCategoriesDocument(categoriesUri);
            posts.Categories.Add(categories);

            ws.Collections.Add(posts);

            doc.Workspaces.Add(ws);

            var response = new HttpResponseMessage(HttpStatusCode.OK);

            var formatter = new AtomPub10ServiceDocumentFormatter(doc);

            var stream = new MemoryStream();
            using (var writer = XmlWriter.Create(stream))
            {
                formatter.WriteTo(writer);
            }

            stream.Position = 0;
            var content = new StreamContent(stream);
            response.Content = content;
            response.Content.Headers.ContentType =
                new MediaTypeHeaderValue("application/atomsvc+xml");

            return response;
        }
 private static void WriteReferencedCategoriesContent(XmlWriter writer, ReferencedCategoriesDocument categories, string version)
 {
     if (categories.Link != null)
     {
         writer.WriteAttributeString("href", FeedUtils.GetUriString(categories.Link));
     }
     ServiceDocumentFormatter.WriteAttributeExtensions(writer, categories, version);
     ServiceDocumentFormatter.WriteElementExtensions(writer, categories, version);
 }
 private static void ReadReferencedCategories(XmlReader reader, ReferencedCategoriesDocument referencedCategories, Uri baseUri, Uri link, string version, bool preserveElementExtensions, bool preserveAttributeExtensions, int maxExtensionSize)
 {
     referencedCategories.BaseUri = baseUri;
     referencedCategories.Link = link;
     if (reader.HasAttributes)
     {
         while (reader.MoveToNextAttribute())
         {
             if ((reader.LocalName == "base") && (reader.NamespaceURI == "http://www.w3.org/XML/1998/namespace"))
             {
                 referencedCategories.BaseUri = FeedUtils.CombineXmlBase(referencedCategories.BaseUri, reader.Value);
             }
             else
             {
                 if ((reader.LocalName == "lang") && (reader.NamespaceURI == "http://www.w3.org/XML/1998/namespace"))
                 {
                     referencedCategories.Language = reader.Value;
                     continue;
                 }
                 if ((reader.LocalName != "href") || (reader.NamespaceURI != string.Empty))
                 {
                     string namespaceURI = reader.NamespaceURI;
                     string localName = reader.LocalName;
                     if (!FeedUtils.IsXmlns(localName, namespaceURI) && !FeedUtils.IsXmlSchemaType(localName, namespaceURI))
                     {
                         string str3 = reader.Value;
                         if (!ServiceDocumentFormatter.TryParseAttribute(localName, namespaceURI, str3, referencedCategories, version))
                         {
                             if (preserveAttributeExtensions)
                             {
                                 referencedCategories.AttributeExtensions.Add(new XmlQualifiedName(reader.LocalName, reader.NamespaceURI), reader.Value);
                                 continue;
                             }
                             SyndicationFeedFormatter.TraceSyndicationElementIgnoredOnRead(reader);
                         }
                     }
                 }
             }
         }
     }
     reader.MoveToElement();
     bool isEmptyElement = reader.IsEmptyElement;
     reader.ReadStartElement();
     if (!isEmptyElement)
     {
         XmlBuffer buffer = null;
         XmlDictionaryWriter extWriter = null;
         try
         {
             while (reader.IsStartElement())
             {
                 if (!ServiceDocumentFormatter.TryParseElement(reader, referencedCategories, version))
                 {
                     if (preserveElementExtensions)
                     {
                         SyndicationFeedFormatter.CreateBufferIfRequiredAndWriteNode(ref buffer, ref extWriter, reader, maxExtensionSize);
                     }
                     else
                     {
                         SyndicationFeedFormatter.TraceSyndicationElementIgnoredOnRead(reader);
                         reader.Skip();
                     }
                 }
             }
             ServiceDocumentFormatter.LoadElementExtensions(buffer, extWriter, referencedCategories);
         }
         finally
         {
             if (extWriter != null)
             {
                 extWriter.Close();
             }
         }
         reader.ReadEndElement();
     }
 }
 static void WriteReferencedCategoriesContent(XmlWriter writer, ReferencedCategoriesDocument categories, string version)
 {
     if (categories.Link != null)
     {
         writer.WriteAttributeString(App10Constants.Href, FeedUtils.GetUriString(categories.Link));
     }
     WriteAttributeExtensions(writer, categories, version);
     WriteElementExtensions(writer, categories, version);
 }
Exemplo n.º 14
0
        private static async Task ReadReferencedCategoriesAsync(XmlReader reader, ReferencedCategoriesDocument referencedCategories, Uri baseUri, Uri link, string version, bool preserveElementExtensions, bool preserveAttributeExtensions, int maxExtensionSize)
        {
            referencedCategories.BaseUri = baseUri;
            referencedCategories.Link    = link;
            if (reader.HasAttributes)
            {
                while (reader.MoveToNextAttribute())
                {
                    if (reader.LocalName == "base" && reader.NamespaceURI == Atom10FeedFormatter.XmlNs)
                    {
                        referencedCategories.BaseUri = FeedUtils.CombineXmlBase(referencedCategories.BaseUri, await reader.GetValueAsync().ConfigureAwait(false));
                    }
                    else if (reader.LocalName == "lang" && reader.NamespaceURI == Atom10FeedFormatter.XmlNs)
                    {
                        referencedCategories.Language = await reader.GetValueAsync().ConfigureAwait(false);
                    }
                    else if (reader.LocalName == App10Constants.Href && reader.NamespaceURI == string.Empty)
                    {
                        continue;
                    }
                    else
                    {
                        string ns   = reader.NamespaceURI;
                        string name = reader.LocalName;
                        if (FeedUtils.IsXmlns(name, ns) || FeedUtils.IsXmlSchemaType(name, ns))
                        {
                            continue;
                        }

                        string val = await reader.GetValueAsync().ConfigureAwait(false);

                        if (!TryParseAttribute(name, ns, val, referencedCategories, version))
                        {
                            if (preserveAttributeExtensions)
                            {
                                referencedCategories.AttributeExtensions.Add(new XmlQualifiedName(reader.LocalName, reader.NamespaceURI), await reader.GetValueAsync().ConfigureAwait(false));
                            }
                        }
                    }
                }
            }

            reader.MoveToElement();
            bool isEmptyElement = reader.IsEmptyElement;
            await reader.ReadStartElementAsync().ConfigureAwait(false);

            if (!isEmptyElement)
            {
                XmlBuffer           buffer    = null;
                XmlDictionaryWriter extWriter = null;
                try
                {
                    while (await reader.IsStartElementAsync().ConfigureAwait(false))
                    {
                        if (!TryParseElement(reader, referencedCategories, version))
                        {
                            if (preserveElementExtensions)
                            {
                                var tuple = await SyndicationFeedFormatter.CreateBufferIfRequiredAndWriteNodeAsync(buffer, extWriter, reader, maxExtensionSize).ConfigureAwait(false);

                                buffer    = tuple.Item1;
                                extWriter = tuple.Item2;
                            }
                        }
                    }

                    LoadElementExtensions(buffer, extWriter, referencedCategories);
                }
                finally
                {
                    if (extWriter != null)
                    {
                        extWriter.Close();
                    }
                }

                await reader.ReadEndElementAsync().ConfigureAwait(false);
            }
        }
        private static void ReadReferencedCategories(XmlReader reader, ReferencedCategoriesDocument referencedCategories, Uri baseUri, Uri link, string version, int maxExtensionSize)
        {
            referencedCategories.BaseUri = baseUri;
            referencedCategories.Link    = link;
            if (reader.HasAttributes)
            {
                while (reader.MoveToNextAttribute())
                {
                    if (reader.LocalName == "base" && reader.NamespaceURI == Atom10FeedFormatter.XmlNs)
                    {
                        referencedCategories.BaseUri = FeedUtils.CombineXmlBase(referencedCategories.BaseUri, reader.Value);
                    }
                    else if (reader.LocalName == "lang" && reader.NamespaceURI == Atom10FeedFormatter.XmlNs)
                    {
                        referencedCategories.Language = reader.Value;
                    }
                    else if (reader.LocalName == App10Constants.Href && reader.NamespaceURI == string.Empty)
                    {
                        continue;
                    }
                    else
                    {
                        string ns   = reader.NamespaceURI;
                        string name = reader.LocalName;
                        if (FeedUtils.IsXmlns(name, ns) || FeedUtils.IsXmlSchemaType(name, ns))
                        {
                            continue;
                        }

                        string val = reader.Value;
                        if (!TryParseAttribute(name, ns, val, referencedCategories, version))
                        {
                            referencedCategories.AttributeExtensions.Add(new XmlQualifiedName(reader.LocalName, reader.NamespaceURI), reader.Value);
                        }
                    }
                }
            }

            reader.MoveToElement();
            bool isEmptyElement = reader.IsEmptyElement;

            reader.ReadStartElement();
            if (!isEmptyElement)
            {
                XmlBuffer           buffer    = null;
                XmlDictionaryWriter extWriter = null;
                try
                {
                    while (reader.IsStartElement())
                    {
                        if (!TryParseElement(reader, referencedCategories, version))
                        {
                            SyndicationFeedFormatter.CreateBufferIfRequiredAndWriteNode(ref buffer, ref extWriter, reader, maxExtensionSize);
                        }
                    }
                    LoadElementExtensions(buffer, extWriter, referencedCategories);
                }
                finally
                {
                    extWriter?.Close();
                }
                reader.ReadEndElement();
            }
        }
        private static void ReadReferencedCategories(XmlReader reader, ReferencedCategoriesDocument referencedCategories, Uri baseUri, Uri link, string version, bool preserveElementExtensions, bool preserveAttributeExtensions, int maxExtensionSize)
        {
            referencedCategories.BaseUri = baseUri;
            referencedCategories.Link    = link;
            if (reader.HasAttributes)
            {
                while (reader.MoveToNextAttribute())
                {
                    if ((reader.LocalName == "base") && (reader.NamespaceURI == "http://www.w3.org/XML/1998/namespace"))
                    {
                        referencedCategories.BaseUri = FeedUtils.CombineXmlBase(referencedCategories.BaseUri, reader.Value);
                    }
                    else
                    {
                        if ((reader.LocalName == "lang") && (reader.NamespaceURI == "http://www.w3.org/XML/1998/namespace"))
                        {
                            referencedCategories.Language = reader.Value;
                            continue;
                        }
                        if ((reader.LocalName != "href") || (reader.NamespaceURI != string.Empty))
                        {
                            string namespaceURI = reader.NamespaceURI;
                            string localName    = reader.LocalName;
                            if (!FeedUtils.IsXmlns(localName, namespaceURI) && !FeedUtils.IsXmlSchemaType(localName, namespaceURI))
                            {
                                string str3 = reader.Value;
                                if (!ServiceDocumentFormatter.TryParseAttribute(localName, namespaceURI, str3, referencedCategories, version))
                                {
                                    if (preserveAttributeExtensions)
                                    {
                                        referencedCategories.AttributeExtensions.Add(new XmlQualifiedName(reader.LocalName, reader.NamespaceURI), reader.Value);
                                        continue;
                                    }
                                    SyndicationFeedFormatter.TraceSyndicationElementIgnoredOnRead(reader);
                                }
                            }
                        }
                    }
                }
            }
            reader.MoveToElement();
            bool isEmptyElement = reader.IsEmptyElement;

            reader.ReadStartElement();
            if (!isEmptyElement)
            {
                XmlBuffer           buffer    = null;
                XmlDictionaryWriter extWriter = null;
                try
                {
                    while (reader.IsStartElement())
                    {
                        if (!ServiceDocumentFormatter.TryParseElement(reader, referencedCategories, version))
                        {
                            if (preserveElementExtensions)
                            {
                                SyndicationFeedFormatter.CreateBufferIfRequiredAndWriteNode(ref buffer, ref extWriter, reader, maxExtensionSize);
                            }
                            else
                            {
                                SyndicationFeedFormatter.TraceSyndicationElementIgnoredOnRead(reader);
                                reader.Skip();
                            }
                        }
                    }
                    ServiceDocumentFormatter.LoadElementExtensions(buffer, extWriter, referencedCategories);
                }
                finally
                {
                    if (extWriter != null)
                    {
                        extWriter.Close();
                    }
                }
                reader.ReadEndElement();
            }
        }
 static void ReadReferencedCategories(XmlReader reader, ReferencedCategoriesDocument referencedCategories, Uri baseUri, Uri link, string version, bool preserveElementExtensions, bool preserveAttributeExtensions, int maxExtensionSize)
 {
     referencedCategories.BaseUri = baseUri;
     referencedCategories.Link = link;
     if (reader.HasAttributes)
     {
         while (reader.MoveToNextAttribute())
         {
             if (reader.LocalName == "base" && reader.NamespaceURI == Atom10FeedFormatter.XmlNs)
             {
                 referencedCategories.BaseUri = FeedUtils.CombineXmlBase(referencedCategories.BaseUri, reader.Value);
             }
             else if (reader.LocalName == "lang" && reader.NamespaceURI == Atom10FeedFormatter.XmlNs)
             {
                 referencedCategories.Language = reader.Value;
             }
             else if (reader.LocalName == App10Constants.Href && reader.NamespaceURI == string.Empty)
             {
                 continue;
             }
             else
             {
                 string ns = reader.NamespaceURI;
                 string name = reader.LocalName;
                 if (FeedUtils.IsXmlns(name, ns) || FeedUtils.IsXmlSchemaType(name, ns))
                 {
                     continue;
                 }
                 string val = reader.Value;
                 if (!TryParseAttribute(name, ns, val, referencedCategories, version))
                 {
                     if (preserveAttributeExtensions)
                     {
                         referencedCategories.AttributeExtensions.Add(new XmlQualifiedName(reader.LocalName, reader.NamespaceURI), reader.Value);
                     }
                     else
                     {
                         SyndicationFeedFormatter.TraceSyndicationElementIgnoredOnRead(reader);
                     }
                 }
             }
         }
     }
     reader.MoveToElement();
     bool isEmptyElement = reader.IsEmptyElement;
     reader.ReadStartElement();
     if (!isEmptyElement)
     {
         XmlBuffer buffer = null;
         XmlDictionaryWriter extWriter = null;
         try
         {
             while (reader.IsStartElement())
             {
                 if (!TryParseElement(reader, referencedCategories, version))
                 {
                     if (preserveElementExtensions)
                     {
                         SyndicationFeedFormatter.CreateBufferIfRequiredAndWriteNode(ref buffer, ref extWriter, reader, maxExtensionSize);
                     }
                     else
                     {
                         SyndicationFeedFormatter.TraceSyndicationElementIgnoredOnRead(reader);
                         reader.Skip();
                     }
                 }
             }
             LoadElementExtensions(buffer, extWriter, referencedCategories);
         }
         finally
         {
             if (extWriter != null)
             {
                 extWriter.Close();
             }
         }
         reader.ReadEndElement();
     }
 }