-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathFactory.cs
125 lines (103 loc) · 4.31 KB
/
Factory.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#if NETSTANDARD1_3_OR_GREATER
using System;
using System.Xml;
using static Atom.Constants;
namespace Atom
{
public static class Factory
{
public static void SetElementValue(ref XmlElement element, string value)
{
element.InnerText = value;
}
public static void SetValueAsElement(XmlDocument doc, ref XmlElement element, string name, string value)
{
XmlElement valueElement;
int sep = name.IndexOf(':');
if (sep <= 0)
{
// Use namespace prefix
string prefix = name.Substring(0, sep);
string localName = name.Substring(sep + 1, name.Length - 1);
valueElement = doc.CreateElement(prefix, localName, null);
}
else
{
valueElement = doc.CreateElement(name);
}
valueElement.InnerText = value;
element.AppendChild(valueElement);
}
public static XmlElement CreateFeed(XmlDocument doc, string localNamespace = null, bool includeOpenSearchNS = false)
{
var nsManager = new XmlNamespaceManager(doc.NameTable);
nsManager.AddNamespace("a", ATOM_NAMESPACE);
if (includeOpenSearchNS)
nsManager.AddNamespace("os", OPENSEARCH_NAMESPACE);
var feed = doc.CreateElement("a", "feed", null);
if (localNamespace != null)
feed.SetAttribute("xmlns", localNamespace);
return feed;
}
public static XmlElement CreateLink(XmlDocument doc, string href, string rel = "self", string type = "application/atom+xml")
{
var link = doc.CreateElement("a", "link", null);
link.SetAttribute("href", href);
link.SetAttribute("rel", rel);
link.SetAttribute("type", type);
return link;
}
public static XmlElement CreateUpdated(XmlDocument doc, DateTimeOffset dateUpdated)
{
var updated = doc.CreateElement("a", "updated", null);
updated.InnerText = dateUpdated.UtcDateTime.ToString("O");
return updated;
}
public static XmlElement CreateTitle(XmlDocument doc, string title, string type = "text")
{
var titleElem = doc.CreateElement("a", "title", null);
titleElem.InnerText = title;
titleElem.SetAttribute("type", type);
return titleElem;
}
public static XmlElement CreateId(XmlDocument doc, string id)
{
var idElem = doc.CreateElement("a", "id", null);
idElem.InnerText = id;
return idElem;
}
public static XmlElement CreateAuthor(XmlDocument doc, string name, string url = null)
{
var author = doc.CreateElement("a", "author", null);
SetValueAsElement(doc, ref author, "name", name);
if (url != null)
SetValueAsElement(doc, ref author, "uri", url);
return author;
}
public static XmlElement CreateEntry(XmlDocument doc, string title, string id, string href, DateTime updated)
{
var entry = doc.CreateElement("a", "entry", null);
entry.AppendChild(CreateId(doc, id));
entry.AppendChild(CreateLink(doc, href));
entry.AppendChild(CreateTitle(doc, title));
entry.AppendChild(CreateUpdated(doc, updated));
return entry;
}
public static XmlElement CreateRootEntry(XmlDocument doc, string title, string id, string href, DateTime updated, string localNamespace = null, bool includeOpenSourceNS = false)
{
var nsManager = new XmlNamespaceManager(doc.NameTable);
nsManager.AddNamespace("a", ATOM_NAMESPACE);
if (includeOpenSourceNS)
nsManager.AddNamespace("os", OPENSEARCH_NAMESPACE);
var entry = doc.CreateElement("a", "entry", null);
if (localNamespace != null)
entry.SetAttribute("xmlns", localNamespace);
entry.AppendChild(CreateId(doc, id));
entry.AppendChild(CreateLink(doc, href));
entry.AppendChild(CreateTitle(doc, title));
entry.AppendChild(CreateUpdated(doc, updated));
return entry;
}
}
}
#endif