-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
PackageRelationshipCollection.cs
188 lines (148 loc) · 5.69 KB
/
PackageRelationshipCollection.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
namespace System.IO.Packaging
{
/// <summary>
/// Collection of all the relationships corresponding to a given source PackagePart.
/// This class is part of the MMCF Packaging Layer. It handles serialization to/from
/// relationship parts, creation of those parts and offers methods to create, delete
/// and enumerate relationships.
/// </summary>
public class PackageRelationshipCollection : IEnumerable<PackageRelationship>
{
#region IEnumerable
/// <summary>
/// Returns an enumerator for all the relationships for a PackagePart
/// </summary>
/// <returns></returns>
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
/// <summary>
/// Returns an enumerator over all the relationships for a PackagePart
/// </summary>
/// <returns></returns>
public IEnumerator<PackageRelationship> GetEnumerator()
{
IEnumerator<PackageRelationship> relationshipsEnumerator = _relationships.GetEnumerator();
if (_filter == null)
return relationshipsEnumerator;
else
return new FilteredEnumerator(relationshipsEnumerator, _filter);
}
#endregion
#region Internal Members
/// <summary>
/// Constructor
/// </summary>
/// <remarks>For use by PackagePart</remarks>
internal PackageRelationshipCollection(InternalRelationshipCollection relationships, string? filter)
{
Debug.Assert(relationships != null, "relationships parameter cannot be null");
_relationships = relationships;
_filter = filter;
}
#endregion
#region Private Members
private readonly InternalRelationshipCollection _relationships;
private readonly string? _filter;
#endregion
#region Private Class
#region FilteredEnumerator Class
/// <summary>
/// Internal class for the FilteredEnumerator
/// </summary>
private sealed class FilteredEnumerator : IEnumerator<PackageRelationship>
{
#region Constructor
/// <summary>
/// Constructs a FilteredEnumerator
/// </summary>
/// <param name="enumerator"></param>
/// <param name="filter"></param>
internal FilteredEnumerator(IEnumerator<PackageRelationship> enumerator, string filter)
{
Debug.Assert((enumerator != null), "Enumerator cannot be null");
Debug.Assert(filter != null, "PackageRelationship filter string cannot be null");
// Look for empty string or string with just spaces
Debug.Assert(filter.Trim() != string.Empty,
"RelationshipType filter cannot be empty string or a string with just spaces");
_enumerator = enumerator;
_filter = filter;
}
#endregion Constructor
#region IEnumerator Methods
/// <summary>
/// This method keeps moving the enumerator the next position till
/// a relationship is found with the matching Name
/// </summary>
/// <returns>Bool indicating if the enumerator successfully moved to the next position</returns>
bool IEnumerator.MoveNext()
{
while (_enumerator.MoveNext())
{
if (RelationshipTypeMatches())
return true;
}
return false;
}
/// <summary>
/// Gets the current object in the enumerator
/// </summary>
/// <value></value>
object IEnumerator.Current
{
get
{
return _enumerator.Current;
}
}
/// <summary>
/// Resets the enumerator to the beginning
/// </summary>
void IEnumerator.Reset()
{
_enumerator.Reset();
}
#endregion IEnumerator Methods
#region IEnumerator<PackageRelationship> Members
/// <summary>
/// Gets the current object in the enumerator
/// </summary>
/// <value></value>
public PackageRelationship Current
{
get
{
return _enumerator.Current;
}
}
#endregion IEnumerator<PackageRelationship> Members
#region IDisposable Members
public void Dispose()
{
//Most enumerators have dispose as a no-op, we follow the same pattern.
_enumerator.Dispose();
}
#endregion IDisposable Members
#region Private Methods
private bool RelationshipTypeMatches()
{
PackageRelationship r = _enumerator.Current;
return _filter == r.RelationshipType; // Case-sensitive comparison
}
#endregion Private Methods
#region Private Members
private readonly IEnumerator<PackageRelationship> _enumerator;
private readonly string _filter;
#endregion Private Members
}
#endregion FilteredEnumerator Class
#endregion Private Class
}
}