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
|
//-----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
namespace System.Activities
{
using System.Activities.Validation;
using System.Collections.Generic;
using System.Runtime;
[Fx.Tag.XamlVisible(false)]
sealed class ActivityLocationReferenceEnvironment : LocationReferenceEnvironment
{
Dictionary<string, LocationReference> declarations;
List<LocationReference> unnamedDeclarations;
public ActivityLocationReferenceEnvironment()
{
}
public ActivityLocationReferenceEnvironment(LocationReferenceEnvironment parent)
{
this.Parent = parent;
if (this.Parent != null)
{
this.InternalRoot = parent.Root;
}
}
public override Activity Root
{
get
{
return this.InternalRoot;
}
}
public Activity InternalRoot
{
get;
set;
}
Dictionary<string, LocationReference> Declarations
{
get
{
if (this.declarations == null)
{
this.declarations = new Dictionary<string, LocationReference>();
}
return this.declarations;
}
}
public override bool IsVisible(LocationReference locationReference)
{
if (locationReference == null)
{
throw FxTrace.Exception.ArgumentNull("locationReference");
}
LocationReferenceEnvironment currentScope = this;
while (currentScope != null)
{
ActivityLocationReferenceEnvironment activityEnvironment = currentScope as ActivityLocationReferenceEnvironment;
if (activityEnvironment != null)
{
if (activityEnvironment.declarations != null)
{
foreach (LocationReference declaration in activityEnvironment.declarations.Values)
{
if (locationReference == declaration)
{
return true;
}
}
}
if (activityEnvironment.unnamedDeclarations != null)
{
for (int i = 0; i < activityEnvironment.unnamedDeclarations.Count; i++)
{
if (locationReference == activityEnvironment.unnamedDeclarations[i])
{
return true;
}
}
}
}
else
{
return currentScope.IsVisible(locationReference);
}
currentScope = currentScope.Parent;
}
return false;
}
public void Declare(LocationReference locationReference, Activity owner, ref IList<ValidationError> validationErrors)
{
Fx.Assert(locationReference != null, "Must not be null");
if (locationReference.Name == null)
{
if (this.unnamedDeclarations == null)
{
this.unnamedDeclarations = new List<LocationReference>();
}
this.unnamedDeclarations.Add(locationReference);
}
else
{
if (this.Declarations.ContainsKey(locationReference.Name))
{
string id = null;
if (owner != null)
{
id = owner.Id;
}
ValidationError validationError = new ValidationError(SR.SymbolNamesMustBeUnique(locationReference.Name))
{
Source = owner,
Id = id
};
ActivityUtilities.Add(ref validationErrors, validationError);
}
else
{
this.Declarations.Add(locationReference.Name, locationReference);
}
}
}
public override bool TryGetLocationReference(string name, out LocationReference result)
{
if (name == null)
{
// We don't allow null names in our LocationReferenceEnvironment but
// a custom declared environment might. We need to walk up
// to the root and see if it chains to a
// non-ActivityLocationReferenceEnvironment implementation
LocationReferenceEnvironment currentEnvironment = this.Parent;
while (currentEnvironment is ActivityLocationReferenceEnvironment)
{
currentEnvironment = currentEnvironment.Parent;
}
if (currentEnvironment != null)
{
Fx.Assert(!(currentEnvironment is ActivityLocationReferenceEnvironment), "We must be at a non-ActivityLocationReferenceEnvironment implementation.");
return currentEnvironment.TryGetLocationReference(name, out result);
}
}
else
{
if (this.declarations != null && this.declarations.TryGetValue(name, out result))
{
return true;
}
bool found = false;
LocationReferenceEnvironment currentEnvironment = this.Parent;
LocationReferenceEnvironment rootEnvironment = this;
// Loop through all of the ActivityLocationReferenceEnvironments we have chained together
while (currentEnvironment != null && currentEnvironment is ActivityLocationReferenceEnvironment)
{
ActivityLocationReferenceEnvironment activityEnvironment = (ActivityLocationReferenceEnvironment)currentEnvironment;
if (activityEnvironment.declarations != null && activityEnvironment.declarations.TryGetValue(name, out result))
{
return true;
}
rootEnvironment = currentEnvironment;
currentEnvironment = currentEnvironment.Parent;
}
if (!found)
{
if (currentEnvironment != null)
{
// Looks like we have a non-ActivityLocationReferenceEnvironment at the root
Fx.Assert(!(currentEnvironment is ActivityLocationReferenceEnvironment), "We should have some other host environment at this point.");
if (currentEnvironment.TryGetLocationReference(name, out result))
{
return true;
}
}
}
}
result = null;
return false;
}
public override IEnumerable<LocationReference> GetLocationReferences()
{
return this.Declarations.Values;
}
}
}
|