private void WriteCollection(XmlWriter writer, ResourceCollectionInfo collection, Uri baseUri)
        {
            writer.WriteStartElement("app", "collection", "http://www.w3.org/2007/app");
            Uri baseUriToWrite = FeedUtils.GetBaseUriToWrite(baseUri, collection.BaseUri);

            if (baseUriToWrite != null)
            {
                baseUri = collection.BaseUri;
                WriteXmlBase(writer, baseUriToWrite);
            }
            if (collection.Link != null)
            {
                writer.WriteAttributeString("href", FeedUtils.GetUriString(collection.Link));
            }
            ServiceDocumentFormatter.WriteAttributeExtensions(writer, collection, this.Version);
            if (collection.Title != null)
            {
                collection.Title.WriteTo(writer, "title", "http://www.w3.org/2005/Atom");
            }
            for (int i = 0; i < collection.Accepts.Count; i++)
            {
                writer.WriteElementString("app", "accept", "http://www.w3.org/2007/app", collection.Accepts[i]);
            }
            for (int j = 0; j < collection.Categories.Count; j++)
            {
                WriteCategories(writer, collection.Categories[j], baseUri, this.Version);
            }
            ServiceDocumentFormatter.WriteElementExtensions(writer, collection, this.Version);
            writer.WriteEndElement();
        }
