Esempio n. 1
0
        public static bool TryFormatRss10Feed(Rss10Feed feed, out XDocument document, ExtensionManifestDirectory extensionManifestDirectory = null)
        {
            document = default;

            if (feed == null)
            {
                return(false);
            }

            document = new XDocument();

            var rdfElement = new XElement(_rdf + "RDF");

            document.Add(rdfElement);

            var namespaceAliases = new XNamespaceAliasSet();

            namespaceAliases.EnsureNamespaceAlias(Rss10Constants.RdfNamespaceAlias, _rdf);
            namespaceAliases.EnsureNamespaceAlias(alias: null, _rss);

            if (extensionManifestDirectory == null)
            {
                extensionManifestDirectory = ExtensionManifestDirectory.DefaultForRss;
            }

            if (!TryFormatRss10Channel(feed.Channel, namespaceAliases, extensionManifestDirectory, out var channelElement))
            {
                return(false);
            }

            rdfElement.Add(channelElement);

            if (TryFormatRss10Image(feed.Channel.Image, referenceOnly: false, namespaceAliases, extensionManifestDirectory, out var imageElement))
            {
                rdfElement.Add(imageElement);
            }

            if (TryFormatRss10TextInput(feed.Channel.TextInput, referenceOnly: false, namespaceAliases, extensionManifestDirectory, out var textInputElement))
            {
                rdfElement.Add(textInputElement);
            }

            foreach (var itemToFormat in feed.Channel.Items)
            {
                if (TryFormatRss10Item(itemToFormat, referenceOnly: false, namespaceAliases, extensionManifestDirectory, out var itemElement))
                {
                    rdfElement.Add(itemElement);
                }
            }

            foreach (var namespaceAlias in namespaceAliases.OrderBy(x => x.Name.LocalName))
            {
                rdfElement.Add(namespaceAlias);
            }

            return(true);
        }
Esempio n. 2
0
        public static bool TryFormatRss20Feed(Rss20Feed feed, out XDocument document, ExtensionManifestDirectory extensionManifestDirectory = null)
        {
            document = default;

            if (feed == null)
            {
                return(false);
            }

            document = new XDocument();

            var rssElement = new XElement("rss", new XAttribute("version", Rss20Constants.Version));

            document.Add(rssElement);

            if (extensionManifestDirectory == null)
            {
                extensionManifestDirectory = ExtensionManifestDirectory.DefaultForRss;
            }

            var namespaceAliases = new XNamespaceAliasSet();

            if (!TryFormatRss20Channel(feed.Channel, namespaceAliases, extensionManifestDirectory, out var channelElement))
            {
                return(false);
            }

            rssElement.Add(channelElement);

            foreach (var namespaceAlias in namespaceAliases.OrderBy(x => x.Name.LocalName))
            {
                rssElement.Add(namespaceAlias);
            }

            return(true);
        }
Esempio n. 3
0
        public static bool TryFormatAtom10Feed(Atom10Feed feed, out XDocument document, ExtensionManifestDirectory extensionManifestDirectory = null)
        {
            document = default;

            if (feed == null)
            {
                return(false);
            }

            document = new XDocument();

            var feedElement = new XElement(_atom + "feed");

            document.Add(feedElement);

            var namespaceAliases = new XNamespaceAliasSet();

            namespaceAliases.EnsureNamespaceAlias(alias: null, _atom);

            if (extensionManifestDirectory == null)
            {
                extensionManifestDirectory = ExtensionManifestDirectory.DefaultForAtom;
            }

            if (TryFormatAtom10OptionalTextAttribute(feed.Lang, _xml + "lang", out var langAttribute))
            {
                feedElement.Add(langAttribute);
            }

            if (TryFormatAtom10OptionalTextAttribute(feed.Base, _xml + "base", out var baseAttribute))
            {
                feedElement.Add(baseAttribute);
            }

            if (TryFormatAtom10RequiredTextElement(feed.Id, _atom + "id", out var idElement))
            {
                feedElement.Add(idElement);
            }

            if (TryFormatAtom10TextRequired(feed.Title, _atom + "title", out var titleElement))
            {
                feedElement.Add(titleElement);
            }

            if (TryFormatAtom10TimestampRequired(feed.Updated, _atom + "updated", out var updatedElement))
            {
                feedElement.Add(updatedElement);
            }

            foreach (var authorToFormat in feed.Authors)
            {
                if (TryFormatAtom10Person(authorToFormat, _atom + "author", out var authorElement))
                {
                    feedElement.Add(authorElement);
                }
            }

            foreach (var linkToFormat in feed.Links)
            {
                if (TryFormatAtom10Link(linkToFormat, out var linkElement))
                {
                    feedElement.Add(linkElement);
                }
            }

            foreach (var categoryToFormat in feed.Categories)
            {
                if (TryFormatAtom10Category(categoryToFormat, out var categoryElement))
                {
                    feedElement.Add(categoryElement);
                }
            }

            foreach (var contributorToFormat in feed.Contributors)
            {
                if (TryFormatAtom10Person(contributorToFormat, _atom + "contributor", out var contributorElement))
                {
                    feedElement.Add(contributorElement);
                }
            }

            if (TryFormatAtom10Generator(feed.Generator, out var generatorElement))
            {
                feedElement.Add(generatorElement);
            }

            if (TryFormatAtom10OptionalTextElement(feed.Icon, _atom + "icon", out var iconElement))
            {
                feedElement.Add(iconElement);
            }

            if (TryFormatAtom10OptionalTextElement(feed.Logo, _atom + "logo", out var logoElement))
            {
                feedElement.Add(logoElement);
            }

            if (TryFormatAtom10TextOptional(feed.Rights, _atom + "rights", out var rightsElement))
            {
                feedElement.Add(rightsElement);
            }

            if (TryFormatAtom10TextOptional(feed.Subtitle, _atom + "subtitle", out var subtitleElement))
            {
                feedElement.Add(subtitleElement);
            }

            // extensions
            if (ExtensibleEntityFormatter.TryFormatXElementExtensions(feed, namespaceAliases, extensionManifestDirectory, out var extensionElements))
            {
                feedElement.AddRange(extensionElements);
            }

            // entries
            foreach (var entryToFormat in feed.Entries)
            {
                if (TryFormatAtom10Entry(entryToFormat, namespaceAliases, extensionManifestDirectory, out var entryElement))
                {
                    feedElement.Add(entryElement);
                }
            }

            foreach (var namespaceAlias in namespaceAliases.OrderBy(x => x.Name.LocalName))
            {
                feedElement.Add(namespaceAlias);
            }

            return(true);
        }