1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4 
5 using System.Collections.ObjectModel;
6 using System.Xml;
7 using System.Xml.Serialization;
8 using System.Runtime.Serialization;
9 using System.Globalization;
10 
11 namespace System.ServiceModel.Syndication
12 {
13     internal static class FeedUtils
14     {
AddLineInfo(XmlReader reader, string error)15         public static string AddLineInfo(XmlReader reader, string error)
16         {
17             IXmlLineInfo lineInfo = reader as IXmlLineInfo;
18             if (lineInfo != null && lineInfo.HasLineInfo())
19             {
20                 error = String.Format(CultureInfo.InvariantCulture, "{0} {1}", SR.Format(SR.ErrorInLine, lineInfo.LineNumber, lineInfo.LinePosition), SR.Format(error));
21             }
22             return error;
23         }
24 
CloneCategories(Collection<SyndicationCategory> categories)25         static internal Collection<SyndicationCategory> CloneCategories(Collection<SyndicationCategory> categories)
26         {
27             if (categories == null)
28             {
29                 return null;
30             }
31             Collection<SyndicationCategory> result = new NullNotAllowedCollection<SyndicationCategory>();
32             for (int i = 0; i < categories.Count; ++i)
33             {
34                 result.Add(categories[i].Clone());
35             }
36             return result;
37         }
38 
CloneLinks(Collection<SyndicationLink> links)39         static internal Collection<SyndicationLink> CloneLinks(Collection<SyndicationLink> links)
40         {
41             if (links == null)
42             {
43                 return null;
44             }
45             Collection<SyndicationLink> result = new NullNotAllowedCollection<SyndicationLink>();
46             for (int i = 0; i < links.Count; ++i)
47             {
48                 result.Add(links[i].Clone());
49             }
50             return result;
51         }
52 
ClonePersons(Collection<SyndicationPerson> persons)53         static internal Collection<SyndicationPerson> ClonePersons(Collection<SyndicationPerson> persons)
54         {
55             if (persons == null)
56             {
57                 return null;
58             }
59             Collection<SyndicationPerson> result = new NullNotAllowedCollection<SyndicationPerson>();
60             for (int i = 0; i < persons.Count; ++i)
61             {
62                 result.Add(persons[i].Clone());
63             }
64             return result;
65         }
66 
CloneTextContent(TextSyndicationContent content)67         static internal TextSyndicationContent CloneTextContent(TextSyndicationContent content)
68         {
69             if (content == null)
70             {
71                 return null;
72             }
73             return (TextSyndicationContent)(content.Clone());
74         }
75 
CombineXmlBase(Uri rootBase, string newBase)76         internal static Uri CombineXmlBase(Uri rootBase, string newBase)
77         {
78             if (string.IsNullOrEmpty(newBase))
79             {
80                 return rootBase;
81             }
82             Uri newBaseUri = new Uri(newBase, UriKind.RelativeOrAbsolute);
83             if (rootBase == null || newBaseUri.IsAbsoluteUri)
84             {
85                 return newBaseUri;
86             }
87             return new Uri(rootBase, newBase);
88         }
89 
GetBaseUriToWrite(Uri rootBase, Uri currentBase)90         internal static Uri GetBaseUriToWrite(Uri rootBase, Uri currentBase)
91         {
92             Uri uriToWrite;
93             if (rootBase == currentBase || currentBase == null)
94             {
95                 uriToWrite = null;
96             }
97             else if (rootBase == null)
98             {
99                 uriToWrite = currentBase;
100             }
101             else
102             {
103                 // rootBase != currentBase and both are not null
104                 // Write the relative base if possible
105                 if (rootBase.IsAbsoluteUri && currentBase.IsAbsoluteUri && rootBase.IsBaseOf(currentBase))
106                 {
107                     uriToWrite = rootBase.MakeRelativeUri(currentBase);
108                 }
109                 else
110                 {
111                     uriToWrite = currentBase;
112                 }
113             }
114             return uriToWrite;
115         }
116 
GetUriString(Uri uri)117         static internal string GetUriString(Uri uri)
118         {
119             if (uri == null)
120             {
121                 return null;
122             }
123             if (uri.IsAbsoluteUri)
124             {
125                 return uri.AbsoluteUri;
126             }
127             else
128             {
129                 return uri.ToString();
130             }
131         }
132 
IsXmlns(string name, string ns)133         static internal bool IsXmlns(string name, string ns)
134         {
135             return name == "xmlns" || ns == "http://www.w3.org/2000/xmlns/";
136         }
137 
IsXmlSchemaType(string name, string ns)138         internal static bool IsXmlSchemaType(string name, string ns)
139         {
140             return name == "type" && ns == "http://www.w3.org/2001/XMLSchema-instance";
141         }
142     }
143 }
144