2010-03-30 Rodrigo Kumpera <rkumpera@novell.com>
[mono-project.git] / docs / HtmlAgilityPack / MixedCodeDocumentFragmentList.cs
blob4a4a41a2f08410f497036fa0e671232f1790d037
1 // HtmlAgilityPack V1.0 - Simon Mourier <simon underscore mourier at hotmail dot com>
2 using System;
3 using System.Collections;
5 namespace HtmlAgilityPack
7 /// <summary>
8 /// Represents a list of mixed code fragments.
9 /// </summary>
10 public class MixedCodeDocumentFragmentList : IEnumerable
12 #region Fields
14 private MixedCodeDocument _doc;
15 private ArrayList _items = new ArrayList();
17 #endregion
19 #region Constructors
21 internal MixedCodeDocumentFragmentList(MixedCodeDocument doc)
23 _doc = doc;
26 #endregion
28 #region Properties
30 ///<summary>
31 /// Gets the Document
32 ///</summary>
33 public MixedCodeDocument Doc
35 get { return _doc; }
38 /// <summary>
39 /// Gets the number of fragments contained in the list.
40 /// </summary>
41 public int Count
43 get { return _items.Count; }
46 /// <summary>
47 /// Gets a fragment from the list using its index.
48 /// </summary>
49 public MixedCodeDocumentFragment this[int index]
51 get { return _items[index] as MixedCodeDocumentFragment; }
54 #endregion
56 #region IEnumerable Members
58 /// <summary>
59 /// Gets an enumerator that can iterate through the fragment list.
60 /// </summary>
61 IEnumerator IEnumerable.GetEnumerator()
63 return GetEnumerator();
66 #endregion
68 #region Public Methods
70 /// <summary>
71 /// Appends a fragment to the list of fragments.
72 /// </summary>
73 /// <param name="newFragment">The fragment to append. May not be null.</param>
74 public void Append(MixedCodeDocumentFragment newFragment)
76 if (newFragment == null)
78 throw new ArgumentNullException("newFragment");
80 _items.Add(newFragment);
83 /// <summary>
84 /// Gets an enumerator that can iterate through the fragment list.
85 /// </summary>
86 public MixedCodeDocumentFragmentEnumerator GetEnumerator()
88 return new MixedCodeDocumentFragmentEnumerator(_items);
91 /// <summary>
92 /// Prepends a fragment to the list of fragments.
93 /// </summary>
94 /// <param name="newFragment">The fragment to append. May not be null.</param>
95 public void Prepend(MixedCodeDocumentFragment newFragment)
97 if (newFragment == null)
99 throw new ArgumentNullException("newFragment");
101 _items.Insert(0, newFragment);
104 /// <summary>
105 /// Remove a fragment from the list of fragments. If this fragment was not in the list, an exception will be raised.
106 /// </summary>
107 /// <param name="fragment">The fragment to remove. May not be null.</param>
108 public void Remove(MixedCodeDocumentFragment fragment)
110 if (fragment == null)
112 throw new ArgumentNullException("fragment");
114 int index = GetFragmentIndex(fragment);
115 if (index == -1)
117 throw new IndexOutOfRangeException();
119 RemoveAt(index);
122 /// <summary>
123 /// Remove all fragments from the list.
124 /// </summary>
125 public void RemoveAll()
127 _items.Clear();
130 /// <summary>
131 /// Remove a fragment from the list of fragments, using its index in the list.
132 /// </summary>
133 /// <param name="index">The index of the fragment to remove.</param>
134 public void RemoveAt(int index)
136 //MixedCodeDocumentFragment frag = (MixedCodeDocumentFragment) _items[index];
137 _items.RemoveAt(index);
140 #endregion
142 #region Internal Methods
144 internal void Clear()
146 _items.Clear();
149 internal int GetFragmentIndex(MixedCodeDocumentFragment fragment)
151 if (fragment == null)
153 throw new ArgumentNullException("fragment");
155 for (int i = 0; i < _items.Count; i++)
157 if ((_items[i]) == fragment)
159 return i;
162 return -1;
165 #endregion
167 #region Nested type: MixedCodeDocumentFragmentEnumerator
169 /// <summary>
170 /// Represents a fragment enumerator.
171 /// </summary>
172 public class MixedCodeDocumentFragmentEnumerator : IEnumerator
174 #region Fields
176 private int _index;
177 private ArrayList _items;
179 #endregion
181 #region Constructors
183 internal MixedCodeDocumentFragmentEnumerator(ArrayList items)
185 _items = items;
186 _index = -1;
189 #endregion
191 #region Properties
193 /// <summary>
194 /// Gets the current element in the collection.
195 /// </summary>
196 public MixedCodeDocumentFragment Current
198 get { return (MixedCodeDocumentFragment) (_items[_index]); }
201 #endregion
203 #region IEnumerator Members
205 /// <summary>
206 /// Gets the current element in the collection.
207 /// </summary>
208 object IEnumerator.Current
210 get { return (Current); }
213 /// <summary>
214 /// Advances the enumerator to the next element of the collection.
215 /// </summary>
216 /// <returns>true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection.</returns>
217 public bool MoveNext()
219 _index++;
220 return (_index < _items.Count);
223 /// <summary>
224 /// Sets the enumerator to its initial position, which is before the first element in the collection.
225 /// </summary>
226 public void Reset()
228 _index = -1;
231 #endregion
234 #endregion