This repository has been archived by the owner on Feb 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Nodes.cs
155 lines (135 loc) · 4.11 KB
/
Nodes.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System.Linq;
using System.Xml;
using System.Xml.XPath;
using uComponents.Core;
using umbraco;
namespace uComponents.XsltExtensions
{
/// <summary>
/// The Nodes class exposes XSLT extensions to access nodes from Umbraco.
/// </summary>
[XsltExtension("ucomponents.nodes")]
public class Nodes
{
/// <summary>
/// Gets the expiry date of a node.
/// </summary>
/// <param name="nodeId">The node id.</param>
/// <returns>Returns the expiry date of a node.</returns>
public static string GetExpireDate(int nodeId)
{
var document = uQuery.GetDocument(nodeId);
if (document != null)
{
return document.ExpireDate.ToString();
}
return string.Empty;
}
/// <summary>
/// Gets the node Id by URL.
/// </summary>
/// <param name="url">The URL to get the XML node from.</param>
/// <returns>Returns the node Id.</returns>
public static int GetNodeIdByUrl(string url)
{
return uQuery.GetNodeIdByUrl(url);
}
/// <summary>
/// Gets the node id by path level.
/// </summary>
/// <param name="path">The path.</param>
/// <param name="level">The level.</param>
/// <returns>Returns the node id for a given path level.</returns>
public static string GetNodeIdByPathLevel(string path, int level)
{
var nodeIds = path.Split(Constants.Common.COMMA).ToList();
if (nodeIds.Count > level)
{
return nodeIds[level];
}
return uQuery.RootNodeId.ToString();
}
/// <summary>
/// Gets the release date of a node.
/// </summary>
/// <param name="nodeId">The node id.</param>
/// <returns>Returns the release date of a node.</returns>
public static string GetReleaseDate(int nodeId)
{
var document = uQuery.GetDocument(nodeId);
if (document != null)
{
return document.ReleaseDate.ToString();
}
return string.Empty;
}
/// <summary>
/// Gets the unique id of a node.
/// </summary>
/// <param name="nodeId">The node id.</param>
/// <returns>Returns the unique id of a node.</returns>
public static string GetUniqueId(int nodeId)
{
return Cms.GetUniqueId(nodeId);
}
/// <summary>
/// Gets a list of XML nodes by CSV.
/// </summary>
/// <param name="csv">The CSV list.</param>
/// <returns>
/// Returns an XPathNodeIterator of the nodes from the CSV list.
/// </returns>
public static XPathNodeIterator GetXmlNodeByCsv(string csv)
{
var ids = uQuery.GetCsvIds(csv);
var xpath = string.Concat("descendant::*[@id='", string.Join("' or @id='", ids.ToArray()), "']");
return library.GetXmlNodeByXPath(xpath);
}
/// <summary>
/// Gets a list of XML nodes by CSV (with an option to persist the order).
/// </summary>
/// <param name="csv">The CSV of node IDs.</param>
/// <param name="persistOrder">if set to <c>true</c> [persist order].</param>
/// <returns>
/// Returns an XPathNodeIterator of the nodes from the CSV list.
/// </returns>
public static XPathNodeIterator GetXmlNodeByCsv(string csv, bool persistOrder)
{
if (!persistOrder)
{
return GetXmlNodeByCsv(csv);
}
var xd = new XmlDocument();
xd.LoadXml("<root/>");
var nodes = uQuery.GetNodesByCsv(csv);
foreach (var node in nodes)
{
var xn = xd.ImportNode(node.ToXml(), true);
xd.DocumentElement.AppendChild(xn);
}
return xd.CreateNavigator().Select("/root/child::*[@isDoc]");
}
// TODO: [LK] Add overload for GetXmlNodeByCsv to accept an XPath expression
/// <summary>
/// Gets the XML node by path level.
/// </summary>
/// <param name="path">The path.</param>
/// <param name="level">The level.</param>
/// <returns>Returns an XML node by path level.</returns>
public static XPathNodeIterator GetXmlNodeByPathLevel(string path, int level)
{
var nodeId = GetNodeIdByPathLevel(path, level);
return library.GetXmlNodeById(nodeId);
}
/// <summary>
/// Gets the XML node by URL.
/// </summary>
/// <param name="url">The URL to get the XML node from.</param>
/// <returns>Returns the XML for the node.</returns>
public static XPathNodeIterator GetXmlNodeByUrl(string url)
{
var nodeId = uQuery.GetNodeIdByUrl(url);
return library.GetXmlNodeById(nodeId.ToString());
}
}
}