Beispiel #2
0
 public ServiceDocumentFormatter GetFormatter()
 {
     if (formatter == null)
     {
         formatter = new AtomPub10ServiceDocumentFormatter(this);
     }
     return(formatter);
 }
 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 WriteInlineCategoriesContent(XmlWriter writer, InlineCategoriesDocument categories, string version)
 {
     if (!string.IsNullOrEmpty(categories.Scheme))
     {
         writer.WriteAttributeString("scheme", categories.Scheme);
     }
     if (categories.IsFixed)
     {
         writer.WriteAttributeString("fixed", "yes");
     }
     ServiceDocumentFormatter.WriteAttributeExtensions(writer, categories, version);
     for (int i = 0; i < categories.Categories.Count; i++)
     {
         Atom10FeedFormatter.WriteCategory(writer, categories.Categories[i], version);
     }
     ServiceDocumentFormatter.WriteElementExtensions(writer, categories, version);
 }
        private void WriteDocument(XmlWriter writer)
        {
            writer.WriteAttributeString("a10", "http://www.w3.org/2000/xmlns/", "http://www.w3.org/2005/Atom");
            if (!string.IsNullOrEmpty(base.Document.Language))
            {
                WriteXmlLang(writer, base.Document.Language);
            }
            Uri baseUri = base.Document.BaseUri;

            if (baseUri != null)
            {
                WriteXmlBase(writer, baseUri);
            }
            ServiceDocumentFormatter.WriteAttributeExtensions(writer, base.Document, this.Version);
            for (int i = 0; i < base.Document.Workspaces.Count; i++)
            {
                this.WriteWorkspace(writer, base.Document.Workspaces[i], baseUri);
            }
            ServiceDocumentFormatter.WriteElementExtensions(writer, base.Document, this.Version);
        }
        private void WriteWorkspace(XmlWriter writer, Workspace workspace, Uri baseUri)
        {
            writer.WriteStartElement("app", "workspace", "http://www.w3.org/2007/app");
            Uri baseUriToWrite = FeedUtils.GetBaseUriToWrite(baseUri, workspace.BaseUri);

            if (baseUriToWrite != null)
            {
                baseUri = workspace.BaseUri;
                WriteXmlBase(writer, baseUriToWrite);
            }
            ServiceDocumentFormatter.WriteAttributeExtensions(writer, workspace, this.Version);
            if (workspace.Title != null)
            {
                workspace.Title.WriteTo(writer, "title", "http://www.w3.org/2005/Atom");
            }
            for (int i = 0; i < workspace.Collections.Count; i++)
            {
                this.WriteCollection(writer, workspace.Collections[i], baseUri);
            }
            ServiceDocumentFormatter.WriteElementExtensions(writer, workspace, this.Version);
            writer.WriteEndElement();
        }
        private ResourceCollectionInfo ReadCollection(XmlReader reader, Workspace workspace)
        {
            CreateInlineCategoriesDelegate     inlineCategoriesFactory     = null;
            CreateReferencedCategoriesDelegate referencedCategoriesFactory = null;
            ResourceCollectionInfo             result = ServiceDocumentFormatter.CreateCollection(workspace);

            result.BaseUri = workspace.BaseUri;
            if (reader.HasAttributes)
            {
                while (reader.MoveToNextAttribute())
                {
                    if ((reader.LocalName == "base") && (reader.NamespaceURI == "http://www.w3.org/XML/1998/namespace"))
                    {
                        result.BaseUri = FeedUtils.CombineXmlBase(result.BaseUri, reader.Value);
                    }
                    else
                    {
                        if ((reader.LocalName == "href") && (reader.NamespaceURI == string.Empty))
                        {
                            result.Link = new Uri(reader.Value, UriKind.RelativeOrAbsolute);
                            continue;
                        }
                        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, result, this.Version))
                            {
                                if (this.preserveAttributeExtensions)
                                {
                                    result.AttributeExtensions.Add(new XmlQualifiedName(reader.LocalName, reader.NamespaceURI), reader.Value);
                                    continue;
                                }
                                SyndicationFeedFormatter.TraceSyndicationElementIgnoredOnRead(reader);
                            }
                        }
                    }
                }
            }
            XmlBuffer           buffer    = null;
            XmlDictionaryWriter extWriter = null;

            reader.ReadStartElement();
            try
            {
                while (reader.IsStartElement())
                {
                    if (reader.IsStartElement("title", "http://www.w3.org/2005/Atom"))
                    {
                        result.Title = Atom10FeedFormatter.ReadTextContentFrom(reader, "//app:service/app:workspace/app:collection/atom:title[@type]", this.preserveAttributeExtensions);
                    }
                    else
                    {
                        if (reader.IsStartElement("categories", "http://www.w3.org/2007/app"))
                        {
                            if (inlineCategoriesFactory == null)
                            {
                                inlineCategoriesFactory = () => ServiceDocumentFormatter.CreateInlineCategories(result);
                            }
                            if (referencedCategoriesFactory == null)
                            {
                                referencedCategoriesFactory = () => ServiceDocumentFormatter.CreateReferencedCategories(result);
                            }
                            result.Categories.Add(ReadCategories(reader, result.BaseUri, inlineCategoriesFactory, referencedCategoriesFactory, this.Version, this.preserveElementExtensions, this.preserveAttributeExtensions, this.maxExtensionSize));
                            continue;
                        }
                        if (reader.IsStartElement("accept", "http://www.w3.org/2007/app"))
                        {
                            result.Accepts.Add(reader.ReadElementString());
                        }
                        else if (!ServiceDocumentFormatter.TryParseElement(reader, result, this.Version))
                        {
                            if (this.preserveElementExtensions)
                            {
                                SyndicationFeedFormatter.CreateBufferIfRequiredAndWriteNode(ref buffer, ref extWriter, reader, this.maxExtensionSize);
                                continue;
                            }
                            SyndicationFeedFormatter.TraceSyndicationElementIgnoredOnRead(reader);
                            reader.Skip();
                        }
                    }
                }
                ServiceDocumentFormatter.LoadElementExtensions(buffer, extWriter, result);
            }
            finally
            {
                if (extWriter != null)
                {
                    extWriter.Close();
                }
            }
            reader.ReadEndElement();
            return(result);
        }
        private Workspace ReadWorkspace(XmlReader reader, ServiceDocument document)
        {
            Workspace workspace = ServiceDocumentFormatter.CreateWorkspace(document);

            workspace.BaseUri = document.BaseUri;
            if (reader.HasAttributes)
            {
                while (reader.MoveToNextAttribute())
                {
                    if ((reader.LocalName == "base") && (reader.NamespaceURI == "http://www.w3.org/XML/1998/namespace"))
                    {
                        workspace.BaseUri = FeedUtils.CombineXmlBase(workspace.BaseUri, reader.Value);
                    }
                    else
                    {
                        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, workspace, this.Version))
                            {
                                if (this.preserveAttributeExtensions)
                                {
                                    workspace.AttributeExtensions.Add(new XmlQualifiedName(reader.LocalName, reader.NamespaceURI), reader.Value);
                                    continue;
                                }
                                SyndicationFeedFormatter.TraceSyndicationElementIgnoredOnRead(reader);
                            }
                        }
                    }
                }
            }
            XmlBuffer           buffer    = null;
            XmlDictionaryWriter extWriter = null;

            reader.ReadStartElement();
            try
            {
                while (reader.IsStartElement())
                {
                    if (reader.IsStartElement("title", "http://www.w3.org/2005/Atom"))
                    {
                        workspace.Title = Atom10FeedFormatter.ReadTextContentFrom(reader, "//app:service/app:workspace/atom:title[@type]", this.preserveAttributeExtensions);
                    }
                    else
                    {
                        if (reader.IsStartElement("collection", "http://www.w3.org/2007/app"))
                        {
                            workspace.Collections.Add(this.ReadCollection(reader, workspace));
                            continue;
                        }
                        if (!ServiceDocumentFormatter.TryParseElement(reader, workspace, this.Version))
                        {
                            if (this.preserveElementExtensions)
                            {
                                SyndicationFeedFormatter.CreateBufferIfRequiredAndWriteNode(ref buffer, ref extWriter, reader, this.maxExtensionSize);
                                continue;
                            }
                            SyndicationFeedFormatter.TraceSyndicationElementIgnoredOnRead(reader);
                            reader.Skip();
                        }
                    }
                }
                ServiceDocumentFormatter.LoadElementExtensions(buffer, extWriter, workspace);
            }
            finally
            {
                if (extWriter != null)
                {
                    extWriter.Close();
                }
            }
            reader.ReadEndElement();
            return(workspace);
        }
        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();
            }
        }
        private void ReadDocument(XmlReader reader)
        {
            ServiceDocument document = this.CreateDocumentInstance();

            try
            {
                SyndicationFeedFormatter.MoveToStartElement(reader);
                bool isEmptyElement = reader.IsEmptyElement;
                if (reader.HasAttributes)
                {
                    while (reader.MoveToNextAttribute())
                    {
                        if ((reader.LocalName == "lang") && (reader.NamespaceURI == "http://www.w3.org/XML/1998/namespace"))
                        {
                            document.Language = reader.Value;
                        }
                        else
                        {
                            if ((reader.LocalName == "base") && (reader.NamespaceURI == "http://www.w3.org/XML/1998/namespace"))
                            {
                                document.BaseUri = new Uri(reader.Value, UriKind.RelativeOrAbsolute);
                                continue;
                            }
                            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, document, this.Version))
                                {
                                    if (this.preserveAttributeExtensions)
                                    {
                                        document.AttributeExtensions.Add(new XmlQualifiedName(reader.LocalName, reader.NamespaceURI), reader.Value);
                                        continue;
                                    }
                                    SyndicationFeedFormatter.TraceSyndicationElementIgnoredOnRead(reader);
                                }
                            }
                        }
                    }
                }
                XmlBuffer           buffer    = null;
                XmlDictionaryWriter extWriter = null;
                reader.ReadStartElement();
                if (!isEmptyElement)
                {
                    try
                    {
                        while (reader.IsStartElement())
                        {
                            if (reader.IsStartElement("workspace", "http://www.w3.org/2007/app"))
                            {
                                document.Workspaces.Add(this.ReadWorkspace(reader, document));
                            }
                            else if (!ServiceDocumentFormatter.TryParseElement(reader, document, this.Version))
                            {
                                if (this.preserveElementExtensions)
                                {
                                    SyndicationFeedFormatter.CreateBufferIfRequiredAndWriteNode(ref buffer, ref extWriter, reader, this.maxExtensionSize);
                                    continue;
                                }
                                SyndicationFeedFormatter.TraceSyndicationElementIgnoredOnRead(reader);
                                reader.Skip();
                            }
                        }
                        ServiceDocumentFormatter.LoadElementExtensions(buffer, extWriter, document);
                    }
                    finally
                    {
                        if (extWriter != null)
                        {
                            extWriter.Close();
                        }
                    }
                }
                reader.ReadEndElement();
            }
            catch (FormatException exception)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(FeedUtils.AddLineInfo(reader, "ErrorParsingDocument"), exception));
            }
            catch (ArgumentException exception2)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(FeedUtils.AddLineInfo(reader, "ErrorParsingDocument"), exception2));
            }
            this.SetDocument(document);
        }