-
Notifications
You must be signed in to change notification settings - Fork 4
/
Fragment.cs
262 lines (251 loc) · 7.48 KB
/
Fragment.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace MOD.Web.Element
{
/// <summary>
/// Fragment: a collection on nodes. Use this when you don't have a parent element
/// </summary>
public class Fragment : Node, IList<Node>, IRenderable
{
#region New Fragment Members
/// <summary>
///
/// </summary>
public List<Node> Children
{
get { return _Children ?? (_Children = new List<Node>()); }
set { _Children = value; }
}
private List<Node> _Children;
#endregion
#region Original Fragment Members
/// <summary>
/// Adds a Node to the fragment.
/// </summary>
/// <param name="item">The Node to Add</param>
/// <returns></returns>
public Fragment Add(Node item)
{
Children.Add(item);
return this;
}
/// <summary>
/// Add a list of objects of any type to the fragment
/// Supported types are:
/// string, long, int, double
/// IEnumerable<string>
/// Node
/// IEnumerable<Node>
/// IEnumerable<Element>
/// IEnumerable<object>
/// </summary>
public Fragment Add(params object[] objectList)
{
if (objectList == null)
{
Debug.WriteLine("Add null == objectList");
return this;
}
foreach (object obj in objectList)
{
if (obj == null)
{
Debug.WriteLine("Add null = obj");
continue;
}
if (obj is string)
{
Add(Element.Text(obj as string));
}
else if (obj is IEnumerable)
{
foreach (var child in (IEnumerable)obj)
{
Add(child);
}
}
else if (obj is Node)
{
Add(obj as Node);
}
else if (obj is long || obj is int || obj is double)
{
Add(obj.ToString());
}
else
{
throw new Exception(
string.Format("Unsupported type bro! :{0}, type: {1}"
, obj.ToString(), obj.GetType().Name));
}
}
return this;
}
/// <summary>
/// Adds the collection to the end of the Fragment
/// </summary>
/// <param name="collection">Enumerable set of nodes to add</param>
/// <returns></returns>
public Fragment AddRange(IEnumerable<Node> collection)
{
Children.AddRange(collection);
return this;
}
/// <summary>
/// Take each object in this node, convert it to a string, and add it to the StringBuilder
/// </summary>
/// <param name="sb">The StringBuilder to which all the stringified objects will be added</param>
public override void ToString(StringBuilder sb)
{
foreach (Node node in Children)
{
node.ToString(sb);
}
}
/// <summary>
/// Create a StringBuilder, add all the contents of the Fragment to that StringBuilder, and retrieve the built string
/// </summary>
/// <returns>One string containing the string values of all the objects in the fragment</returns>
public override string ToString()
{
StringBuilder sb = new StringBuilder();
ToString(sb);
return sb.ToString();
}
#endregion
#region List<T> Members
/// <summary>
/// Inserts the elements of a collection into the List<T> at the specified index.
/// </summary>
/// <param name="index">The zero-based index at which the new elements should be inserted.</param>
/// <param name="collection">The collection whose elements should be inserted into the List<T>. The collection itself cannot be null, but it can contain elements that are null, if type T is a reference type.</param>
public void InsertRange(int index, IEnumerable<Node> collection)
{
Children.InsertRange(index, collection);
}
#endregion
#region IList<T> Members
/// <summary>
/// Adds an item to the ICollection<T>.
/// </summary>
/// <param name="item">The object to add to the ICollection<T>.</param>
void ICollection<Node>.Add(Node item)
{
Children.Add(item);
}
/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
/// <returns>An enumerator that can be used to iterate through the collection.</returns>
public IEnumerator<Node> GetEnumerator()
{
return Children.GetEnumerator();
}
/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
/// <returns>An enumerator that can be used to iterate through the collection.</returns>
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
/// <summary>
/// Removes all items from the ICollection<T>.
/// </summary>
public void Clear()
{
Children.Clear();
}
/// <summary>
/// Gets the number of elements contained in the ICollection<T>.
/// </summary>
/// <param name="item">The object to locate in the ICollection<T>.</param>
/// <returns>true if item is found in the ICollection<T>; otherwise, false.</returns>
public bool Contains(Node item)
{
return Children.Contains(item);
}
/// <summary>
/// Gets the number of elements contained in the ICollection<T>.
/// </summary>
/// <param name="array">The one-dimensional Array that is the destination of the elements copied from ICollection<T>. The Array must have zero-based indexing.</param>
/// <param name="arrayIndex">The zero-based index in array at which copying begins.</param>
public void CopyTo(Node[] array, int arrayIndex)
{
Children.CopyTo(array, arrayIndex);
}
/// <summary>
/// Gets the number of elements contained in the ICollection<T>.
/// </summary>
public int Count
{
get { return Children.Count; }
}
/// <summary>
/// Determines the index of a specific item in the IList<T>.
/// </summary>
/// <param name="item">The object to locate in the IList<T>.</param>
/// <returns>The object to locate in the IList<T>.</returns>
public int IndexOf(Node item)
{
return Children.IndexOf(item);
}
/// <summary>
/// Inserts an item to the IList<T> at the specified index.
/// </summary>
/// <param name="index">The zero-based index at which item should be inserted.</param>
/// <param name="item">The object to insert into the IList<T>.</param>
public void Insert(int index, Node item)
{
Children.Insert(index, item);
}
/// <summary>
/// Gets a value indicating whether the ICollection<T> is read-only.
/// </summary>
public bool IsReadOnly
{
get { return false; }
}
/// <summary>
/// Removes the first occurrence of a specific object from the ICollection<T>.
/// </summary>
/// <param name="item">The object to remove from the ICollection<T>.</param>
/// <returns>true if item was successfully removed from the ICollection<T>; otherwise, false. This method also returns false if item is not found in the original ICollection<T>.</returns>
public bool Remove(Node item)
{
return Children.Remove(item);
}
/// <summary>
/// Removes the IList<T> item at the specified index.
/// </summary>
/// <param name="index">The zero-based index of the item to remove.</param>
public void RemoveAt(int index)
{
Children.RemoveAt(index);
}
/// <summary>
/// Gets or sets the element at the specified index.
/// </summary>
/// <param name="index">The zero-based index of the element to get or set.</param>
/// <returns>The element at the specified index.</returns>
public Node this[int index]
{
get { return Children[index]; }
set { Children[index] = value; }
}
#endregion
#region IRenderable Members
/// <summary>
/// Renders a Fragment to a Node.
/// </summary>
/// <returns>A Node instance.</returns>
public Node Render()
{
return this;
}
#endregion
}
}